dbug.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*
* dbug.c
*
* Purpose:
* Implements the dbug_routine class.
* This code is derived from the public domain DBUG
* package written by Fred Fish.
*
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/* Copyright (c) 1994-1997 by Sun Microsystems, Inc. */
#ifndef DBUG_OFF
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <thread.h>
#include <signal.h>
#include "flist.h"
#include "mdbug.h"
#include "priv.h"
/* forward references */
int indent);
static void changeowner(char *pathname);
void doabort();
/* initialize static members of class */
int sd_on = 0;
char sd_process[128];
long sd_lineno = 0;
/* this structure defines thread specific data */
typedef struct thread_data {
#ifdef STACKINIT
unsigned long td_stackinit; /* Begining of stack. */
#endif
int td_line; /* Current line number. */
#ifdef _REENTRANT
int mdt_once = 0;
#else
#endif
/*
* format of control string
* command[:command:...]
*
* commands
* debugging on 'd' d[,<keyword>[,...]]
* delay value 'D' D[,<delay value>]
* function list 'f' f[,<function name>[,...]]
* print filename 'F' F
* print pid 'i' i
* print line number 'L' L
* print call depth 'n' n
* number each line 'N' N
* output file 'o' o[,<filename>
* process name list 'p' p[,<process name>[,...]]
* print proc name 'P' P
* reset indentation 'r' r
* print runtime 'R' R
* print thread info 'T' T
* print trace 't' t
* print stack depth 's' s
*/
/*
*
* dbug_object_create
*
* Description:
* Constructor for the dbug_routine class.
* Arguments:
* line - line number where object was created.
* file - file name object was created in.
* function- routine name object was created in.
* Returns:
* Errors:
* Preconditions:
*/
void
{
int created = 0;
char *cptr;
#ifdef _REENTRANT
if (!mdt_once) {
doabort();
mdt_once++;
}
doabort();
created = 1;
}
#else
#endif
if (dbug_object_p == NULL)
doabort();
/* save the function name */
if (function)
else
/* save the base of the file name */
if (file) {
else
} else
/* Chain this onto our list of them */
/* set the default routine exit point line number to zero */
dbug_object_p->d_leaveline = 0;
/* If debugging is off, then all done */
if (NOT db_debugon())
goto out;
/* if the active state is null initialize it */
db_push("d,:f,:F:i:L:n:N:o,cfsd_debug.out:p,:P:r:R:T:t:s");
/* get a pointer to the active state */
#ifdef STACKINIT
/*
* Get the new stack depth.
* There a two problems associated with this.
* One is because c++ allows declarations anywhere inside of
* a routine. So it is difficult to position the dbug_enter()
* macro after all declarations and still be useful.
* Two is that the dbug_enter() macro should be before all
* other automatic objects so that its destructor gets called
* last as the routine is returning.
* The solution is to advise placing the dbug_enter() macro at
* the start of the routine and specifying that that stack
* values apply upto but not including the current routine.
*/
if (GROWDOWN)
else
#endif
/* record the new nesting level */
/* if producing a trace of function calls */
if (dbug_state_object_p->sf_stack)
else
}
/* if a new thread */
}
out:;
}
/*
*
* dbug_object_destroy
*
* Description:
* Destructor for the dbug_routine class.
* Unchains this object from the list.
* Arguments:
* Returns:
* Errors:
* Preconditions:
*/
void
{
/* unchain from the list of objects */
/* If debugging is off, then nothing else to do */
if (NOT db_debugon())
goto out;
/* get a pointer to the active state */
/*
* Make sure the last one created is being deleted.
* This will not be the case if there are multiple dbug_routine
* objects per routine or if one is created outside of a routine.
*/
"<expected %s, actual %s, ERROR: "
"dbug_enter/dbug_leave out of sequence.\n",
/* delay(dbug_state_object_p->s_delay); */
}
/* if producing a trace of function calls */
#if 0
#endif
}
/* record the new nesting level */
out:;
}
/*
*
* db_keyword
*
* Description:
* Test a keyword to determine if it is in the currently active
* keyword list. As with the function list, a keyword is accepted
* if the list is null, otherwise it must match one of the list
* members. When debugging is not on, no keywords are accepted.
* After the maximum trace level is exceeded, no keywords are
* accepted (this behavior subject to change). Additionally,
* the current function and process must be accepted based on
* their respective lists.
* Arguments:
* keyword - the keyword to test
* Returns:
* Returns 1 if keyword accepted, 0 otherwise.
* Errors:
* Preconditions:
* precond(keyword)
*/
int
{
int ret = 0;
/* return FALSE if not debugging */
if (NOT db_debugon())
return (0);
/* return FALSE if not debugging */
if (NOT db_debugon())
goto out;
/* get a pointer to the active state */
dbug_object_p->d_func)) {
sd_process)) {
keyword)) {
ret = 1;
goto out;
}
}
}
}
out:
return (ret);
}
/*
*
* db_pargs
*
* Description:
* Saves arguments for subsequent usage by db_printf.
* Arguments:
* line - the line number the db_print occurs on
* keyword - determines whether or not to really print anything
* Returns:
* Errors:
* Preconditions:
* precond(keyword)
*/
void
{
/* return if no debugging yet */
if (NOT db_debugon())
return;
if (keyword)
else
}
int
db_getfd()
{
}
/*
*
* db_printf
*
* Description:
* Outputs the specified message if the keyword specified
* by db_pargs() has been selected. The line number specified
* by db_pargs() is also used as the line number the db_printf()
* occurs on. The format string should NOT include a terminating
* newline as one is supplied automatically.
* Arguments:
* format - printf style printing control string
* ... - additional arguments required by the control string
* Returns:
* Errors:
* Preconditions:
* precond(format)
*/
void
{
/* return if no debugging yet */
if (NOT db_debugon())
return;
/* return if keyword not selected */
return;
/* get a pointer to the active state */
if (dbug_state_object_p->sf_trace)
else
if (tdp->td_keyword[0])
tdp->td_keyword);
}
/*
*
* db_traceprint
*
* Description:
* Prints out a trace of the call stack.
* Arguments:
* line - the line number where this call was made
* keyword - keyword to test against
* Returns:
* Errors:
* Preconditions:
*/
void
{
/* return if no debugging yet */
if (NOT db_debugon())
return;
doabort();
/* If the specified keyword is enabled */
/* perform setup for using db_printf */
/* Output a header message */
/* walk the stack of dbug_routine objects */
/* output the routine name */
}
}
}
/*
*
* db_assert
*
* Description:
* Called when an assert fails.
* Prints out a stack trace and aborts.
* Arguments:
* line line number assert occurred at
* msgp string form of assert code that failed
* Returns:
* Preconditions:
* precond(msgp)
*/
void
{
if (NOT db_debugon())
db_push("-#:d");
doabort();
}
/*
*
* db_precond
*
* Description:
* Called when an precond fails.
* Prints out a stack trace and aborts.
* Arguments:
* line line number precond occurred at
* msgp string form of precond code that failed
* Returns:
* Preconditions:
* precond(msgp)
*/
void
{
if (NOT db_debugon())
db_push("-#:d");
doabort();
}
/*
*
* db_push
*
* Description:
* Push current debugger state and set up a new one.
* Returns NULL if no errors, an error string if there
* is an error.
*
* format of control string
* command[:command:...]
*
* commands
* debugging on 'd' d[,<keyword>[,...]]
* delay value 'D' D[,<delay value>]
* function list 'f' f[,<function name>[,...]]
* print filename 'F' F
* print pid 'i' i
* print line number 'L' L
* print call depth 'n' n
* number each line 'N' N
* output file 'o' o[,<filename>
* process name list 'p' p[,<process name>[,...]]
* print proc name 'P' P
* reset indentation 'r' r
* print runtime 'R' R
* print thread info 'T' T
* print trace 't' t
* print stack depth 's' s
*/
char *
{
char *dupcontrol = NULL;
register char *scan;
int retval;
char res[100];
int level;
/* error if the control string is NULL */
goto out;
}
/* turn debugging flag off */
/* get the level from the old state if it exists */
level = 0;
else
/* Create a new state */
if (dbug_state_object_p == NULL) {
goto out;
}
/* add it to our list of states and make it the current one */
/* Strip off -# if in the control string */
control += 2;
/* make a copy of the control string so we can modify it with strtok */
if (dupcontrol == NULL) {
goto out;
}
/* parse the control string */
switch (*scan++) {
case 'd': /* debugging on */
if (*scan++ == ',') {
if (retval < 0) {
"mdbug: -d too many keywords");
goto out;
}
}
break;
case 'D': /* specify delay value */
dbug_state_object_p->s_delay = 0;
if (*scan++ == ',') {
if (retval < 0) {
"mdbug: -D too many delays");
goto out;
}
if (flist_object_p->f_count > 0) {
(char *)fl_top(flist_object_p)));
}
}
break;
case 'f': /* list of functions to watch */
if (*scan++ == ',') {
if (retval < 0) {
"mdbug: -f too many functions");
goto out;
}
}
break;
case 'F': /* print file name with dbug output */
break;
case 'i': /* print pid with dbug output */
break;
case 'L': /* print line nums with dbug output */
break;
case 'n': /* print function call depth */
break;
case 'N': /* number each line of dbug output */
break;
case 'o': /* specifies output file for dbug */
if (*scan++ == ',') {
if (retval < 0) {
"mdbug: -o too many output files");
goto out;
}
if (flist_object_p->f_count > 0) {
openfile((char *)
if (dbug_state_object_p->s_out_file !=
NULL)
= 1;
} else
} else
"mdbug: -o cannot open output file");
goto out;
}
break;
case 'p': /* debug specified processes */
if (*scan++ == ',') {
if (retval < 0) {
"mdbug: -p too many processes");
goto out;
}
}
break;
case 'P': /* print process name on dbug output */
break;
case 'r': /* reset indentation to zero */
dbug_state_object_p->s_level = 0;
break;
case 's': /* print stack depth on enter */
break;
case 'R': /* print time prog has been running */
break;
case 'T': /* print thread information */
break;
case 't': /* print trace of functions called */
if (*scan++ == ',') {
if (retval < 0) {
"mdbug: -t too many traces");
goto out;
}
if (flist_object_p->f_count > 0) {
atoi((char *)
}
}
break;
}
}
out:
/* free up the dupped control string */
/* return result */
return (NULL);
}
/*
*
* db_pop
*
* Description:
* Pop the debug stack.
*/
void
db_pop()
{
/* return if no debugging yet */
goto out;
/* get and remove the top item from the list */
/* Delete the item. */
/* get the current top of the stack */
if (dbug_state_object_p) {
/* See if debugging is turned on */
if (dbug_state_object_p->sf_debug)
else
}
out:;
}
/*
*
* db_process
*
* Description:
* Specifies the name of the process.
* Only the pointer is saved, the string is not copied.
* Arguments:
* namep
* Returns:
* Preconditions:
*/
void
db_process(const char *namep)
{
#ifdef STACKINIT
#endif
}
/*
*
* listparse
*
* Description:
* parse list of modifiers in debug control string
*
* Given pointer to a comma separated list of strings in "cltp",
* parses the list, building a list and returning a pointer to it.
* The original comma separated list is destroyed in the process of
* building the linked list, thus it had better be a duplicate
* if it is important.
*
* This routine is only called from db_push.
* Returns 0 for success, -1 for failure.
*/
static int
{
char *start;
char *item;
/* scan the string until end */
while (*ctlp != '\0') {
/* See if no more room on the list */
return (-1);
/* save the begining of this section */
/* loop until the end of the token is found */
ctlp++;
/* add a string terminator if necessary, for strdup */
if (*ctlp == ',')
*ctlp++ = '\0';
/* make a copy of the string */
return (-1);
/* add it to the list */
}
return (0);
}
/*
*
* inlist
*
* Description:
* Tests the string pointed to by "cp" to determine if it is in
* the list pointed to by "flist_object_p". Linkp points to the first
* link in the list. If flist_object_p is empty then the string is treated
* as if it is in the list (I.E all strings are in the null list).
* This may seem rather strange at first but leads to the desired
* operation if no list is given. The net effect is that all
* strings will be accepted when there is no list, and when there
* is a list, only those strings in the list will be accepted.
*/
static boolean
{
register char *item;
else {
/* walk the list of items */
/* see if a match */
break;
}
}
}
return (accept);
}
/*
*
* dotrace
*
* Description:
* Checks to see if tracing is enabled based on whether the
* user has specified tracing, the maximum trace depth has
* not yet been reached, the current function is selected,
* and the current process is selected. Returns TRUE if
* tracing is enabled, FALSE otherwise.
*/
static boolean
const char *process)
{
if (dbug_state_object_p->sf_trace) {
if (dbug_state_object_p->s_level <=
process)) {
}
}
}
}
return (trace);
}
/*
*
* indent
*
* Description:
* Indent a line to the given level. Note that this is
* a simple minded but portable implementation.
* There are better ways.
*
* Also, the indent must be scaled by the compile time option
* of character positions per nesting level.
*/
static void
{
register int count;
for (count = 0;
count++) {
else
}
}
/*
*
* doprefix
*
* Description:
* Print prefix common to all debugger output lines, prior to
* doing indentation if necessary. Print such information as
* current process name, current source file name and line number,
* and current function nesting depth.
*/
static void
{
#if DBUG_UNIX
if (dbug_state_object_p->sf_pid)
#endif
if (dbug_state_object_p->sf_thread)
if (dbug_state_object_p->sf_number)
if (dbug_state_object_p->sf_file)
if (dbug_state_object_p->sf_line)
if (dbug_state_object_p->sf_depth)
}
/*
*
* openfile
*
* Description:
* Given name of a new file (or NULL for stdout) opens the file
* and sets the output stream to the new file.
*/
static FILE *
{
return (stdout);
return (NULL);
/* see if the file already exists */
if (file_exists(name))
else
/* open the file */
return (NULL);
/*
* If the file is newly created, give it away to the user
* that started the program.
*/
if (newfile) {
}
return (fp);
}
/*
*
* writable
*
* Description:
* Because the debugger might be linked in with a program that
* runs with the set-uid-bit (suid) set, we have to be careful
* about opening a user named file for debug output. This consists
* of checking the file for write access with the real user id,
* or checking the directory where the file will be created.
*
* Returns TRUE if the user would normally be allowed write or
* create access to the named file. Returns FALSE otherwise.
*/
static boolean
{
#if DBUG_UNIX
char *lastslash;
if (file_exists(pathname)) {
if (file_writable(pathname)) {
}
} else {
*lastslash = '\0';
} else {
pathname = ".";
}
if (file_writable(pathname)) {
}
*lastslash = '/';
}
}
return (granted);
#else
return (TRUE);
#endif
}
/*
*
* changeowner
*
* Description:
* For unix systems, change the owner of the newly created debug
* file to the real owner. This is strictly for the benefit of
* programs that are running with the set-user-id bit set.
*
* Note that at this point, the fact that pathname represents
* a newly created file has already been established. If the
* program that the debugger is linked to is not running with
* the suid bit set, then this operation is redundant (but
* harmless).
*/
static void
changeowner(char *pathname)
{
#if DBUG_UNIX
#endif
}
/*
*
* delayarg
*
* Description:
* Converts delay argument, given in tenths of a second, to the
* appropriate numerical argument used by the system to delay
* that that many tenths of a second. For example, on the
* amiga, there is a system call "Delay()" which takes an
* argument in ticks (50 per second). On unix, the sleep
* command takes seconds. Thus a value of "10", for one
* second of delay, gets converted to 50 on the amiga, and 1
* on unix. Other systems will need to use a timing loop.
*/
static int
{
unsigned int delayarg = 0;
#endif
return (delayarg);
}
/*
*
* delay
*
* Description:
* Implements the delay function.
*
* A dummy delay stub for systems that do not support delays.
* With a little work, this can be turned into a timing loop.
*/
static void
{
#endif
#if amiga
#endif
#ifdef __ZTC__
#endif
}
/*
*
* getclock
*
* Description:
* Returns the time in milliseconds used by this process
* so far.
*/
#include <sys/resource.h>
static u_long
getclock()
{
#if 0
#else
return (0);
#endif
}
#else
static u_long
getclock()
{
return (0);
}
#endif
#endif /* unix */
#ifdef MSDOS
static u_long
getclock()
{
return (clock() * 10);
}
#endif
/*
*
* mystrtok
*
* Description:
* A version of strtok for those systems without it
*/
static char *
{
register char *rtnval;
if (*end != '\0') {
end++;
}
if (*end != '\0') {
*end++ = '\0';
}
}
}
}
return (rtnval);
}
/*
*
* dbug_thread_exit
*
* Description:
* Called when a thread exits.
* Arguments:
* data pointer to thread specific data
* Returns:
* Preconditions:
*/
void
dbug_thread_exit(void *data)
{
/* If debugging is off, then nothing else to do */
if (NOT db_debugon())
goto out;
/* get a pointer to the active state */
if (dbug_state_object_p->sf_thread) {
}
out:;
}
/*
*
* doabort
*
* Description:
* Causes the process to exit immediatly with a core dump.
* Arguments:
* Returns:
* Preconditions:
*/
void
doabort()
{
for (;;) {
}
}
/*
*
* dbug_state_create
*
* Description:
* Constructor for the dbug_state class.
* Arguments:
* The current level in the call stack.
* Returns:
* Preconditions:
*/
dbug_state_create(int level)
{
if (dbug_state_object_p == NULL)
doabort();
dbug_state_object_p->sf_trace = 0;
dbug_state_object_p->sf_debug = 0;
dbug_state_object_p->sf_file = 0;
dbug_state_object_p->sf_line = 0;
dbug_state_object_p->sf_depth = 0;
dbug_state_object_p->sf_number = 0;
dbug_state_object_p->sf_pid = 0;
dbug_state_object_p->sf_stack = 0;
dbug_state_object_p->sf_time = 0;
dbug_state_object_p->sf_thread = 0;
dbug_state_object_p->s_delay = 0;
return (dbug_state_object_p);
}
/*
*
* dbug_state_destroy
*
* Description:
* Destructor for the dbug_state class.
* Arguments:
* Returns:
* Preconditions:
*/
void
{
}
/*
*
* db_debugon
*
* Description:
* Returns 1 if debugging is currently enabled, 0 otherwise.
* Arguments:
* Returns:
* Errors:
* Preconditions:
*/
int
{
return (sd_on);
}
file_exists(const char *pathname)
{
}
file_writable(const char *pathname)
{
}
{
}
#endif /* DBUG_OFF */