diff options
| author | Donald Sharp <donaldsharp72@gmail.com> | 2022-01-14 14:15:03 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-14 14:15:03 -0500 |
| commit | 25c44cf5de2a71c771996e7703819decf4299b18 (patch) | |
| tree | 9b9db8cbe2520456c6964f6028144c1769572e47 /lib/thread.c | |
| parent | cce7c33396e8b30f92e3474860f78d0e21d01a57 (diff) | |
| parent | 54929fd38a352bd8096383a7aa65cc89e0c8ab0c (diff) | |
Merge pull request #10339 from opensourcerouting/printfrr-20220114
lib: printfrr shenanigans
Diffstat (limited to 'lib/thread.c')
| -rw-r--r-- | lib/thread.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/lib/thread.c b/lib/thread.c index 7b223ed6de..77e34f48f3 100644 --- a/lib/thread.c +++ b/lib/thread.c @@ -2056,3 +2056,70 @@ bool thread_is_scheduled(struct thread *thread) return true; } + +static ssize_t printfrr_thread_dbg(struct fbuf *buf, struct printfrr_eargs *ea, + const struct thread *thread) +{ + static const char * const types[] = { + [THREAD_READ] = "read", + [THREAD_WRITE] = "write", + [THREAD_TIMER] = "timer", + [THREAD_EVENT] = "event", + [THREAD_READY] = "ready", + [THREAD_UNUSED] = "unused", + [THREAD_EXECUTE] = "exec", + }; + ssize_t rv = 0; + char info[16] = ""; + + if (!thread) + return bputs(buf, "{(thread *)NULL}"); + + rv += bprintfrr(buf, "{(thread *)%p arg=%p", thread, thread->arg); + + if (thread->type < array_size(types) && types[thread->type]) + rv += bprintfrr(buf, " %-6s", types[thread->type]); + else + rv += bprintfrr(buf, " INVALID(%u)", thread->type); + + switch (thread->type) { + case THREAD_READ: + case THREAD_WRITE: + snprintfrr(info, sizeof(info), "fd=%d", thread->u.fd); + break; + + case THREAD_TIMER: + snprintfrr(info, sizeof(info), "r=%pTVMud", &thread->u.sands); + break; + } + + rv += bprintfrr(buf, " %-12s %s() %s from %s:%d}", info, + thread->xref->funcname, thread->xref->dest, + thread->xref->xref.file, thread->xref->xref.line); + return rv; +} + +printfrr_ext_autoreg_p("TH", printfrr_thread); +static ssize_t printfrr_thread(struct fbuf *buf, struct printfrr_eargs *ea, + const void *ptr) +{ + const struct thread *thread = ptr; + struct timespec remain = {}; + + if (ea->fmt[0] == 'D') { + ea->fmt++; + return printfrr_thread_dbg(buf, ea, thread); + } + + if (!thread) { + /* need to jump over time formatting flag characters in the + * input format string, i.e. adjust ea->fmt! + */ + printfrr_time(buf, ea, &remain, + TIMEFMT_TIMER_DEADLINE | TIMEFMT_SKIP); + return bputch(buf, '-'); + } + + TIMEVAL_TO_TIMESPEC(&thread->u.sands, &remain); + return printfrr_time(buf, ea, &remain, TIMEFMT_TIMER_DEADLINE); +} |
