home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume7 / dbug / part01 next >
Encoding:
Text File  |  1989-07-02  |  60.8 KB  |  2,346 lines

  1. Newsgroups: comp.sources.misc
  2. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  3. Subject: v07i057: DBUG Package (Part 1 of 3)
  4. Reply-To: fnf@estinc.UUCP
  5.  
  6. Posting-number: Volume 7, Issue 57
  7. Submitted-by: fnf@estinc.UUCP
  8. Archive-name: dbug/part01
  9.  
  10. This is the latest version of my dbug package, a relatively portable and
  11. machine independent macro based C debugging package.  The dbug package
  12. has proven to be a very flexible and useful tool for debugging, testing,
  13. and porting C programs.
  14.  
  15. All of the features of the dbug package can be enabled or disabled
  16. dynamically at execution time.  This means that production programs will
  17. run normally when debugging is not enabled, and eliminates the need to
  18. maintain two separate versions of a program during testing.
  19.  
  20. Many of the things easily accomplished with conventional debugging tools,
  21. such as symbolic debuggers, are difficult or impossible with this package,
  22. and vice versa.  Thus the dbug package should not be thought of as a
  23. replacement or substitute for other debugging tools, but simply as a useful
  24. addition to the program development and maintenance environment.
  25.  
  26. One of the new features with this version is stack usage accounting.  You
  27. can discover the total amount of stack used by your program, and the amount
  28. used by each individual function.  You will need to know in which direction
  29. you stack grows (up or down).
  30.  
  31. -Fred Fish   12-Jun-89
  32.  
  33. #--------CUT---------CUT---------CUT---------CUT--------#
  34. #########################################################
  35. #                                                       #
  36. # This is a shell archive file.  To extract files:      #
  37. #                                                       #
  38. #    1)    Make a directory for the files.                 #
  39. #    2) Write a file, such as "file.shar", containing   #
  40. #       this archive file into the directory.           #
  41. #    3) Type "sh file.shar".  Do not use csh.           #
  42. #                                                       #
  43. #########################################################
  44. #
  45. #
  46. echo Extracting dbug.h:
  47. sed 's/^Z//' >dbug.h <<\STUNKYFLUFF
  48. Z/******************************************************************************
  49. Z *                                          *
  50. Z *                               N O T I C E                      *
  51. Z *                                          *
  52. Z *                  Copyright Abandoned, 1987, Fred Fish              *
  53. Z *                                          *
  54. Z *                                          *
  55. Z *    This previously copyrighted work has been placed into the  public     *
  56. Z *    domain  by  the  author  and  may be freely used for any purpose,     *
  57. Z *    private or commercial.                              *
  58. Z *                                          *
  59. Z *    Because of the number of inquiries I was receiving about the  use     *
  60. Z *    of this product in commercially developed works I have decided to     *
  61. Z *    simply make it public domain to further its unrestricted use.   I     *
  62. Z *    specifically  would  be  most happy to see this material become a     *
  63. Z *    part of the standard Unix distributions by AT&T and the  Berkeley     *
  64. Z *    Computer  Science  Research Group, and a standard part of the GNU     *
  65. Z *    system from the Free Software Foundation.                  *
  66. Z *                                          *
  67. Z *    I would appreciate it, as a courtesy, if this notice is  left  in     *
  68. Z *    all copies and derivative works.  Thank you.                  *
  69. Z *                                          *
  70. Z *    The author makes no warranty of any kind  with  respect  to  this     *
  71. Z *    product  and  explicitly disclaims any implied warranties of mer-     *
  72. Z *    chantability or fitness for any particular purpose.              *
  73. Z *                                          *
  74. Z ******************************************************************************
  75. Z */
  76. Z
  77. Z
  78. Z/*
  79. Z *  FILE
  80. Z *
  81. Z *    dbug.h    user include file for programs using the dbug package
  82. Z *
  83. Z *  SYNOPSIS
  84. Z *
  85. Z *    #include <local/dbug.h>
  86. Z *
  87. Z *  SCCS ID
  88. Z *
  89. Z *    @(#)dbug.h    1.12 4/2/89
  90. Z *
  91. Z *  DESCRIPTION
  92. Z *
  93. Z *    Programs which use the dbug package must include this file.
  94. Z *    It contains the appropriate macros to call support routines
  95. Z *    in the dbug runtime library.
  96. Z *
  97. Z *    To disable compilation of the macro expansions define the
  98. Z *    preprocessor symbol "DBUG_OFF".  This will result in null
  99. Z *    macros expansions so that the resulting code will be smaller
  100. Z *    and faster.  (The difference may be smaller than you think
  101. Z *    so this step is recommended only when absolutely necessary).
  102. Z *    In general, tradeoffs between space and efficiency are
  103. Z *    decided in favor of efficiency since space is seldom a
  104. Z *    problem on the new machines).
  105. Z *
  106. Z *    All externally visible symbol names follow the pattern
  107. Z *    "_db_xxx..xx_" to minimize the possibility of a dbug package
  108. Z *    symbol colliding with a user defined symbol.
  109. Z *    
  110. Z *    The DBUG_<N> style macros are obsolete and should not be used
  111. Z *    in new code.  Macros to map them to instances of DBUG_PRINT
  112. Z *    are provided for compatibility with older code.  They may go
  113. Z *    away completely in subsequent releases.
  114. Z *
  115. Z *  AUTHOR
  116. Z *
  117. Z *    Fred Fish
  118. Z *    (Currently employed by Motorola Computer Division, Tempe, Az.)
  119. Z *    hao!noao!mcdsun!fnf
  120. Z *    (602) 438-3614
  121. Z *
  122. Z */
  123. Z
  124. Z
  125. Z/*
  126. Z *    Internally used dbug variables which must be global.
  127. Z */
  128. Z
  129. Z#ifndef DBUG_OFF
  130. Z    extern int _db_on_;            /* TRUE if debug currently enabled */
  131. Z    extern FILE *_db_fp_;        /* Current debug output stream */
  132. Z    extern char *_db_process_;        /* Name of current process */
  133. Z    extern int _db_keyword_ ();        /* Accept/reject keyword */
  134. Z    extern void _db_push_ ();        /* Push state, set up new state */
  135. Z    extern void _db_pop_ ();        /* Pop previous debug state */
  136. Z    extern void _db_enter_ ();        /* New user function entered */
  137. Z    extern void _db_return_ ();        /* User function return */
  138. Z    extern void _db_pargs_ ();        /* Remember args for line */
  139. Z    extern void _db_doprnt_ ();        /* Print debug output */
  140. Z    extern void _db_setjmp_ ();        /* Save debugger environment */
  141. Z    extern void _db_longjmp_ ();    /* Restore debugger environment */
  142. Z# endif
  143. Z
  144. Z
  145. Z/*
  146. Z *    These macros provide a user interface into functions in the
  147. Z *    dbug runtime support library.  They isolate users from changes
  148. Z *    in the MACROS and/or runtime support.
  149. Z *
  150. Z *    The symbols "__LINE__" and "__FILE__" are expanded by the
  151. Z *    preprocessor to the current source file line number and file
  152. Z *    name respectively.
  153. Z *
  154. Z *    WARNING ---  Because the DBUG_ENTER macro allocates space on
  155. Z *    the user function's stack, it must precede any executable
  156. Z *    statements in the user function.
  157. Z *
  158. Z */
  159. Z
  160. Z# ifdef DBUG_OFF
  161. Z#    define DBUG_ENTER(a1)
  162. Z#    define DBUG_RETURN(a1) return(a1)
  163. Z#    define DBUG_VOID_RETURN return
  164. Z#    define DBUG_EXECUTE(keyword,a1)
  165. Z#    define DBUG_PRINT(keyword,arglist)
  166. Z#    define DBUG_2(keyword,format)        /* Obsolete */
  167. Z#    define DBUG_3(keyword,format,a1)        /* Obsolete */
  168. Z#    define DBUG_4(keyword,format,a1,a2)    /* Obsolete */
  169. Z#    define DBUG_5(keyword,format,a1,a2,a3)    /* Obsolete */
  170. Z#    define DBUG_PUSH(a1)
  171. Z#    define DBUG_POP()
  172. Z#    define DBUG_PROCESS(a1)
  173. Z#    define DBUG_FILE (stderr)
  174. Z#    define DBUG_SETJMP setjmp
  175. Z#    define DBUG_LONGJMP longjmp
  176. Z# else
  177. Z#    define DBUG_ENTER(a) \
  178. Z    auto char *_db_func_; auto char *_db_file_; auto int _db_level_; \
  179. Z    auto char *_db_framep_; \
  180. Z    _db_enter_ (a,__FILE__,__LINE__,&_db_func_,&_db_file_,&_db_level_, \
  181. Z            &_db_framep_)
  182. Z#    define DBUG_LEAVE \
  183. Z    (_db_return_ (__LINE__, &_db_func_, &_db_file_, &_db_level_))
  184. Z#    define DBUG_RETURN(a1) return (DBUG_LEAVE, (a1))
  185. Z/*   define DBUG_RETURN(a1) {DBUG_LEAVE; return(a1);}  Alternate form */
  186. Z#    define DBUG_VOID_RETURN {DBUG_LEAVE; return;}
  187. Z#    define DBUG_EXECUTE(keyword,a1) \
  188. Z    {if (_db_on_) {if (_db_keyword_ (keyword)) { a1 }}}
  189. Z#    define DBUG_PRINT(keyword,arglist) \
  190. Z    {if (_db_on_) {_db_pargs_(__LINE__,keyword); _db_doprnt_ arglist;}}
  191. Z#    define DBUG_2(keyword,format) \
  192. Z    DBUG_PRINT(keyword,(format))        /* Obsolete */
  193. Z#    define DBUG_3(keyword,format,a1) \
  194. Z    DBUG_PRINT(keyword,(format,a1))        /* Obsolete */
  195. Z#    define DBUG_4(keyword,format,a1,a2) \
  196. Z    DBUG_PRINT(keyword,(format,a1,a2))    /* Obsolete */
  197. Z#    define DBUG_5(keyword,format,a1,a2,a3) \
  198. Z    DBUG_PRINT(keyword,(format,a1,a2,a3))    /* Obsolete */
  199. Z#    define DBUG_PUSH(a1) _db_push_ (a1)
  200. Z#    define DBUG_POP() _db_pop_ ()
  201. Z#    define DBUG_PROCESS(a1) (_db_process_ = a1)
  202. Z#    define DBUG_FILE (_db_fp_)
  203. Z#    define DBUG_SETJMP(a1) (_db_setjmp_ (), setjmp (a1))
  204. Z#    define DBUG_LONGJMP(a1,a2) (_db_longjmp_ (), longjmp (a1, a2))
  205. Z# endif
  206. STUNKYFLUFF
  207. set `sum dbug.h`
  208. if test 16096 != $1
  209. then
  210. echo dbug.h: Checksum error. Is: $1, should be: 16096.
  211. fi
  212. #
  213. #
  214. echo Extracting dbug.c:
  215. sed 's/^Z//' >dbug.c <<\STUNKYFLUFF
  216. Z/******************************************************************************
  217. Z *                                          *
  218. Z *                               N O T I C E                      *
  219. Z *                                          *
  220. Z *                  Copyright Abandoned, 1987, Fred Fish              *
  221. Z *                                          *
  222. Z *                                          *
  223. Z *    This previously copyrighted work has been placed into the  public     *
  224. Z *    domain  by  the  author  and  may be freely used for any purpose,     *
  225. Z *    private or commercial.                              *
  226. Z *                                          *
  227. Z *    Because of the number of inquiries I was receiving about the  use     *
  228. Z *    of this product in commercially developed works I have decided to     *
  229. Z *    simply make it public domain to further its unrestricted use.   I     *
  230. Z *    specifically  would  be  most happy to see this material become a     *
  231. Z *    part of the standard Unix distributions by AT&T and the  Berkeley     *
  232. Z *    Computer  Science  Research Group, and a standard part of the GNU     *
  233. Z *    system from the Free Software Foundation.                  *
  234. Z *                                          *
  235. Z *    I would appreciate it, as a courtesy, if this notice is  left  in     *
  236. Z *    all copies and derivative works.  Thank you.                  *
  237. Z *                                          *
  238. Z *    The author makes no warranty of any kind  with  respect  to  this     *
  239. Z *    product  and  explicitly disclaims any implied warranties of mer-     *
  240. Z *    chantability or fitness for any particular purpose.              *
  241. Z *                                          *
  242. Z ******************************************************************************
  243. Z */
  244. Z
  245. Z
  246. Z/*
  247. Z *  FILE
  248. Z *
  249. Z *    dbug.c   runtime support routines for dbug package
  250. Z *
  251. Z *  SCCS
  252. Z *
  253. Z *    @(#)dbug.c    1.19 9/5/87
  254. Z *
  255. Z *  DESCRIPTION
  256. Z *
  257. Z *    These are the runtime support routines for the dbug package.
  258. Z *    The dbug package has two main components; the user include
  259. Z *    file containing various macro definitions, and the runtime
  260. Z *    support routines which are called from the macro expansions.
  261. Z *
  262. Z *    Externally visible functions in the runtime support module
  263. Z *    use the naming convention pattern "_db_xx...xx_", thus
  264. Z *    they are unlikely to collide with user defined function names.
  265. Z *
  266. Z *  AUTHOR(S)
  267. Z *
  268. Z *    Fred Fish        (base code)
  269. Z *    Enhanced Software Technologies, Tempe, AZ
  270. Z *    asuvax!mcdphx!estinc!fnf
  271. Z *
  272. Z *    Binayak Banerjee    (profiling enhancements)
  273. Z *    seismo!bpa!sjuvax!bbanerje
  274. Z */
  275. Z
  276. Z
  277. Z#include <stdio.h>
  278. Z
  279. Z#if NO_VARARGS
  280. Z#include "vargs.h"        /* Use our "fake" varargs */
  281. Z#else
  282. Z#include <varargs.h>        /* Use system supplied varargs package */
  283. Z#endif
  284. Z
  285. Z#if amiga
  286. Z#define HZ (50)            /* Probably in some header somewhere */
  287. Z#endif
  288. Z
  289. Z#ifdef M_XENIX            /* Some xenix compilers predefine this */
  290. Z#ifndef xenix
  291. Z#define xenix 1
  292. Z#endif
  293. Z#endif
  294. Z
  295. Z/*
  296. Z *    Manifest constants that should not require any changes.
  297. Z */
  298. Z
  299. Z#define FALSE        0    /* Boolean FALSE */
  300. Z#define TRUE        1    /* Boolean TRUE */
  301. Z#define EOS        '\000'    /* End Of String marker */
  302. Z
  303. Z/*
  304. Z *    Manifest constants which may be "tuned" if desired.
  305. Z */
  306. Z
  307. Z#define PRINTBUF    1024    /* Print buffer size */
  308. Z#define INDENT        4    /* Indentation per trace level */
  309. Z#define MAXDEPTH    200    /* Maximum trace depth default */
  310. Z
  311. Z/*
  312. Z *    The following flags are used to determine which
  313. Z *    capabilities the user has enabled with the state
  314. Z *    push macro.
  315. Z */
  316. Z
  317. Z#define TRACE_ON    000001    /* Trace enabled */
  318. Z#define DEBUG_ON    000002    /* Debug enabled */
  319. Z#define FILE_ON     000004    /* File name print enabled */
  320. Z#define LINE_ON        000010    /* Line number print enabled */
  321. Z#define DEPTH_ON    000020    /* Function nest level print enabled */
  322. Z#define PROCESS_ON    000040    /* Process name print enabled */
  323. Z#define NUMBER_ON    000100    /* Number each line of output */
  324. Z#define PROFILE_ON    000200    /* Print out profiling code */
  325. Z#define PID_ON        000400    /* Identify each line with process id */
  326. Z
  327. Z#define TRACING (stack -> flags & TRACE_ON)
  328. Z#define DEBUGGING (stack -> flags & DEBUG_ON)
  329. Z#define PROFILING (stack -> flags & PROFILE_ON)
  330. Z#define STREQ(a,b) (strcmp(a,b) == 0)
  331. Z
  332. Z/*
  333. Z *    Typedefs to make things more obvious.
  334. Z */
  335. Z
  336. Z#define VOID void        /* Can't use typedef for most compilers */
  337. Ztypedef int BOOLEAN;
  338. Z
  339. Z/*
  340. Z *    Make it easy to change storage classes if necessary.
  341. Z */
  342. Z
  343. Z#define LOCAL static        /* Names not needed by outside world */
  344. Z#define IMPORT extern        /* Names defined externally */
  345. Z#define EXPORT            /* Allocated here, available globally */
  346. Z#define AUTO auto        /* Names to be allocated on stack */
  347. Z#define REGISTER register    /* Names to be placed in registers */
  348. Z
  349. Z/*
  350. Z * The default file for profiling.  Could also add another flag
  351. Z * (G?) which allowed the user to specify this.
  352. Z *
  353. Z * If the automatic variables get allocated on the stack in
  354. Z * reverse order from their declarations, then define AUTOS_REVERSE.
  355. Z * This is used by the code that keeps track of stack usage.  For
  356. Z * forward allocation, the difference in the dbug frame pointers 
  357. Z * represents stack used by the callee function.  For reverse allocation,
  358. Z * the difference represents stack used by the caller function.
  359. Z *
  360. Z */
  361. Z
  362. Z#define PROF_FILE    "dbugmon.out"
  363. Z#define PROF_EFMT    "E\t%ld\t%s\n"
  364. Z#define PROF_SFMT    "S\t%lx\t%lx\t%s\n"
  365. Z#define PROF_XFMT    "X\t%ld\t%s\n"
  366. Z
  367. Z#if M_I386        /* predefined by xenix 386 compiler */
  368. Z#define AUTOS_REVERSE 1
  369. Z#endif
  370. Z
  371. Z/*
  372. Z *    Variables which are available externally but should only
  373. Z *    be accessed via the macro package facilities.
  374. Z */
  375. Z
  376. ZEXPORT FILE *_db_fp_ = stderr;        /* Output stream, default stderr */
  377. ZEXPORT FILE *_db_pfp_ = (FILE *)0;    /* Profile stream, 'dbugmon.out' */
  378. ZEXPORT char *_db_process_ = "dbug";    /* Pointer to process name; argv[0] */
  379. ZEXPORT BOOLEAN _db_on_ = FALSE;        /* TRUE if debugging currently on */
  380. ZEXPORT BOOLEAN _db_pon_ = FALSE;    /* TRUE if debugging currently on */
  381. Z
  382. Z/*
  383. Z *    Externally supplied functions.
  384. Z */
  385. Z
  386. Z#if (unix || xenix)        /* Only needed for unix */
  387. ZIMPORT VOID perror ();        /* Print system/library error */
  388. ZIMPORT int chown ();        /* Change owner of a file */
  389. ZIMPORT int getgid ();        /* Get real group id */
  390. ZIMPORT int getuid ();        /* Get real user id */
  391. ZIMPORT int access ();        /* Test file for access */
  392. Z#else
  393. Z#if !(amiga && LATTICE)
  394. ZLOCAL VOID perror ();        /* Fake system/library error print routine */
  395. Z#endif
  396. Z#endif
  397. Z
  398. Z# if BSD4_3 || sun
  399. ZIMPORT int getrusage ();
  400. Z#endif
  401. Z
  402. ZIMPORT int atoi ();        /* Convert ascii to integer */
  403. ZIMPORT VOID exit ();        /* Terminate execution */
  404. ZIMPORT int fclose ();        /* Close a stream */
  405. ZIMPORT FILE *fopen ();        /* Open a stream */
  406. ZIMPORT int fprintf ();        /* Formatted print on file */
  407. ZIMPORT int vfprintf ();        /* Varargs form of fprintf */
  408. ZIMPORT VOID free ();
  409. ZIMPORT char *malloc ();        /* Allocate memory */
  410. ZIMPORT int strcmp ();        /* Compare strings */
  411. ZIMPORT char *strcpy ();        /* Copy strings around */
  412. ZIMPORT int strlen ();        /* Find length of string */
  413. Z
  414. Z#ifndef fflush            /* This is sometimes a macro */
  415. ZIMPORT int fflush ();        /* Flush output for stream */
  416. Z#endif
  417. Z
  418. Z
  419. Z/*
  420. Z *    The user may specify a list of functions to trace or 
  421. Z *    debug.  These lists are kept in a linear linked list,
  422. Z *    a very simple implementation.
  423. Z */
  424. Z
  425. Zstruct link {
  426. Z    char *string;        /* Pointer to link's contents */
  427. Z    struct link *next_link;    /* Pointer to the next link */
  428. Z};
  429. Z
  430. Z
  431. Z/*
  432. Z *    Debugging states can be pushed or popped off of a
  433. Z *    stack which is implemented as a linked list.  Note
  434. Z *    that the head of the list is the current state and the
  435. Z *    stack is pushed by adding a new state to the head of the
  436. Z *    list or popped by removing the first link.
  437. Z */
  438. Z
  439. Zstruct state {
  440. Z    int flags;                /* Current state flags */
  441. Z    int maxdepth;            /* Current maximum trace depth */
  442. Z    unsigned int delay;            /* Delay after each output line */
  443. Z    int level;                /* Current function nesting level */
  444. Z    FILE *out_file;            /* Current output stream */
  445. Z    FILE *prof_file;            /* Current profiling stream */
  446. Z    struct link *functions;        /* List of functions */
  447. Z    struct link *p_functions;        /* List of profiled functions */
  448. Z    struct link *keywords;        /* List of debug keywords */
  449. Z    struct link *processes;        /* List of process names */
  450. Z    struct state *next_state;        /* Next state in the list */
  451. Z};
  452. Z
  453. ZLOCAL struct state *stack = NULL;    /* Linked list of stacked states */
  454. Z
  455. Z/*
  456. Z *    Local variables not seen by user.
  457. Z */
  458. Z
  459. ZLOCAL int lineno = 0;        /* Current debugger output line number */
  460. ZLOCAL char *func = "?func";    /* Name of current user function */
  461. ZLOCAL char *file = "?file";    /* Name of current user file */
  462. ZLOCAL BOOLEAN init_done = FALSE;/* Set to TRUE when initialization done */
  463. ZLOCAL char **framep = NULL;    /* Pointer to current frame */
  464. Z
  465. Z#if (unix || xenix || amiga)
  466. ZLOCAL int jmplevel;        /* Remember nesting level at setjmp () */
  467. ZLOCAL char *jmpfunc;        /* Remember current function for setjmp */
  468. ZLOCAL char *jmpfile;        /* Remember current file for setjmp */
  469. Z#endif
  470. Z
  471. ZLOCAL struct link *ListParse ();/* Parse a debug command string */
  472. ZLOCAL char *StrDup ();        /* Make a fresh copy of a string */
  473. ZLOCAL VOID OpenFile ();        /* Open debug output stream */
  474. ZLOCAL VOID OpenProfile ();    /* Open profile output stream */
  475. ZLOCAL VOID CloseFile ();    /* Close debug output stream */
  476. ZLOCAL VOID PushState ();    /* Push current debug state */
  477. ZLOCAL VOID ChangeOwner ();    /* Change file owner and group */
  478. ZLOCAL BOOLEAN DoTrace ();    /* Test for tracing enabled */
  479. ZLOCAL BOOLEAN Writable ();    /* Test to see if file is writable */
  480. ZLOCAL unsigned long Clock ();    /* Return current user time (ms) */
  481. ZLOCAL long *DbugMalloc ();    /* Allocate memory for runtime support */
  482. ZLOCAL char *BaseName ();    /* Remove leading pathname components */
  483. ZLOCAL VOID DoPrefix ();        /* Print debugger line prefix */
  484. ZLOCAL VOID FreeList ();        /* Free memory from linked list */
  485. ZLOCAL VOID Indent ();        /* Indent line to specified indent */
  486. Z
  487. Z                /* Supplied in Sys V runtime environ */
  488. ZLOCAL char *strtok ();        /* Break string into tokens */
  489. ZLOCAL char *strrchr ();        /* Find last occurance of char */
  490. Z
  491. Z/*
  492. Z *    The following local variables are used to hold the state information
  493. Z *    between the call to _db_pargs_() and _db_doprnt_(), during
  494. Z *    expansion of the DBUG_PRINT macro.  This is the only macro
  495. Z *    that currently uses these variables.
  496. Z *
  497. Z *    These variables are currently used only by _db_pargs_() and
  498. Z *    _db_doprnt_().
  499. Z */
  500. Z
  501. ZLOCAL int u_line = 0;        /* User source code line number */
  502. ZLOCAL char *u_keyword = "?";    /* Keyword for current macro */
  503. Z
  504. Z/*
  505. Z *    Miscellaneous printf format strings.
  506. Z */
  507. Z#define ERR_MISSING_RETURN "%s: missing DBUG_RETURN or DBUG_VOID_RETURN macro in function \"%s\"\n"
  508. Z#define ERR_OPEN "%s: can't open debug output stream \"%s\": "
  509. Z#define ERR_CLOSE "%s: can't close debug file: "
  510. Z#define ERR_ABORT "%s: debugger aborting because %s\n"
  511. Z#define ERR_CHOWN "%s: can't change owner/group of \"%s\": "
  512. Z
  513. Z/*
  514. Z *    Macros and defines for testing file accessibility under UNIX.
  515. Z */
  516. Z
  517. Z#if (unix || xenix)
  518. Z#  define A_EXISTS    00        /* Test for file existance */
  519. Z#  define A_EXECUTE    01        /* Test for execute permission */
  520. Z#  define A_WRITE    02        /* Test for write access */
  521. Z#  define A_READ    03        /* Test for read access */
  522. Z#  define EXISTS(pathname) (access (pathname, A_EXISTS) == 0)
  523. Z#  define WRITABLE(pathname) (access (pathname, A_WRITE) == 0)
  524. Z#else
  525. Z#  define EXISTS(pathname) (FALSE)    /* Assume no existance */
  526. Z#endif
  527. Z
  528. Z/*
  529. Z *    Translate some calls among different systems.
  530. Z */
  531. Z
  532. Z#if (unix || xenix)
  533. Z# define Delay sleep
  534. ZIMPORT unsigned int sleep ();    /* Pause for given number of seconds */
  535. Z#endif
  536. Z
  537. Z#if amiga
  538. ZIMPORT int Delay ();        /* Pause for given number of ticks */
  539. Z#endif
  540. Z
  541. Z
  542. Z/*
  543. Z *  FUNCTION
  544. Z *
  545. Z *    _db_push_    push current debugger state and set up new one
  546. Z *
  547. Z *  SYNOPSIS
  548. Z *
  549. Z *    VOID _db_push_ (control)
  550. Z *    char *control;
  551. Z *
  552. Z *  DESCRIPTION
  553. Z *
  554. Z *    Given pointer to a debug control string in "control", pushes
  555. Z *    the current debug state, parses the control string, and sets
  556. Z *    up a new debug state.
  557. Z *
  558. Z *    The only attribute of the new state inherited from the previous
  559. Z *    state is the current function nesting level.  This can be
  560. Z *    overridden by using the "r" flag in the control string.
  561. Z *
  562. Z *    The debug control string is a sequence of colon separated fields
  563. Z *    as follows:
  564. Z *
  565. Z *        <field_1>:<field_2>:...:<field_N>
  566. Z *
  567. Z *    Each field consists of a mandatory flag character followed by
  568. Z *    an optional "," and comma separated list of modifiers:
  569. Z *
  570. Z *        flag[,modifier,modifier,...,modifier]
  571. Z *
  572. Z *    The currently recognized flag characters are:
  573. Z *
  574. Z *        d    Enable output from DBUG_<N> macros for
  575. Z *            for the current state.  May be followed
  576. Z *            by a list of keywords which selects output
  577. Z *            only for the DBUG macros with that keyword.
  578. Z *            A null list of keywords implies output for
  579. Z *            all macros.
  580. Z *
  581. Z *        D    Delay after each debugger output line.
  582. Z *            The argument is the number of tenths of seconds
  583. Z *            to delay, subject to machine capabilities.
  584. Z *            I.E.  -#D,20 is delay two seconds.
  585. Z *
  586. Z *        f    Limit debugging and/or tracing, and profiling to the
  587. Z *            list of named functions.  Note that a null list will
  588. Z *            disable all functions.  The appropriate "d" or "t"
  589. Z *            flags must still be given, this flag only limits their
  590. Z *            actions if they are enabled.
  591. Z *
  592. Z *        F    Identify the source file name for each
  593. Z *            line of debug or trace output.
  594. Z *
  595. Z *        i    Identify the process with the pid for each line of
  596. Z *            debug or trace output.
  597. Z *
  598. Z *        g    Enable profiling.  Create a file called 'dbugmon.out'
  599. Z *            containing information that can be used to profile
  600. Z *            the program.  May be followed by a list of keywords
  601. Z *            that select profiling only for the functions in that
  602. Z *            list.  A null list implies that all functions are
  603. Z *            considered.
  604. Z *
  605. Z *        L    Identify the source file line number for
  606. Z *            each line of debug or trace output.
  607. Z *
  608. Z *        n    Print the current function nesting depth for
  609. Z *            each line of debug or trace output.
  610. Z *    
  611. Z *        N    Number each line of dbug output.
  612. Z *
  613. Z *        o    Redirect the debugger output stream to the
  614. Z *            specified file.  The default output is stderr.
  615. Z *
  616. Z *        p    Limit debugger actions to specified processes.
  617. Z *            A process must be identified with the
  618. Z *            DBUG_PROCESS macro and match one in the list
  619. Z *            for debugger actions to occur.
  620. Z *
  621. Z *        P    Print the current process name for each
  622. Z *            line of debug or trace output.
  623. Z *
  624. Z *        r    When pushing a new state, do not inherit
  625. Z *            the previous state's function nesting level.
  626. Z *            Useful when the output is to start at the
  627. Z *            left margin.
  628. Z *
  629. Z *        t    Enable function call/exit trace lines.
  630. Z *            May be followed by a list (containing only
  631. Z *            one modifier) giving a numeric maximum
  632. Z *            trace level, beyond which no output will
  633. Z *            occur for either debugging or tracing
  634. Z *            macros.  The default is a compile time
  635. Z *            option.
  636. Z *
  637. Z *    Some examples of debug control strings which might appear
  638. Z *    on a shell command line (the "-#" is typically used to
  639. Z *    introduce a control string to an application program) are:
  640. Z *
  641. Z *        -#d:t
  642. Z *        -#d:f,main,subr1:F:L:t,20
  643. Z *        -#d,input,output,files:n
  644. Z *
  645. Z *    For convenience, any leading "-#" is stripped off.
  646. Z *
  647. Z */
  648. Z
  649. Z
  650. ZVOID _db_push_ (control)
  651. Zchar *control;
  652. Z{
  653. Z    REGISTER char *scan;
  654. Z    REGISTER struct link *temp;
  655. Z
  656. Z    if (control && *control == '-') {
  657. Z    if (*++control == '#') {
  658. Z        control++;
  659. Z    }    
  660. Z    }
  661. Z    control = StrDup (control);
  662. Z    PushState ();
  663. Z    scan = strtok (control, ":");
  664. Z    for (; scan != NULL; scan = strtok ((char *)NULL, ":")) {
  665. Z    switch (*scan++) {
  666. Z        case 'd': 
  667. Z        _db_on_ = TRUE;
  668. Z        stack -> flags |= DEBUG_ON;
  669. Z        if (*scan++ == ',') {
  670. Z            stack -> keywords = ListParse (scan);
  671. Z        }
  672. Z            break;
  673. Z        case 'D': 
  674. Z        stack -> delay = 0;
  675. Z        if (*scan++ == ',') {
  676. Z            temp = ListParse (scan);
  677. Z            stack -> delay = DelayArg (atoi (temp -> string));
  678. Z            FreeList (temp);
  679. Z        }
  680. Z        break;
  681. Z        case 'f': 
  682. Z        if (*scan++ == ',') {
  683. Z            stack -> functions = ListParse (scan);
  684. Z        }
  685. Z        break;
  686. Z        case 'F': 
  687. Z        stack -> flags |= FILE_ON;
  688. Z        break;
  689. Z        case 'i':
  690. Z        stack -> flags |= PID_ON;
  691. Z        break;
  692. Z        case 'g': 
  693. Z        _db_pon_ = TRUE;
  694. Z        OpenProfile(PROF_FILE);
  695. Z        stack -> flags |= PROFILE_ON;
  696. Z        if (*scan++ == ',') {
  697. Z            stack -> p_functions = ListParse (scan);
  698. Z        }
  699. Z        break;
  700. Z        case 'L': 
  701. Z        stack -> flags |= LINE_ON;
  702. Z        break;
  703. Z        case 'n': 
  704. Z        stack -> flags |= DEPTH_ON;
  705. Z        break;
  706. Z        case 'N':
  707. Z        stack -> flags |= NUMBER_ON;
  708. Z        break;
  709. Z        case 'o': 
  710. Z        if (*scan++ == ',') {
  711. Z            temp = ListParse (scan);
  712. Z            OpenFile (temp -> string);
  713. Z            FreeList (temp);
  714. Z        } else {
  715. Z            OpenFile ("-");
  716. Z        }
  717. Z        break;
  718. Z        case 'p':
  719. Z        if (*scan++ == ',') {
  720. Z            stack -> processes = ListParse (scan);
  721. Z        }
  722. Z        break;
  723. Z        case 'P': 
  724. Z        stack -> flags |= PROCESS_ON;
  725. Z        break;
  726. Z        case 'r': 
  727. Z        stack -> level = 0;
  728. Z        break;
  729. Z        case 't': 
  730. Z        stack -> flags |= TRACE_ON;
  731. Z        if (*scan++ == ',') {
  732. Z            temp = ListParse (scan);
  733. Z            stack -> maxdepth = atoi (temp -> string);
  734. Z            FreeList (temp);
  735. Z        }
  736. Z        break;
  737. Z    }
  738. Z    }
  739. Z    free (control);
  740. Z}
  741. Z
  742. Z
  743. Z
  744. Z/*
  745. Z *  FUNCTION
  746. Z *
  747. Z *    _db_pop_    pop the debug stack
  748. Z *
  749. Z *  DESCRIPTION
  750. Z *
  751. Z *    Pops the debug stack, returning the debug state to its
  752. Z *    condition prior to the most recent _db_push_ invocation.
  753. Z *    Note that the pop will fail if it would remove the last
  754. Z *    valid state from the stack.  This prevents user errors
  755. Z *    in the push/pop sequence from screwing up the debugger.
  756. Z *    Maybe there should be some kind of warning printed if the
  757. Z *    user tries to pop too many states.
  758. Z *
  759. Z */
  760. Z
  761. ZVOID _db_pop_ ()
  762. Z{
  763. Z    REGISTER struct state *discard;
  764. Z
  765. Z    discard = stack;
  766. Z    if (discard != NULL && discard -> next_state != NULL) {
  767. Z    stack = discard -> next_state;
  768. Z    _db_fp_ = stack -> out_file;
  769. Z    _db_pfp_ = stack -> prof_file;
  770. Z    if (discard -> keywords != NULL) {
  771. Z        FreeList (discard -> keywords);
  772. Z    }
  773. Z    if (discard -> functions != NULL) {
  774. Z        FreeList (discard -> functions);
  775. Z    }
  776. Z    if (discard -> processes != NULL) {
  777. Z        FreeList (discard -> processes);
  778. Z    }
  779. Z    if (discard -> p_functions != NULL) {
  780. Z        FreeList (discard -> p_functions);
  781. Z    }
  782. Z    CloseFile (discard -> out_file);
  783. Z    CloseFile (discard -> prof_file);
  784. Z    free ((char *) discard);
  785. Z    }
  786. Z}
  787. Z
  788. Z
  789. Z/*
  790. Z *  FUNCTION
  791. Z *
  792. Z *    _db_enter_    process entry point to user function
  793. Z *
  794. Z *  SYNOPSIS
  795. Z *
  796. Z *    VOID _db_enter_ (_func_, _file_, _line_,
  797. Z *             _sfunc_, _sfile_, _slevel_, _sframep_)
  798. Z *    char *_func_;        points to current function name
  799. Z *    char *_file_;        points to current file name
  800. Z *    int _line_;        called from source line number
  801. Z *    char **_sfunc_;        save previous _func_
  802. Z *    char **_sfile_;        save previous _file_
  803. Z *    int *_slevel_;        save previous nesting level
  804. Z *    char ***_sframep_;    save previous frame pointer
  805. Z *
  806. Z *  DESCRIPTION
  807. Z *
  808. Z *    Called at the beginning of each user function to tell
  809. Z *    the debugger that a new function has been entered.
  810. Z *    Note that the pointers to the previous user function
  811. Z *    name and previous user file name are stored on the
  812. Z *    caller's stack (this is why the ENTER macro must be
  813. Z *    the first "executable" code in a function, since it
  814. Z *    allocates these storage locations).  The previous nesting
  815. Z *    level is also stored on the callers stack for internal
  816. Z *    self consistency checks.
  817. Z *
  818. Z *    Also prints a trace line if tracing is enabled and
  819. Z *    increments the current function nesting depth.
  820. Z *
  821. Z *    Note that this mechanism allows the debugger to know
  822. Z *    what the current user function is at all times, without
  823. Z *    maintaining an internal stack for the function names.
  824. Z *
  825. Z */
  826. Z
  827. ZVOID _db_enter_ (_func_, _file_, _line_, _sfunc_, _sfile_, _slevel_,
  828. Z         _sframep_)
  829. Zchar *_func_;
  830. Zchar *_file_;
  831. Zint _line_;
  832. Zchar **_sfunc_;
  833. Zchar **_sfile_;
  834. Zint *_slevel_;
  835. Zchar ***_sframep_;
  836. Z{
  837. Z    long stackused;
  838. Z
  839. Z    if (!init_done) {
  840. Z    _db_push_ ("");
  841. Z    }
  842. Z    *_sfunc_ = func;
  843. Z    func = _func_;
  844. Z    *_sfile_ = file;
  845. Z    file = BaseName (_file_);
  846. Z    stack -> level++;
  847. Z    *_slevel_ = stack -> level;
  848. Z    *_sframep_ = framep;
  849. Z    framep = (char **) _sframep_;
  850. Z    if (DoProfile ()) {
  851. Z    if (*framep == NULL) {
  852. Z        stackused = 0;
  853. Z    } else {
  854. Z        stackused = ((long)(*framep)) - ((long)(framep));
  855. Z        stackused = stackused > 0 ? stackused : -stackused;
  856. Z    }
  857. Z    (VOID) fprintf (_db_pfp_, PROF_EFMT , Clock (), func);
  858. Z#if AUTOS_REVERSE
  859. Z    (VOID) fprintf (_db_pfp_, PROF_SFMT, framep, stackused, *_sfunc_);
  860. Z#else
  861. Z    (VOID) fprintf (_db_pfp_, PROF_SFMT, framep, stackused, func);
  862. Z#endif
  863. Z    (VOID) fflush (_db_pfp_);
  864. Z    }
  865. Z    if (DoTrace ()) {
  866. Z    DoPrefix (_line_);
  867. Z    Indent (stack -> level);
  868. Z    (VOID) fprintf (_db_fp_, ">%s\n", func);
  869. Z    (VOID) fflush (_db_fp_);
  870. Z    (VOID) Delay (stack -> delay);
  871. Z    }
  872. Z}
  873. Z
  874. Z
  875. Z/*
  876. Z *  FUNCTION
  877. Z *
  878. Z *    _db_return_    process exit from user function
  879. Z *
  880. Z *  SYNOPSIS
  881. Z *
  882. Z *    VOID _db_return_ (_line_, _sfunc_, _sfile_, _slevel_)
  883. Z *    int _line_;        current source line number
  884. Z *    char **_sfunc_;        where previous _func_ is to be retrieved
  885. Z *    char **_sfile_;        where previous _file_ is to be retrieved
  886. Z *    int *_slevel_;        where previous level was stashed
  887. Z *
  888. Z *  DESCRIPTION
  889. Z *
  890. Z *    Called just before user function executes an explicit or implicit
  891. Z *    return.  Prints a trace line if trace is enabled, decrements
  892. Z *    the current nesting level, and restores the current function and
  893. Z *    file names from the defunct function's stack.
  894. Z *
  895. Z */
  896. Z
  897. ZVOID _db_return_ (_line_, _sfunc_, _sfile_, _slevel_)
  898. Zint _line_;
  899. Zchar **_sfunc_;
  900. Zchar **_sfile_;
  901. Zint *_slevel_;
  902. Z{
  903. Z    if (!init_done) {
  904. Z    _db_push_ ("");
  905. Z    }
  906. Z    if (stack -> level != *_slevel_ && (TRACING || DEBUGGING || PROFILING)) {
  907. Z    (VOID) fprintf (_db_fp_, ERR_MISSING_RETURN, _db_process_, func);
  908. Z    } else if (DoProfile ()) {
  909. Z    (VOID) fprintf (_db_pfp_, PROF_XFMT, Clock(), func);
  910. Z    } else if (DoTrace ()) {
  911. Z    DoPrefix (_line_);
  912. Z    Indent (stack -> level);
  913. Z    (VOID) fprintf (_db_fp_, "<%s\n", func);
  914. Z    }
  915. Z    (VOID) fflush (_db_fp_);
  916. Z    (VOID) Delay (stack -> delay);
  917. Z    stack -> level = *_slevel_ - 1;
  918. Z    func = *_sfunc_;
  919. Z    file = *_sfile_;
  920. Z    if (framep != NULL) {
  921. Z    framep = (char **) *framep;
  922. Z    }
  923. Z}
  924. Z
  925. Z
  926. Z/*
  927. Z *  FUNCTION
  928. Z *
  929. Z *    _db_pargs_    log arguments for subsequent use by _db_doprnt_()
  930. Z *
  931. Z *  SYNOPSIS
  932. Z *
  933. Z *    VOID _db_pargs_ (_line_, keyword)
  934. Z *    int _line_;
  935. Z *    char *keyword;
  936. Z *
  937. Z *  DESCRIPTION
  938. Z *
  939. Z *    The new universal printing macro DBUG_PRINT, which replaces
  940. Z *    all forms of the DBUG_N macros, needs two calls to runtime
  941. Z *    support routines.  The first, this function, remembers arguments
  942. Z *    that are used by the subsequent call to _db_doprnt_().
  943. Z*
  944. Z */
  945. Z
  946. ZVOID _db_pargs_ (_line_, keyword)
  947. Zint _line_;
  948. Zchar *keyword;
  949. Z{
  950. Z    u_line = _line_;
  951. Z    u_keyword = keyword;
  952. Z}
  953. Z
  954. Z
  955. Z/*
  956. Z *  FUNCTION
  957. Z *
  958. Z *    _db_doprnt_    handle print of debug lines
  959. Z *
  960. Z *  SYNOPSIS
  961. Z *
  962. Z *    VOID _db_doprnt_ (format, va_alist)
  963. Z *    char *format;
  964. Z *    va_dcl;
  965. Z *
  966. Z *  DESCRIPTION
  967. Z *
  968. Z *    When invoked via one of the DBUG macros, tests the current keyword
  969. Z *    set by calling _db_pargs_() to see if that macro has been selected
  970. Z *    for processing via the debugger control string, and if so, handles
  971. Z *    printing of the arguments via the format string.  The line number
  972. Z *    of the DBUG macro in the source is found in u_line.
  973. Z *
  974. Z *    Note that the format string SHOULD NOT include a terminating
  975. Z *    newline, this is supplied automatically.
  976. Z *
  977. Z */
  978. Z
  979. Z/*VARARGS1*/
  980. ZVOID _db_doprnt_ (format, va_alist)
  981. Zchar *format;
  982. Zva_dcl
  983. Z{
  984. Z    va_list args;
  985. Z
  986. Z    va_start (args);
  987. Z    if (_db_keyword_ (u_keyword)) {
  988. Z    DoPrefix (u_line);
  989. Z    if (TRACING) {
  990. Z        Indent (stack -> level + 1);
  991. Z    } else {
  992. Z        (VOID) fprintf (_db_fp_, "%s: ", func);
  993. Z    }
  994. Z    (VOID) fprintf (_db_fp_, "%s: ", u_keyword);
  995. Z    (VOID) vfprintf (_db_fp_, format, args);
  996. Z    (VOID) fprintf (_db_fp_, "\n");
  997. Z    (VOID) fflush (_db_fp_);
  998. Z    (VOID) Delay (stack -> delay);
  999. Z    }
  1000. Z    va_end (args);
  1001. Z}
  1002. Z
  1003. Z
  1004. Z/*
  1005. Z *  FUNCTION
  1006. Z *
  1007. Z *    ListParse    parse list of modifiers in debug control string
  1008. Z *
  1009. Z *  SYNOPSIS
  1010. Z *
  1011. Z *    LOCAL struct link *ListParse (ctlp)
  1012. Z *    char *ctlp;
  1013. Z *
  1014. Z *  DESCRIPTION
  1015. Z *
  1016. Z *    Given pointer to a comma separated list of strings in "cltp",
  1017. Z *    parses the list, building a list and returning a pointer to it.
  1018. Z *    The original comma separated list is destroyed in the process of
  1019. Z *    building the linked list, thus it had better be a duplicate
  1020. Z *    if it is important.
  1021. Z *
  1022. Z *    Note that since each link is added at the head of the list,
  1023. Z *    the final list will be in "reverse order", which is not
  1024. Z *    significant for our usage here.
  1025. Z *
  1026. Z */
  1027. Z
  1028. ZLOCAL struct link *ListParse (ctlp)
  1029. Zchar *ctlp;
  1030. Z{
  1031. Z    REGISTER char *start;
  1032. Z    REGISTER struct link *new;
  1033. Z    REGISTER struct link *head;
  1034. Z
  1035. Z    head = NULL;
  1036. Z    while (*ctlp != EOS) {
  1037. Z    start = ctlp;
  1038. Z    while (*ctlp != EOS && *ctlp != ',') {
  1039. Z        ctlp++;
  1040. Z    }
  1041. Z    if (*ctlp == ',') {
  1042. Z        *ctlp++ = EOS;
  1043. Z    }
  1044. Z    new = (struct link *) DbugMalloc (sizeof (struct link));
  1045. Z    new -> string = StrDup (start);
  1046. Z    new -> next_link = head;
  1047. Z    head = new;
  1048. Z    }
  1049. Z    return (head);
  1050. Z}
  1051. Z
  1052. Z
  1053. Z/*
  1054. Z *  FUNCTION
  1055. Z *
  1056. Z *    InList    test a given string for member of a given list
  1057. Z *
  1058. Z *  SYNOPSIS
  1059. Z *
  1060. Z *    LOCAL BOOLEAN InList (linkp, cp)
  1061. Z *    struct link *linkp;
  1062. Z *    char *cp;
  1063. Z *
  1064. Z *  DESCRIPTION
  1065. Z *
  1066. Z *    Tests the string pointed to by "cp" to determine if it is in
  1067. Z *    the list pointed to by "linkp".  Linkp points to the first
  1068. Z *    link in the list.  If linkp is NULL then the string is treated
  1069. Z *    as if it is in the list (I.E all strings are in the null list).
  1070. Z *    This may seem rather strange at first but leads to the desired
  1071. Z *    operation if no list is given.  The net effect is that all
  1072. Z *    strings will be accepted when there is no list, and when there
  1073. Z *    is a list, only those strings in the list will be accepted.
  1074. Z *
  1075. Z */
  1076. Z
  1077. ZLOCAL BOOLEAN InList (linkp, cp)
  1078. Zstruct link *linkp;
  1079. Zchar *cp;
  1080. Z{
  1081. Z    REGISTER struct link *scan;
  1082. Z    REGISTER BOOLEAN accept;
  1083. Z
  1084. Z    if (linkp == NULL) {
  1085. Z    accept = TRUE;
  1086. Z    } else {
  1087. Z    accept = FALSE;
  1088. Z    for (scan = linkp; scan != NULL; scan = scan -> next_link) {
  1089. Z        if (STREQ (scan -> string, cp)) {
  1090. Z        accept = TRUE;
  1091. Z        break;
  1092. Z        }
  1093. Z    }
  1094. Z    }
  1095. Z    return (accept);
  1096. Z}
  1097. Z
  1098. Z
  1099. Z/*
  1100. Z *  FUNCTION
  1101. Z *
  1102. Z *    PushState    push current state onto stack and set up new one
  1103. Z *
  1104. Z *  SYNOPSIS
  1105. Z *
  1106. Z *    LOCAL VOID PushState ()
  1107. Z *
  1108. Z *  DESCRIPTION
  1109. Z *
  1110. Z *    Pushes the current state on the state stack, and initializes
  1111. Z *    a new state.  The only parameter inherited from the previous
  1112. Z *    state is the function nesting level.  This action can be
  1113. Z *    inhibited if desired, via the "r" flag.
  1114. Z *
  1115. Z *    The state stack is a linked list of states, with the new
  1116. Z *    state added at the head.  This allows the stack to grow
  1117. Z *    to the limits of memory if necessary.
  1118. Z *
  1119. Z */
  1120. Z
  1121. ZLOCAL VOID PushState ()
  1122. Z{
  1123. Z    REGISTER struct state *new;
  1124. Z
  1125. Z    new = (struct state *) DbugMalloc (sizeof (struct state));
  1126. Z    new -> flags = 0;
  1127. Z    new -> delay = 0;
  1128. Z    new -> maxdepth = MAXDEPTH;
  1129. Z    if (stack != NULL) {
  1130. Z    new -> level = stack -> level;
  1131. Z    } else {
  1132. Z    new -> level = 0;
  1133. Z    }
  1134. Z    new -> out_file = stderr;
  1135. Z    new -> functions = NULL;
  1136. Z    new -> p_functions = NULL;
  1137. Z    new -> keywords = NULL;
  1138. Z    new -> processes = NULL;
  1139. Z    new -> next_state = stack;
  1140. Z    stack = new;
  1141. Z    init_done = TRUE;
  1142. Z}
  1143. Z
  1144. Z
  1145. Z/*
  1146. Z *  FUNCTION
  1147. Z *
  1148. Z *    DoTrace    check to see if tracing is current enabled
  1149. Z *
  1150. Z *  SYNOPSIS
  1151. Z *
  1152. Z *    LOCAL BOOLEAN DoTrace ()
  1153. Z *
  1154. Z *  DESCRIPTION
  1155. Z *
  1156. Z *    Checks to see if tracing is enabled based on whether the
  1157. Z *    user has specified tracing, the maximum trace depth has
  1158. Z *    not yet been reached, the current function is selected,
  1159. Z *    and the current process is selected.  Returns TRUE if
  1160. Z *    tracing is enabled, FALSE otherwise.
  1161. Z *
  1162. Z */
  1163. Z
  1164. ZLOCAL BOOLEAN DoTrace ()
  1165. Z{
  1166. Z    REGISTER BOOLEAN trace;
  1167. Z
  1168. Z    trace = FALSE;
  1169. Z    if (TRACING) {
  1170. Z    if (stack -> level <= stack -> maxdepth) {
  1171. Z        if (InList (stack -> functions, func)) {
  1172. Z        if (InList (stack -> processes, _db_process_)) {
  1173. Z            trace = TRUE;
  1174. Z        }
  1175. Z        }
  1176. Z    }
  1177. Z    }
  1178. Z    return (trace);
  1179. Z}
  1180. Z
  1181. Z
  1182. Z/*
  1183. Z *  FUNCTION
  1184. Z *
  1185. Z *    DoProfile    check to see if profiling is current enabled
  1186. Z *
  1187. Z *  SYNOPSIS
  1188. Z *
  1189. Z *    LOCAL BOOLEAN DoProfile ()
  1190. Z *
  1191. Z *  DESCRIPTION
  1192. Z *
  1193. Z *    Checks to see if profiling is enabled based on whether the
  1194. Z *    user has specified profiling, the maximum trace depth has
  1195. Z *    not yet been reached, the current function is selected,
  1196. Z *    and the current process is selected.  Returns TRUE if
  1197. Z *    profiling is enabled, FALSE otherwise.
  1198. Z *
  1199. Z */
  1200. Z
  1201. ZLOCAL BOOLEAN DoProfile ()
  1202. Z{
  1203. Z    REGISTER BOOLEAN profile;
  1204. Z
  1205. Z    profile = FALSE;
  1206. Z    if (PROFILING) {
  1207. Z    if (stack -> level <= stack -> maxdepth) {
  1208. Z        if (InList (stack -> p_functions, func)) {
  1209. Z        if (InList (stack -> processes, _db_process_)) {
  1210. Z            profile = TRUE;
  1211. Z        }
  1212. Z        }
  1213. Z    }
  1214. Z    }
  1215. Z    return (profile);
  1216. Z}
  1217. Z
  1218. Z
  1219. Z/*
  1220. Z *  FUNCTION
  1221. Z *
  1222. Z *    _db_keyword_    test keyword for member of keyword list
  1223. Z *
  1224. Z *  SYNOPSIS
  1225. Z *
  1226. Z *    BOOLEAN _db_keyword_ (keyword)
  1227. Z *    char *keyword;
  1228. Z *
  1229. Z *  DESCRIPTION
  1230. Z *
  1231. Z *    Test a keyword to determine if it is in the currently active
  1232. Z *    keyword list.  As with the function list, a keyword is accepted
  1233. Z *    if the list is null, otherwise it must match one of the list
  1234. Z *    members.  When debugging is not on, no keywords are accepted.
  1235. Z *    After the maximum trace level is exceeded, no keywords are
  1236. Z *    accepted (this behavior subject to change).  Additionally,
  1237. Z *    the current function and process must be accepted based on
  1238. Z *    their respective lists.
  1239. Z *
  1240. Z *    Returns TRUE if keyword accepted, FALSE otherwise.
  1241. Z *
  1242. Z */
  1243. Z
  1244. ZBOOLEAN _db_keyword_ (keyword)
  1245. Zchar *keyword;
  1246. Z{
  1247. Z    REGISTER BOOLEAN accept;
  1248. Z
  1249. Z    if (!init_done) {
  1250. Z    _db_push_ ("");
  1251. Z    }
  1252. Z    accept = FALSE;
  1253. Z    if (DEBUGGING) {
  1254. Z    if (stack -> level <= stack -> maxdepth) {
  1255. Z        if (InList (stack -> functions, func)) {
  1256. Z        if (InList (stack -> keywords, keyword)) {
  1257. Z            if (InList (stack -> processes, _db_process_)) {
  1258. Z            accept = TRUE;
  1259. Z            }
  1260. Z        }
  1261. Z        }
  1262. Z    }
  1263. Z    }
  1264. Z    return (accept);
  1265. Z}
  1266. Z
  1267. Z
  1268. Z/*
  1269. Z *  FUNCTION
  1270. Z *
  1271. Z *    Indent    indent a line to the given indentation level
  1272. Z *
  1273. Z *  SYNOPSIS
  1274. Z *
  1275. Z *    LOCAL VOID Indent (indent)
  1276. Z *    int indent;
  1277. Z *
  1278. Z *  DESCRIPTION
  1279. Z *
  1280. Z *    Indent a line to the given level.  Note that this is
  1281. Z *    a simple minded but portable implementation.
  1282. Z *    There are better ways.
  1283. Z *
  1284. Z *    Also, the indent must be scaled by the compile time option
  1285. Z *    of character positions per nesting level.
  1286. Z *
  1287. Z */
  1288. Z
  1289. ZLOCAL VOID Indent (indent)
  1290. Zint indent;
  1291. Z{
  1292. Z    REGISTER int count;
  1293. Z    AUTO char buffer[PRINTBUF];
  1294. Z
  1295. Z    indent *= INDENT;
  1296. Z    for (count = 0; (count < (indent - INDENT)) && (count < (PRINTBUF - 1)); count++) {
  1297. Z    if ((count % INDENT) == 0) {
  1298. Z        buffer[count] = '|';
  1299. Z    } else {
  1300. Z        buffer[count] = ' ';
  1301. Z    }
  1302. Z    }
  1303. Z    buffer[count] = EOS;
  1304. Z    (VOID) fprintf (_db_fp_, buffer);
  1305. Z    (VOID) fflush (_db_fp_);
  1306. Z}
  1307. Z
  1308. Z
  1309. Z/*
  1310. Z *  FUNCTION
  1311. Z *
  1312. Z *    FreeList    free all memory associated with a linked list
  1313. Z *
  1314. Z *  SYNOPSIS
  1315. Z *
  1316. Z *    LOCAL VOID FreeList (linkp)
  1317. Z *    struct link *linkp;
  1318. Z *
  1319. Z *  DESCRIPTION
  1320. Z *
  1321. Z *    Given pointer to the head of a linked list, frees all
  1322. Z *    memory held by the list and the members of the list.
  1323. Z *
  1324. Z */
  1325. Z
  1326. ZLOCAL VOID FreeList (linkp)
  1327. Zstruct link *linkp;
  1328. Z{
  1329. Z    REGISTER struct link *old;
  1330. Z
  1331. Z    while (linkp != NULL) {
  1332. Z    old = linkp;
  1333. Z    linkp = linkp -> next_link;
  1334. Z    if (old -> string != NULL) {
  1335. Z        free (old -> string);
  1336. Z    }
  1337. Z    free ((char *) old);
  1338. Z    }
  1339. Z}
  1340. Z
  1341. Z
  1342. Z/*
  1343. Z *  FUNCTION
  1344. Z *
  1345. Z *    StrDup   make a duplicate of a string in new memory
  1346. Z *
  1347. Z *  SYNOPSIS
  1348. Z *
  1349. Z *    LOCAL char *StrDup (string)
  1350. Z *    char *string;
  1351. Z *
  1352. Z *  DESCRIPTION
  1353. Z *
  1354. Z *    Given pointer to a string, allocates sufficient memory to make
  1355. Z *    a duplicate copy, and copies the string to the newly allocated
  1356. Z *    memory.  Failure to allocated sufficient memory is immediately
  1357. Z *    fatal.
  1358. Z *
  1359. Z */
  1360. Z
  1361. Z
  1362. ZLOCAL char *StrDup (string)
  1363. Zchar *string;
  1364. Z{
  1365. Z    REGISTER char *new;
  1366. Z
  1367. Z    new = (char *) DbugMalloc (strlen (string) + 1);
  1368. Z    (VOID) strcpy (new, string);
  1369. Z    return (new);
  1370. Z}
  1371. Z
  1372. Z
  1373. Z/*
  1374. Z *  FUNCTION
  1375. Z *
  1376. Z *    DoPrefix    print debugger line prefix prior to indentation
  1377. Z *
  1378. Z *  SYNOPSIS
  1379. Z *
  1380. Z *    LOCAL VOID DoPrefix (_line_)
  1381. Z *    int _line_;
  1382. Z *
  1383. Z *  DESCRIPTION
  1384. Z *
  1385. Z *    Print prefix common to all debugger output lines, prior to
  1386. Z *    doing indentation if necessary.  Print such information as
  1387. Z *    current process name, current source file name and line number,
  1388. Z *    and current function nesting depth.
  1389. Z *
  1390. Z */
  1391. Z  
  1392. ZLOCAL VOID DoPrefix (_line_)
  1393. Zint _line_;
  1394. Z{
  1395. Z#if (unix || xenix)
  1396. Z    extern int getpid ();
  1397. Z#endif
  1398. Z
  1399. Z    lineno++;
  1400. Z#if (unix || xenix)
  1401. Z    if (stack -> flags & PID_ON) {
  1402. Z    (VOID) fprintf (_db_fp_, "%5d: ", getpid ());
  1403. Z    }
  1404. Z#endif
  1405. Z    if (stack -> flags & NUMBER_ON) {
  1406. Z    (VOID) fprintf (_db_fp_, "%5d: ", lineno);
  1407. Z    }
  1408. Z    if (stack -> flags & PROCESS_ON) {
  1409. Z    (VOID) fprintf (_db_fp_, "%s: ", _db_process_);
  1410. Z    }
  1411. Z    if (stack -> flags & FILE_ON) {
  1412. Z    (VOID) fprintf (_db_fp_, "%14s: ", file);
  1413. Z    }
  1414. Z    if (stack -> flags & LINE_ON) {
  1415. Z    (VOID) fprintf (_db_fp_, "%5d: ", _line_);
  1416. Z    }
  1417. Z    if (stack -> flags & DEPTH_ON) {
  1418. Z    (VOID) fprintf (_db_fp_, "%4d: ", stack -> level);
  1419. Z    }
  1420. Z    (VOID) fflush (_db_fp_);
  1421. Z}
  1422. Z
  1423. Z
  1424. Z/*
  1425. Z *  FUNCTION
  1426. Z *
  1427. Z *    OpenFile    open new output stream for debugger output
  1428. Z *
  1429. Z *  SYNOPSIS
  1430. Z *
  1431. Z *    LOCAL VOID OpenFile (name)
  1432. Z *    char *name;
  1433. Z *
  1434. Z *  DESCRIPTION
  1435. Z *
  1436. Z *    Given name of a new file (or "-" for stdout) opens the file
  1437. Z *    and sets the output stream to the new file.
  1438. Z *
  1439. Z */
  1440. Z
  1441. ZLOCAL VOID OpenFile (name)
  1442. Zchar *name;
  1443. Z{
  1444. Z    REGISTER FILE *fp;
  1445. Z    REGISTER BOOLEAN newfile;
  1446. Z
  1447. Z    if (name != NULL) {
  1448. Z    if (strcmp (name, "-") == 0) {
  1449. Z        _db_fp_ = stdout;
  1450. Z        stack -> out_file = _db_fp_;
  1451. Z    } else {
  1452. Z        if (!Writable (name)) {
  1453. Z        (VOID) fprintf (_db_fp_, ERR_OPEN, _db_process_, name);
  1454. Z        perror ("");
  1455. Z        (VOID) fflush (_db_fp_);
  1456. Z        (VOID) Delay (stack -> delay);
  1457. Z        } else {
  1458. Z        if (EXISTS (name)) {
  1459. Z            newfile = FALSE;
  1460. Z        } else {
  1461. Z            newfile = TRUE;
  1462. Z        }
  1463. Z        fp = fopen (name, "a");
  1464. Z        if (fp == NULL) {
  1465. Z             (VOID) fprintf (_db_fp_, ERR_OPEN, _db_process_, name);
  1466. Z            perror ("");
  1467. Z            (VOID) fflush (_db_fp_);
  1468. Z            (VOID) Delay (stack -> delay);
  1469. Z        } else {
  1470. Z            _db_fp_ = fp;
  1471. Z            stack -> out_file = fp;
  1472. Z            if (newfile) {
  1473. Z            ChangeOwner (name);
  1474. Z            }
  1475. Z        }
  1476. Z        }
  1477. Z    }
  1478. Z    }
  1479. Z}
  1480. Z
  1481. Z
  1482. Z/*
  1483. Z *  FUNCTION
  1484. Z *
  1485. Z *    OpenProfile    open new output stream for profiler output
  1486. Z *
  1487. Z *  SYNOPSIS
  1488. Z *
  1489. Z *    LOCAL VOID OpenProfile (name)
  1490. Z *    char *name;
  1491. Z *
  1492. Z *  DESCRIPTION
  1493. Z *
  1494. Z *    Given name of a new file, opens the file
  1495. Z *    and sets the profiler output stream to the new file.
  1496. Z *
  1497. Z *    It is currently unclear whether the prefered behavior is
  1498. Z *    to truncate any existing file, or simply append to it.
  1499. Z *    The latter behavior would be desirable for collecting
  1500. Z *    accumulated runtime history over a number of separate
  1501. Z *    runs.  It might take some changes to the analyzer program
  1502. Z *    though, and the notes that Binayak sent with the profiling
  1503. Z *    diffs indicated that append was the normal mode, but this
  1504. Z *    does not appear to agree with the actual code. I haven't
  1505. Z *    investigated at this time [fnf; 24-Jul-87].
  1506. Z */
  1507. Z
  1508. ZLOCAL VOID OpenProfile (name)
  1509. Zchar *name;
  1510. Z{
  1511. Z    REGISTER FILE *fp;
  1512. Z    REGISTER BOOLEAN newfile;
  1513. Z
  1514. Z    if (name != NULL) {
  1515. Z    if (!Writable (name)) {
  1516. Z        (VOID) fprintf (_db_fp_, ERR_OPEN, _db_process_, name);
  1517. Z        perror ("");
  1518. Z        (VOID) fflush (_db_fp_);
  1519. Z        (VOID) Delay (stack -> delay);
  1520. Z    } else {
  1521. Z        if (EXISTS (name)) {
  1522. Z        newfile = FALSE;
  1523. Z        } else {
  1524. Z        newfile = TRUE;
  1525. Z        }
  1526. Z        fp = fopen (name, "w");
  1527. Z        if (fp == NULL) {
  1528. Z        (VOID) fprintf (_db_fp_, ERR_OPEN, _db_process_, name);
  1529. Z        perror ("");
  1530. Z        (VOID) fflush (_db_fp_);
  1531. Z        (VOID) Delay (stack -> delay);
  1532. Z        } else {
  1533. Z        _db_pfp_ = fp;
  1534. Z        stack -> prof_file = fp;
  1535. Z        if (newfile) {
  1536. Z            ChangeOwner (name);
  1537. Z        }
  1538. Z        }
  1539. Z    }
  1540. Z    }
  1541. Z}
  1542. Z
  1543. Z
  1544. Z/*
  1545. Z *  FUNCTION
  1546. Z *
  1547. Z *    CloseFile    close the debug output stream
  1548. Z *
  1549. Z *  SYNOPSIS
  1550. Z *
  1551. Z *    LOCAL VOID CloseFile (fp)
  1552. Z *    FILE *fp;
  1553. Z *
  1554. Z *  DESCRIPTION
  1555. Z *
  1556. Z *    Closes the debug output stream unless it is standard output
  1557. Z *    or standard error.
  1558. Z *
  1559. Z */
  1560. Z
  1561. ZLOCAL VOID CloseFile (fp)
  1562. ZFILE *fp;
  1563. Z{
  1564. Z    if (fp != stderr && fp != stdout) {
  1565. Z    if (fclose (fp) == EOF) {
  1566. Z        (VOID) fprintf (stderr, ERR_CLOSE, _db_process_);
  1567. Z        perror ("");
  1568. Z        (VOID) fflush (stderr);
  1569. Z        (VOID) Delay (stack -> delay);
  1570. Z    }
  1571. Z    }
  1572. Z}
  1573. Z
  1574. Z
  1575. Z/*
  1576. Z *  FUNCTION
  1577. Z *
  1578. Z *    DbugExit    print error message and exit
  1579. Z *
  1580. Z *  SYNOPSIS
  1581. Z *
  1582. Z *    LOCAL VOID DbugExit (why)
  1583. Z *    char *why;
  1584. Z *
  1585. Z *  DESCRIPTION
  1586. Z *
  1587. Z *    Prints error message using current process name, the reason for
  1588. Z *    aborting (typically out of memory), and exits with status 1.
  1589. Z *    This should probably be changed to use a status code
  1590. Z *    defined in the user's debugger include file.
  1591. Z *
  1592. Z */
  1593. ZLOCAL VOID DbugExit (why)
  1594. Zchar *why;
  1595. Z{
  1596. Z    (VOID) fprintf (stderr, ERR_ABORT, _db_process_, why);
  1597. Z    (VOID) fflush (stderr);
  1598. Z    (VOID) Delay (stack -> delay);
  1599. Z    exit (1);
  1600. Z}
  1601. Z
  1602. Z
  1603. Z/*
  1604. Z *  FUNCTION
  1605. Z *
  1606. Z *    DbugMalloc    allocate memory for debugger runtime support
  1607. Z *
  1608. Z *  SYNOPSIS
  1609. Z *
  1610. Z *    LOCAL long *DbugMalloc (size)
  1611. Z *    int size;
  1612. Z *
  1613. Z *  DESCRIPTION
  1614. Z *
  1615. Z *    Allocate more memory for debugger runtime support functions.
  1616. Z *    Failure to to allocate the requested number of bytes is
  1617. Z *    immediately fatal to the current process.  This may be
  1618. Z *    rather unfriendly behavior.  It might be better to simply
  1619. Z *    print a warning message, freeze the current debugger state,
  1620. Z *    and continue execution.
  1621. Z *
  1622. Z */
  1623. ZLOCAL long *DbugMalloc (size)
  1624. Zint size;
  1625. Z{
  1626. Z    register long *new;
  1627. Z
  1628. Z    new = (long *) malloc ((unsigned int) size);
  1629. Z    if (new == NULL) {
  1630. Z    DbugExit ("out of memory");
  1631. Z    }
  1632. Z    return (new);
  1633. Z}
  1634. Z
  1635. Z
  1636. Z/*
  1637. Z *    This function may be eliminated when strtok is available
  1638. Z *    in the runtime environment (missing from BSD4.1).
  1639. Z */
  1640. Z
  1641. ZLOCAL char *strtok (s1, s2)
  1642. Zchar *s1, *s2;
  1643. Z{
  1644. Z    static char *end = NULL;
  1645. Z    REGISTER char *rtnval;
  1646. Z
  1647. Z    rtnval = NULL;
  1648. Z    if (s2 != NULL) {
  1649. Z    if (s1 != NULL) {
  1650. Z        end = s1;
  1651. Z        rtnval = strtok ((char *) NULL, s2);
  1652. Z    } else if (end != NULL) {
  1653. Z        if (*end != EOS) {
  1654. Z        rtnval = end;
  1655. Z        while (*end != *s2 && *end != EOS) {end++;}
  1656. Z        if (*end != EOS) {
  1657. Z            *end++ = EOS;
  1658. Z        }
  1659. Z        }
  1660. Z    }
  1661. Z    }
  1662. Z    return (rtnval);
  1663. Z}
  1664. Z
  1665. Z
  1666. Z/*
  1667. Z *  FUNCTION
  1668. Z *
  1669. Z *    BaseName    strip leading pathname components from name
  1670. Z *
  1671. Z *  SYNOPSIS
  1672. Z *
  1673. Z *    LOCAL char *BaseName (pathname)
  1674. Z *    char *pathname;
  1675. Z *
  1676. Z *  DESCRIPTION
  1677. Z *
  1678. Z *    Given pointer to a complete pathname, locates the base file
  1679. Z *    name at the end of the pathname and returns a pointer to
  1680. Z *    it.
  1681. Z *
  1682. Z */
  1683. Z
  1684. ZLOCAL char *BaseName (pathname)
  1685. Zchar *pathname;
  1686. Z{
  1687. Z    register char *base;
  1688. Z
  1689. Z    base = strrchr (pathname, '/');
  1690. Z    if (base++ == NULL) {
  1691. Z    base = pathname;
  1692. Z    }
  1693. Z    return (base);
  1694. Z}
  1695. Z
  1696. Z
  1697. Z/*
  1698. Z *  FUNCTION
  1699. Z *
  1700. Z *    Writable    test to see if a pathname is writable/creatable
  1701. Z *
  1702. Z *  SYNOPSIS
  1703. Z *
  1704. Z *    LOCAL BOOLEAN Writable (pathname)
  1705. Z *    char *pathname;
  1706. Z *
  1707. Z *  DESCRIPTION
  1708. Z *
  1709. Z *    Because the debugger might be linked in with a program that
  1710. Z *    runs with the set-uid-bit (suid) set, we have to be careful
  1711. Z *    about opening a user named file for debug output.  This consists
  1712. Z *    of checking the file for write access with the real user id,
  1713. Z *    or checking the directory where the file will be created.
  1714. Z *
  1715. Z *    Returns TRUE if the user would normally be allowed write or
  1716. Z *    create access to the named file.  Returns FALSE otherwise.
  1717. Z *
  1718. Z */
  1719. Z
  1720. ZLOCAL BOOLEAN Writable (pathname)
  1721. Zchar *pathname;
  1722. Z{
  1723. Z    REGISTER BOOLEAN granted;
  1724. Z#if (unix || xenix)
  1725. Z    REGISTER char *lastslash;
  1726. Z#endif
  1727. Z
  1728. Z#if (!unix && !xenix)
  1729. Z    granted = TRUE;
  1730. Z#else
  1731. Z    granted = FALSE;
  1732. Z    if (EXISTS (pathname)) {
  1733. Z    if (WRITABLE (pathname)) {
  1734. Z        granted = TRUE;
  1735. Z    }
  1736. Z    } else {
  1737. Z    lastslash = strrchr (pathname, '/');
  1738. Z    if (lastslash != NULL) {
  1739. Z        *lastslash = EOS;
  1740. Z    } else {
  1741. Z        pathname = ".";
  1742. Z    }
  1743. Z    if (WRITABLE (pathname)) {
  1744. Z        granted = TRUE;
  1745. Z    }
  1746. Z    if (lastslash != NULL) {
  1747. Z        *lastslash = '/';
  1748. Z    }
  1749. Z    }
  1750. Z#endif
  1751. Z    return (granted);
  1752. Z}
  1753. Z
  1754. Z
  1755. Z/*
  1756. Z *    This function may be eliminated when strrchr is available
  1757. Z *    in the runtime environment (missing from BSD4.1).
  1758. Z *    Alternately, you can use rindex() on BSD systems.
  1759. Z */
  1760. Z
  1761. ZLOCAL char *strrchr (s, c)
  1762. Zchar *s;
  1763. Zchar c;
  1764. Z{
  1765. Z    REGISTER char *scan;
  1766. Z
  1767. Z    for (scan = s; *scan != EOS; scan++) {;}
  1768. Z    while (scan > s && *--scan != c) {;}
  1769. Z    if (*scan != c) {
  1770. Z    scan = NULL;
  1771. Z    }
  1772. Z    return (scan);
  1773. Z}
  1774. Z
  1775. Z
  1776. Z/*
  1777. Z *  FUNCTION
  1778. Z *
  1779. Z *    ChangeOwner    change owner to real user for suid programs
  1780. Z *
  1781. Z *  SYNOPSIS
  1782. Z *
  1783. Z *    LOCAL VOID ChangeOwner (pathname)
  1784. Z *
  1785. Z *  DESCRIPTION
  1786. Z *
  1787. Z *    For unix systems, change the owner of the newly created debug
  1788. Z *    file to the real owner.  This is strictly for the benefit of
  1789. Z *    programs that are running with the set-user-id bit set.
  1790. Z *
  1791. Z *    Note that at this point, the fact that pathname represents
  1792. Z *    a newly created file has already been established.  If the
  1793. Z *    program that the debugger is linked to is not running with
  1794. Z *    the suid bit set, then this operation is redundant (but
  1795. Z *    harmless).
  1796. Z *
  1797. Z */
  1798. Z
  1799. ZLOCAL VOID ChangeOwner (pathname)
  1800. Zchar *pathname;
  1801. Z{
  1802. Z#if (unix || xenix)
  1803. Z    if (chown (pathname, getuid (), getgid ()) == -1) {
  1804. Z    (VOID) fprintf (stderr, ERR_CHOWN, _db_process_, pathname);
  1805. Z    perror ("");
  1806. Z    (VOID) fflush (stderr);
  1807. Z    (VOID) Delay (stack -> delay);
  1808. Z    }
  1809. Z#endif
  1810. Z}
  1811. Z
  1812. Z
  1813. Z/*
  1814. Z *  FUNCTION
  1815. Z *
  1816. Z *    _db_setjmp_    save debugger environment
  1817. Z *
  1818. Z *  SYNOPSIS
  1819. Z *
  1820. Z *    VOID _db_setjmp_ ()
  1821. Z *
  1822. Z *  DESCRIPTION
  1823. Z *
  1824. Z *    Invoked as part of the user's DBUG_SETJMP macro to save
  1825. Z *    the debugger environment in parallel with saving the user's
  1826. Z *    environment.
  1827. Z *
  1828. Z */
  1829. Z
  1830. ZVOID _db_setjmp_ ()
  1831. Z{
  1832. Z   jmplevel = stack -> level;
  1833. Z   jmpfunc = func;
  1834. Z   jmpfile = file;
  1835. Z}
  1836. Z
  1837. Z
  1838. Z/*
  1839. Z *  FUNCTION
  1840. Z *
  1841. Z *    _db_longjmp_    restore previously saved debugger environment
  1842. Z *
  1843. Z *  SYNOPSIS
  1844. Z *
  1845. Z *    VOID _db_longjmp_ ()
  1846. Z *
  1847. Z *  DESCRIPTION
  1848. Z *
  1849. Z *    Invoked as part of the user's DBUG_LONGJMP macro to restore
  1850. Z *    the debugger environment in parallel with restoring the user's
  1851. Z *    previously saved environment.
  1852. Z *
  1853. Z */
  1854. Z
  1855. ZVOID _db_longjmp_ ()
  1856. Z{
  1857. Z    stack -> level = jmplevel;
  1858. Z    if (jmpfunc) {
  1859. Z    func = jmpfunc;
  1860. Z    }
  1861. Z    if (jmpfile) {
  1862. Z    file = jmpfile;
  1863. Z    }
  1864. Z}
  1865. Z
  1866. Z
  1867. Z/*
  1868. Z *  FUNCTION
  1869. Z *
  1870. Z *    DelayArg   convert D flag argument to appropriate value
  1871. Z *
  1872. Z *  SYNOPSIS
  1873. Z *
  1874. Z *    LOCAL int DelayArg (value)
  1875. Z *    int value;
  1876. Z *
  1877. Z *  DESCRIPTION
  1878. Z *
  1879. Z *    Converts delay argument, given in tenths of a second, to the
  1880. Z *    appropriate numerical argument used by the system to delay
  1881. Z *    that that many tenths of a second.  For example, on the
  1882. Z *    amiga, there is a system call "Delay()" which takes an
  1883. Z *    argument in ticks (50 per second).  On unix, the sleep
  1884. Z *    command takes seconds.  Thus a value of "10", for one
  1885. Z *    second of delay, gets converted to 50 on the amiga, and 1
  1886. Z *    on unix.  Other systems will need to use a timing loop.
  1887. Z *
  1888. Z */
  1889. Z
  1890. ZLOCAL int DelayArg (value)
  1891. Zint value;
  1892. Z{
  1893. Z    unsigned int delayarg = 0;
  1894. Z    
  1895. Z#if (unix || xenix)
  1896. Z    delayarg = value / 10;        /* Delay is in seconds for sleep () */
  1897. Z#endif
  1898. Z#if amiga
  1899. Z    delayarg = (HZ * value) / 10;    /* Delay in ticks for Delay () */
  1900. Z#endif
  1901. Z    return (delayarg);
  1902. Z}
  1903. Z
  1904. Z
  1905. Z/*
  1906. Z *    A dummy delay stub for systems that do not support delays.
  1907. Z *    With a little work, this can be turned into a timing loop.
  1908. Z */
  1909. Z
  1910. Z#if (!unix && !xenix && !amiga)
  1911. ZDelay ()
  1912. Z{
  1913. Z}
  1914. Z#endif
  1915. Z
  1916. Z
  1917. Z/*
  1918. Z *  FUNCTION
  1919. Z *
  1920. Z *    perror    perror simulation for systems that don't have it
  1921. Z *
  1922. Z *  SYNOPSIS
  1923. Z *
  1924. Z *    LOCAL VOID perror (s)
  1925. Z *    char *s;
  1926. Z *
  1927. Z *  DESCRIPTION
  1928. Z *
  1929. Z *    Perror produces a message on the standard error stream which
  1930. Z *    provides more information about the library or system error
  1931. Z *    just encountered.  The argument string s is printed, followed
  1932. Z *    by a ':', a blank, and then a message and a newline.
  1933. Z *
  1934. Z *    An undocumented feature of the unix perror is that if the string
  1935. Z *    's' is a null string (NOT a NULL pointer!), then the ':' and
  1936. Z *    blank are not printed.
  1937. Z *
  1938. Z *    This version just complains about an "unknown system error".
  1939. Z *
  1940. Z */
  1941. Z
  1942. Z#if !unix && !xenix && !(amiga && LATTICE)
  1943. Z
  1944. ZLOCAL VOID perror (s)
  1945. Zchar *s;
  1946. Z{
  1947. Z    if (s && *s != EOS) {
  1948. Z    (VOID) fprintf (stderr, "%s: ", s);
  1949. Z    }
  1950. Z    (VOID) fprintf (stderr, "<unknown system error>\n");
  1951. Z}
  1952. Z
  1953. Z#endif    /* !unix && !xenix && !(amiga && LATTICE) */
  1954. Z
  1955. Z/*
  1956. Z * Here we need the definitions of the clock routine.  Add your
  1957. Z * own for whatever system that you have.
  1958. Z */
  1959. Z
  1960. Z#if (unix || xenix)
  1961. Z
  1962. Z# include <sys/param.h>
  1963. Z# if BSD4_3 || sun
  1964. Z
  1965. Z/*
  1966. Z * Definition of the Clock() routine for 4.3 BSD.
  1967. Z */
  1968. Z
  1969. Z#include <sys/time.h>
  1970. Z#include <sys/resource.h>
  1971. Z
  1972. Z/*
  1973. Z * Returns the user time in milliseconds used by this process so
  1974. Z * far.
  1975. Z */
  1976. Z
  1977. ZLOCAL unsigned long Clock ()
  1978. Z{
  1979. Z    struct rusage ru;
  1980. Z
  1981. Z    (VOID) getrusage (RUSAGE_SELF, &ru);
  1982. Z    return ((ru.ru_utime.tv_sec * 1000) + (ru.ru_utime.tv_usec / 1000));
  1983. Z}
  1984. Z
  1985. Z#else
  1986. Z
  1987. ZLOCAL unsigned long Clock ()
  1988. Z{
  1989. Z    return (0);
  1990. Z}
  1991. Z
  1992. Z# endif
  1993. Z
  1994. Z#else
  1995. Z
  1996. Z#if amiga
  1997. Z
  1998. Zstruct DateStamp {        /* Yes, this is a hack, but doing it right */
  1999. Z    long ds_Days;        /* is incredibly ugly without splitting this */
  2000. Z    long ds_Minute;        /* off into a separate file */
  2001. Z    long ds_Tick;
  2002. Z};
  2003. Z
  2004. Zstatic int first_clock = TRUE;
  2005. Zstatic struct DateStamp begin;
  2006. Zstatic struct DateStamp elapsed;
  2007. Z
  2008. ZLOCAL unsigned long Clock ()
  2009. Z{
  2010. Z    register struct DateStamp *now;
  2011. Z    register unsigned long millisec = 0;
  2012. Z    extern VOID *AllocMem ();
  2013. Z
  2014. Z    now = (struct DateStamp *) AllocMem ((long) sizeof (struct DateStamp), 0L);
  2015. Z    if (now != NULL) {
  2016. Z    if (first_clock == TRUE) {
  2017. Z        first_clock = FALSE;
  2018. Z        (VOID) DateStamp (now);
  2019. Z        begin = *now;
  2020. Z    }
  2021. Z    (VOID) DateStamp (now);
  2022. Z    millisec = 24 * 3600 * (1000 / HZ) * (now -> ds_Days - begin.ds_Days);
  2023. Z    millisec += 60 * (1000 / HZ) * (now -> ds_Minute - begin.ds_Minute);
  2024. Z    millisec += (1000 / HZ) * (now -> ds_Tick - begin.ds_Tick);
  2025. Z    (VOID) FreeMem (now, (long) sizeof (struct DateStamp));
  2026. Z    }
  2027. Z    return (millisec);
  2028. Z}
  2029. Z
  2030. Z#endif    /* amiga */
  2031. Z#endif    /* unix */
  2032. STUNKYFLUFF
  2033. set `sum dbug.c`
  2034. if test 40274 != $1
  2035. then
  2036. echo dbug.c: Checksum error. Is: $1, should be: 40274.
  2037. fi
  2038. #
  2039. #
  2040. echo Extracting Makefile:
  2041. sed 's/^Z//' >Makefile <<\STUNKYFLUFF
  2042. Z#
  2043. Z#  FILE
  2044. Z#
  2045. Z#    Makefile    Makefile for dbug package
  2046. Z#
  2047. Z#  SCCS ID
  2048. Z#
  2049. Z#    @(#)Makefile    1.11 3/12/88
  2050. Z#
  2051. Z#  DESCRIPTION
  2052. Z#
  2053. Z#    Makefile for the dbug package (under UNIX system V or 4.2BSD).
  2054. Z#
  2055. Z#    Interesting things to make are:
  2056. Z#
  2057. Z#    lib    =>    Makes the runtime support library in the
  2058. Z#            current directory.
  2059. Z#
  2060. Z#    lintlib    =>    Makes the lint library in the current directory.
  2061. Z#
  2062. Z#    install    =>    Install pieces from current directory to
  2063. Z#            where they belong.
  2064. Z#
  2065. Z#    doc    =>    Makes the documentation in the current
  2066. Z#            directory.
  2067. Z#
  2068. Z#    clean    =>    Remove objects, temporary files, etc from
  2069. Z#            current directory.
  2070. Z#
  2071. Z#    superclean =>    Remove everything except sccs source files.
  2072. Z#            Uses interactive remove for verification.
  2073. Z
  2074. Z# Define NO_VARARGS if you have no <varargs.h> to include.
  2075. Z#VARARGS =    -DNO_VARARGS
  2076. Z
  2077. ZSH =        /bin/sh
  2078. ZAR =        ar
  2079. ZRM =        rm
  2080. ZCFLAGS =    -O $(VARARGS)
  2081. ZGFLAGS =    -s
  2082. ZINSTALL =    $(SH) install.sh
  2083. ZCHMOD =        chmod
  2084. ZMAKE =        make
  2085. ZINC =        /usr/include/local
  2086. ZLIB =        /usr/lib
  2087. ZRANLIB =    $(SH) ranlib.sh
  2088. ZMODE =        664
  2089. Z
  2090. Z# The following is provided for example only, it is set by "doinstall.sh".
  2091. ZLLIB =        /usr/lib
  2092. Z
  2093. Z.SUFFIXES:    .r .r~ .c .c~
  2094. Z
  2095. Z.c~.c:
  2096. Z        $(GET) $(GFLAGS) -p $< >$*.c
  2097. Z
  2098. Z.r~.r:
  2099. Z        $(GET) $(GFLAGS) -p $< >$*.r
  2100. Z
  2101. Z.c~.r:
  2102. Z        $(GET) $(GFLAGS) -p $< >$*.c
  2103. Z        sed "s/\\\/\\\e/" <$*.c >$*.r
  2104. Z        $(RM) -f $*.c
  2105. Z
  2106. Z.c.r:
  2107. Z        sed "s/\\\/\\\e/" <$< >$*.r
  2108. Z
  2109. ZEXAMPLES =    example1.r example2.r example3.r
  2110. ZOUTPUTS =    output1.r output2.r output3.r output4.r output5.r
  2111. Z
  2112. ZNROFF_INC =    main.r factorial.r $(OUTPUTS) $(EXAMPLES)
  2113. Z
  2114. Z
  2115. Z#
  2116. Z#    The default thing to do is remake the local runtime support
  2117. Z#    library, local lint library, and local documentation as
  2118. Z#    necessary.
  2119. Z#
  2120. Z
  2121. Zall :        lib lintlib analyze doc
  2122. Z
  2123. Zlib :        libdbug.a
  2124. Z
  2125. Zlintlib :    llib-ldbug.ln
  2126. Z
  2127. Zdoc :        factorial user.t
  2128. Z
  2129. Z#
  2130. Z#    Make the local runtime support library "libdbug.a" from
  2131. Z#    sources.
  2132. Z#
  2133. Z
  2134. Zlibdbug.a :    dbug.o
  2135. Z        rm -f $@
  2136. Z        $(AR) cr $@ $?
  2137. Z        $(RANLIB) $@
  2138. Z
  2139. Z#
  2140. Z#    Clean up the local directory.
  2141. Z#
  2142. Z
  2143. Zclean :
  2144. Z        $(RM) -f *.o *.ln *.a *.BAK nohup.out factorial $(NROFF_INC)
  2145. Z
  2146. Zsuperclean :
  2147. Z        $(RM) -i ?[!.]*
  2148. Z
  2149. Z#
  2150. Z#    Install the new header and library files.  Since things go in
  2151. Z#    different places depending upon the system (lint libraries
  2152. Z#    go in /usr/lib under SV and /usr/lib/lint under BSD for example),
  2153. Z#    the shell script "doinstall.sh" figures out the environment and
  2154. Z#    does a recursive make with the appropriate pathnames set.
  2155. Z#
  2156. Z
  2157. Zinstall :        
  2158. Z            $(SH) doinstall.sh $(MAKE) sysinstall
  2159. Z
  2160. Zsysinstall:        $(INC) $(INC)/dbug.h $(LIB)/libdbug.a \
  2161. Z            $(LLIB)/llib-ldbug.ln $(LLIB)/llib-ldbug
  2162. Z
  2163. Z$(INC) :
  2164. Z            -if test -d $@ ;then true ;else mkdir $@ ;fi
  2165. Z
  2166. Z$(INC)/dbug.h :        dbug.h
  2167. Z            $(INSTALL) $? $@
  2168. Z            $(CHMOD) $(MODE) $@
  2169. Z
  2170. Z$(LIB)/libdbug.a :    libdbug.a
  2171. Z            $(INSTALL) $? $@
  2172. Z            $(CHMOD) $(MODE) $@
  2173. Z
  2174. Z$(LLIB)/llib-ldbug.ln :    llib-ldbug.ln
  2175. Z            $(INSTALL) $? $@
  2176. Z            $(CHMOD) $(MODE) $@
  2177. Z
  2178. Z$(LLIB)/llib-ldbug :    llib-ldbug
  2179. Z            $(INSTALL) $? $@
  2180. Z            $(CHMOD) $(MODE) $@
  2181. Z
  2182. Z#
  2183. Z#    Make the local lint library.
  2184. Z#
  2185. Z
  2186. Zllib-ldbug.ln :     llib-ldbug
  2187. Z            $(SH) mklintlib.sh $? $@
  2188. Z
  2189. Z#
  2190. Z#    Make the test/example program "factorial".
  2191. Z#
  2192. Z#    Note that the objects depend on the LOCAL dbug.h file and
  2193. Z#    the compilations are set up to find dbug.h in the current
  2194. Z#    directory even though the sources have "#include <dbug.h>".
  2195. Z#    This allows the examples to look like the code a user would
  2196. Z#    write but still be used as test cases for new versions
  2197. Z#    of dbug.
  2198. Z
  2199. Zfactorial :    main.o factorial.o libdbug.a
  2200. Z        $(CC) -o $@ main.o factorial.o libdbug.a
  2201. Z
  2202. Zmain.o :    main.c dbug.h
  2203. Z        $(CC) $(CFLAGS) -c -I. main.c
  2204. Z
  2205. Zfactorial.o :    factorial.c dbug.h
  2206. Z        $(CC) $(CFLAGS) -c -I. factorial.c
  2207. Z
  2208. Z
  2209. Z#
  2210. Z#    Make the analyze program for runtime profiling support.
  2211. Z#
  2212. Z
  2213. Zanalyze :    analyze.o libdbug.a
  2214. Z        $(CC) -o $@ analyze.o libdbug.a
  2215. Z
  2216. Zanalyze.o :    analyze.c useful.h dbug.h
  2217. Z        $(CC) $(CFLAGS) -c -I. analyze.c
  2218. Z
  2219. Z#
  2220. Z#    Rebuild the documentation
  2221. Z#
  2222. Z
  2223. Zuser.t :    user.r $(NROFF_INC)
  2224. Z        nroff -cm user.r >$@
  2225. Z
  2226. Z#
  2227. Z#    Run the factorial program to produce the sample outputs.
  2228. Z#
  2229. Z
  2230. Zoutput1.r:    factorial
  2231. Z        ./factorial 1 2 3 4 5 >$@
  2232. Z
  2233. Zoutput2.r:    factorial
  2234. Z        ./factorial -#t:o 2 3 >$@
  2235. Z
  2236. Zoutput3.r:    factorial
  2237. Z        ./factorial -#d:t:o 3 >$@
  2238. Z
  2239. Zoutput4.r:    factorial
  2240. Z        ./factorial -#d,result:o 4 >$@
  2241. Z
  2242. Zoutput5.r:    factorial
  2243. Z        ./factorial -#d:f,factorial:F:L:o 3 >$@
  2244. Z
  2245. Z#
  2246. Z#    All files included by user.r depend on user.r, thus
  2247. Z#    forcing them to be remade if user.r changes.
  2248. Z#
  2249. Z
  2250. Z$(NROFF_INC) :    user.r
  2251. Z
  2252. STUNKYFLUFF
  2253. set `sum Makefile`
  2254. if test 62626 != $1
  2255. then
  2256. echo Makefile: Checksum error. Is: $1, should be: 62626.
  2257. fi
  2258. #
  2259. #
  2260. echo Extracting README.prof:
  2261. sed 's/^Z//' >README.prof <<\STUNKYFLUFF
  2262. ZHi,
  2263. Z
  2264. ZI'm sending you the modifications I made to your Dbug routines to
  2265. Zallow profiling in a (relatively) machine independent fashion.
  2266. ZI use your Dbug routines fairly extensively.  Unfortunately, it's
  2267. Za royal pain to have to keep profiled versions of various libraries
  2268. Zaround.  The modifications allow profiling without the need for this.
  2269. Z
  2270. ZHow it works.
  2271. Z------------
  2272. Z
  2273. ZBasically, I just added code in the dbug routines to write out a file
  2274. Zcalled dbugmon.out (by default).  This is an ascii file containing lines
  2275. Zof the form:
  2276. Z
  2277. Z<function-name> E <time-entered>
  2278. Z<function-name> X <time-exited>
  2279. Z
  2280. ZA second program (analyze) reads this file, and produces a report on
  2281. Zstandard output.
  2282. Z
  2283. ZProfiling is enabled through the `g' flag.  It can take a list of
  2284. Zprocedure names for which profiling is enabled.  By default, it
  2285. Zprofiles all procedures.
  2286. Z
  2287. ZThe code in ``dbug.c'' opens the profile file for appending.  This
  2288. Zis in order that one can run a program several times, and get the
  2289. Zsum total of all the times, etc.
  2290. Z
  2291. ZThe only system dependent part that I'm aware of is the routine
  2292. ZClock() at the end of dbug.c.  This returns the elapsed user time
  2293. Zin milliseconds.  The version which I have is for 4.3 BSD.  As I
  2294. Zdon't have access to other systems, I'm not certain how this would
  2295. Zchange.
  2296. Z
  2297. ZAn example of the report generated follows:
  2298. Z
  2299. Z        Profile of Execution
  2300. Z        Execution times are in milliseconds
  2301. Z
  2302. Z            Calls                Time
  2303. Z            -----                ----
  2304. Z        Times    Percentage    Time Spent    Percentage
  2305. ZFunction    Called    of total    in Function    of total    Importance
  2306. Z========    ======    ==========    ===========    ==========  ==========
  2307. Zfactorial               5         83.33             30        100.00        8333
  2308. Zmain                    1         16.67              0          0.00           0
  2309. Z========    ======    ==========    ===========    ==========
  2310. ZTotals                  6        100.00             30        100.00
  2311. Z
  2312. Z
  2313. ZAs you can see, it's quite self-evident.  The ``Importance'' column is a
  2314. Zmetric obtained by multiplying the percentage of the calls and the percentage
  2315. Zof the time.  Functions with higher 'importance' benefit the most from
  2316. Zbeing sped up.
  2317. Z
  2318. ZI'm really not certain how to add support for setjmp/longjmp, or for
  2319. Zchild processes, so I've ignored that for the time being.  In most of
  2320. Zthe code that I write, it isn't necessary.  If you have any good ideas,
  2321. Zfeel free to add them.
  2322. Z
  2323. ZThis has been very useful to me.  If you can use it as part of your
  2324. Zdbug distribution, please feel free to do so.
  2325. Z
  2326. ZRegards,
  2327. Z
  2328. Z                Binayak Banerjee
  2329. Z        {allegra | astrovax | bpa | burdvax}!sjuvax!bbanerje
  2330. Z            bbanerje%sjuvax.sju.edu@relay.cs.net
  2331. Z                July 9, 1987
  2332. STUNKYFLUFF
  2333. set `sum README.prof`
  2334. if test 15724 != $1
  2335. then
  2336. echo README.prof: Checksum error. Is: $1, should be: 15724.
  2337. fi
  2338. echo ALL DONE BUNKY!
  2339. exit 0
  2340.  
  2341.  
  2342.