[Libsysio-commit] HEAD: libsysio/src init.c module.mk
Brought to you by:
lward
From: Lee W. <lw...@us...> - 2007-08-20 19:12:12
|
Update of /cvsroot/libsysio/libsysio/src In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv15038/src Modified Files: init.c module.mk Log Message: Split tracing support out. Added an initializer queue so that trace functionality may be added external to this library. to use it, register your initializer function with _sysio_register_trace using the _sysio_initializer_trace_q. Only the "func" argument is valid when the pseudo-event is called from _sysio_boot_trace. You might want to remove your initializer event from the initializer queue once called, unless it's ok to call it more than once. Index: init.c =================================================================== RCS file: /cvsroot/libsysio/libsysio/src/init.c,v retrieving revision 1.37 retrieving revision 1.38 diff -u -w -b -B -p -r1.37 -r1.38 --- init.c 20 Aug 2007 16:02:56 -0000 1.37 +++ init.c 20 Aug 2007 19:12:08 -0000 1.38 @@ -41,23 +41,9 @@ * le...@sa... */ -#ifdef __linux__ -#define _BSD_SOURCE -#endif - -#ifdef SYSIO_TRACING -#include <stdio.h> -#endif #include <stdlib.h> -#ifdef _BSD_SOURCE -#include <sys/syscall.h> -#endif -#include <unistd.h> #include <string.h> #include <errno.h> -#ifdef SYSIO_TRACING -#include <stdarg.h> -#endif #include <limits.h> #include <assert.h> #include <sys/types.h> @@ -67,10 +53,6 @@ #include <sys/queue.h> #include "sysio.h" -#include "xtio.h" -#ifdef SYSIO_TRACING -#include "native.h" -#endif #include "inode.h" #include "fs.h" #include "mount.h" @@ -81,48 +63,6 @@ #include "stdfd.h" #endif -#ifdef SYSIO_TRACING - -/* - * Tracing callback record. - */ -struct trace_callback { - TAILQ_ENTRY(trace_callback) links; /* trace list links */ - void (*f)(const char *file, /* callback function */ - const char *func, - int line, - void *data, - int tag, - const char *fmt, - va_list ap); - void *data; /* callback data */ - void (*destructor)(void *data); /* data destructor */ -}; - -/* - * Initialize a tracing callback record. - */ -#define TCB_INIT(__tcb, __f, __d, __destroy) \ - do { \ - (__tcb)->f = (__f); \ - (__tcb)->data = (__d); \ - (__tcb)->destructor = (__destroy); \ - } while (0); - -/* - * Trace queue head record. - */ -TAILQ_HEAD(trace_q, trace_callback); - -/* - * The entry and exit queue heads, and queue pointers. - */ -static struct trace_q _sysio_entry_trace_head; -void *_sysio_entry_trace_q = &_sysio_entry_trace_head; -static struct trace_q _sysio_exit_trace_head; -void *_sysio_exit_trace_q = &_sysio_exit_trace_head; -#endif - /* * White space characters. */ @@ -145,6 +85,100 @@ void *_sysio_exit_trace_q = &_sysio_exit */ #define COMMENT_INTRO '#' +#ifdef SYSIO_TRACING +static void *entry_tcb = NULL; /* entry callback */ +static void *exit_tcb = NULL; /* exit callback */ +#endif /* defined(SYSIO_TRACING) */ + +#ifdef SYSIO_TRACING +static void +trace_entry(const char *file __IS_UNUSED, + const char *func, + int line __IS_UNUSED, + void *data __IS_UNUSED, + tracing_tag tag, + const char *fmt __IS_UNUSED, + va_list ap __IS_UNUSED) +{ + + _sysio_cprintf("+ENTER+ %s (%d)\n", func, tag); +} + +static void +trace_exit(const char *file __IS_UNUSED, + const char *func, + int line __IS_UNUSED, + void *data __IS_UNUSED, + tracing_tag tag, + const char *fmt __IS_UNUSED, + va_list ap __IS_UNUSED) +{ + + _sysio_cprintf("+EXIT+ %s (%d)\n", func, tag); +} + +/* + * Start/Stop our simple tracer. + * + * Normally, these things would remove themselves from the trace + * initializers queue when called. For backward compatibility, this one + * must stick around so it can be turned on and off. Ugh! It all does get + * cleaned up at exit, though. + */ +static void +trace_initializer(const char *file __IS_UNUSED, + const char *func, + int line __IS_UNUSED, + void *data __IS_UNUSED, + tracing_tag tag __IS_UNUSED, + const char *fmt __IS_UNUSED, + va_list ap __IS_UNUSED) +{ + long l; + char *cp; + + /* + * Func is overloaded, for initializers. It contains the initializer + * argument string. + * + * We are looking for an integer value. If non-zero, turn our + * little tracer on. Otherwise, off. + */ + l = strtol(func, (char **)&cp, 0); + if (*func != '\0' && *cp != '\0') { + /* + * Not for us. Must be some other. + */ + return; + } + if (l) { + if (!entry_tcb) + entry_tcb = + _sysio_register_trace(_sysio_entry_trace_q, + &trace_entry, + NULL, + NULL); + if (!exit_tcb) + exit_tcb = + _sysio_register_trace(_sysio_exit_trace_q, + &trace_exit, + NULL, + NULL); + } else { + if (entry_tcb) { + _sysio_remove_trace(_sysio_entry_trace_q, + entry_tcb); + entry_tcb = NULL; + } + if (exit_tcb) { + _sysio_remove_trace(_sysio_exit_trace_q, + exit_tcb); + exit_tcb = NULL; + } + } +} +#endif /* defined(SYSIO_TRACING) */ + /* * Sysio library initialization. Must be called before anything else in the * library. @@ -158,13 +192,19 @@ _sysio_init() #endif #ifdef SYSIO_TRACING - /* - * Initialize tracing callback queues. - */ - TAILQ_INIT(&_sysio_entry_trace_head); - TAILQ_INIT(&_sysio_exit_trace_head); + err = _sysio_trace_init(); + if (err) + goto error; + err = + _sysio_register_trace(_sysio_initializer_trace_q, + &trace_initializer, + NULL, + NULL) + ? 0 + : -ENOMEM; + if (err) + goto error; #endif - err = _sysio_ioctx_init(); if (err) goto error; @@ -208,18 +247,7 @@ _sysio_shutdown() { #ifdef SYSIO_TRACING - _sysio_trace_dump_stop(); - { - struct trace_callback *tcb; - - /* - * Empty the trace queues and free the entries. - */ - while ((tcb = _sysio_entry_trace_head.tqh_first) != NULL) - _sysio_remove_trace(&_sysio_entry_trace_head, tcb); - while ((tcb = _sysio_exit_trace_head.tqh_first) != NULL) - _sysio_remove_trace(&_sysio_exit_trace_head, tcb); - } + _sysio_trace_shutdown(); #endif if (!(_sysio_fd_close_all() == 0 && _sysio_unmount_all() == 0)) @@ -233,103 +260,6 @@ _sysio_shutdown() #endif } -#ifdef SYSIO_TRACING -/* - * Register a trace callback. - * - * The pointer to the trace record is returned. - */ -void * -_sysio_register_trace(void *q, - void (*f)(const char *file, - const char *func, - int line, - void *data, - int tag, - const char *fmt, - va_list ap), - void *data, - void (*destructor)(void *data)) -{ - struct trace_callback *tcb; - - tcb = malloc(sizeof(struct trace_callback)); - if (!tcb) - return NULL; - TCB_INIT(tcb, f, data, destructor); - TAILQ_INSERT_TAIL((struct trace_q *)q, tcb, links); - return tcb; -} - -/* - * Remove a registered trace callback. - */ -void -_sysio_remove_trace(void *q, void *p) -{ - struct trace_callback *tcb; - - tcb = (struct trace_callback *)p; - - if (tcb->destructor) - (*tcb->destructor)(tcb->data); - TAILQ_REMOVE((struct trace_q *)q, tcb, links); - free(tcb); -} - -void -/* - * Run a trace queue, making all the callbacks. - */ -_sysio_run_trace_q(void *q, - const char *file, - const char *func, - int line, - int tag, - const char *fmt, - ...) -{ - va_list ap, aq; - struct trace_callback *tcb; - - va_start(ap, fmt); - tcb = ((struct trace_q *)q)->tqh_first; - while (tcb) { - va_copy(aq, ap); - (*tcb->f)(file, func, line, tcb->data, tag, fmt, ap); - va_end(aq); - tcb = tcb->links.tqe_next; - } - va_end(ap); -} - -static void -_sysio_trace_entry(const char *file __IS_UNUSED, - const char *func, - int line __IS_UNUSED, - void *data __IS_UNUSED, - tracing_tag tag, - const char *fmt __IS_UNUSED, - va_list ap __IS_UNUSED) -{ - - _sysio_cprintf("+ENTER+ %s (%d)\n", func, tag); -} - -static void -_sysio_trace_exit(const char *file __IS_UNUSED, - const char *func, - int line __IS_UNUSED, - void *data __IS_UNUSED, - tracing_tag tag, - const char *fmt __IS_UNUSED, - va_list ap __IS_UNUSED) -{ - - _sysio_cprintf("+EXIT+ %s (%d)\n", func, tag); -} -#endif /* defined(SYSIO_TRACING) */ - /* * (kind of)Duplicates strtok function. * @@ -857,102 +787,23 @@ do_command(char *buf) static int _sysio_boot_tracing(const char *arg) { - int err; - const char *dirpath; - long l; - char *cp; - int stop; - struct hook { - struct trace_q *q; - void (*f)(const char *file, - const char *func, - int line, - void *data, - int tag, - const char *fmt, - va_list ap); - void *data; - void (*destructor)(void *data); - struct trace_callback *cb; - }; - static struct hook hooks[] = { - { - &_sysio_entry_trace_head, - _sysio_trace_entry, - NULL, - NULL, - NULL - }, - { - &_sysio_exit_trace_head, - _sysio_trace_exit, - NULL, - NULL, - NULL - }, - { - NULL, - NULL, - NULL, - NULL, - NULL - } - }; - struct hook *h; - err = 0; - dirpath = NULL; - l = strtol(arg, (char **)&cp, 0); - if (*arg != '\0' && *cp != '\0') - dirpath = arg; - stop = 0; - if (dirpath) - err = _sysio_trace_dump_start(dirpath); - else if (l) { - /* - * Start console tracing. - */ - do { - if (l != 1) { - err = -EINVAL; - break; - } - for (h = hooks; h->f != NULL; h++) { - if (h->cb) - continue; - h->cb = - _sysio_register_trace(h->q, - h->f, - h->data, - h->destructor); - if (!h->cb) { - err = -ENOMEM; - break; - } - } - } while (0); - } else { - /* - * Trace dump stop. - */ - stop = 1; - } - if (!(dirpath || l) || err) { - /* - * Trace dump stop. - */ - _sysio_trace_dump_stop(); + if (!arg) { /* - * Stop console tracing. + * NULL arg means not set at all. We really shouldn't + * have even been called. However, sometimes we are. + * It's not an error, then. Just a no-op. */ - for (h = hooks; h->f != NULL; h++) { - if (!h->cb) - continue; - _sysio_remove_trace(h->q, h->cb); - h->cb = NULL; - } + return 0; } - return err; + + _sysio_run_trace_q(_sysio_initializer_trace_q, + NULL, + arg, + -1, + -1, + NULL); + return 0; } #endif Index: module.mk =================================================================== RCS file: /cvsroot/libsysio/libsysio/src/module.mk,v retrieving revision 1.14 retrieving revision 1.15 diff -u -w -b -B -p -r1.14 -r1.15 --- module.mk 20 Aug 2007 16:02:56 -0000 1.14 +++ module.mk 20 Aug 2007 19:12:08 -0000 1.15 @@ -1,3 +1,9 @@ +if WITH_TRACING +TRACING_SRCS = src/tracing.c +else +TRACING_SRCS = +endif + if WITH_LUSTRE_HACK FILE_SUPPORT = src/file_hack.c else @@ -29,6 +35,6 @@ SRCDIR_SRCS = src/access.c src/chdir.c s src/symlink.c src/readlink.c \ src/truncate.c src/unlink.c src/utime.c \ $(FILE_SUPPORT) $(LUSTRE_SRCDIR_SRCS) \ - src/tracing.c src/cprintf.c + $(TRACING_SRCS) src/cprintf.c SRCDIR_EXTRA = src/module.mk |