#include "pth_p.h"
#if cpp
#ifndef PTH_DEBUG
#define pth_debug1(a1)
#define pth_debug2(a1, a2)
#define pth_debug3(a1, a2, a3)
#define pth_debug4(a1, a2, a3, a4)
#define pth_debug5(a1, a2, a3, a4, a5)
#define pth_debug6(a1, a2, a3, a4, a5, a6)
#else
#define pth_debug1(a1) pth_debug(__FILE__, __LINE__, 1, a1)
#define pth_debug2(a1, a2) pth_debug(__FILE__, __LINE__, 2, a1, a2)
#define pth_debug3(a1, a2, a3) pth_debug(__FILE__, __LINE__, 3, a1, a2, a3)
#define pth_debug4(a1, a2, a3, a4) pth_debug(__FILE__, __LINE__, 4, a1, a2, a3, a4)
#define pth_debug5(a1, a2, a3, a4, a5) pth_debug(__FILE__, __LINE__, 5, a1, a2, a3, a4, a5)
#define pth_debug6(a1, a2, a3, a4, a5, a6) pth_debug(__FILE__, __LINE__, 6, a1, a2, a3, a4, a5, a6)
#endif
#endif
intern void pth_debug(const char *file, int line, int argc, const char *fmt, ...)
{
va_list ap;
static char str[1024];
size_t n;
errno_shield {
va_start(ap, fmt);
if (file != NULL)
pth_snprintf(str, sizeof(str), "%d:%s:%04d: ", (int)getpid(), file, line);
else
str[0] = NUL;
n = strlen(str);
if (argc == 1)
pth_util_cpystrn(str+n, fmt, sizeof(str)-n);
else
pth_vsnprintf(str+n, sizeof(str)-n, fmt, ap);
va_end(ap);
n = strlen(str);
str[n++] = '\n';
pth_sc(write)(STDERR_FILENO, str, n);
}
return;
}
intern void pth_dumpstate(FILE *fp)
{
fprintf(fp, "+----------------------------------------------------------------------\n");
fprintf(fp, "| Pth Version: %s\n", PTH_VERSION_STR);
fprintf(fp, "| Load Average: %.2f\n", pth_loadval);
pth_dumpqueue(fp, "NEW", &pth_NQ);
pth_dumpqueue(fp, "READY", &pth_RQ);
fprintf(fp, "| Thread Queue RUNNING:\n");
fprintf(fp, "| 1. thread 0x%lx (\"%s\")\n",
(unsigned long)pth_current, pth_current->name);
pth_dumpqueue(fp, "WAITING", &pth_WQ);
pth_dumpqueue(fp, "SUSPENDED", &pth_SQ);
pth_dumpqueue(fp, "DEAD", &pth_DQ);
fprintf(fp, "+----------------------------------------------------------------------\n");
return;
}
intern void pth_dumpqueue(FILE *fp, const char *qn, pth_pqueue_t *q)
{
pth_t t;
int n;
int i;
fprintf(fp, "| Thread Queue %s:\n", qn);
n = pth_pqueue_elements(q);
if (n == 0)
fprintf(fp, "| no threads\n");
i = 1;
for (t = pth_pqueue_head(q); t != NULL; t = pth_pqueue_walk(q, t, PTH_WALK_NEXT)) {
fprintf(fp, "| %d. thread 0x%lx (\"%s\")\n", i++, (unsigned long)t, t->name);
}
return;
}