dbug.cc 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.cc
//
// 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 by Sun Microsystems, Inc.
#ifndef DBUG_OFF
#include <stdio.h>
#include <stdlib.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
static void changeowner(char *pathname);
static void doabort();
// initialize static members of class
int dbug_routine::sd_on = 0;
long dbug_routine::sd_lineno = 0;
// this structure defines thread specific data
struct thread_data {
unsigned long td_stackinit; // Begining of stack.
int td_line; // Current line number.
const char *td_keyword; // Current keyword.
};
#ifdef _REENTRANT
int mdt_once = 0;
#else
#endif
// ------------------------------------------------------------
//
// dbug_routine
//
// 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:
{
int created = 0;
#ifdef _REENTRANT
if (!mdt_once) {
doabort();
mdt_once++;
}
doabort();
created = 1;
}
#else
#endif
// save the function name
if (function)
else
d_func = "unknown";
// save the base of the file name
if (file) {
else
d_file++;
} else
d_file = "unknown";
// Chain this onto our list of them
// set the default routine exit point line number to zero
d_leaveline = 0;
// If debugging is off, then all done
if (NOT db_debugon())
goto out;
// get a pointer to the active state
// 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
// record the new nesting level
// if producing a trace of function calls
else
}
// if a new thread
}
out:;
#ifdef _REENTRANT
#endif
}
// ------------------------------------------------------------
//
// ~dbug_routine
//
// Description:
// Destructor for the dbug_routine class.
// Unchains this object from the list.
// Arguments:
// Returns:
// Errors:
// Preconditions:
{
#ifdef _REENTRANT
#else
#endif
// 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.
if (this != cur) {
"<%s, ERROR: dbug_enter/dbug_leave out of sequence.\n",
d_func);
}
// if producing a trace of function calls
}
// record the new nesting level
out:;
#ifdef _REENTRANT
#endif
}
// ------------------------------------------------------------
//
// db_leave
//
// Description:
// Indicates the line number that the routine exits at.
// Arguments:
// line - the line number on or one before the return
// statement is executed at.
// Returns:
// Errors:
// Preconditions:
void
{
d_leaveline = line;
}
// ------------------------------------------------------------
//
// 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
{
// return FALSE if not debugging
if (NOT db_debugon())
return (0);
#ifdef _REENTRANT
#endif
int ret = 0;
// return FALSE if not debugging
if (NOT db_debugon())
goto out;
// get a pointer to the active state
ret = 1;
goto out;
}
}
}
}
out:
#ifdef _REENTRANT
#endif
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;
#ifdef _REENTRANT
#else
#endif
}
// ------------------------------------------------------------
//
// 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;
#ifdef _REENTRANT
#else
#endif
// return if keyword not selected
return;
#ifdef _REENTRANT
#endif
// get a pointer to the active state
else
if (tdp->td_keyword)
#ifdef _REENTRANT
#endif
}
// ------------------------------------------------------------
//
// 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;
// If the specified keyword is enabled
if (db_keyword(keyword)) {
// perform setup for using db_printf
// Output a header message
db_printf("Stack Trace");
// walk the stack of dbug_routine objects
for (dbug_routine *pdr = this;
// output the routine name
db_printf(" %s() (%s)",
}
}
}
// -----------------------------------------------------------------
//
// 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");
db_printf("Assertion Failed %s:%s():%d \"%s\"",
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");
db_printf("Precondition Failed %s:%s():%d \"%s\"",
doabort();
}
// ------------------------------------------------------------
//
// dbug_routine::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.
const char *
{
char *dupcontrol = NULL;
#ifdef _REENTRANT
#endif
// error if the control string is NULL
res = "mdbug: control string is NULL";
goto out;
}
// turn debugging flag off
// get the level from the old state if it exists
int level;
level = 0;
else
// Create a new state
res = "mdbug: out of memory, dbug_state";
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) {
res = "mdbug: out of memory, strdup";
goto out;
}
// parse the control string
register char *scan;
int retval;
switch (*scan++) {
case 'd': // debugging on
if (*scan++ == ',') {
if (retval < 0) {
res = "mdbug: -d too many keywords";
goto out;
}
}
break;
case 'D': // specify delay value
if (*scan++ == ',') {
if (retval < 0) {
res = "mdbug: -D too many delays";
goto out;
}
}
}
break;
case 'f': // list of functions to watch
if (*scan++ == ',') {
if (retval < 0) {
res = "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 numbers 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) {
res = "mdbug: -o too many"
" output files";
goto out;
}
pst->s_out_file =
} else
} else
res = "mdbug: -o cannot open output file";
goto out;
}
break;
case 'p': // debug specified processes
if (*scan++ == ',') {
if (retval < 0) {
res = "mdbug: -p too many processes";
goto out;
}
}
break;
case 'P': // print process name on dbug output
break;
case 'r': // reset indentation to zero
break;
case 's': // print stack depth on enter
break;
case 'R': // print time program has been running
break;
case 'T': // print thread information
break;
case 't': // print trace of functions called
if (*scan++ == ',') {
if (retval < 0) {
res = "mdbug: -t too many traces";
goto out;
}
pst->s_maxdepth =
}
}
break;
}
}
out:
// free up the dupped control string
#ifdef _REENTRANT
#endif
// return result
return (res);
}
// ------------------------------------------------------------
//
// dbug_routine::db_pop
//
// Description:
// Pop the debug stack.
void
{
#ifdef _REENTRANT
#endif
// return if no debugging yet
goto out;
// get and remove the top item from the list
// Delete the item.
delete pst;
// get the current top of the stack
if (pst) {
// See if debugging is turned on
else
}
out:;
#ifdef _REENTRANT
#endif
}
// -----------------------------------------------------------------
//
// db_process
//
// Description:
// Specifies the name of the process.
// Only the pointer is saved, the string is not copied.
// Arguments:
// namep
// Returns:
// Preconditions:
void
{
sd_process = namep;
#ifdef _REENTRANT
#else
#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
{
// 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 "linkp". Linkp points to the first
// link in the list. If linkp 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
{
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
{
}
}
}
}
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
#endif
}
// ------------------------------------------------------------
//
// 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
{
#ifdef _REENTRANT
#endif
// If debugging is off, then nothing else to do
if (NOT db_debugon())
goto out;
// get a pointer to the active state
}
out:;
#ifdef _REENTRANT
#endif
}
// -----------------------------------------------------------------
//
// doabort
//
// Description:
// Causes the process to exit immediatly with a core dump.
// Arguments:
// Returns:
// Preconditions:
static void
doabort()
{
for (;;) {
}
}
// -----------------------------------------------------------------
//
// dbug_state::dbug_state
//
// Description:
// Constructor for the dbug_state class.
// Arguments:
// The current level in the call stack.
// Returns:
// Preconditions:
{
sf_trace = 0;
sf_debug = 0;
sf_file = 0;
sf_line = 0;
sf_depth = 0;
sf_process = 0;
sf_number = 0;
sf_pid = 0;
sf_stack = 0;
sf_time = 0;
sf_didopen = 0;
sf_thread = 0;
s_delay = 0;
s_starttime = 0;
s_out_file = stderr;
}
// -----------------------------------------------------------------
//
// dbug_state::~dbug_state
//
// Description:
// Destructor for the dbug_state class.
// Arguments:
// Returns:
// Preconditions:
dbug_state::~dbug_state()
{
if (sf_didopen)
}
#endif /* DBUG_OFF */