From e90b65284812aaa5ff9e9935ce9bbad7791cbbcd Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 15 Jul 2002 10:35:28 +0000 Subject: updated the 3.0 branch from the head branch - ready for alpha18 (This used to be commit 03ac082dcb375b6f3ca3d810a6a6367542bc23ce) --- source3/lib/debug.c | 363 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 290 insertions(+), 73 deletions(-) (limited to 'source3/lib/debug.c') diff --git a/source3/lib/debug.c b/source3/lib/debug.c index 7960c32b58..f41c3b6497 100644 --- a/source3/lib/debug.c +++ b/source3/lib/debug.c @@ -2,6 +2,8 @@ Unix SMB/CIFS implementation. Samba utility functions Copyright (C) Andrew Tridgell 1992-1998 + Copyright (C) Elrond 2002 + Copyright (C) Simo Sorce 2002 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -81,11 +83,23 @@ XFILE *dbf = NULL; pstring debugf = ""; BOOL append_log = False; +BOOL debug_warn_unknown_class = True; +BOOL debug_auto_add_unknown_class = True; +BOOL AllowDebugChange = True; -int DEBUGLEVEL_CLASS[DBGC_LAST]; -BOOL DEBUGLEVEL_CLASS_ISSET[DBGC_LAST]; -int DEBUGLEVEL = DEBUGLEVEL_CLASS; -BOOL AllowDebugChange = True; +/* + * This is to allow assignment to DEBUGLEVEL before the debug + * system has been initialised. + */ +static int debug_all_class_hack = 1; +static BOOL debug_all_class_isset_hack = True; + +static int debug_num_classes = 0; +int *DEBUGLEVEL_CLASS = &debug_all_class_hack; +BOOL *DEBUGLEVEL_CLASS_ISSET = &debug_all_class_isset_hack; + +/* DEBUGLEVEL is #defined to *debug_level */ +int DEBUGLEVEL = &debug_all_class_hack; /* -------------------------------------------------------------------------- ** @@ -129,46 +143,230 @@ static BOOL log_overflow = False; * white space. There must be one name for each DBGC_, and they * must be in the table in the order of DBGC_.. */ -char *classname_table[] = { +static const char *default_classname_table[] = { "all", /* DBGC_ALL; index refs traditional DEBUGLEVEL */ "tdb", /* DBGC_TDB */ "printdrivers", /* DBGC_PRINTDRIVERS */ "lanman", /* DBGC_LANMAN */ "smb", /* DBGC_SMB */ - "rpc", /* DBGC_RPC */ - "rpc_hdr", /* DBGC_RPC_HDR */ - "bdc", /* DBGC_BDC */ + "rpc_parse", /* DBGC_RPC_PARSE */ + "rpc_srv", /* DBGC_RPC_SRV */ + "rpc_cli", /* DBGC_RPC_CLI */ + "passdb", /* DBGC_PASSDB */ + "auth", /* DBGC_AUTH */ + "winbind", /* DBGC_WINBIND */ + NULL }; +static char **classname_table = NULL; + /* -------------------------------------------------------------------------- ** * Functions... */ + +/**************************************************************************** +utility lists registered debug class names's +****************************************************************************/ + +#define MAX_CLASS_NAME_SIZE 1024 + +static char *debug_list_class_names_and_levels(void) +{ + int i, dim; + char **list; + char *buf = NULL; + char *b; + BOOL err = False; + + if (DEBUGLEVEL_CLASS == &debug_all_class_hack) + return NULL; + + list = calloc(debug_num_classes + 1, sizeof(char *)); + if (!list) + return NULL; + + /* prepare strings */ + for (i = 0, dim = 0; i < debug_num_classes; i++) { + int l = asprintf(&list[i], + "%s:%d ", + classname_table[i], + DEBUGLEVEL_CLASS_ISSET[i]?DEBUGLEVEL_CLASS[i]:DEBUGLEVEL); + if (l < 0 || l > MAX_CLASS_NAME_SIZE) { + err = True; + goto done; + } + dim += l; + } + + /* create single string list */ + b = buf = malloc(dim); + if (!buf) { + err = True; + goto done; + } + for (i = 0; i < debug_num_classes; i++) { + int l = strlen(list[i]); + strncpy(b, list[i], l); + b = b + l; + } + b[-1] = '\0'; + +done: + /* free strings list */ + for (i = 0; i < debug_num_classes; i++) + if (list[i]) free(list[i]); + free(list); + + if (err) { + if (buf) + free(buf); + return NULL; + } else { + return buf; + } +} + /**************************************************************************** utility access to debug class names's ****************************************************************************/ -char* debug_classname_from_index(int ndx) +const char *debug_classname_from_index(int ndx) { - return classname_table[ndx]; + if (ndx < 0 || ndx >= debug_num_classes) + return NULL; + else + return classname_table[ndx]; } /**************************************************************************** -utility to translate names to debug class index's +utility to translate names to debug class index's (internal version) ****************************************************************************/ -int debug_lookup_classname(char* classname) +static int debug_lookup_classname_int(const char* classname) { int i; if (!classname) return -1; - for (i=0; i= 0) + return ndx; + ndx = debug_num_classes; + + new_ptr = DEBUGLEVEL_CLASS; + if (DEBUGLEVEL_CLASS == &debug_all_class_hack) + { + /* Initial loading... */ + new_ptr = NULL; + } + new_ptr = Realloc(new_ptr, + sizeof(int) * (debug_num_classes + 1)); + if (!new_ptr) + return -1; + DEBUGLEVEL_CLASS = new_ptr; + DEBUGLEVEL_CLASS[ndx] = 0; + + /* debug_level is the pointer used for the DEBUGLEVEL-thingy */ + if (ndx==0) + { + /* Transfer the initial level from debug_all_class_hack */ + DEBUGLEVEL_CLASS[ndx] = DEBUGLEVEL; + } + debug_level = DEBUGLEVEL_CLASS; + + new_ptr = DEBUGLEVEL_CLASS_ISSET; + if (new_ptr == &debug_all_class_isset_hack) + { + new_ptr = NULL; + } + new_ptr = Realloc(new_ptr, + sizeof(BOOL) * (debug_num_classes + 1)); + if (!new_ptr) + return -1; + DEBUGLEVEL_CLASS_ISSET = new_ptr; + DEBUGLEVEL_CLASS_ISSET[ndx] = False; + + new_ptr = Realloc(classname_table, + sizeof(char *) * (debug_num_classes + 1)); + if (!new_ptr) + return -1; + classname_table = new_ptr; + + classname_table[ndx] = strdup(classname); + if (! classname_table[ndx]) + return -1; + + debug_num_classes++; + + return ndx; +} + +/**************************************************************************** +utility to translate names to debug class index's (public version) +****************************************************************************/ +int debug_lookup_classname(const char *classname) +{ + int ndx; + + if (!classname || !*classname) return -1; + + ndx = debug_lookup_classname_int(classname); + + if (ndx != -1) + return ndx; + + if (debug_warn_unknown_class) + { + DEBUG(0, ("debug_lookup_classname(%s): Unknown class\n", + classname)); + } + if (debug_auto_add_unknown_class) + { + return debug_add_class(classname); + } + return -1; +} + + +/**************************************************************************** +dump the current registered denug levels +****************************************************************************/ +static void debug_dump_status(int level) +{ + int q; + + DEBUG(level, ("INFO: Current debug levels:\n")); + for (q = 0; q < debug_num_classes; q++) + { + DEBUGADD(level, (" %s: %s/%d\n", + classname_table[q], + (DEBUGLEVEL_CLASS_ISSET[q] + ? "True" : "False"), + DEBUGLEVEL_CLASS[q])); + } +} + /**************************************************************************** parse the debug levels from smbcontrol. Example debug level parameter: printdrivers:7 @@ -179,9 +377,9 @@ BOOL debug_parse_params(char **params, int *debuglevel_class, int i, ndx; char *class_name; char *class_level; - - /* Set the new debug level array to the current DEBUGLEVEL array */ - memcpy(debuglevel_class, DEBUGLEVEL_CLASS, sizeof(DEBUGLEVEL_CLASS)); + + if (!params) + return False; /* Allow DBGC_ALL to be specified w/o requiring its class name e.g."10" * v.s. "all:10", this is the traditional way to set DEBUGLEVEL @@ -195,7 +393,7 @@ BOOL debug_parse_params(char **params, int *debuglevel_class, i = 0; /* DBGC_ALL not specified OR class name was included */ /* Fill in new debug class levels */ - for (; i < DBGC_LAST && params[i]; i++) { + for (; i < debug_num_classes && params[i]; i++) { if ((class_name=strtok(params[i],":")) && (class_level=strtok(NULL, "\0")) && ((ndx = debug_lookup_classname(class_name)) != -1)) { @@ -215,83 +413,102 @@ parse the debug levels from smb.conf. Example debug level string: 3 tdb:5 printdrivers:7 Note: the 1st param has no "name:" preceeding it. ****************************************************************************/ -BOOL debug_parse_levels(char *params_str) +BOOL debug_parse_levels(const char *params_str) { - int i; - char *params[DBGC_LAST]; - int debuglevel_class[DBGC_LAST]; - BOOL debuglevel_class_isset[DBGC_LAST]; + char **params; + + /* Just in case */ + debug_init(); if (AllowDebugChange == False) return True; - ZERO_ARRAY(params); - ZERO_ARRAY(debuglevel_class); - ZERO_ARRAY(debuglevel_class_isset); - - if ((params[0]=strtok(params_str," ,"))) { - for (i=1; idebuglevel_class, sizeof(dm->debuglevel_class)); - memcpy(DEBUGLEVEL_CLASS_ISSET, dm->debuglevel_class_isset, sizeof(dm->debuglevel_class_isset)); + /* Check, it's a proper string! */ + if (params_str[len-1] != '\0') + { + DEBUG(1, ("Invalid debug message from pid %u to pid %u\n", + (unsigned int)src, (unsigned int)getpid())); + return; + } - DEBUG(3,("INFO: Debug class %s level = %d (pid %u from pid %u)\n", - classname_table[DBGC_ALL], - DEBUGLEVEL_CLASS[DBGC_ALL], (unsigned int)sys_getpid(), (unsigned int)src)); + DEBUG(3, ("INFO: Remote set of debug to `%s' (pid %u from pid %u)\n", + params_str, (unsigned int)getpid(), (unsigned int)src)); - for (i=1; i