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

  1. Newsgroups: comp.sources.misc
  2. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  3. Subject: v07i058: DBUG Package (Part 2 of 3)
  4. Reply-To: fnf@estinc.UUCP
  5.  
  6. Posting-number: Volume 7, Issue 58
  7. Submitted-by: fnf@estinc.UUCP
  8. Archive-name: dbug/part02
  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 analyze.c:
  47. sed 's/^Z//' >analyze.c <<\STUNKYFLUFF
  48. Z/*
  49. Z * Analyze the profile file (cmon.out) written out by the dbug
  50. Z * routines with profiling enabled.
  51. Z *
  52. Z * Copyright June 1987, Binayak Banerjee
  53. Z * All rights reserved.
  54. Z *
  55. Z * This program may be freely distributed under the same terms and
  56. Z * conditions as Fred Fish's Dbug package.
  57. Z *
  58. Z * Compile with -- cc -O -s -o %s analyze.c
  59. Z *
  60. Z * Analyze will read an trace file created by the dbug package
  61. Z * (when run with traceing enabled).  It will then produce a
  62. Z * summary on standard output listing the name of each traced
  63. Z * function, the number of times it was called, the percentage
  64. Z * of total calls, the time spent executing the function, the
  65. Z * proportion of the total time and the 'importance'.  The last
  66. Z * is a metric which is obtained by multiplying the proportions
  67. Z * of calls and the proportions of time for each function.  The
  68. Z * greater the importance, the more likely it is that a speedup
  69. Z * could be obtained by reducing the time taken by that function.
  70. Z *
  71. Z * Note that the timing values that you obtain are only rough
  72. Z * measures.  The overhead of the dbug package is included
  73. Z * within.  However, there is no need to link in special profiled
  74. Z * libraries and the like.
  75. Z *
  76. Z * CHANGES:
  77. Z *
  78. Z *    2-Mar-89: fnf
  79. Z *    Changes to support tracking of stack usage.  This required
  80. Z *    reordering the fields in the profile log file to make
  81. Z *    parsing of different record types easier.  Corresponding
  82. Z *    changes made in dbug runtime library.  Also used this
  83. Z *    opportunity to reformat the code more to my liking (my
  84. Z *    apologies to Binayak Banerjee for "uglifying" his code).
  85. Z *
  86. Z *    24-Jul-87: fnf
  87. Z *    Because I tend to use functions names like
  88. Z *    "ExternalFunctionDoingSomething", I've rearranged the
  89. Z *    printout to put the function name last in each line, so
  90. Z *    long names don't screw up the formatting unless they are
  91. Z *    *very* long and wrap around the screen width...
  92. Z *
  93. Z *    24-Jul-87: fnf
  94. Z *    Modified to put out table very similar to Unix profiler
  95. Z *    by default, but also puts out original verbose table
  96. Z *    if invoked with -v flag.
  97. Z */
  98. Z
  99. Z#include <stdio.h>
  100. Z#include "useful.h"
  101. Z
  102. Zstatic char *my_name;
  103. Zstatic int verbose;
  104. Z
  105. Z/*
  106. Z * Structure of the stack.
  107. Z */
  108. Z
  109. Z#define PRO_FILE    "dbugmon.out"    /* Default output file name */
  110. Z#define STACKSIZ    100        /* Maximum function nesting */
  111. Z#define MAXPROCS    1000        /* Maximum number of function calls */
  112. Z
  113. Zstruct stack_t {
  114. Z    unsigned int pos;            /* which function? */
  115. Z    unsigned long time;            /* Time that this was entered */
  116. Z    unsigned long children;        /* Time spent in called funcs */
  117. Z};
  118. Z
  119. Zstatic struct stack_t fn_stack[STACKSIZ+1];
  120. Z
  121. Zstatic unsigned int stacktop = 0;    /* Lowest stack position is a dummy */
  122. Z
  123. Zstatic unsigned long tot_time = 0;
  124. Zstatic unsigned long tot_calls = 0;
  125. Zstatic unsigned long highstack = 0;
  126. Zstatic unsigned long lowstack = ~0;
  127. Z
  128. Z/*
  129. Z * top() returns a pointer to the top item on the stack.
  130. Z * (was a function, now a macro)
  131. Z */
  132. Z
  133. Z#define top()    &fn_stack[stacktop]
  134. Z    
  135. Z/*
  136. Z * Push - Push the given record on the stack.
  137. Z */
  138. Z    
  139. Zvoid push (name_pos, time_entered)
  140. Zregister unsigned int name_pos;
  141. Zregister unsigned long time_entered;
  142. Z{
  143. Z    register struct stack_t *t;
  144. Z    
  145. Z    DBUG_ENTER("push");
  146. Z    if (++stacktop > STACKSIZ) {
  147. Z    fprintf (DBUG_FILE,"%s: stack overflow (%s:%d)\n",
  148. Z        my_name, __FILE__, __LINE__);
  149. Z    exit (EX_SOFTWARE);
  150. Z    }
  151. Z    DBUG_PRINT ("push", ("%d %ld",name_pos,time_entered));
  152. Z    t = &fn_stack[stacktop];
  153. Z    t -> pos = name_pos;
  154. Z    t -> time = time_entered;
  155. Z    t -> children = 0;
  156. Z    DBUG_VOID_RETURN;
  157. Z}
  158. Z
  159. Z/*
  160. Z * Pop - pop the top item off the stack, assigning the field values
  161. Z * to the arguments. Returns 0 on stack underflow, or on popping first
  162. Z * item off stack.
  163. Z */
  164. Z
  165. Zunsigned int pop (name_pos, time_entered, child_time)
  166. Zregister unsigned int *name_pos;
  167. Zregister unsigned long *time_entered;
  168. Zregister unsigned long *child_time;
  169. Z{
  170. Z    register struct stack_t *temp;
  171. Z    register unsigned int rtnval;
  172. Z    
  173. Z    DBUG_ENTER ("pop");
  174. Z    
  175. Z    if (stacktop < 1) {
  176. Z    rtnval = 0;
  177. Z    } else {
  178. Z    temp =  &fn_stack[stacktop];
  179. Z    *name_pos = temp->pos;
  180. Z    *time_entered = temp->time;
  181. Z    *child_time = temp->children;
  182. Z    DBUG_PRINT ("pop", ("%d %d %d",*name_pos,*time_entered,*child_time));
  183. Z    rtnval = stacktop--;
  184. Z    }
  185. Z    DBUG_RETURN (rtnval);
  186. Z}
  187. Z
  188. Z/*
  189. Z * We keep the function info in another array (serves as a simple
  190. Z * symbol table)
  191. Z */
  192. Z
  193. Zstruct module_t {
  194. Z    char *name;
  195. Z    unsigned long m_time;
  196. Z    unsigned long m_calls;
  197. Z    unsigned long m_stkuse;
  198. Z};
  199. Z
  200. Zstatic struct module_t modules[MAXPROCS];
  201. Z
  202. Z/*
  203. Z * We keep a binary search tree in order to look up function names
  204. Z * quickly (and sort them at the end.
  205. Z */
  206. Z
  207. Zstruct bnode {
  208. Z    unsigned int lchild;    /* Index of left subtree */
  209. Z    unsigned int rchild;    /* Index of right subtree */
  210. Z    unsigned int pos;        /* Index of module_name entry */
  211. Z};
  212. Z
  213. Zstatic struct bnode s_table[MAXPROCS];
  214. Z
  215. Zstatic unsigned int n_items = 0;    /* No. of items in the array so far */
  216. Z
  217. Z/*
  218. Z * Need a function to allocate space for a string and squirrel it away.
  219. Z */
  220. Z
  221. Zchar *strsave (s)
  222. Zchar *s;
  223. Z{
  224. Z    register char *retval;
  225. Z    register unsigned int len;
  226. Z    extern char *malloc ();
  227. Z    
  228. Z    DBUG_ENTER ("strsave");
  229. Z    DBUG_PRINT ("strsave", ("%s",s));
  230. Z    if (s == Nil (char) || (len = strlen (s)) == 0) {
  231. Z    DBUG_RETURN (Nil (char));
  232. Z    }    
  233. Z    MALLOC (retval, ++len, char);
  234. Z    strcpy (retval, s);
  235. Z    DBUG_RETURN (retval);
  236. Z}
  237. Z
  238. Z/*
  239. Z * add() - adds m_name to the table (if not already there), and returns
  240. Z * the index of its location in the table.  Checks s_table (which is a
  241. Z * binary search tree) to see whether or not it should be added.
  242. Z */
  243. Z
  244. Zunsigned int add (m_name)
  245. Zchar *m_name;
  246. Z{
  247. Z    register unsigned int ind = 0;
  248. Z    register int cmp;
  249. Z    
  250. Z    DBUG_ENTER ("add");
  251. Z    if (n_items == 0) {        /* First item to be added */
  252. Z    s_table[0].pos = ind;
  253. Z    s_table[0].lchild = s_table[0].rchild = MAXPROCS;
  254. Z    addit:
  255. Z    modules[n_items].name = strsave (m_name);
  256. Z    modules[n_items].m_time = 0;
  257. Z    modules[n_items].m_calls = 0;
  258. Z    modules[n_items].m_stkuse = 0;
  259. Z    DBUG_RETURN (n_items++);
  260. Z    }
  261. Z    while (cmp = strcmp (m_name,modules[ind].name)) {
  262. Z    if (cmp < 0) {    /* In left subtree */
  263. Z        if (s_table[ind].lchild == MAXPROCS) {
  264. Z        /* Add as left child */
  265. Z        if (n_items >= MAXPROCS) {
  266. Z            fprintf (DBUG_FILE,
  267. Z                "%s: Too many functions being profiled\n",
  268. Z                 my_name);
  269. Z            exit (EX_SOFTWARE);
  270. Z        }
  271. Z        s_table[n_items].pos = s_table[ind].lchild = n_items;
  272. Z        s_table[n_items].lchild = s_table[n_items].rchild = MAXPROCS;
  273. Z#ifdef notdef
  274. Z        modules[n_items].name = strsave (m_name);
  275. Z        modules[n_items].m_time = modules[n_items].m_calls = 0;
  276. Z        DBUG_RETURN (n_items++);
  277. Z#else
  278. Z        goto addit;
  279. Z#endif
  280. Z        
  281. Z        }
  282. Z        ind = s_table[ind].lchild; /* else traverse l-tree */
  283. Z    } else {
  284. Z        if (s_table[ind].rchild == MAXPROCS) {
  285. Z        /* Add as right child */
  286. Z        if (n_items >= MAXPROCS) {
  287. Z            fprintf (DBUG_FILE,
  288. Z                 "%s: Too many functions being profiled\n",
  289. Z                 my_name);
  290. Z            exit (EX_SOFTWARE);
  291. Z        }
  292. Z        s_table[n_items].pos = s_table[ind].rchild = n_items;
  293. Z        s_table[n_items].lchild = s_table[n_items].rchild = MAXPROCS;
  294. Z#ifdef notdef
  295. Z        modules[n_items].name = strsave (m_name);
  296. Z        modules[n_items].m_time = modules[n_items].m_calls = 0;
  297. Z        DBUG_RETURN (n_items++);
  298. Z#else
  299. Z        goto addit;
  300. Z#endif
  301. Z        
  302. Z        }
  303. Z        ind = s_table[ind].rchild; /* else traverse r-tree */
  304. Z    }
  305. Z    }
  306. Z    DBUG_RETURN (ind);
  307. Z}
  308. Z
  309. Z/*
  310. Z * process() - process the input file, filling in the modules table.
  311. Z */
  312. Z
  313. Zvoid process (inf)
  314. ZFILE *inf;
  315. Z{
  316. Z    char buf[BUFSIZ];
  317. Z    char fn_name[64];    /* Max length of fn_name */
  318. Z    unsigned long fn_time;
  319. Z    unsigned long fn_sbot;
  320. Z    unsigned long fn_ssz;
  321. Z    unsigned long lastuse;
  322. Z    unsigned int pos;
  323. Z    unsigned long time;
  324. Z    unsigned int oldpos;
  325. Z    unsigned long oldtime;
  326. Z    unsigned long oldchild;
  327. Z    struct stack_t *t;
  328. Z    
  329. Z    DBUG_ENTER ("process");
  330. Z    while (fgets (buf,BUFSIZ,inf) != NULL) {
  331. Z    switch (buf[0]) {
  332. Z        case 'E':
  333. Z        sscanf (buf+2, "%ld %64s", &fn_time, fn_name);
  334. Z        DBUG_PRINT ("erec", ("%ld %s", fn_time, fn_name));
  335. Z        pos = add (fn_name);
  336. Z        push (pos, fn_time);
  337. Z        break;
  338. Z        case 'X':
  339. Z        sscanf (buf+2, "%ld %64s", &fn_time, fn_name);
  340. Z        DBUG_PRINT ("xrec", ("%ld %s", fn_time, fn_name));
  341. Z        pos = add (fn_name);
  342. Z        /*
  343. Z         * An exited function implies that all stacked
  344. Z         * functions are also exited, until the matching
  345. Z         * function is found on the stack.
  346. Z         */
  347. Z        while (pop (&oldpos, &oldtime, &oldchild)) {
  348. Z            DBUG_PRINT ("popped", ("%d %d", oldtime, oldchild));
  349. Z            time = fn_time - oldtime;
  350. Z            t = top ();
  351. Z            t -> children += time;
  352. Z            DBUG_PRINT ("update", ("%s", modules[t -> pos].name));
  353. Z            DBUG_PRINT ("update", ("%d", t -> children));
  354. Z            time -= oldchild;
  355. Z            modules[oldpos].m_time += time;
  356. Z            modules[oldpos].m_calls++;
  357. Z            tot_time += time;
  358. Z            tot_calls++;
  359. Z            if (pos == oldpos) {
  360. Z            goto next_line;    /* Should be a break2 */
  361. Z            }
  362. Z        }
  363. Z        /*
  364. Z         * Assume that item seen started at time 0.
  365. Z         * (True for function main).  But initialize
  366. Z         * it so that it works the next time too.
  367. Z         */
  368. Z        t = top ();
  369. Z        time = fn_time - t -> time - t -> children;
  370. Z        t -> time = fn_time; t -> children = 0;
  371. Z        modules[pos].m_time += time;
  372. Z        modules[pos].m_calls++;
  373. Z        tot_time += time;
  374. Z        tot_calls++;
  375. Z        break;
  376. Z        case 'S':
  377. Z        sscanf (buf+2, "%lx %lx %64s", &fn_sbot, &fn_ssz, fn_name);
  378. Z        DBUG_PRINT ("srec", ("%lx %lx %s", fn_sbot, fn_ssz, fn_name));
  379. Z        pos = add (fn_name);
  380. Z        lastuse = modules[pos].m_stkuse;
  381. Z#if 0
  382. Z        /*
  383. Z         *  Needs further thought.  Stack use is determined by
  384. Z         *  difference in stack between two functions with DBUG_ENTER
  385. Z         *  macros.  If A calls B calls C, where A and C have the
  386. Z         *  macros, and B doesn't, then B's stack use will be lumped
  387. Z         *  in with either A's or C's.  If somewhere else A calls
  388. Z         *  C directly, the stack use will seem to change.  Just
  389. Z         *  take the biggest for now...
  390. Z         */
  391. Z        if (lastuse > 0 && lastuse != fn_ssz) {
  392. Z            fprintf (stderr, 
  393. Z                 "warning - %s stack use changed (%lx to %lx)\n",
  394. Z                 fn_name, lastuse, fn_ssz);
  395. Z        }
  396. Z#endif
  397. Z        if (fn_ssz > lastuse) {
  398. Z            modules[pos].m_stkuse = fn_ssz;
  399. Z        }
  400. Z        if (fn_sbot > highstack) {
  401. Z            highstack = fn_sbot;
  402. Z        } else if (fn_sbot < lowstack) {
  403. Z            lowstack = fn_sbot;
  404. Z        }
  405. Z        break;
  406. Z        default:
  407. Z        fprintf (stderr, "unknown record type '%s'\n", buf[0]);
  408. Z        break;
  409. Z    }
  410. Z    next_line:;
  411. Z    }
  412. Z    
  413. Z    /*
  414. Z     * Now, we've hit eof.  If we still have stuff stacked, then we
  415. Z     * assume that the user called exit, so give everything the exited
  416. Z     * time of fn_time.
  417. Z     */
  418. Z    while (pop (&oldpos,&oldtime,&oldchild)) {
  419. Z    time = fn_time - oldtime;
  420. Z    t = top ();
  421. Z    t -> children += time;
  422. Z    time -= oldchild;
  423. Z    modules[oldpos].m_time += time;
  424. Z    modules[oldpos].m_calls++;
  425. Z    tot_time += time;
  426. Z    tot_calls++;
  427. Z    }
  428. Z    DBUG_VOID_RETURN;
  429. Z}
  430. Z
  431. Z/*
  432. Z * out_header () -- print out the header of the report.
  433. Z */
  434. Z
  435. Zvoid out_header (outf)
  436. ZFILE *outf;
  437. Z{
  438. Z    DBUG_ENTER ("out_header");
  439. Z    if (verbose) {
  440. Z    fprintf (outf, "Profile of Execution\n");
  441. Z    fprintf (outf, "Execution times are in milliseconds\n\n");
  442. Z    fprintf (outf, "    Calls\t\t\t    Time\n");
  443. Z    fprintf (outf, "    -----\t\t\t    ----\n");
  444. Z    fprintf (outf, "Times\tPercentage\tTime Spent\tPercentage\n");
  445. Z    fprintf (outf, "Called\tof total\tin Function\tof total    Importance\tFunction\n");
  446. Z    fprintf (outf, "======\t==========\t===========\t==========  ==========\t========\t\n");
  447. Z    } else {
  448. Z    fprintf (outf, "%ld bytes of stack used, from %lx down to %lx\n\n",
  449. Z         highstack - lowstack, highstack, lowstack);
  450. Z    fprintf (outf,
  451. Z         "   %%time     sec   #call ms/call  %%calls  weight   stack  name\n");
  452. Z    }
  453. Z    DBUG_VOID_RETURN;
  454. Z}
  455. Z
  456. Z/*
  457. Z * out_trailer () - writes out the summary line of the report.
  458. Z */
  459. Z
  460. Zvoid out_trailer (outf,sum_calls,sum_time)
  461. ZFILE *outf;
  462. Zunsigned long int sum_calls, sum_time;
  463. Z{
  464. Z    DBUG_ENTER ("out_trailer");
  465. Z    if (verbose) {
  466. Z    fprintf (outf, "======\t==========\t===========\t==========\t========\n");
  467. Z    fprintf (outf, "%6d\t%10.2f\t%11d\t%10.2f\t\t%-15s\n",
  468. Z        sum_calls, 100.0, sum_time, 100.0, "Totals");
  469. Z    }
  470. Z    DBUG_VOID_RETURN;
  471. Z}
  472. Z
  473. Z/*
  474. Z * out_item () - prints out the output line for a single entry,
  475. Z * and sets the calls and time fields appropriately.
  476. Z */
  477. Z
  478. Zvoid out_item (outf, m,called,timed)
  479. ZFILE *outf;
  480. Zregister struct module_t *m;
  481. Zunsigned long int *called, *timed;
  482. Z{
  483. Z    char *name = m -> name;
  484. Z    register unsigned int calls = m -> m_calls;
  485. Z    register unsigned long time = m -> m_time;
  486. Z    register unsigned long stkuse = m -> m_stkuse;
  487. Z    unsigned int import;
  488. Z    double per_time = 0.0;
  489. Z    double per_calls = 0.0; 
  490. Z    double ms_per_call, ftime;
  491. Z    
  492. Z    DBUG_ENTER ("out_item");
  493. Z    
  494. Z    if (tot_time > 0) {
  495. Z    per_time = (double) (time * 100) / (double) tot_time;
  496. Z    }
  497. Z    if (tot_calls > 0) {
  498. Z    per_calls = (double) (calls * 100) / (double) tot_calls;
  499. Z    }
  500. Z    import = (unsigned int) (per_time * per_calls);
  501. Z    
  502. Z    if (verbose) {
  503. Z    fprintf (outf, "%6d\t%10.2f\t%11d\t%10.2f  %10d\t%-15s\n",
  504. Z        calls, per_calls, time, per_time, import, name);
  505. Z    } else {
  506. Z    ms_per_call = time;
  507. Z    ms_per_call /= calls;
  508. Z    ftime = time;
  509. Z    ftime /= 1000;
  510. Z    fprintf (outf, "%8.2f%8.3f%8u%8.3f%8.2f%8u%8u  %-s\n",
  511. Z        per_time, ftime, calls, ms_per_call, per_calls, import,
  512. Z         stkuse, name);
  513. Z    }
  514. Z    *called = calls;
  515. Z    *timed = time;
  516. Z    DBUG_VOID_RETURN;
  517. Z}
  518. Z
  519. Z/*
  520. Z * out_body (outf, root,s_calls,s_time) -- Performs an inorder traversal
  521. Z * on the binary search tree (root).  Calls out_item to actually print
  522. Z * the item out.
  523. Z */
  524. Z
  525. Zvoid out_body (outf, root,s_calls,s_time)
  526. ZFILE *outf;
  527. Zregister unsigned int root;
  528. Zregister unsigned long int *s_calls, *s_time;
  529. Z{
  530. Z    unsigned long int calls, time;
  531. Z    
  532. Z    DBUG_ENTER ("out_body");
  533. Z    DBUG_PRINT ("out_body", ("%d,%d",*s_calls,*s_time));
  534. Z    if (root == MAXPROCS) {
  535. Z    DBUG_PRINT ("out_body", ("%d,%d",*s_calls,*s_time));
  536. Z    } else {
  537. Z    while (root != MAXPROCS) {
  538. Z        out_body (outf, s_table[root].lchild,s_calls,s_time);
  539. Z        out_item (outf, &modules[s_table[root].pos],&calls,&time);
  540. Z        DBUG_PRINT ("out_body", ("-- %d -- %d --", calls, time));
  541. Z        *s_calls += calls;
  542. Z        *s_time += time;
  543. Z        root = s_table[root].rchild;
  544. Z    }
  545. Z    DBUG_PRINT ("out_body", ("%d,%d", *s_calls, *s_time));
  546. Z    }
  547. Z    DBUG_VOID_RETURN;
  548. Z}
  549. Z
  550. Z/*
  551. Z * output () - print out a nice sorted output report on outf.
  552. Z */
  553. Z
  554. Zvoid output (outf)
  555. ZFILE *outf;
  556. Z{
  557. Z    unsigned long int sum_calls = 0;
  558. Z    unsigned long int sum_time = 0;
  559. Z    
  560. Z    DBUG_ENTER ("output");
  561. Z    if (n_items == 0) {
  562. Z    fprintf (outf, "%s: No functions to trace\n", my_name);
  563. Z    exit (EX_DATAERR);
  564. Z    }
  565. Z    out_header (outf);
  566. Z    out_body (outf, 0,&sum_calls,&sum_time);
  567. Z    out_trailer (outf, sum_calls,sum_time);
  568. Z    DBUG_VOID_RETURN;
  569. Z}
  570. Z
  571. Z
  572. Z#define usage() fprintf (DBUG_FILE,"Usage: %s [-v] [prof-file]\n",my_name)
  573. Z
  574. Zmain (argc, argv, environ)
  575. Zint argc;
  576. Zchar *argv[], *environ[];
  577. Z{
  578. Z    extern int optind, getopt ();
  579. Z    extern char *optarg;
  580. Z    register int c;
  581. Z    int badflg = 0;
  582. Z    FILE *infile;
  583. Z    FILE *outfile = {stdout};
  584. Z    
  585. Z    DBUG_ENTER ("main");
  586. Z    DBUG_PROCESS (argv[0]);
  587. Z    my_name = argv[0];
  588. Z    while ((c = getopt (argc,argv,"#:v")) != EOF) {
  589. Z    switch (c) {
  590. Z        case '#': /* Debugging Macro enable */
  591. Z        DBUG_PUSH (optarg);
  592. Z        break;
  593. Z        case 'v': /* Verbose mode */
  594. Z        verbose++;
  595. Z        break;
  596. Z        default:
  597. Z        badflg++;
  598. Z        break;
  599. Z    }
  600. Z    }
  601. Z    if (badflg) {
  602. Z    usage ();
  603. Z    DBUG_RETURN (EX_USAGE);
  604. Z    }
  605. Z    if (optind < argc) {
  606. Z    FILEOPEN (infile, argv[optind], "r");
  607. Z    } else {
  608. Z    FILEOPEN (infile, PRO_FILE, "r");
  609. Z    }    
  610. Z    process (infile);
  611. Z    output (outfile);
  612. Z    DBUG_RETURN (EX_OK);
  613. Z}
  614. STUNKYFLUFF
  615. set `sum analyze.c`
  616. if test 37177 != $1
  617. then
  618. echo analyze.c: Checksum error. Is: $1, should be: 37177.
  619. fi
  620. #
  621. #
  622. echo Extracting doinstall.sh:
  623. sed 's/^Z//' >doinstall.sh <<\STUNKYFLUFF
  624. Z
  625. Z# Warning - first line left blank for sh/csh/ksh compatibility.  Do not
  626. Z# remove it.  fnf@Unisoft
  627. Z
  628. Z# doinstall.sh --- figure out environment and do recursive make with
  629. Z# appropriate pathnames.  Works under SV or BSD.
  630. Z
  631. Zif [ -r /usr/include/search.h ]
  632. Zthen
  633. Z    # System V
  634. Z    $* LLIB=/usr/lib
  635. Zelse
  636. Z    # 4.2 BSD
  637. Z    $* LLIB=/usr/lib/lint
  638. Zfi
  639. STUNKYFLUFF
  640. set `sum doinstall.sh`
  641. if test 27205 != $1
  642. then
  643. echo doinstall.sh: Checksum error. Is: $1, should be: 27205.
  644. fi
  645. #
  646. #
  647. echo Extracting example1.c:
  648. sed 's/^Z//' >example1.c <<\STUNKYFLUFF
  649. Z#include <stdio.h>
  650. Z
  651. Zmain (argc, argv)
  652. Zint argc;
  653. Zchar *argv[];
  654. Z{
  655. Z    printf ("argv[0] = %d\n", argv[0]);
  656. Z    /*
  657. Z     *  Rest of program
  658. Z     */
  659. Z    printf ("== done ==\n");
  660. Z}
  661. STUNKYFLUFF
  662. set `sum example1.c`
  663. if test 12484 != $1
  664. then
  665. echo example1.c: Checksum error. Is: $1, should be: 12484.
  666. fi
  667. #
  668. #
  669. echo Extracting example2.c:
  670. sed 's/^Z//' >example2.c <<\STUNKYFLUFF
  671. Z#include <stdio.h>
  672. Z
  673. Zint debug = 0;
  674. Z
  675. Zmain (argc, argv)
  676. Zint argc;
  677. Zchar *argv[];
  678. Z{
  679. Z    /* printf ("argv = %x\n", argv) */
  680. Z    if (debug) printf ("argv[0] = %d\n", argv[0]);
  681. Z    /*
  682. Z     *  Rest of program
  683. Z     */
  684. Z#ifdef DEBUG
  685. Z    printf ("== done ==\n");
  686. Z#endif
  687. Z}
  688. STUNKYFLUFF
  689. set `sum example2.c`
  690. if test 18642 != $1
  691. then
  692. echo example2.c: Checksum error. Is: $1, should be: 18642.
  693. fi
  694. #
  695. #
  696. echo Extracting example3.c:
  697. sed 's/^Z//' >example3.c <<\STUNKYFLUFF
  698. Z#include <stdio.h>
  699. Z
  700. Zmain (argc, argv)
  701. Zint argc;
  702. Zchar *argv[];
  703. Z{
  704. Z#   ifdef DEBUG
  705. Z    printf ("argv[0] = %d\n", argv[0]);
  706. Z#   endif
  707. Z    /*
  708. Z     *  Rest of program
  709. Z     */
  710. Z#   ifdef DEBUG
  711. Z    printf ("== done ==\n");
  712. Z#   endif
  713. Z}
  714. STUNKYFLUFF
  715. set `sum example3.c`
  716. if test 15886 != $1
  717. then
  718. echo example3.c: Checksum error. Is: $1, should be: 15886.
  719. fi
  720. #
  721. #
  722. echo Extracting factorial.c:
  723. sed 's/^Z//' >factorial.c <<\STUNKYFLUFF
  724. Z#include <stdio.h>
  725. Z/* User programs should use <local/dbug.h> */
  726. Z#include "dbug.h"
  727. Z
  728. Zint factorial (value)
  729. Zregister int value;
  730. Z{
  731. Z    DBUG_ENTER ("factorial");
  732. Z    DBUG_PRINT ("find", ("find %d factorial", value));
  733. Z    if (value > 1) {
  734. Z        value *= factorial (value - 1);
  735. Z    }
  736. Z    DBUG_PRINT ("result", ("result is %d", value));
  737. Z    DBUG_RETURN (value);
  738. Z}
  739. STUNKYFLUFF
  740. set `sum factorial.c`
  741. if test 27082 != $1
  742. then
  743. echo factorial.c: Checksum error. Is: $1, should be: 27082.
  744. fi
  745. #
  746. #
  747. echo Extracting install.sh:
  748. sed 's/^Z//' >install.sh <<\STUNKYFLUFF
  749. Z
  750. Z#    WARNING -- first line intentionally left blank for sh/csh/ksh
  751. Z#    compatibility.  Do not remove it!  FNF, UniSoft Systems.
  752. Z#
  753. Z#    Usage is:
  754. Z#            install <from> <to>
  755. Z#
  756. Z#    The file <to> is replaced with the file <from>, after first
  757. Z#    moving <to> to a backup file.  The backup file name is created
  758. Z#    by prepending the filename (after removing any leading pathname
  759. Z#    components) with "OLD".
  760. Z#
  761. Z#    This script is currently not real robust in the face of signals
  762. Z#    or permission problems.  It also does not do (by intention) all
  763. Z#    the things that the System V or BSD install scripts try to do
  764. Z#
  765. Z
  766. Zif [ $# -ne 2 ]
  767. Zthen
  768. Z    echo  "usage: $0 <from> <to>"
  769. Z    exit 1
  770. Zfi
  771. Z
  772. Z# Now extract the dirname and basename components.  Unfortunately, BSD does
  773. Z# not have dirname, so we do it the hard way.
  774. Z
  775. Zfd=`expr $1'/' : '\(/\)[^/]*/$' \| $1'/' : '\(.*[^/]\)//*[^/][^/]*//*$' \| .`
  776. Zff=`basename $1`
  777. Ztd=`expr $2'/' : '\(/\)[^/]*/$' \| $2'/' : '\(.*[^/]\)//*[^/][^/]*//*$' \| .`
  778. Ztf=`basename $2`
  779. Z
  780. Z# Now test to make sure that they are not the same files.
  781. Z
  782. Zif [ $fd/$ff = $td/$tf ]
  783. Zthen
  784. Z    echo "install: input and output are same files"
  785. Z    exit 2
  786. Zfi
  787. Z
  788. Z# Save a copy of the "to" file as a backup.
  789. Z
  790. Zif test -f $td/$tf
  791. Zthen
  792. Z    if test -f $td/OLD$tf
  793. Z    then
  794. Z        rm -f $td/OLD$tf
  795. Z    fi
  796. Z    mv $td/$tf $td/OLD$tf
  797. Z    if [ $? != 0 ]
  798. Z    then
  799. Z        exit 3
  800. Z    fi
  801. Zfi
  802. Z
  803. Z# Now do the copy and return appropriate status
  804. Z
  805. Zcp $fd/$ff $td/$tf
  806. Zif [ $? != 0 ]
  807. Zthen
  808. Z    exit 4
  809. Zelse
  810. Z    exit 0
  811. Zfi
  812. Z
  813. STUNKYFLUFF
  814. set `sum install.sh`
  815. if test 46271 != $1
  816. then
  817. echo install.sh: Checksum error. Is: $1, should be: 46271.
  818. fi
  819. #
  820. #
  821. echo Extracting llib-ldbug:
  822. sed 's/^Z//' >llib-ldbug <<\STUNKYFLUFF
  823. Z/*
  824. Z ******************************************************************************
  825. Z *                                          *
  826. Z *                               N O T I C E                      *
  827. Z *                                          *
  828. Z *                  Copyright Abandoned, 1987, Fred Fish              *
  829. Z *                                          *
  830. Z *                                          *
  831. Z *    This previously copyrighted work has been placed into the  public     *
  832. Z *    domain  by  the  author  and  may be freely used for any purpose,     *
  833. Z *    private or commercial.                              *
  834. Z *                                          *
  835. Z *    Because of the number of inquiries I was receiving about the  use     *
  836. Z *    of this product in commercially developed works I have decided to     *
  837. Z *    simply make it public domain to further its unrestricted use.   I     *
  838. Z *    specifically  would  be  most happy to see this material become a     *
  839. Z *    part of the standard Unix distributions by AT&T and the  Berkeley     *
  840. Z *    Computer  Science  Research Group, and a standard part of the GNU     *
  841. Z *    system from the Free Software Foundation.                  *
  842. Z *                                          *
  843. Z *    I would appreciate it, as a courtesy, if this notice is  left  in     *
  844. Z *    all copies and derivative works.  Thank you.                  *
  845. Z *                                          *
  846. Z *    The author makes no warranty of any kind  with  respect  to  this     *
  847. Z *    product  and  explicitly disclaims any implied warranties of mer-     *
  848. Z *    chantability or fitness for any particular purpose.              *
  849. Z *                                          *
  850. Z ******************************************************************************
  851. Z */
  852. Z
  853. Z
  854. Z/*
  855. Z *  FILE
  856. Z *
  857. Z *    llib-ldbug    lint library source for debugging package
  858. Z *
  859. Z *  SCCS ID
  860. Z *
  861. Z *    @(#)llib-ldbug    1.9 6/12/89
  862. Z *
  863. Z *  DESCRIPTION
  864. Z *
  865. Z *    Function definitions for use in building lint library.
  866. Z *    Note that these must stay in syncronization with actual
  867. Z *    declarations in "dbug.c".
  868. Z *
  869. Z */
  870. Z
  871. Z
  872. Z/*LINTLIBRARY*/
  873. Z
  874. Z#include <stdio.h>
  875. Z
  876. Z#define VOID void
  877. Ztypedef int BOOLEAN;
  878. Z#define FALSE 0
  879. Z#define ARGLIST a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15
  880. Z
  881. Zint _db_on_ = FALSE;
  882. Zint _db_pon_ = FALSE;
  883. ZFILE *_db_fp_ = stderr;
  884. ZFILE *_db_pfp_ = stderr;
  885. Zchar *_db_process_ = "dbug";
  886. Z
  887. ZVOID _db_push_ (control)
  888. Zchar *control;
  889. Z{
  890. Z}
  891. Z
  892. ZVOID _db_pop_ ()
  893. Z{
  894. Z}
  895. Z
  896. ZVOID _db_enter_ (_func_, _file_, _line_, _sfunc_, _sfile_, _slevel_, _sframep_)
  897. Zchar *_func_;
  898. Zchar *_file_;
  899. Zint _line_;
  900. Zchar **_sfunc_;
  901. Zchar **_sfile_;
  902. Zint *_slevel_;
  903. Zchar ***_sframep_;
  904. Z{
  905. Z}
  906. Z
  907. ZVOID _db_return_ (_line_, _sfunc_, _sfile_, _slevel_)
  908. Zint _line_;
  909. Zchar **_sfunc_;
  910. Zchar **_sfile_;
  911. Zint *_slevel_;
  912. Z{
  913. Z}
  914. Z
  915. ZVOID _db_pargs_ (_line_, keyword)
  916. Zint _line_;
  917. Zchar *keyword;
  918. Z{
  919. Z}
  920. Z
  921. Z/*VARARGS1*/
  922. ZVOID _db_doprnt_ (format, ARGLIST)
  923. Zchar *format;
  924. Zlong ARGLIST;
  925. Z{
  926. Z}
  927. Z
  928. Z/* WARNING -- the following function is obsolete and may not be supported */
  929. Z/* in future releases... */
  930. Z
  931. Z/*VARARGS3*/
  932. ZVOID _db_printf_ (_line_, keyword, format, ARGLIST)
  933. Zint _line_;
  934. Zchar *keyword,  *format;
  935. Zlong ARGLIST;
  936. Z{
  937. Z}
  938. Z
  939. ZBOOLEAN _db_keyword_ (keyword)
  940. Zchar *keyword;
  941. Z{
  942. Z    return (0);
  943. Z}
  944. Z
  945. ZVOID _db_longjmp_ ()
  946. Z{
  947. Z}
  948. Z
  949. ZVOID _db_setjmp_ ()
  950. Z{
  951. Z}
  952. STUNKYFLUFF
  953. set `sum llib-ldbug`
  954. if test 20072 != $1
  955. then
  956. echo llib-ldbug: Checksum error. Is: $1, should be: 20072.
  957. fi
  958. #
  959. #
  960. echo Extracting main.c:
  961. sed 's/^Z//' >main.c <<\STUNKYFLUFF
  962. Z#include <stdio.h>
  963. Z/* User programs should use <local/dbug.h> */
  964. Z#include "dbug.h"
  965. Z
  966. Zmain (argc, argv)
  967. Zint argc;
  968. Zchar *argv[];
  969. Z{
  970. Z    register int result, ix;
  971. Z    extern int factorial (), atoi ();
  972. Z
  973. Z    DBUG_ENTER ("main");
  974. Z    DBUG_PROCESS (argv[0]);
  975. Z    for (ix = 1; ix < argc && argv[ix][0] == '-'; ix++) {
  976. Z    switch (argv[ix][1]) {
  977. Z        case '#':
  978. Z        DBUG_PUSH (&(argv[ix][2]));
  979. Z        break;
  980. Z    }
  981. Z    }
  982. Z    for (; ix < argc; ix++) {
  983. Z    DBUG_PRINT ("args", ("argv[%d] = %s", ix, argv[ix]));
  984. Z    result = factorial (atoi (argv[ix]));
  985. Z    printf ("%d\n", result);
  986. Z    }
  987. Z    DBUG_RETURN (0);
  988. Z}
  989. STUNKYFLUFF
  990. set `sum main.c`
  991. if test 42153 != $1
  992. then
  993. echo main.c: Checksum error. Is: $1, should be: 42153.
  994. fi
  995. #
  996. #
  997. echo Extracting mklintlib.sh:
  998. sed 's/^Z//' >mklintlib.sh <<\STUNKYFLUFF
  999. Z
  1000. Z# Warning - first line left blank for sh/csh/ksh compatibility.  Do not
  1001. Z# remove it.  fnf@Unisoft
  1002. Z
  1003. Z# mklintlib --- make a lint library, under either System V or 4.2 BSD
  1004. Z#
  1005. Z# usage:  mklintlib <infile> <outfile>
  1006. Z#
  1007. Z
  1008. Zif test $# -ne 2
  1009. Zthen
  1010. Z    echo "usage: mklintlib <infile> <outfile>"
  1011. Z    exit 1
  1012. Zfi
  1013. Z
  1014. Zif grep SIGTSTP /usr/include/signal.h >/dev/null
  1015. Zthen                            # BSD
  1016. Z    if test -r /usr/include/whoami.h        # 4.1
  1017. Z    then
  1018. Z        /lib/cpp -C -Dlint $1 >hlint
  1019. Z        (/usr/lib/lint/lint1 <hlint >$2) 2>&1 | grep -v warning
  1020. Z    else                        # 4.2
  1021. Z        lint -Cxxxx $1
  1022. Z        mv llib-lxxxx.ln $2
  1023. Z    fi
  1024. Zelse                            # USG
  1025. Z    cc -E -C -Dlint $1 | /usr/lib/lint1 -vx -Hhlint >$2
  1026. Z    rm -f hlint
  1027. Zfi
  1028. Zexit 0                            # don't kill make
  1029. STUNKYFLUFF
  1030. set `sum mklintlib.sh`
  1031. if test 51376 != $1
  1032. then
  1033. echo mklintlib.sh: Checksum error. Is: $1, should be: 51376.
  1034. fi
  1035. #
  1036. #
  1037. echo Extracting ranlib.sh:
  1038. sed 's/^Z//' >ranlib.sh <<\STUNKYFLUFF
  1039. Z
  1040. Z# Warning - first line left blank for sh/csh/ksh compatibility.  Do not
  1041. Z# remove it.  fnf@Unisoft
  1042. Z
  1043. Z# ranlib --- do a ranlib if necessary
  1044. Z
  1045. Zif [ -x /usr/bin/ranlib ]
  1046. Zthen
  1047. Z    /usr/bin/ranlib $*
  1048. Zelif [ -x /bin/ranlib ]
  1049. Zthen
  1050. Z    /bin/ranlib $*
  1051. Zelse
  1052. Z    :
  1053. Zfi
  1054. STUNKYFLUFF
  1055. set `sum ranlib.sh`
  1056. if test 20181 != $1
  1057. then
  1058. echo ranlib.sh: Checksum error. Is: $1, should be: 20181.
  1059. fi
  1060. #
  1061. #
  1062. echo Extracting useful.h:
  1063. sed 's/^Z//' >useful.h <<\STUNKYFLUFF
  1064. Z/*
  1065. Z * Copyright June 1987, Binayak Banerjee
  1066. Z * All rights reserved.
  1067. Z *
  1068. Z * This program may be freely distributed under the same terms and
  1069. Z * conditions as Fred Fish's Dbug package.
  1070. Z *
  1071. Z * Useful macros which I use a lot.
  1072. Z *
  1073. Z * Conditionally include some useful files.
  1074. Z */
  1075. Z
  1076. Z# ifndef EOF
  1077. Z#    include <stdio.h>
  1078. Z# endif
  1079. Z
  1080. Z/*
  1081. Z *    For BSD systems, you can include <sysexits.h> for more detailed
  1082. Z *    exit information.  For non-BSD systems (which also includes
  1083. Z *    non-unix systems) just map everything to "failure" = 1 and
  1084. Z *    "success" = 0.        -Fred Fish 9-Sep-87
  1085. Z */
  1086. Z
  1087. Z# ifdef BSD
  1088. Z#    include <sysexits.h>
  1089. Z# else
  1090. Z#    define EX_SOFTWARE 1
  1091. Z#    define EX_DATAERR 1
  1092. Z#    define EX_USAGE 1
  1093. Z#    define EX_OSERR 1
  1094. Z#    define EX_IOERR 1
  1095. Z#    define EX_OK 0
  1096. Z# endif
  1097. Z
  1098. Z
  1099. Z/*
  1100. Z * Fred Fish's debugging stuff.  Define DBUG_OFF in order to disable if
  1101. Z * you don't have these.
  1102. Z */
  1103. Z
  1104. Z# ifndef DBUG_OFF
  1105. Z#    include "dbug.h"        /* Use local version */
  1106. Z# else
  1107. Z#    define DBUG_ENTER(a1)
  1108. Z#    define DBUG_RETURN(a1) return(a1)
  1109. Z#    define DBUG_VOID_RETURN return
  1110. Z#    define DBUG_EXECUTE(keyword,a1)
  1111. Z#    define DBUG_2(keyword,format)
  1112. Z#    define DBUG_3(keyword,format,a1)
  1113. Z#    define DBUG_4(keyword,format,a1,a2)
  1114. Z#    define DBUG_5(keyword,format,a1,a2,a3)
  1115. Z#    define DBUG_PUSH(a1)
  1116. Z#    define DBUG_POP()
  1117. Z#    define DBUG_PROCESS(a1)
  1118. Z#    define DBUG_PRINT(x,y)
  1119. Z#    define DBUG_FILE (stderr)
  1120. Z# endif
  1121. Z
  1122. Z#define __MERF_OO_ "%s: Malloc Failed in %s: %d\n"
  1123. Z
  1124. Z#define Nil(Typ)    ((Typ *) 0)    /* Make Lint happy */
  1125. Z
  1126. Z#define MALLOC(Ptr,Num,Typ) do    /* Malloc w/error checking & exit */ \
  1127. Z    if ((Ptr = (Typ *)malloc((Num)*(sizeof(Typ)))) == Nil(Typ)) \
  1128. Z        {fprintf(stderr,__MERF_OO_,my_name,__FILE__,__LINE__);\
  1129. Z        exit(EX_OSERR);} while(0)
  1130. Z
  1131. Z#define Malloc(Ptr,Num,Typ) do    /* Weaker version of above */\
  1132. Z    if ((Ptr = (Typ *)malloc((Num)*(sizeof(Typ)))) == Nil(Typ)) \
  1133. Z        fprintf(stderr,__MERF_OO_,my_name,__FILE__,__LINE__);\
  1134. Z         while(0)
  1135. Z
  1136. Z#define FILEOPEN(Fp,Fn,Mod) do    /* File open with error exit */ \
  1137. Z    if((Fp = fopen(Fn,Mod)) == Nil(FILE))\
  1138. Z        {fprintf(stderr,"%s: Couldn't open %s\n",my_name,Fn);\
  1139. Z        exit(EX_IOERR);} while(0)
  1140. Z
  1141. Z#define Fileopen(Fp,Fn,Mod) do    /* Weaker version of above */ \
  1142. Z    if((Fp = fopen(Fn,Mod)) == Nil(FILE)) \
  1143. Z        fprintf(stderr,"%s: Couldn't open %s\n",my_name,Fn);\
  1144. Z    while(0)
  1145. Z
  1146. Z
  1147. Zextern char *my_name;    /* The name that this was called as */
  1148. STUNKYFLUFF
  1149. set `sum useful.h`
  1150. if test 45206 != $1
  1151. then
  1152. echo useful.h: Checksum error. Is: $1, should be: 45206.
  1153. fi
  1154. #
  1155. #
  1156. echo Extracting user.r:
  1157. sed 's/^Z//' >user.r <<\STUNKYFLUFF
  1158. Z.\"    @(#)user.r    1.16 8/7/88
  1159. Z.\"
  1160. Z.\"    DBUG (Macro Debugger Package) nroff source
  1161. Z.\"
  1162. Z.\"    nroff -mm user.r >user.t
  1163. Z.\"
  1164. Z.\" ===================================================
  1165. Z.\"
  1166. Z.\"    === Some sort of black magic, but I forget...
  1167. Z.tr ~
  1168. Z.\"    === Hyphenation control (1 = on)
  1169. Z.\".nr Hy 1
  1170. Z.\"    === Force all first level headings to start on new page
  1171. Z.nr Ej 1
  1172. Z.\"    === Set for breaks after headings for levels 1-3
  1173. Z.nr Hb 3
  1174. Z.\"    === Set for space after headings for levels 1-3
  1175. Z.nr Hs 3
  1176. Z.\"    === Set standard indent for one/half inch
  1177. Z.nr Si 10
  1178. Z.\"    === Set page header
  1179. Z.PH "/DBUG User Manual//\*(DT/"
  1180. Z.\"    === Set page footer
  1181. Z.PF "// - % - //"
  1182. Z.\"    === Set page offset
  1183. Z.\".po 0.60i
  1184. Z.\"    === Set line length
  1185. Z.\".ll 6.5i
  1186. Z.TL
  1187. ZD B U G
  1188. Z.P 0
  1189. ZC Program Debugging Package
  1190. Z.P 0
  1191. Zby
  1192. Z.AU "Fred Fish"
  1193. Z.AF ""
  1194. Z.SA 1
  1195. Z.\"    === All paragraphs indented.
  1196. Z.nr Pt 1
  1197. Z.AS 1
  1198. ZThis document introduces
  1199. Z.I dbug ,
  1200. Za macro based C debugging
  1201. Zpackage which has proven to be a very flexible and useful tool
  1202. Zfor debugging, testing, and porting C programs.
  1203. Z
  1204. Z.P
  1205. ZAll of the features of the
  1206. Z.I dbug
  1207. Zpackage can be enabled or disabled dynamically at execution time.
  1208. ZThis means that production programs will run normally when
  1209. Zdebugging is not enabled, and eliminates the need to maintain two
  1210. Zseparate versions of a program.
  1211. Z
  1212. Z.P
  1213. ZMany of the things easily accomplished with conventional debugging
  1214. Ztools, such as symbolic debuggers, are difficult or impossible with this
  1215. Zpackage, and vice versa.
  1216. ZThus the
  1217. Z.I dbug
  1218. Zpackage should 
  1219. Z.I not
  1220. Zbe thought of as a replacement or substitute for
  1221. Zother debugging tools, but simply as a useful
  1222. Z.I addition
  1223. Zto the
  1224. Zprogram development and maintenance environment.
  1225. Z
  1226. Z.AE
  1227. Z.MT 4
  1228. Z.SK
  1229. Z.B
  1230. ZINTRODUCTION
  1231. Z.R
  1232. Z
  1233. Z.P
  1234. ZAlmost every program development environment worthy of the name
  1235. Zprovides some sort of debugging facility.
  1236. ZUsually this takes the form of a program which is capable of
  1237. Zcontrolling execution of other programs and examining the internal
  1238. Zstate of other executing programs.
  1239. ZThese types of programs will be referred to as external debuggers
  1240. Zsince the debugger is not part of the executing program.
  1241. ZExamples of this type of debugger include the
  1242. Z.B adb
  1243. Zand
  1244. Z.B sdb
  1245. Zdebuggers provided with the 
  1246. Z.B UNIX\*F
  1247. Z.FS
  1248. ZUNIX is a trademark of AT&T Bell Laboratories.
  1249. Z.FE
  1250. Zoperating system.
  1251. Z
  1252. Z.P
  1253. ZOne of the problems associated with developing programs in an environment
  1254. Zwith good external debuggers is that developed programs tend to have 
  1255. Zlittle or no internal instrumentation.
  1256. ZThis is usually not a problem for the developer since he is,
  1257. Zor at least should be, intimately familiar with the internal organization,
  1258. Zdata structures, and control flow of the program being debugged.
  1259. ZIt is a serious problem for maintenance programmers, who
  1260. Zare unlikely to have such familiarity with the program being
  1261. Zmaintained, modified, or ported to another environment.
  1262. ZIt is also a problem, even for the developer, when the program is
  1263. Zmoved to an environment with a primitive or unfamiliar debugger,
  1264. Zor even no debugger.
  1265. Z
  1266. Z.P
  1267. ZOn the other hand,
  1268. Z.I dbug
  1269. Zis an example of an internal debugger.
  1270. ZBecause it requires internal instrumentation of a program,
  1271. Zand its usage does not depend on any special capabilities of
  1272. Zthe execution environment, it is always available and will
  1273. Zexecute in any environment that the program itself will
  1274. Zexecute in.
  1275. ZIn addition, since it is a complete package with a specific
  1276. Zuser interface, all programs which use it will be provided
  1277. Zwith similar debugging capabilities.
  1278. ZThis is in sharp contrast to other forms of internal instrumentation
  1279. Zwhere each developer has their own, usually less capable, form
  1280. Zof internal debugger.
  1281. ZIn summary,
  1282. Zbecause 
  1283. Z.I dbug
  1284. Zis an internal debugger it provides consistency across operating
  1285. Zenvironments, 
  1286. Zand because it is available to all developers it provides
  1287. Zconsistency across all programs in the same environment.
  1288. Z
  1289. Z.P
  1290. ZThe
  1291. Z.I dbug
  1292. Zpackage imposes only a slight speed penalty on executing
  1293. Zprograms, typically much less than 10 percent, and a modest size
  1294. Zpenalty, typically 10 to 20 percent.
  1295. ZBy defining a specific C preprocessor symbol both of these
  1296. Zcan be reduced to zero with no changes required to the
  1297. Zsource code.
  1298. Z
  1299. Z.P
  1300. ZThe following list is a quick summary of the capabilities
  1301. Zof the
  1302. Z.I dbug
  1303. Zpackage.
  1304. ZEach capability can be individually enabled or disabled
  1305. Zat the time a program is invoked by specifying the appropriate
  1306. Zcommand line arguments.
  1307. Z.SP 1
  1308. Z.ML o 1i
  1309. Z.LI
  1310. ZExecution trace showing function level control flow in a
  1311. Zsemi-graphically manner using indentation to indicate nesting
  1312. Zdepth.
  1313. Z.LI
  1314. ZOutput the values of all, or any subset of, key internal variables.
  1315. Z.LI
  1316. ZLimit actions to a specific set of named functions.
  1317. Z.LI
  1318. ZLimit function trace to a specified nesting depth.
  1319. Z.LI
  1320. ZLabel each output line with source file name and line number.
  1321. Z.LI
  1322. ZLabel each output line with name of current process.
  1323. Z.LI
  1324. ZPush or pop internal debugging state to allow execution with
  1325. Zbuilt in debugging defaults.
  1326. Z.LI
  1327. ZRedirect the debug output stream to standard output (stdout)
  1328. Zor a named file.
  1329. ZThe default output stream is standard error (stderr).
  1330. ZThe redirection mechanism is completely independent of
  1331. Znormal command line redirection to avoid output conflicts.
  1332. Z.LE
  1333. Z
  1334. Z.SK
  1335. Z.B
  1336. ZPRIMITIVE DEBUGGING TECHNIQUES
  1337. Z.R
  1338. Z
  1339. Z.P
  1340. ZInternal instrumentation is already a familiar concept
  1341. Zto most programmers, since it is usually the first debugging
  1342. Ztechnique learned.
  1343. ZTypically, "print\ statements" are inserted in the source
  1344. Zcode at interesting points, the code is recompiled and executed,
  1345. Zand the resulting output is examined in an attempt to determine
  1346. Zwhere the problem is.
  1347. Z
  1348. ZThe procedure is iterative, with each iteration yielding more
  1349. Zand more output, and hopefully the source of the problem is
  1350. Zdiscovered before the output becomes too large to deal with
  1351. Zor previously inserted statements need to be removed.
  1352. ZFigure 1 is an example of this type of primitive debugging
  1353. Ztechnique.
  1354. Z.DS I N
  1355. Z.SP 2
  1356. Z.so example1.r
  1357. Z.SP 2
  1358. Z.ll -5
  1359. Z.ce
  1360. ZFigure 1
  1361. Z.ce
  1362. ZPrimitive Debugging Technique
  1363. Z.ll +5
  1364. Z.SP 2
  1365. Z.DE
  1366. Z
  1367. Z.P
  1368. ZEventually, and usually after at least several iterations, the
  1369. Zproblem will be found and corrected.
  1370. ZAt this point, the newly inserted print statements must be 
  1371. Zdealt with.
  1372. ZOne obvious solution is to simply delete them all.
  1373. ZBeginners usually do this a few times until they have to
  1374. Zrepeat the entire process every time a new bug pops up.
  1375. ZThe second most obvious solution is to somehow disable
  1376. Zthe output, either through the source code comment facility,
  1377. Zcreation of a debug variable to be switched on or off, or by using the
  1378. ZC preprocessor.
  1379. ZFigure 2 is an example of all three techniques.
  1380. Z.DS I N
  1381. Z.SP 2
  1382. Z.so example2.r
  1383. Z.SP 2
  1384. Z.ll -5
  1385. Z.ce
  1386. ZFigure 2
  1387. Z.ce
  1388. ZDebug Disable Techniques
  1389. Z.ll +5
  1390. Z.SP 2
  1391. Z.DE
  1392. Z
  1393. Z.P
  1394. ZEach technique has its advantages and disadvantages with respect
  1395. Zto dynamic vs static activation, source code overhead, recompilation
  1396. Zrequirements, ease of use, program readability, etc.
  1397. ZOveruse of the preprocessor solution quickly leads to problems with
  1398. Zsource code readability and maintainability when multiple 
  1399. Z.B #ifdef
  1400. Zsymbols are to be defined or undefined based on specific types
  1401. Zof debug desired.
  1402. ZThe source code can be made slightly more readable by suitable indentation
  1403. Zof the 
  1404. Z.B #ifdef
  1405. Zarguments to match the indentation of the code, but
  1406. Znot all C preprocessors allow this.
  1407. ZThe only requirement for the standard 
  1408. Z.B UNIX
  1409. ZC preprocessor is for the '#' character to appear
  1410. Zin the first column, but even this seems
  1411. Zlike an arbitrary and unreasonable restriction.
  1412. ZFigure 3 is an example of this usage.
  1413. Z.DS I N
  1414. Z.SP 2
  1415. Z.so example3.r
  1416. Z.SP 2
  1417. Z.ll -5
  1418. Z.ce
  1419. ZFigure 3
  1420. Z.ce
  1421. ZMore Readable Preprocessor Usage
  1422. Z.ll +5
  1423. Z.SP 2
  1424. Z.DE
  1425. Z
  1426. Z.SK
  1427. Z.B
  1428. ZFUNCTION TRACE EXAMPLE
  1429. Z.R
  1430. Z
  1431. Z.P
  1432. ZWe will start off learning about the capabilities of the
  1433. Z.I dbug
  1434. Zpackage by using a simple minded program which computes the
  1435. Zfactorial of a number.
  1436. ZIn order to better demonstrate the function trace mechanism, this
  1437. Zprogram is implemented recursively.
  1438. ZFigure 4 is the main function for this factorial program.
  1439. Z.DS I N
  1440. Z.SP 2
  1441. Z.so main.r
  1442. Z.SP 2
  1443. Z.ll -5
  1444. Z.ce
  1445. ZFigure 4
  1446. Z.ce
  1447. ZFactorial Program Mainline
  1448. Z.ll +5
  1449. Z.SP 2
  1450. Z.DE
  1451. Z
  1452. Z.P
  1453. ZThe 
  1454. Z.B main
  1455. Zfunction is responsible for processing any command line
  1456. Zoption arguments and then computing and printing the factorial of
  1457. Zeach non-option argument.
  1458. Z.P
  1459. ZFirst of all, notice that all of the debugger functions are implemented
  1460. Zvia preprocessor macros.
  1461. ZThis does not detract from the readability of the code and makes disabling
  1462. Zall debug compilation trivial (a single preprocessor symbol, 
  1463. Z.B DBUG_OFF ,
  1464. Zforces the macro expansions to be null).
  1465. Z.P
  1466. ZAlso notice the inclusion of the header file
  1467. Z.B dbug.h
  1468. Zfrom the local header file directory.
  1469. Z(The version included here is the test version in the dbug source
  1470. Zdistribution directory).
  1471. ZThis file contains all the definitions for the debugger macros, which
  1472. Zall have the form 
  1473. Z.B DBUG_XX...XX .
  1474. Z
  1475. Z.P
  1476. ZThe 
  1477. Z.B DBUG_ENTER 
  1478. Zmacro informs that debugger that we have entered the
  1479. Zfunction named 
  1480. Z.B main .
  1481. ZIt must be the very first "executable" line in a function, after
  1482. Zall declarations and before any other executable line.
  1483. ZThe 
  1484. Z.B DBUG_PROCESS
  1485. Zmacro is generally used only once per program to
  1486. Zinform the debugger what name the program was invoked with.
  1487. ZThe
  1488. Z.B DBUG_PUSH
  1489. Zmacro modifies the current debugger state by
  1490. Zsaving the previous state and setting a new state based on the
  1491. Zcontrol string passed as its argument.
  1492. ZThe
  1493. Z.B DBUG_PRINT
  1494. Zmacro is used to print the values of each argument
  1495. Zfor which a factorial is to be computed.
  1496. ZThe 
  1497. Z.B DBUG_RETURN
  1498. Zmacro tells the debugger that the end of the current
  1499. Zfunction has been reached and returns a value to the calling
  1500. Zfunction.
  1501. ZAll of these macros will be fully explained in subsequent sections.
  1502. Z.P
  1503. ZTo use the debugger, the factorial program is invoked with a command
  1504. Zline of the form:
  1505. Z.DS CB N
  1506. Zfactorial -#d:t 1 2 3
  1507. Z.DE
  1508. ZThe 
  1509. Z.B main
  1510. Zfunction recognizes the "-#d:t" string as a debugger control
  1511. Zstring, and passes the debugger arguments ("d:t") to the 
  1512. Z.I dbug
  1513. Zruntime support routines via the
  1514. Z.B DBUG_PUSH 
  1515. Zmacro.
  1516. ZThis particular string enables output from the
  1517. Z.B DBUG_PRINT
  1518. Zmacro with the 'd' flag and enables function tracing with the 't' flag.
  1519. ZThe factorial function is then called three times, with the arguments
  1520. Z"1", "2", and "3".
  1521. ZNote that the DBUG_PRINT takes exactly
  1522. Z.B two
  1523. Zarguments, with the second argument (a format string and list
  1524. Zof printable values) enclosed in parenthesis.
  1525. Z.P
  1526. ZDebug control strings consist of a header, the "-#", followed
  1527. Zby a colon separated list of debugger arguments.
  1528. ZEach debugger argument is a single character flag followed
  1529. Zby an optional comma separated list of arguments specific
  1530. Zto the given flag.
  1531. ZSome examples are:
  1532. Z.DS CB N
  1533. Z-#d:t:o
  1534. Z-#d,in,out:f,main:F:L
  1535. Z.DE
  1536. ZNote that previously enabled debugger actions can be disabled by the
  1537. Zcontrol string "-#".
  1538. Z
  1539. Z.P
  1540. ZThe definition of the factorial function, symbolized as "N!", is
  1541. Zgiven by:
  1542. Z.DS CB N
  1543. ZN! = N * N-1 * ... 2 * 1
  1544. Z.DE
  1545. ZFigure 5 is the factorial function which implements this algorithm
  1546. Zrecursively.
  1547. ZNote that this is not necessarily the best way to do factorials
  1548. Zand error conditions are ignored completely.
  1549. Z.DS I N
  1550. Z.SP 2
  1551. Z.so factorial.r
  1552. Z.SP 2
  1553. Z.ll -5
  1554. Z.ce
  1555. ZFigure 5
  1556. Z.ce
  1557. ZFactorial Function
  1558. Z.ll +5
  1559. Z.SP 2
  1560. Z.DE
  1561. Z
  1562. Z.P
  1563. ZOne advantage (some may not consider it so) to using the
  1564. Z.I dbug
  1565. Zpackage is that it strongly encourages fully structured coding
  1566. Zwith only one entry and one exit point in each function.
  1567. ZMultiple exit points, such as early returns to escape a loop,
  1568. Zmay be used, but each such point requires the use of an
  1569. Zappropriate 
  1570. Z.B DBUG_RETURN
  1571. Zor
  1572. Z.B DBUG_VOID_RETURN
  1573. Zmacro.
  1574. Z
  1575. Z.P
  1576. ZTo build the factorial program on a 
  1577. Z.B UNIX
  1578. Zsystem, compile and
  1579. Zlink with the command:
  1580. Z.DS CB N
  1581. Zcc -o factorial main.c factorial.c -ldbug
  1582. Z.DE
  1583. ZThe "-ldbug" argument tells the loader to link in the
  1584. Zruntime support modules for the
  1585. Z.I dbug
  1586. Zpackage.
  1587. ZExecuting the factorial program with a command of the form:
  1588. Z.DS CB N
  1589. Zfactorial 1 2 3 4 5
  1590. Z.DE
  1591. Zgenerates the output shown in figure 6.
  1592. Z.DS I N
  1593. Z.SP 2
  1594. Z.so output1.r
  1595. Z.SP 2
  1596. Z.ll -5
  1597. Z.ce
  1598. ZFigure 6
  1599. Z.ce
  1600. Zfactorial 1 2 3 4 5
  1601. Z.ll +5
  1602. Z.SP 2
  1603. Z.DE
  1604. Z
  1605. Z.P
  1606. ZFunction level tracing is enabled by passing the debugger
  1607. Zthe 't' flag in the debug control string.
  1608. ZFigure 7 is the output resulting from the command
  1609. Z"factorial\ -#t:o\ 3\ 2".
  1610. Z.DS I N
  1611. Z.SP 2
  1612. Z.so output2.r
  1613. Z.SP 2
  1614. Z.ll -5
  1615. Z.ce
  1616. ZFigure 7
  1617. Z.ce
  1618. Zfactorial -#t:o 3 2
  1619. Z.ll +5
  1620. Z.SP 2
  1621. Z.DE
  1622. Z
  1623. Z.P
  1624. ZEach entry to or return from a function is indicated by '>' for the
  1625. Zentry point and '<' for the exit point, connected by
  1626. Zvertical bars to allow matching points to be easily found
  1627. Zwhen separated by large distances.
  1628. Z
  1629. Z.P
  1630. ZThis trace output indicates that there was an initial call
  1631. Zto factorial from main (to compute 2!), followed by
  1632. Za single recursive call to factorial to compute 1!.
  1633. ZThe main program then output the result for 2! and called the
  1634. Zfactorial function again with the second argument, 3.
  1635. ZFactorial called itself recursively to compute 2! and 1!, then
  1636. Zreturned control to main, which output the value for 3! and exited.
  1637. Z
  1638. Z.P
  1639. ZNote that there is no matching entry point "main>" for the
  1640. Zreturn point "<main" because at the time the 
  1641. Z.B DBUG_ENTER
  1642. Zmacro was reached in main, tracing was not enabled yet.
  1643. ZIt was only after the macro
  1644. Z.B DBUG_PUSH
  1645. Zwas executing that tracing became enabled.
  1646. ZThis implies that the argument list should be processed as early as
  1647. Zpossible since all code preceding the first call to
  1648. Z.B DBUG_PUSH 
  1649. Zis
  1650. Zessentially invisible to 
  1651. Z.B dbug
  1652. Z(this can be worked around by
  1653. Zinserting a temporary 
  1654. Z.B DBUG_PUSH(argv[1])
  1655. Zimmediately after the
  1656. Z.B DBUG_ENTER("main")
  1657. Zmacro.
  1658. Z
  1659. Z.P
  1660. ZOne last note,
  1661. Zthe trace output normally comes out on the standard error.
  1662. ZSince the factorial program prints its result on the standard
  1663. Zoutput, there is the possibility of the output on the terminal
  1664. Zbeing scrambled if the two streams are not synchronized.
  1665. ZThus the debugger is told to write its output on the standard
  1666. Zoutput instead, via the 'o' flag character.
  1667. ZNote that no 'o' implies the default (standard error), a 'o' 
  1668. Zwith no arguments means standard output, and a 'o' 
  1669. Zwith an argument means used the named file.
  1670. ZI.E, "factorial\ -#t:o,logfile\ 3\ 2" would write the trace
  1671. Zoutput in "logfile".
  1672. ZBecause of 
  1673. Z.B UNIX
  1674. Zimplementation details, programs usually run
  1675. Zfaster when writing to stdout rather than stderr, though this
  1676. Zis not a prime consideration in this example.
  1677. Z
  1678. Z.SK
  1679. Z.B
  1680. ZUSE OF DBUG_PRINT MACRO
  1681. Z.R
  1682. Z
  1683. Z.P
  1684. ZThe mechanism used to produce "printf" style output is the
  1685. Z.B DBUG_PRINT
  1686. Zmacro.
  1687. Z
  1688. Z.P
  1689. ZTo allow selection of output from specific macros, the first argument
  1690. Zto every 
  1691. Z.B DBUG_PRINT
  1692. Zmacro is a 
  1693. Z.I dbug
  1694. Zkeyword.
  1695. ZWhen this keyword appears in the argument list of the 'd' flag in
  1696. Za debug control string, as in "-#d,keyword1,keyword2,...:t",
  1697. Zoutput from the corresponding macro is enabled.
  1698. ZThe default when there is no 'd' flag in the control string is to
  1699. Zenable output from all 
  1700. Z.B DBUG_PRINT
  1701. Zmacros.
  1702. Z
  1703. Z.P
  1704. ZTypically, a program will be run once, with no keywords specified,
  1705. Zto determine what keywords are significant for the current problem
  1706. Z(the keywords are printed in the macro output line).
  1707. ZThen the program will be run again, with the desired keywords,
  1708. Zto examine only specific areas of interest.
  1709. Z
  1710. Z.P
  1711. ZThe second argument to a
  1712. Z.B DBUG_PRINT 
  1713. Zmacro is a standard printf style
  1714. Zformat string and one or more arguments to print, all
  1715. Zenclosed in parenthesis so that they collectively become a single macro
  1716. Zargument.
  1717. ZThis is how variable numbers of printf arguments are supported.
  1718. ZAlso note that no explicit newline is required at the end of the format string.
  1719. ZAs a matter of style, two or three small 
  1720. Z.B DBUG_PRINT
  1721. Zmacros are preferable
  1722. Zto a single macro with a huge format string.
  1723. ZFigure 8 shows the output for default tracing and debug.
  1724. Z.DS I N
  1725. Z.SP 2
  1726. Z.so output3.r
  1727. Z.SP 2
  1728. Z.ll -5
  1729. Z.ce
  1730. ZFigure 8
  1731. Z.ce
  1732. Zfactorial -#d:t:o 3
  1733. Z.ll +5
  1734. Z.SP 2
  1735. Z.DE
  1736. Z
  1737. Z.P
  1738. ZThe output from the 
  1739. Z.B DBUG_PRINT
  1740. Zmacro is indented to match the trace output
  1741. Zfor the function in which the macro occurs.
  1742. ZWhen debugging is enabled, but not trace, the output starts at the left
  1743. Zmargin, without indentation.
  1744. Z
  1745. Z.P
  1746. ZTo demonstrate selection of specific macros for output, figure
  1747. Z9 shows the result when the factorial program is invoked with
  1748. Zthe debug control string "-#d,result:o".
  1749. Z.DS I N
  1750. Z.SP 2
  1751. Z.so output4.r
  1752. Z.SP 2
  1753. Z.ll -5
  1754. Z.ce
  1755. ZFigure 9
  1756. Z.ce
  1757. Zfactorial -#d,result:o 4
  1758. Z.ll +5
  1759. Z.SP 2
  1760. Z.DE
  1761. Z
  1762. Z.P
  1763. ZIt is sometimes desirable to restrict debugging and trace actions
  1764. Zto a specific function or list of functions.
  1765. ZThis is accomplished with the 'f' flag character in the debug
  1766. Zcontrol string.
  1767. ZFigure 10 is the output of the factorial program when run with the
  1768. Zcontrol string "-#d:f,factorial:F:L:o".
  1769. ZThe 'F' flag enables printing of the source file name and the 'L'
  1770. Zflag enables printing of the source file line number.
  1771. Z.DS I N
  1772. Z.SP 2
  1773. Z.so output5.r
  1774. Z.SP 2
  1775. Z.ll -5
  1776. Z.ce
  1777. ZFigure 10
  1778. Z.ce
  1779. Zfactorial -#d:f,factorial:F:L:o 3
  1780. Z.ll +5
  1781. Z.SP 2
  1782. Z.DE
  1783. Z
  1784. Z.P
  1785. ZThe output in figure 10 shows that the "find" macro is in file
  1786. Z"factorial.c" at source line 8 and the "result" macro is in the same
  1787. Zfile at source line 12.
  1788. Z
  1789. Z.SK
  1790. Z.B
  1791. ZSUMMARY OF MACROS
  1792. Z.R
  1793. Z
  1794. Z.P
  1795. ZThis section summarizes the usage of all currently defined macros
  1796. Zin the 
  1797. Z.I dbug
  1798. Zpackage.
  1799. ZThe macros definitions are found in the user include file
  1800. Z.B dbug.h
  1801. Zfrom the standard include directory.
  1802. Z
  1803. Z.SP 2
  1804. Z.BL 20
  1805. Z.LI DBUG_ENTER\ 
  1806. ZUsed to tell the runtime support module the name of the function
  1807. Zbeing entered.
  1808. ZThe argument must be of type "pointer to character".
  1809. ZThe 
  1810. ZDBUG_ENTER
  1811. Zmacro must precede all executable lines in the
  1812. Zfunction just entered, and must come after all local declarations.
  1813. ZEach 
  1814. ZDBUG_ENTER
  1815. Zmacro must have a matching 
  1816. ZDBUG_RETURN 
  1817. Zor
  1818. ZDBUG_VOID_RETURN
  1819. Zmacro 
  1820. Zat the function exit points.
  1821. ZDBUG_ENTER 
  1822. Zmacros used without a matching 
  1823. ZDBUG_RETURN 
  1824. Zor 
  1825. ZDBUG_VOID_RETURN
  1826. Zmacro 
  1827. Zwill cause warning messages from the 
  1828. Z.I dbug
  1829. Zpackage runtime support module.
  1830. Z.SP 1
  1831. ZEX:\ DBUG_ENTER\ ("main");
  1832. Z.SP 1
  1833. Z.LI DBUG_RETURN\ 
  1834. ZUsed at each exit point of a function containing a 
  1835. ZDBUG_ENTER 
  1836. Zmacro
  1837. Zat the entry point.
  1838. ZThe argument is the value to return.
  1839. ZFunctions which return no value (void) should use the 
  1840. ZDBUG_VOID_RETURN
  1841. Zmacro.
  1842. ZIt 
  1843. Zis an error to have a 
  1844. ZDBUG_RETURN 
  1845. Zor 
  1846. ZDBUG_VOID_RETURN 
  1847. Zmacro in a function
  1848. Zwhich has no matching 
  1849. ZDBUG_ENTER 
  1850. Zmacro, and the compiler will complain
  1851. Zif the macros are actually used (expanded).
  1852. Z.SP 1
  1853. ZEX:\ DBUG_RETURN\ (value);
  1854. Z.br
  1855. ZEX:\ DBUG_VOID_RETURN;
  1856. Z.SP 1
  1857. Z.LI DBUG_PROCESS\ 
  1858. ZUsed to name the current process being executed.
  1859. ZA typical argument for this macro is "argv[0]", though
  1860. Zit will be perfectly happy with any other string.
  1861. Z.SP 1
  1862. ZEX:\ DBUG_PROCESS\ (argv[0]);
  1863. Z.SP 1
  1864. Z.LI DBUG_PUSH\ 
  1865. ZSets a new debugger state by pushing the current
  1866. Z.B dbug
  1867. Zstate onto an
  1868. Zinternal stack and setting up the new state using the debug control
  1869. Zstring passed as the macro argument.
  1870. ZThe most common usage is to set the state specified by a debug
  1871. Zcontrol string retrieved from the argument list.
  1872. ZNote that the leading "-#" in a debug control string specified
  1873. Zas a command line argument must
  1874. Z.B not
  1875. Zbe passed as part of the macro argument.
  1876. ZThe proper usage is to pass a pointer to the first character
  1877. Z.B after
  1878. Zthe "-#" string.
  1879. Z.SP 1
  1880. ZEX:\ DBUG_PUSH\ (\&(argv[i][2]));
  1881. Z.br
  1882. ZEX:\ DBUG_PUSH\ ("d:t");
  1883. Z.br
  1884. ZEX:\ DBUG_PUSH\ ("");
  1885. Z.SP 1
  1886. Z.LI DBUG_POP\ 
  1887. ZRestores the previous debugger state by popping the state stack.
  1888. ZAttempting to pop more states than pushed will be ignored and no
  1889. Zwarning will be given.
  1890. ZThe 
  1891. ZDBUG_POP 
  1892. Zmacro has no arguments.
  1893. Z.SP 1
  1894. ZEX:\ DBUG_POP\ ();
  1895. Z.SP 1
  1896. Z.LI DBUG_FILE\ 
  1897. ZThe 
  1898. ZDBUG_FILE 
  1899. Zmacro is used to do explicit I/O on the debug output
  1900. Zstream.
  1901. ZIt is used in the same manner as the symbols "stdout" and "stderr"
  1902. Zin the standard I/O package.
  1903. Z.SP 1
  1904. ZEX:\ fprintf\ (DBUG_FILE,\ "Doing my own I/O!\n");
  1905. Z.SP 1
  1906. Z.LI DBUG_EXECUTE\ 
  1907. ZThe DBUG_EXECUTE macro is used to execute any arbitrary C code.
  1908. ZThe first argument is the debug keyword, used to trigger execution
  1909. Zof the code specified as the second argument.
  1910. ZThis macro must be used cautiously because, like the 
  1911. ZDBUG_PRINT 
  1912. Zmacro,
  1913. Zit is automatically selected by default whenever the 'd' flag has
  1914. Zno argument list (I.E., a "-#d:t" control string).
  1915. Z.SP 1
  1916. ZEX:\ DBUG_EXECUTE\ ("abort",\ abort\ ());
  1917. Z.SP 1
  1918. Z.LI DBUG_N\ 
  1919. ZThese macros, where N is in the range 2-5, are currently obsolete
  1920. Zand will be removed in a future release.
  1921. ZUse the new DBUG_PRINT macro.
  1922. Z.LI DBUG_PRINT\ 
  1923. ZUsed to do printing via the "fprintf" library function on the
  1924. Zcurrent debug stream,
  1925. ZDBUG_FILE.
  1926. ZThe first argument is a debug keyword, the second is a format string
  1927. Zand the corresponding argument list.
  1928. ZNote that the format string and argument list are all one macro argument
  1929. Zand
  1930. Z.B must
  1931. Zbe enclosed in parenthesis.
  1932. Z.SP 1
  1933. ZEX:\ DBUG_PRINT\ ("eof",\ ("end\ of\ file\ found"));
  1934. Z.br
  1935. ZEX:\ DBUG_PRINT\ ("type",\ ("type\ is\ %x", type));
  1936. Z.br
  1937. ZEX:\ DBUG_PRINT\ ("stp",\ ("%x\ ->\ %s", stp, stp\ ->\ name));
  1938. Z.LI DBUG_SETJMP\ 
  1939. ZUsed in place of the setjmp() function to first save the current
  1940. Zdebugger state and then execute the standard setjmp call.
  1941. ZThis allows the debugger to restore its state when the
  1942. ZDBUG_LONGJMP macro is used to invoke the standard longjmp() call.
  1943. ZCurrently all instances of DBUG_SETJMP must occur within the
  1944. Zsame function and at the same function nesting level.
  1945. Z.SP 1
  1946. ZEX:\ DBUG_SETJMP\ (env);
  1947. Z.LI DBUG_LONGJMP\ 
  1948. ZUsed in place of the longjmp() function to first restore the
  1949. Zprevious debugger state at the time of the last DBUG_SETJMP
  1950. Zand then execute the standard longjmp() call.
  1951. ZNote that currently all DBUG_LONGJMP macros restore the state
  1952. Zat the time of the last DBUG_SETJMP.
  1953. ZIt would be possible to maintain separate DBUG_SETJMP and DBUG_LONGJMP
  1954. Zpairs by having the debugger runtime support module use the first
  1955. Zargument to differentiate the pairs.
  1956. Z.SP 1
  1957. ZEX:\ DBUG_LONGJMP\ (env,val);
  1958. Z.LE
  1959. Z
  1960. Z.SK
  1961. Z.B
  1962. ZDEBUG CONTROL STRING
  1963. Z.R
  1964. Z
  1965. Z.P
  1966. ZThe debug control string is used to set the state of the debugger
  1967. Zvia the 
  1968. Z.B DBUG_PUSH 
  1969. Zmacro.
  1970. ZThis section summarizes the currently available debugger options
  1971. Zand the flag characters which enable or disable them.
  1972. ZArgument lists enclosed in '[' and ']' are optional.
  1973. Z.SP 2
  1974. Z.BL 22
  1975. Z.LI d[,keywords]
  1976. ZEnable output from macros with specified keywords.
  1977. ZA null list of keywords implies that all keywords are selected.
  1978. Z.LI D[,time]
  1979. ZDelay for specified time after each output line, to let output drain.
  1980. ZTime is given in tenths of a second (value of 10 is one second).
  1981. ZDefault is zero.
  1982. Z.LI f[,functions]
  1983. ZLimit debugger actions to the specified list of functions.
  1984. ZA null list of functions implies that all functions are selected.
  1985. Z.LI F
  1986. ZMark each debugger output line with the name of the source file
  1987. Zcontaining the macro causing the output.
  1988. Z.LI g
  1989. ZTurn on machine independent profiling.
  1990. ZA profiling data collection file, named dbugmon.out, will be written
  1991. Zfor postprocessing by the "analyze" program.
  1992. ZThe accuracy of this feature is relatively unknown at this time.
  1993. Z.LI i
  1994. ZIdentify the process emitting each line of debug or trace output
  1995. Zwith the process id for that process.
  1996. Z.LI L
  1997. ZMark each debugger output line with the source file line number of
  1998. Zthe macro causing the output.
  1999. Z.LI n
  2000. ZMark each debugger output line with the current function nesting depth.
  2001. Z.LI N
  2002. ZSequentially number each debugger output line starting at 1.
  2003. ZThis is useful for reference purposes when debugger output is
  2004. Zinterspersed with program output.
  2005. Z.LI o[,file]
  2006. ZRedirect the debugger output stream to the specified file.
  2007. ZThe default output stream is stderr.
  2008. ZA null argument list causes output to be redirected to stdout.
  2009. Z.LI p[,processes]
  2010. ZLimit debugger actions to the specified processes.
  2011. ZA null list implies all processes.
  2012. ZThis is useful for processes which run child processes.
  2013. ZNote that each debugger output line can be marked with the name of
  2014. Zthe current process via the 'P' flag.
  2015. ZThe process name must match the argument passed to the
  2016. Z.B DBUG_PROCESS
  2017. Zmacro.
  2018. Z.LI P
  2019. ZMark each debugger output line with the name of the current process
  2020. Zfrom argv[0].
  2021. ZMost useful when used with a process which runs child processes that
  2022. Zare also being debugged.
  2023. ZNote that the parent process must arrange for the debugger control
  2024. Zstring to be passed to the child processes.
  2025. Z.LI r
  2026. ZUsed in conjunction with the 
  2027. Z.B DBUG_PUSH 
  2028. Zmacro to reset the current
  2029. Zindentation level back to zero.
  2030. ZMost useful with 
  2031. Z.B DBUG_PUSH 
  2032. Zmacros used to temporarily alter the
  2033. Zdebugger state.
  2034. Z.LI t[,N]
  2035. ZEnable function control flow tracing.
  2036. ZThe maximum nesting depth is specified by N, and defaults to
  2037. Z200.
  2038. Z.LE
  2039. Z.SK
  2040. Z.B
  2041. ZHINTS AND MISCELLANEOUS
  2042. Z.R
  2043. Z
  2044. Z.P
  2045. ZOne of the most useful capabilities of the 
  2046. Z.I dbug 
  2047. Zpackage is to compare the executions of a given program in two
  2048. Zdifferent environments.
  2049. ZThis is typically done by executing the program in the environment
  2050. Zwhere it behaves properly and saving the debugger output in a
  2051. Zreference file.
  2052. ZThe program is then run with identical inputs in the environment where 
  2053. Zit misbehaves and the output is again captured in a reference file.
  2054. ZThe two reference files can then be differentially compared to
  2055. Zdetermine exactly where execution of the two processes diverges.
  2056. Z
  2057. Z.P
  2058. ZA related usage is regression testing where the execution of a current
  2059. Zversion is compared against executions of previous versions.
  2060. ZThis is most useful when there are only minor changes.
  2061. Z
  2062. Z.P
  2063. ZIt is not difficult to modify an existing compiler to implement
  2064. Zsome of the functionality of the 
  2065. Z.I dbug
  2066. Zpackage automatically, without source code changes to the
  2067. Zprogram being debugged.
  2068. ZIn fact, such changes were implemented in a version of the
  2069. ZPortable C Compiler by the author in less than a day.
  2070. ZHowever, it is strongly encouraged that all newly
  2071. Zdeveloped code continue to use the debugger macros
  2072. Zfor the portability reasons noted earlier.
  2073. ZThe modified compiler should be used only for testing existing
  2074. Zprograms.
  2075. Z
  2076. Z.SK
  2077. Z.B
  2078. ZCAVEATS
  2079. Z.R
  2080. Z
  2081. Z.P
  2082. ZThe 
  2083. Z.I dbug
  2084. Zpackage works best with programs which have "line\ oriented"
  2085. Zoutput, such as text processors, general purpose utilities, etc.
  2086. ZIt can be interfaced with screen oriented programs such as
  2087. Zvisual editors by redefining the appropriate macros to call
  2088. Zspecial functions for displaying the debugger results.
  2089. ZOf course, this caveat is not applicable if the debugger output
  2090. Zis simply dumped into a file for post-execution examination.
  2091. Z
  2092. Z.P
  2093. ZPrograms which use memory allocation functions other than
  2094. Z.B malloc
  2095. Zwill usually have problems using the standard
  2096. Z.I dbug
  2097. Zpackage.
  2098. ZThe most common problem is multiply allocated memory.
  2099. Z.SP 2
  2100. Z.CS
  2101. STUNKYFLUFF
  2102. set `sum user.r`
  2103. if test 9250 != $1
  2104. then
  2105. echo user.r: Checksum error. Is: $1, should be: 9250.
  2106. fi
  2107. #
  2108. #
  2109. echo Extracting vargs.h:
  2110. sed 's/^Z//' >vargs.h <<\STUNKYFLUFF
  2111. Z/******************************************************************************
  2112. Z *                                          *
  2113. Z *                               N O T I C E                      *
  2114. Z *                                          *
  2115. Z *                  Copyright Abandoned, 1987, Fred Fish              *
  2116. Z *                                          *
  2117. Z *                                          *
  2118. Z *    This previously copyrighted work has been placed into the  public     *
  2119. Z *    domain  by  the  author  and  may be freely used for any purpose,     *
  2120. Z *    private or commercial.                              *
  2121. Z *                                          *
  2122. Z *    Because of the number of inquiries I was receiving about the  use     *
  2123. Z *    of this product in commercially developed works I have decided to     *
  2124. Z *    simply make it public domain to further its unrestricted use.   I     *
  2125. Z *    specifically  would  be  most happy to see this material become a     *
  2126. Z *    part of the standard Unix distributions by AT&T and the  Berkeley     *
  2127. Z *    Computer  Science  Research Group, and a standard part of the GNU     *
  2128. Z *    system from the Free Software Foundation.                  *
  2129. Z *                                          *
  2130. Z *    I would appreciate it, as a courtesy, if this notice is  left  in     *
  2131. Z *    all copies and derivative works.  Thank you.                  *
  2132. Z *                                          *
  2133. Z *    The author makes no warranty of any kind  with  respect  to  this     *
  2134. Z *    product  and  explicitly disclaims any implied warranties of mer-     *
  2135. Z *    chantability or fitness for any particular purpose.              *
  2136. Z *                                          *
  2137. Z ******************************************************************************
  2138. Z */
  2139. Z
  2140. Z
  2141. Z/*
  2142. Z *  FILE
  2143. Z *
  2144. Z *    vargs.h    include file for environments without varargs.h
  2145. Z *
  2146. Z *  SCCS
  2147. Z *
  2148. Z *    @(#)vargs.h    1.2    5/8/88
  2149. Z *
  2150. Z *  SYNOPSIS
  2151. Z *
  2152. Z *    #include "vargs.h"
  2153. Z *
  2154. Z *  DESCRIPTION
  2155. Z *
  2156. Z *    This file implements a varargs macro set for use in those
  2157. Z *    environments where there is no system supplied varargs.  This
  2158. Z *    generally works because systems which don't supply a varargs
  2159. Z *    package are precisely those which don't strictly need a varargs
  2160. Z *    package.  Using this one then allows us to minimize source
  2161. Z *    code changes.  So in some sense, this is a "portable" varargs
  2162. Z *    since it is only used for convenience, when it is not strictly
  2163. Z *    needed.
  2164. Z *
  2165. Z */
  2166. Z
  2167. Z/*
  2168. Z *    These macros allow us to rebuild an argument list on the stack
  2169. Z *    given only a va_list.  We can use these to fake a function like
  2170. Z *    vfprintf, which gets a fixed number of arguments, the last of
  2171. Z *    which is a va_list, by rebuilding a stack and calling the variable
  2172. Z *    argument form fprintf.  Of course this only works when vfprintf
  2173. Z *    is not available in the host environment, and thus is not available
  2174. Z *    for fprintf to call (which would give us an infinite loop).
  2175. Z *
  2176. Z *    Note that ARGS_TYPE is a long, which lets us get several bytes
  2177. Z *    at a time while also preventing lots of "possible pointer alignment
  2178. Z *    problem" messages from lint.  The messages are valid, because this
  2179. Z *    IS nonportable, but then we should only be using it in very
  2180. Z *    nonrestrictive environments, and using the real varargs where it
  2181. Z *    really counts.
  2182. Z *
  2183. Z */
  2184. Z
  2185. Z#define ARG0 a0
  2186. Z#define ARG1 a1
  2187. Z#define ARG2 a2
  2188. Z#define ARG3 a3
  2189. Z#define ARG4 a4
  2190. Z#define ARG5 a5
  2191. Z#define ARG6 a6
  2192. Z#define ARG7 a7
  2193. Z#define ARG8 a8
  2194. Z#define ARG9 a9
  2195. Z
  2196. Z#define ARGS_TYPE long
  2197. Z#define ARGS_LIST ARG0,ARG1,ARG2,ARG3,ARG4,ARG5,ARG6,ARG7,ARG8,ARG9
  2198. Z#define ARGS_DCL auto ARGS_TYPE ARGS_LIST
  2199. Z
  2200. Z/*
  2201. Z *    A pointer of type "va_list" points to a section of memory
  2202. Z *    containing an array of variable sized arguments of unknown
  2203. Z *    number.  This pointer is initialized by the va_start
  2204. Z *    macro to point to the first byte of the first argument.
  2205. Z *    We can then use it to walk through the argument list by 
  2206. Z *    incrementing it by the size of the argument being referenced.
  2207. Z */
  2208. Z
  2209. Ztypedef char *va_list;
  2210. Z
  2211. Z/*
  2212. Z *    The first variable argument overlays va_alist, which is
  2213. Z *    nothing more than a "handle" which allows us to get the
  2214. Z *    address of the first argument on the stack.  Note that
  2215. Z *    by definition, the va_dcl macro includes the terminating
  2216. Z *    semicolon, which makes use of va_dcl in the source code
  2217. Z *    appear to be missing a semicolon.
  2218. Z */
  2219. Z
  2220. Z#define va_dcl ARGS_TYPE va_alist;
  2221. Z
  2222. Z/*
  2223. Z *    The va_start macro takes a variable of type "va_list" and
  2224. Z *    initializes it.  In our case, it initializes a local variable
  2225. Z *    of type "pointer to char" to point to the first argument on
  2226. Z *    the stack.
  2227. Z */
  2228. Z
  2229. Z#define va_start(list) list = (char *) &va_alist
  2230. Z
  2231. Z/*
  2232. Z *    The va_end macro is a null operation for our use.
  2233. Z */
  2234. Z
  2235. Z#define va_end(list)
  2236. Z
  2237. Z/*
  2238. Z *    The va_arg macro is the tricky one.  This one takes
  2239. Z *    a va_list as the first argument, and a type as the second
  2240. Z *    argument, and returns a value of the appropriate type
  2241. Z *    while advancing the va_list to the following argument.
  2242. Z *    For our case, we first increment the va_list arg by the
  2243. Z *    size of the type being recovered, cast the result to 
  2244. Z *    a pointer of the appropriate type, and then dereference
  2245. Z *    that pointer as an array to get the previous arg (which
  2246. Z *    is the one we wanted.
  2247. Z */
  2248. Z
  2249. Z#define va_arg(list,type) ((type *) (list += sizeof (type)))[-1]
  2250. Z
  2251. STUNKYFLUFF
  2252. set `sum vargs.h`
  2253. if test 1668 != $1
  2254. then
  2255. echo vargs.h: Checksum error. Is: $1, should be: 1668.
  2256. fi
  2257. echo ALL DONE BUNKY!
  2258. exit 0
  2259.  
  2260.  
  2261.