📄 debug.c
字号:
/* 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 the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "includes.h"/* -------------------------------------------------------------------------- ** * Defines... * * FORMAT_BUFR_MAX - Index of the last byte of the format buffer; * format_bufr[FORMAT_BUFR_MAX] should always be reserved * for a terminating null byte. */#define FORMAT_BUFR_MAX ( sizeof( format_bufr ) - 1 )/* -------------------------------------------------------------------------- ** * This module implements Samba's debugging utility. * * The syntax of a debugging log file is represented as: * * <debugfile> :== { <debugmsg> } * * <debugmsg> :== <debughdr> '\n' <debugtext> * * <debughdr> :== '[' TIME ',' LEVEL ']' [ [FILENAME ':'] [FUNCTION '()'] ] * * <debugtext> :== { <debugline> } * * <debugline> :== TEXT '\n' * * TEXT is a string of characters excluding the newline character. * LEVEL is the DEBUG level of the message (an integer in the range 0..10). * TIME is a timestamp. * FILENAME is the name of the file from which the debug message was generated. * FUNCTION is the function from which the debug message was generated. * * Basically, what that all means is: * * - A debugging log file is made up of debug messages. * * - Each debug message is made up of a header and text. The header is * separated from the text by a newline. * * - The header begins with the timestamp and debug level of the message * enclosed in brackets. The filename and function from which the * message was generated may follow. The filename is terminated by a * colon, and the function name is terminated by parenthesis. * * - The message text is made up of zero or more lines, each terminated by * a newline. *//* -------------------------------------------------------------------------- ** * External variables. * * dbf - Global debug file handle. * debugf - Debug file name. * DEBUGLEVEL - System-wide debug message limit. Messages with message- * levels higher than DEBUGLEVEL will not be processed. */XFILE *dbf = NULL;pstring debugf = "";BOOL debug_warn_unknown_class = True;BOOL debug_auto_add_unknown_class = True;BOOL AllowDebugChange = True;/* used to check if the user specified a logfile on the command line */BOOL override_logfile; /* * 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;/* -------------------------------------------------------------------------- ** * Internal variables. * * stdout_logging - Default False, if set to True then dbf will be set to * stdout and debug output will go to dbf only, and not * to syslog. Set in setup_logging() and read in Debug1(). * * debug_count - Number of debug messages that have been output. * Used to check log size. * * syslog_level - Internal copy of the message debug level. Written by * dbghdr() and read by Debug1(). * * format_bufr - Used to format debug messages. The dbgtext() function * prints debug messages to a string, and then passes the * string to format_debug_text(), which uses format_bufr * to build the formatted output. * * format_pos - Marks the first free byte of the format_bufr. * * * log_overflow - When this variable is True, never attempt to check the * size of the log. This is a hack, so that we can write * a message using DEBUG, from open_logs() when we * are unable to open a new log file for some reason. */static BOOL stdout_logging = False;static int debug_count = 0;#ifdef WITH_SYSLOGstatic int syslog_level = 0;#endifstatic pstring format_bufr = { '\0' };static size_t format_pos = 0;static BOOL log_overflow = False;/* * Define all the debug class selection names here. Names *MUST NOT* contain * white space. There must be one name for each DBGC_<class name>, and they * must be in the table in the order of DBGC_<class name>.. */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_parse", /* DBGC_RPC_PARSE */ "rpc_srv", /* DBGC_RPC_SRV */ "rpc_cli", /* DBGC_RPC_CLI */ "passdb", /* DBGC_PASSDB */ "sam", /* DBGC_SAM */ "auth", /* DBGC_AUTH */ "winbind", /* DBGC_WINBIND */ "vfs", /* DBGC_VFS */ "idmap", /* DBGC_IDMAP */ "quota", /* DBGC_QUOTA */ "acls", /* DBGC_ACLS */ "locking", /* DBGC_LOCKING */ "msdfs", /* DBGC_MSDFS */ NULL};static char **classname_table = NULL;/* -------------------------------------------------------------------------- ** * Functions... *//****************************************************************************utility lists registered debug class names's****************************************************************************/#define MAX_CLASS_NAME_SIZE 1024static 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 = SMB_CALLOC_ARRAY(char *, debug_num_classes + 1); 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 - add space for newline */ b = buf = SMB_MALLOC(dim+1); 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] = '\n'; /* replace last space with newline */ b[0] = '\0'; /* null terminate string */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.****************************************************************************/const char *debug_classname_from_index(int ndx){ if (ndx < 0 || ndx >= debug_num_classes) return NULL; else return classname_table[ndx];}/**************************************************************************** Utility to translate names to debug class index's (internal version).****************************************************************************/static int debug_lookup_classname_int(const char* classname){ int i; if (!classname) return -1; for (i=0; i < debug_num_classes; i++) { if (strcmp(classname, classname_table[i])==0) return i; } return -1;}/**************************************************************************** Add a new debug class to the system.****************************************************************************/int debug_add_class(const char *classname){ int ndx; void *new_ptr; if (!classname) return -1; /* check the init has yet been called */ debug_init(); ndx = debug_lookup_classname_int(classname); if (ndx >= 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 = SMB_REALLOC_ARRAY(new_ptr, 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 = SMB_REALLOC_ARRAY(new_ptr, BOOL, debug_num_classes + 1); if (!new_ptr) return -1; DEBUGLEVEL_CLASS_ISSET = new_ptr; DEBUGLEVEL_CLASS_ISSET[ndx] = False; new_ptr = SMB_REALLOC_ARRAY(classname_table, char *, debug_num_classes + 1); if (!new_ptr) return -1; classname_table = new_ptr; classname_table[ndx] = SMB_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 debug 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****************************************************************************/static BOOL debug_parse_params(char **params){ int i, ndx; char *class_name; char *class_level; 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 */ if (isdigit((int)params[0][0])) { DEBUGLEVEL_CLASS[DBGC_ALL] = atoi(params[0]); DEBUGLEVEL_CLASS_ISSET[DBGC_ALL] = True; i = 1; /* start processing at the next params */ } else { i = 0; /* DBGC_ALL not specified OR class name was included */ } /* Fill in new debug class levels */ 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)) { DEBUGLEVEL_CLASS[ndx] = atoi(class_level); DEBUGLEVEL_CLASS_ISSET[ndx] = True; } else { DEBUG(0,("debug_parse_params: unrecognized debug class name or format [%s]\n", params[i])); return False; } } return True;}/**************************************************************************** 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(const char *params_str){ char **params; /* Just in case */ debug_init(); if (AllowDebugChange == False) return True; params = str_list_make(params_str, NULL); if (debug_parse_params(params)) { debug_dump_status(5); str_list_free(¶ms); return True; } else { str_list_free(¶ms); return False; }}/**************************************************************************** Receive a "set debug level" message.****************************************************************************/static void debug_message(int msg_type, struct process_id src, void *buf, size_t len){ const char *params_str = buf; /* 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)procid_to_pid(&src), (unsigned int)getpid())); return; } DEBUG(3, ("INFO: Remote set of debug to `%s' (pid %u from pid %u)\n", params_str, (unsigned int)getpid(), (unsigned int)procid_to_pid(&src))); debug_parse_levels(params_str);}/**************************************************************************** Send a "set debug level" message.****************************************************************************/void debug_message_send(pid_t pid, const char *params_str){ if (!params_str) return; message_send_pid(pid_to_procid(pid), MSG_DEBUG, params_str, strlen(params_str) + 1, False);}/**************************************************************************** Return current debug level.****************************************************************************/static void debuglevel_message(int msg_type, struct process_id src, void *buf, size_t len){ char *message = debug_list_class_names_and_levels(); DEBUG(1,("INFO: Received REQ_DEBUGLEVEL message from PID %u\n", (unsigned int)procid_to_pid(&src))); message_send_pid(src, MSG_DEBUGLEVEL, message, strlen(message) + 1, True);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -