home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8709 / dbug / 2 < prev    next >
Encoding:
Internet Message Format  |  1990-07-13  |  61.4 KB

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