dvadura@watdragon.waterloo.edu (Dennis Vadura) (10/15/90)
Posting-number: Volume 15, Issue 75 Submitted-by: Dennis Vadura <dvadura@watdragon.waterloo.edu> Archive-name: dmake-3.6/part23 #!/bin/sh # this is part 23 of a multipart archive # do not concatenate these parts, unpack them in order with /bin/sh # file common/malloc.c continued # CurArch=23 if test ! -r s2_seq_.tmp then echo "Please unpack part 1 first!" exit 1; fi ( read Scheck if test "$Scheck" != $CurArch then echo "Please unpack part $Scheck next!" exit 1; else exit 0; fi ) < s2_seq_.tmp || exit 1 echo "x - Continuing file common/malloc.c" sed 's/^X//' << 'SHAR_EOF' >> common/malloc.c X X free( ptr ); X} X X Xchar * XMy_malloc( n, fil, line )/* X=========================== X A routine that check alloc */ Xunsigned int n; Xchar *fil; Xint line; X{ X#ifdef DB_MALLOC X _malldstr( "alloc: file:" ); X _malldstr( fil ); X _malldstr( " line: " ); X _dbdumpint( line ); X _malldstr( " :: " ); X#endif X X return( malloc( n )); X} X X X Xchar * XMy_calloc( n, size, fil, line )/* X================================= X A routine that check alloc */ Xunsigned int n; Xunsigned int size; Xchar *fil; Xint line; X{ X#ifdef DB_MALLOC X _malldstr( "alloc: file:" ); X _malldstr( fil ); X _malldstr( " line: " ); X _dbdumpint( line ); X _malldstr( " :: " ); X#endif X X return( calloc( n, size )); X} X X X X#ifdef DB_MALLOC X Xstruct _Dmi { X struct _Dmi *m_next; X struct _Dmi *m_prev; X long m_size; X char m_blk[1]; X}; X Xstatic struct _Dmi *_fab = (struct _Dmi *) 0; Xstatic struct _Dmi *_ffb = (struct _Dmi *) 0; Xstatic char *_xbrk = 0; Xstatic int _in_malloc = 0; Xstatic int _st_malloc = 0; Xint _mall_opt = 1; X X/* X * initialize stuff, we want to _malldmp() on a bus/seg error X */ X Xstatic _mall_sig(sig) { X if (sig == SIGSEGV) X _malldstr("\nsegmentation violation\n\n"); X else if (sig == SIGBUS) X _malldstr("\nbus error\n\n"); X else if (sig == SIGSYS) X _malldstr("\ninvalid syscall arg\n\n"); X else { X _malldstr("\nsignal "); X _malldptr(sig); X _malldstr("\n\n"); X } X _malldmp(); X kill(getpid(), sig); X} X Xstatic _mall_init() { X if (_st_malloc) X return; X signal(SIGSEGV, _mall_sig); X signal(SIGBUS, _mall_sig); X _st_malloc = 1; X} X X/* X * figure out which allocation block this pointer came from X * return NULL if none X */ X Xstatic struct _Dmi *_mallgb(s) Xchar *s; { X register struct _Dmi *blk; X X for (blk = _fab; blk != (struct _Dmi *) 0; blk = blk->m_next) X if (blk->m_blk == s) X break; X return blk; X} X X X/* X * internal: write a pointer in hex without using stdio X */ X Xstatic _malldptr(x) Xregister long x; { X char buf[20]; X static char hex[] = "0123456789abcdef"; X register long dx; X register char *p; X X if (x == 0) X return _malldstr("0x0(0)"); X _malldstr("0x"); X p = buf; X dx = x; X while (x > 0) X *p++ = hex[x % 16], x = x / 16; X while (p != buf) X write(2, --p, 1); X _malldstr("("); X p = buf; X x = dx; X while (x > 0) X *p++ = hex[x % 10], x /= 10; X while (p != buf) X write(2, --p, 1); X _malldstr(")"); X} X X/* X * internal: dump a string X */ X Xstatic _malldstr(s) Xregister char *s; { X register int len; X X for (len = 0; s[len] != '\0'; len++) X ; X write(2, s, len); X} X X Xstatic _dbdumpint(x) Xregister int x; { X char buf[20]; X static char hex[] = "0123456789abcdef"; X register long dx; X register char *p; X X if (x == 0) return _malldstr("0"); X p = buf; X while (x > 0) X *p++ = hex[x % 10], x /= 10; X while (p != buf) X write(2, --p, 1); X} X X X/* X * dump arena; can be called externally, and is non-destructive X */ X X_malldmp() { X register struct _Dmi *blk; X int oldf; X X oldf = _in_malloc; X _in_malloc = 0; X _malldstr("brk = "); X _malldptr(sbrk(0)); X _malldstr(" xbrk = "); X _malldptr(_xbrk); X _malldstr("\n_fab = "); X _malldptr(_fab); X _malldstr(" _ffb = "); X _malldptr(_ffb); X _malldstr(" blksiz = "); X _malldptr(sizeof (struct _Dmi)); X _malldstr("\netext = "); X _malldptr(etext); X _malldstr(" edata = "); X _malldptr(edata); X _malldstr(" end = "); X _malldptr(end); X _malldstr("\n\nallocated blocks\n\n"); X if (_fab == (struct _Dmi *) 0) X _malldstr("(none)\n"); X else { X for (blk = _fab; blk != (struct _Dmi *) 0 && (char *) blk >= _xbrk && (char *) blk < sbrk(0); blk = blk->m_next) { X _malldstr("("); X _malldptr(blk); X _malldstr(") "); X _malldptr(blk->m_prev); X _malldstr("< "); X _malldptr(blk->m_size); X _malldstr(" >"); X _malldptr(blk->m_next); X _malldstr("\n"); X } X if (blk != (struct _Dmi *) 0) X _malldstr("(subsequent block pointers corrupted)\n"); X } X _malldstr("\nfree blocks\n\n"); X if (_ffb == (struct _Dmi *) 0) X _malldstr("(none)\n"); X else { X for (blk = _ffb; blk != (struct _Dmi *) 0 && (char *) blk >= _xbrk && (char *) blk < sbrk(0); blk = blk->m_next) { X _malldstr("("); X _malldptr(blk); X _malldstr(") "); X _malldptr(blk->m_prev); X _malldstr("< "); X _malldptr(blk->m_size); X _malldstr(" >"); X _malldptr(blk->m_next); X _malldstr("\n"); X } X if (blk != (struct _Dmi *) 0) X _malldstr("(subsequent block pointers corrupted)\n"); X } X _in_malloc = oldf; X} X X/* X * internal error routine: print error message (without using stdio) and X * drop core X */ X Xstatic _mallerr(fn, s, ptr) Xchar *fn, *s; Xlong ptr; { X _malldstr(fn); X _malldstr(": "); X _malldstr(s); X _malldptr(ptr); X _malldstr("\n"); X _malldmp(); X signal(SIGQUIT, SIG_DFL); X kill(getpid(), SIGQUIT); X} X Xchar *malloc(n ) Xregister unsigned n; X{ X register struct _Dmi *blk; X X _in_malloc = 1; X _mall_init(); X if (_mall_opt) X { X _malldstr("malloc: size: " ); X _malldptr(n); X _malldstr("\n"); X } X _mallchk("malloc"); X if (n == 0) { X _malldstr("malloc(0) is illegal!\n"); X _mall_sig(SIGSYS); X } X for (blk = _ffb; blk != (struct _Dmi *) 0; blk = blk->m_next) X if (blk->m_size >= n) { X if (blk->m_next != (struct _Dmi *) 0) X blk->m_next->m_prev = blk->m_prev; X if (blk->m_prev != (struct _Dmi *) 0) X blk->m_prev->m_next = blk->m_next; X if (blk == _ffb) X _ffb = blk->m_next; X blk->m_next = _fab; X blk->m_prev = (struct _Dmi *) 0; X if (_fab != (struct _Dmi *) 0) X _fab->m_prev = blk; X _fab = blk; X _in_malloc = 0; X return blk->m_blk; X } X if ((blk = (struct _Dmi *) sbrk(sizeof (struct _Dmi) + n - 1)) == (struct _Dmi *) -1) { X _in_malloc = 0; X return (char *) 0; /* no space */ X } X if (_xbrk == (char *) 0) X _xbrk = (char *) blk; X blk->m_next = _fab; X blk->m_prev = (struct _Dmi *) 0; X if (_fab != (struct _Dmi *) 0) X _fab->m_prev = blk; X _fab = blk; X blk->m_size = n; X _in_malloc = 0; X return blk->m_blk; X} X X/* The free-block list is sorted in size order */ X Xfree(s) Xregister char *s; X{ X register struct _Dmi *blk, *fblk; X int didit; X X _in_malloc = 1; X _mall_init(); X if (_mall_opt) X { X _malldstr("free: ptr: "); X _malldptr(s); X _malldstr("\n"); X } X _mallchk("free"); X if (s == (char *) 0) { X _malldstr("free((char *) 0) is illegal!\n"); X _mall_sig(SIGSYS); X } X if ((blk = _mallgb(s)) == (struct _Dmi *) 0) X _mallerr("non-allocated pointer passed to free(): ", s); X if (blk->m_prev != (struct _Dmi *) 0) X blk->m_prev->m_next = blk->m_next; X if (blk->m_next != (struct _Dmi *) 0) X blk->m_next->m_prev = blk->m_prev; X if (blk == _fab) X _fab = blk->m_next; X if (_ffb == (struct _Dmi *) 0) { X _ffb = blk; X blk->m_next = (struct _Dmi *) 0; X blk->m_prev = (struct _Dmi *) 0; X goto crunch; X } X for (fblk = _ffb; fblk->m_next != (struct _Dmi *) 0; fblk = fblk->m_next) X if (fblk->m_next->m_size >= blk->m_size) X break; X blk->m_next = fblk->m_next; X if (fblk->m_next != (struct _Dmi *) 0) X fblk->m_next->m_prev = blk; X blk->m_prev = fblk; X fblk->m_next = blk; X X/* X * crunch the free list by dropping consecutive end-of-brk until we hit xbrk X * or a "hole" (i.e. allocated block). coalescing is possible but not supp- X * orted in malloc, so we don't bother here. X */ X Xcrunch: X didit = 1; X while (_ffb != (struct _Dmi *) 0 && didit) { X didit = 0; X for (fblk = _ffb; fblk != (struct _Dmi *) 0; fblk = fblk->m_next) X if ((char *) fblk + sizeof *fblk + fblk->m_size - 1 == sbrk(0)) { X didit = 1; X if (fblk->m_next != (struct _Dmi *) 0) X fblk->m_next->m_prev = fblk->m_prev; X if (fblk->m_prev != (struct _Dmi *) 0) X fblk->m_prev->m_next = fblk->m_next; X if (fblk == _ffb) X _ffb = fblk->m_next; X sbrk(- fblk->m_size); X break; X } X } X _in_malloc = 0; X} X Xchar *realloc(s, n) Xregister char *s; Xregister unsigned n; { X register char *s1, *d, *d1; X register struct _Dmi *blk; X X if (_mall_opt) X _malldstr("called realloc("), _malldptr(s), _malldstr(", "), _malldptr(n), _malldstr(")\n"); X _mallchk("realloc"); X if (s == (char *) 0) { X _malldstr("realloc((char *) 0, size) is illegal!\n"); X _mall_sig(SIGSYS); X } X if (n == 0) { X _malldstr("realloc(ptr, 0) is illegal!\n"); X _mall_sig(SIGSYS); X } X if ((blk = _mallgb(s)) == (struct _Dmi *) 0) X _mallerr("non-allocated pointer passed to realloc(): ", s); X if ((s1 = malloc(n)) == (char *) 0) X return (char *) 0; X if (blk->m_size < n) X n = blk->m_size; X d1 = s1; X d = s; X while (n-- != 0) X *d1++ = *d++; X free(s); X return s1; X} X X/* X * _mallchk() is global, so external routines can do discreet checks on the X * arena. If the arena is detectibly corrupted, it will abort(). X */ X X_mallchk(fn) Xchar *fn; { X register struct _Dmi *blk, *cblk; X register char *send; X register int cnt; X X send = sbrk(0); X cblk = (struct _Dmi *) 0; X for (blk = _fab; blk != (struct _Dmi *) 0; cblk = blk, blk = blk->m_next) { X if ((char *) blk < _xbrk || (char *) blk >= send) X _mallerr(fn, "allocated block list corrupted: blkptr = ", blk); X if (blk->m_prev != cblk) X _mallerr(fn, "allocated block list corrupted: back pointer incorrect blk ", blk); X if (blk->m_size < 0) X _mallerr(fn, "allocated block list corrupted: blk->m_size = ", blk->m_size); X } X cblk = (struct _Dmi *) 0; X for (blk = _ffb; blk != (struct _Dmi *) 0; cblk = blk, blk = blk->m_next) { X if ((char *) blk < _xbrk || (char *) blk >= sbrk(0)) X _mallerr(fn, "free block list corrupted: blkptr = ", blk); X if (blk->m_prev != cblk) X _mallerr(fn, "free block list corrupted: back pointer incorrect blk ", blk); X if (blk->m_size < 0) X _mallerr(fn, "free block list corrupted: blk->m_size = ", blk->m_size); X } X for (blk = _fab; blk != (struct _Dmi *) 0; blk = blk->m_next) { X if ((char *) blk + sizeof *blk + blk->m_size - 1 > send) { X _malldstr("(brk = "); X _malldptr(send); X _malldstr(", eblk = "); X _malldptr((char *) blk + sizeof *blk + blk->m_size - 1); X _malldstr(")\n"); X _mallerr(fn, "allocated block extends past brk: ", blk); X } X cnt = 0; X for (cblk = _fab; cblk != (struct _Dmi *) 0; cblk = cblk->m_next) { X if (blk == cblk) X if (cnt++ == 0) X continue; X else X _mallerr(fn, "block allocated twice: ", blk); X if (blk > cblk && (char *) blk < (char *) cblk + sizeof *cblk + cblk->m_size - 1) { X _malldstr("(blk = "); X _malldptr(blk); X _malldstr(", cblk = "); X _malldptr((char *) cblk + sizeof *cblk + cblk->m_size - 1); X _malldstr(")\n"); X _mallerr(fn, "nested block in allocated list: ", blk); X } X } X for (cblk = _ffb; cblk != (struct _Dmi *) 0; cblk = cblk->m_next) { X if (blk == cblk) X _mallerr(fn, "block on allocated and free lists: ", blk); X if (blk > cblk && (char *) blk < (char *) cblk + sizeof *cblk + cblk->m_size - 1) { X _malldstr("(blk = "); X _malldptr(blk); X _malldstr(", cblk = "); X _malldptr((char *) cblk + sizeof *cblk + cblk->m_size - 1); X _malldstr(")\n"); X _mallerr(fn, "allocated block nested in free block: ", blk); X } X } X } X for (blk = _ffb; blk != (struct _Dmi *) 0; blk = blk->m_next) { X if ((char *) blk + sizeof *blk + blk->m_size - 1 > send) { X _malldstr("(brk = "); X _malldptr(send); X _malldstr(", eblk = "); X _malldptr((char *) blk + sizeof *blk + blk->m_size - 1); X _malldstr(")\n"); X _mallerr(fn, "free block extends past brk: ", blk); X } X cnt = 0; X for (cblk = _ffb; cblk != (struct _Dmi *) 0; cblk = cblk->m_next) { X if (blk == cblk) X if (cnt++ == 0) X continue; X else X _mallerr(fn, "block freed twice: ", blk); X if (blk > cblk && (char *) blk < (char *) cblk + sizeof *cblk + cblk->m_size - 1) { X _malldstr("(blk = "); X _malldptr(blk); X _malldstr(", cblk = "); X _malldptr((char *) cblk + sizeof *cblk + cblk->m_size - 1); X _malldstr(")\n"); X _mallerr(fn, "nested block in free list: ", blk); X } X } X for (cblk = _fab; cblk != (struct _Dmi *) 0; cblk = cblk->m_next) { X if (blk == cblk) X _mallerr(fn, "block on allocated and free lists: ", blk); X if (blk > cblk && (char *) blk < (char *) cblk + sizeof *cblk + cblk->m_size - 1) { X _malldstr("(blk = "); X _malldptr(blk); X _malldstr(", cblk = "); X _malldptr((char *) cblk + sizeof *cblk + cblk->m_size - 1); X _malldstr(")\n"); X _mallerr(fn, "free block nested in allocated block: ", blk); X } X } X } X} X X/* X * malloc objects and zero storage X */ X Xchar *calloc(n, size ) Xregister unsigned n, size; X{ X register char *s, *s1; X X if (_mall_opt) X { X _malldstr("calloc: num: "); X _malldptr(n); X _malldstr( " size: " ); X _malldptr(size); X _malldstr("\n"); X } X n *= size; X if ((s = malloc(n)) == (char *) 0) X return (char *) 0; X for (s1 = s; n != 0; n--) X *s1++ = 0; X return s; X} X X/* X * for some reason this is in /lib/libc.a(calloc.o) X */ X Xcfree(s) Xchar *s; { X free(s); X} X#endif X#endif X SHAR_EOF echo "File common/malloc.c is complete" chmod 0440 common/malloc.c || echo "restore of common/malloc.c fails" echo "x - extracting common/itypes.h (Text)" sed 's/^X//' << 'SHAR_EOF' > common/itypes.h && X/* RCS -- $Header: /u2/dvadura/src/generic/dmake/src/common/RCS/itypes.h,v 1.1 90/10/06 12:04:45 dvadura Exp $ X-- SYNOPSIS -- type declarations for common types X-- X-- DESCRIPTION X-- portable type declarations. X-- X-- AUTHOR X-- Dennis Vadura, dvadura@watdragon.uwaterloo.ca X-- CS DEPT, University of Waterloo, Waterloo, Ont., Canada X-- X-- COPYRIGHT X-- Copyright (c) 1990 by Dennis Vadura. All rights reserved. X-- X-- This program is free software; you can redistribute it and/or X-- modify it under the terms of the GNU General Public License X-- (version 1), as published by the Free Software Foundation, and X-- found in the file 'LICENSE' included with this distribution. X-- X-- This program is distributed in the hope that it will be useful, X-- but WITHOUT ANY WARRANTY; without even the implied warrant of X-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the X-- GNU General Public License for more details. X-- X-- You should have received a copy of the GNU General Public License X-- along with this program; if not, write to the Free Software X-- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. X-- X-- LOG X-- $Log: itypes.h,v $ X * Revision 1.1 90/10/06 12:04:45 dvadura X * dmake Release, Version 3.6 X * X*/ X X X#ifndef ITYPES_h X#define ITYPES_h X X#if defined(M_I86) || defined(MC68000) Xtypedef char int8; /* typedefs for right size ints */ Xtypedef int int16; Xtypedef long int32; Xtypedef unsigned char uint8; Xtypedef unsigned int uint16; Xtypedef unsigned long uint32; X#else Xtypedef char int8; /* typedefs for right size ints */ Xtypedef short int16; Xtypedef long int32; Xtypedef unsigned char uint8; Xtypedef unsigned short uint16; Xtypedef unsigned long uint32; X#endif X X#endif X SHAR_EOF chmod 0440 common/itypes.h || echo "restore of common/itypes.h fails" echo "x - extracting common/dbug.h (Text)" sed 's/^X//' << 'SHAR_EOF' > common/dbug.h && X/****************************************************************************** X * * X * N O T I C E * X * * X * Copyright Abandoned, 1987, Fred Fish * X * * X * * X * This previously copyrighted work has been placed into the public * X * domain by the author and may be freely used for any purpose, * X * private or commercial. * X * * X * Because of the number of inquiries I was receiving about the use * X * of this product in commercially developed works I have decided to * X * simply make it public domain to further its unrestricted use. I * X * specifically would be most happy to see this material become a * X * part of the standard Unix distributions by AT&T and the Berkeley * X * Computer Science Research Group, and a standard part of the GNU * X * system from the Free Software Foundation. * X * * X * I would appreciate it, as a courtesy, if this notice is left in * X * all copies and derivative works. Thank you. * X * * X * The author makes no warranty of any kind with respect to this * X * product and explicitly disclaims any implied warranties of mer- * X * chantability or fitness for any particular purpose. * X * * X ****************************************************************************** X */ X X X/* X * FILE X * X * dbug.h user include file for programs using the dbug package X * X * SYNOPSIS X * X * #include <local/dbug.h> X * X * SCCS ID X * X * @(#)dbug.h 1.11 9/5/87 X * X * DESCRIPTION X * X * Programs which use the dbug package must include this file. X * It contains the appropriate macros to call support routines X * in the dbug runtime library. X * X * To disable compilation of the macro expansions define the X * preprocessor symbol "DBUG_OFF". This will result in null X * macros expansions so that the resulting code will be smaller X * and faster. (The difference may be smaller than you think X * so this step is recommended only when absolutely necessary). X * In general, tradeoffs between space and efficiency are X * decided in favor of efficiency since space is seldom a X * problem on the new machines). X * X * All externally visible symbol names follow the pattern X * "_db_xxx..xx_" to minimize the possibility of a dbug package X * symbol colliding with a user defined symbol. X * X * The DBUG_<N> style macros are obsolete and should not be used X * in new code. Macros to map them to instances of DBUG_PRINT X * are provided for compatibility with older code. They may go X * away completely in subsequent releases. X * X * AUTHOR X * X * Fred Fish X * (Currently employed by Motorola Computer Division, Tempe, Az.) X * hao!noao!mcdsun!fnf X * (602) 438-3614 X * X */ X X X/* X * Internally used dbug variables which must be global. X */ X X#ifndef DBUG_OFF X extern int _db_on_; /* TRUE if debug currently enabled */ X extern FILE *_db_fp_; /* Current debug output stream */ X extern char *_db_process_; /* Name of current process */ X extern int _db_keyword_ (); /* Accept/reject keyword */ X extern void _db_push_ (); /* Push state, set up new state */ X extern void _db_pop_ (); /* Pop previous debug state */ X extern void _db_enter_ (); /* New user function entered */ X extern void _db_return_ (); /* User function return */ X extern void _db_pargs_ (); /* Remember args for line */ X extern void _db_doprnt_ (); /* Print debug output */ X extern void _db_setjmp_ (); /* Save debugger environment */ X extern void _db_longjmp_ (); /* Restore debugger environment */ X# endif X X X/* X * These macros provide a user interface into functions in the X * dbug runtime support library. They isolate users from changes X * in the MACROS and/or runtime support. X * X * The symbols "__LINE__" and "__FILE__" are expanded by the X * preprocessor to the current source file line number and file X * name respectively. X * X * WARNING --- Because the DBUG_ENTER macro allocates space on X * the user function's stack, it must precede any executable X * statements in the user function. X * X */ X X# ifdef DBUG_OFF X# define DBUG_ENTER(a1) X# define DBUG_RETURN(a1) return(a1) X# define DBUG_VOID_RETURN return X# define DBUG_EXECUTE(keyword,a1) X# define DBUG_PRINT(keyword,arglist) X# define DBUG_2(keyword,format) /* Obsolete */ X# define DBUG_3(keyword,format,a1) /* Obsolete */ X# define DBUG_4(keyword,format,a1,a2) /* Obsolete */ X# define DBUG_5(keyword,format,a1,a2,a3) /* Obsolete */ X# define DBUG_PUSH(a1) X# define DBUG_POP() X# define DBUG_PROCESS(a1) X# define DBUG_FILE (stderr) X# define DBUG_SETJMP setjmp X# define DBUG_LONGJMP longjmp X# else X# define DBUG_ENTER(a) \ X auto char *_db_func_, *_db_file_; \ X int _db_level_; \ X _db_enter_ (a,__FILE__,__LINE__,&_db_func_,&_db_file_,&_db_level_) X# define DBUG_LEAVE \ X (_db_return_ (__LINE__, &_db_func_, &_db_file_, &_db_level_)) X# define DBUG_RETURN(a1) return (DBUG_LEAVE, (a1)) X/* define DBUG_RETURN(a1) {DBUG_LEAVE; return(a1);} Alternate form */ X# define DBUG_VOID_RETURN DBUG_LEAVE; return X# define DBUG_EXECUTE(keyword,a1) \ X {if (_db_on_) {if (_db_keyword_ (keyword)) { a1 }}} X# define DBUG_PRINT(keyword,arglist) \ X {if (_db_on_) {_db_pargs_(__LINE__,keyword); _db_doprnt_ arglist;}} X# define DBUG_2(keyword,format) \ X DBUG_PRINT(keyword,(format)) /* Obsolete */ X# define DBUG_3(keyword,format,a1) \ X DBUG_PRINT(keyword,(format,a1)) /* Obsolete */ X# define DBUG_4(keyword,format,a1,a2) \ X DBUG_PRINT(keyword,(format,a1,a2)) /* Obsolete */ X# define DBUG_5(keyword,format,a1,a2,a3) \ X DBUG_PRINT(keyword,(format,a1,a2,a3)) /* Obsolete */ X# define DBUG_PUSH(a1) _db_push_ (a1) X# define DBUG_POP() _db_pop_ () X# define DBUG_PROCESS(a1) (_db_process_ = a1) X# define DBUG_FILE (_db_fp_) X# define DBUG_SETJMP(a1) (_db_setjmp_ (), setjmp (a1)) X# define DBUG_LONGJMP(a1,a2) (_db_longjmp_ (), longjmp (a1, a2)) X# endif X SHAR_EOF chmod 0440 common/dbug.h || echo "restore of common/dbug.h fails" echo "x - extracting common/dbug.c (Text)" sed 's/^X//' << 'SHAR_EOF' > common/dbug.c && X/****************************************************************************** X * * X * N O T I C E * X * * X * Copyright Abandoned, 1987, Fred Fish * X * * X * * X * This previously copyrighted work has been placed into the public * X * domain by the author and may be freely used for any purpose, * X * private or commercial. * X * * X * Because of the number of inquiries I was receiving about the use * X * of this product in commercially developed works I have decided to * X * simply make it public domain to further its unrestricted use. I * X * specifically would be most happy to see this material become a * X * part of the standard Unix distributions by AT&T and the Berkeley * X * Computer Science Research Group, and a standard part of the GNU * X * system from the Free Software Foundation. * X * * X * I would appreciate it, as a courtesy, if this notice is left in * X * all copies and derivative works. Thank you. * X * * X * The author makes no warranty of any kind with respect to this * X * product and explicitly disclaims any implied warranties of mer- * X * chantability or fitness for any particular purpose. * X * * X ****************************************************************************** X */ X X X/* X * FILE X * X * dbug.c runtime support routines for dbug package X * X * SCCS X * X * @(#)dbug.c 1.19 9/5/87 X * X * DESCRIPTION X * X * These are the runtime support routines for the dbug package. X * The dbug package has two main components; the user include X * file containing various macro definitions, and the runtime X * support routines which are called from the macro expansions. X * X * Externally visible functions in the runtime support module X * use the naming convention pattern "_db_xx...xx_", thus X * they are unlikely to collide with user defined function names. X * X * AUTHOR(S) X * X * Fred Fish (base code) X * (Currently at Motorola Computer Division, Tempe, Az.) X * hao!noao!mcdsun!fnf X * (602) 438-3614 X * X * Binayak Banerjee (profiling enhancements) X * seismo!bpa!sjuvax!bbanerje X */ X X X#include <stdio.h> X#ifdef amiga X#define AMIGA X#endif X X#ifdef AMIGA X#define HZ (50) /* Probably in some header somewhere */ X#endif X X/* X * Manifest constants that should not require any changes. X */ X X#define FALSE 0 /* Boolean FALSE */ X#define TRUE 1 /* Boolean TRUE */ X#define EOS '\000' /* End Of String marker */ X X/* X * Manifest constants which may be "tuned" if desired. X */ X X#define PRINTBUF 1024 /* Print buffer size */ X#define INDENT 4 /* Indentation per trace level */ X#define MAXDEPTH 200 /* Maximum trace depth default */ X X/* X * The following flags are used to determine which X * capabilities the user has enabled with the state X * push macro. X */ X X#define TRACE_ON 000001 /* Trace enabled */ X#define DEBUG_ON 000002 /* Debug enabled */ X#define FILE_ON 000004 /* File name print enabled */ X#define LINE_ON 000010 /* Line number print enabled */ X#define DEPTH_ON 000020 /* Function nest level print enabled */ X#define PROCESS_ON 000040 /* Process name print enabled */ X#define NUMBER_ON 000100 /* Number each line of output */ X#define PROFILE_ON 000200 /* Print out profiling code */ X X#define TRACING (stack -> flags & TRACE_ON) X#define DEBUGGING (stack -> flags & DEBUG_ON) X#define PROFILING (stack -> flags & PROFILE_ON) X#define STREQ(a,b) (strcmp(a,b) == 0) X X/* X * Typedefs to make things more obvious. X */ X X#define VOID void /* Can't use typedef for most compilers */ Xtypedef int BOOLEAN; X X/* X * Make it easy to change storage classes if necessary. X */ X X#define LOCAL static /* Names not needed by outside world */ X#define IMPORT extern /* Names defined externally */ X#define EXPORT /* Allocated here, available globally */ X#define AUTO auto /* Names to be allocated on stack */ X#define REGISTER register /* Names to be placed in registers */ X X/* X * The following define is for the variable arguments kluge, see X * the comments in _db_doprnt_(). X * X * Also note that the longer this list, the less prone to failing X * on long argument lists, but the more stuff that must be moved X * around for each call to the runtime support routines. The X * length may really be critical if the machine convention is X * to pass arguments in registers. X * X * Note that the default define allows up to 16 integral arguments, X * or 8 floating point arguments (doubles), on most machines. X * X * Someday this may be replaced with true varargs support, when X * ANSI C has had time to take root. X */ X X#define ARGLIST a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15 X X/* X * The default file for profiling. Could also add another flag X * (G?) which allowed the user to specify this. X */ X X#define PROF_FILE "dbugmon.out" X X/* X * Variables which are available externally but should only X * be accessed via the macro package facilities. X */ X XEXPORT FILE *_db_fp_ = stderr; /* Output stream, default stderr */ XEXPORT FILE *_db_pfp_ = (FILE *)0; /* Profile stream, 'dbugmon.out' */ XEXPORT char *_db_process_ = "dbug"; /* Pointer to process name; argv[0] */ XEXPORT BOOLEAN _db_on_ = FALSE; /* TRUE if debugging currently on */ XEXPORT BOOLEAN _db_pon_ = FALSE; /* TRUE if debugging currently on */ X X/* X * Externally supplied functions. X */ X X#ifdef unix /* Only needed for unix */ XIMPORT VOID perror (); /* Print system/library error */ XIMPORT int chown (); /* Change owner of a file */ XIMPORT int getgid (); /* Get real group id */ XIMPORT int getuid (); /* Get real user id */ XIMPORT int access (); /* Test file for access */ X#else X#if !(AMIGA && LATTICE) XLOCAL VOID perror (); /* Fake system/library error print routine */ X#endif X#endif X X# if BSD4_3 || sun XIMPORT int getrusage (); X#endif X XIMPORT int atoi (); /* Convert ascii to integer */ XIMPORT VOID exit (); /* Terminate execution */ XIMPORT int fclose (); /* Close a stream */ XIMPORT FILE *fopen (); /* Open a stream */ XIMPORT int fprintf (); /* Formatted print on file */ XIMPORT VOID free (); XIMPORT char *malloc (); /* Allocate memory */ XIMPORT int strcmp (); /* Compare strings */ XIMPORT char *strcpy (); /* Copy strings around */ XIMPORT int strlen (); /* Find length of string */ X X#ifndef fflush /* This is sometimes a macro */ XIMPORT int fflush (); /* Flush output for stream */ X#endif X X X/* X * The user may specify a list of functions to trace or X * debug. These lists are kept in a linear linked list, X * a very simple implementation. X */ X Xstruct link { X char *string; /* Pointer to link's contents */ X struct link *next_link; /* Pointer to the next link */ X}; X X X/* X * Debugging states can be pushed or popped off of a X * stack which is implemented as a linked list. Note X * that the head of the list is the current state and the X * stack is pushed by adding a new state to the head of the X * list or popped by removing the first link. X */ X Xstruct state { X int flags; /* Current state flags */ X int maxdepth; /* Current maximum trace depth */ X unsigned int delay; /* Delay after each output line */ X int level; /* Current function nesting level */ X FILE *out_file; /* Current output stream */ X FILE *prof_file; /* Current profiling stream */ X struct link *functions; /* List of functions */ X struct link *p_functions; /* List of profiled functions */ X struct link *keywords; /* List of debug keywords */ X struct link *processes; /* List of process names */ X struct state *next_state; /* Next state in the list */ X}; X XLOCAL struct state *stack = NULL; /* Linked list of stacked states */ X X/* X * Local variables not seen by user. X */ X XLOCAL int lineno = 0; /* Current debugger output line number */ XLOCAL char *func = "?func"; /* Name of current user function */ XLOCAL char *file = "?file"; /* Name of current user file */ XLOCAL BOOLEAN init_done = FALSE;/* Set to TRUE when initialization done */ X X/*#if unix || AMIGA || M_I86*/ XLOCAL int jmplevel; /* Remember nesting level at setjmp () */ XLOCAL char *jmpfunc; /* Remember current function for setjmp */ XLOCAL char *jmpfile; /* Remember current file for setjmp */ X/*#endif*/ X XLOCAL struct link *ListParse ();/* Parse a debug command string */ XLOCAL char *StrDup (); /* Make a fresh copy of a string */ XLOCAL VOID OpenFile (); /* Open debug output stream */ XLOCAL VOID OpenProfile (); /* Open profile output stream */ XLOCAL VOID CloseFile (); /* Close debug output stream */ XLOCAL VOID PushState (); /* Push current debug state */ XLOCAL VOID ChangeOwner (); /* Change file owner and group */ XLOCAL BOOLEAN DoTrace (); /* Test for tracing enabled */ XLOCAL BOOLEAN Writable (); /* Test to see if file is writable */ XLOCAL unsigned long Clock (); /* Return current user time (ms) */ XLOCAL char *DbugMalloc (); /* Allocate memory for runtime support */ XLOCAL char *BaseName (); /* Remove leading pathname components */ XLOCAL VOID DoPrefix (); /* Print debugger line prefix */ XLOCAL VOID FreeList (); /* Free memory from linked list */ XLOCAL VOID Indent (); /* Indent line to specified indent */ X X /* Supplied in Sys V runtime environ */ XLOCAL char *strtok (); /* Break string into tokens */ XLOCAL char *strrchr (); /* Find last occurance of char */ X X/* X * The following local variables are used to hold the state information X * between the call to _db_pargs_() and _db_doprnt_(), during X * expansion of the DBUG_PRINT macro. This is the only macro X * that currently uses these variables. The DBUG_PRINT macro X * and the new _db_doprnt_() routine replace the older DBUG_N macros X * and their corresponding runtime support routine _db_printf_(). X * X * These variables are currently used only by _db_pargs_() and X * _db_doprnt_(). X */ X XLOCAL int u_line = 0; /* User source code line number */ XLOCAL char *u_keyword = "?"; /* Keyword for current macro */ X X/* X * Miscellaneous printf format strings. X */ X X#define ERR_MISSING_RETURN "%s: missing DBUG_RETURN or DBUG_VOID_RETURN macro in function \"%s\"\n" X#define ERR_OPEN "%s: can't open debug output stream \"%s\": " X#define ERR_CLOSE "%s: can't close debug file: " X#define ERR_ABORT "%s: debugger aborting because %s\n" X#define ERR_CHOWN "%s: can't change owner/group of \"%s\": " X#define ERR_PRINTF "%s: obsolete object file for '%s', please recompile!\n" X X/* X * Macros and defines for testing file accessibility under UNIX. X */ X X#ifdef unix X# define A_EXISTS 00 /* Test for file existance */ X# define A_EXECUTE 01 /* Test for execute permission */ X# define A_WRITE 02 /* Test for write access */ X# define A_READ 03 /* Test for read access */ X# define EXISTS(pathname) (access (pathname, A_EXISTS) == 0) X# define WRITABLE(pathname) (access (pathname, A_WRITE) == 0) X#else X# define EXISTS(pathname) (FALSE) /* Assume no existance */ X#endif X X/* X * Translate some calls among different systems. X */ X X#ifdef unix X# define XDelay sleep XIMPORT unsigned int sleep (); /* Pause for given number of seconds */ X#endif X X#ifdef AMIGA XIMPORT int XDelay (); /* Pause for given number of ticks */ X#endif X X X/* X * FUNCTION X * X * _db_push_ push current debugger state and set up new one X * X * SYNOPSIS X * X * VOID _db_push_ (control) X * char *control; X * X * DESCRIPTION X * X * Given pointer to a debug control string in "control", pushes X * the current debug state, parses the control string, and sets X * up a new debug state. X * X * The only attribute of the new state inherited from the previous X * state is the current function nesting level. This can be X * overridden by using the "r" flag in the control string. X * X * The debug control string is a sequence of colon separated fields X * as follows: X * X * <field_1>:<field_2>:...:<field_N> X * X * Each field consists of a mandatory flag character followed by X * an optional "," and comma separated list of modifiers: X * X * flag[,modifier,modifier,...,modifier] X * X * The currently recognized flag characters are: X * X * d Enable output from DBUG_<N> macros for X * for the current state. May be followed X * by a list of keywords which selects output X * only for the DBUG macros with that keyword. X * A null list of keywords implies output for X * all macros. X * X * D Delay after each debugger output line. X * The argument is the number of tenths of seconds X * to delay, subject to machine capabilities. X * I.E. -#D,20 is delay two seconds. X * X * f Limit debugging and/or tracing, and profiling to the X * list of named functions. Note that a null list will X * disable all functions. The appropriate "d" or "t" X * flags must still be given, this flag only limits their X * actions if they are enabled. X * X * F Identify the source file name for each X * line of debug or trace output. X * X * g Enable profiling. Create a file called 'dbugmon.out' X * containing information that can be used to profile X * the program. May be followed by a list of keywords X * that select profiling only for the functions in that X * list. A null list implies that all functions are X * considered. X * X * L Identify the source file line number for X * each line of debug or trace output. X * X * n Print the current function nesting depth for X * each line of debug or trace output. X * X * N Number each line of dbug output. X * X * p Limit debugger actions to specified processes. X * A process must be identified with the X * DBUG_PROCESS macro and match one in the list X * for debugger actions to occur. X * X * P Print the current process name for each X * line of debug or trace output. X * X * r When pushing a new state, do not inherit X * the previous state's function nesting level. X * Useful when the output is to start at the X * left margin. X * X * t Enable function call/exit trace lines. X * May be followed by a list (containing only X * one modifier) giving a numeric maximum X * trace level, beyond which no output will X * occur for either debugging or tracing X * macros. The default is a compile time X * option. X * X * Some examples of debug control strings which might appear X * on a shell command line (the "-#" is typically used to X * introduce a control string to an application program) are: X * X * -#d:t X * -#d:f,main,subr1:F:L:t,20 X * -#d,input,output,files:n X * X * For convenience, any leading "-#" is stripped off. X * X */ X X XVOID _db_push_ (control) Xchar *control; X{ X REGISTER char *scan; X REGISTER struct link *temp; X X if (control && *control == '-') { X if (*++control == '#') { X control++; X } X } X control = StrDup (control); X PushState (); X scan = strtok (control, ":"); X for (; scan != NULL; scan = strtok ((char *)NULL, ":")) { X switch (*scan++) { X case 'd': X _db_on_ = TRUE; X stack -> flags |= DEBUG_ON; X if (*scan++ == ',') { X stack -> keywords = ListParse (scan); X } X break; X case 'D': X stack -> delay = 0; X if (*scan++ == ',') { X temp = ListParse (scan); X stack -> delay = DelayArg (atoi (temp -> string)); X FreeList (temp); X } X break; X case 'f': X if (*scan++ == ',') { X stack -> functions = ListParse (scan); X } X break; X case 'F': X stack -> flags |= FILE_ON; X break; X case 'g': X _db_pon_ = TRUE; X OpenProfile(PROF_FILE); X stack -> flags |= PROFILE_ON; X if (*scan++ == ',') { X stack -> p_functions = ListParse (scan); X } X break; X case 'L': X stack -> flags |= LINE_ON; X break; X case 'n': X stack -> flags |= DEPTH_ON; X break; X case 'N': X stack -> flags |= NUMBER_ON; X break; X case 'o': X if (*scan++ == ',') { X temp = ListParse (scan); X OpenFile (temp -> string); X FreeList (temp); X } else { X OpenFile ("-"); X } X break; X case 'p': X if (*scan++ == ',') { X stack -> processes = ListParse (scan); X } X break; X case 'P': X stack -> flags |= PROCESS_ON; X break; X case 'r': X stack -> level = 0; X break; X case 't': X stack -> flags |= TRACE_ON; X if (*scan++ == ',') { X temp = ListParse (scan); X stack -> maxdepth = atoi (temp -> string); X FreeList (temp); X } X break; X } X } X free (control); X} X X X X/* X * FUNCTION X * X * _db_pop_ pop the debug stack X * X * DESCRIPTION X * X * Pops the debug stack, returning the debug state to its X * condition prior to the most recent _db_push_ invocation. X * Note that the pop will fail if it would remove the last X * valid state from the stack. This prevents user errors X * in the push/pop sequence from screwing up the debugger. X * Maybe there should be some kind of warning printed if the X * user tries to pop too many states. X * X */ X XVOID _db_pop_ () X{ X REGISTER struct state *discard; X X discard = stack; SHAR_EOF echo "End of part 23" echo "File common/dbug.c is continued in part 24" echo "24" > s2_seq_.tmp exit 0