BSD 3 development
[unix-history] / usr / doc / sdb / sdbrp.n
CommitLineData
95e5b261
HK
1.RP
2.TL
3Sdb: A Symbolic Debugger
4.AU "HO 4F-605" 5337
5Howard P. Katseff
6.AI
7.HO
8.OK
9UNIX
10C programming
11Program testing
12.AB
13Sdb is a symbolic debugging program currently implemented for the
14language C on the UNIX/32V\s-2\u\(dg\d\s+2 Operating System.
15.FS
16\(dg\dUNIX is a trademark of Bell Laboratories
17.FE
18Sdb allows one to interact with a debugged program at the
19C language level.
20When debugging a core image from an aborted program,
21sdb reports which line in the C program caused the error
22and allows all variables,
23including array and structure elements,
24to be accessed symbolically
25and displayed in the correct format.
26.PP
27One may place breakpoints at selected statements
28or single step on a line by line basis.
29To facilitate specification of lines in the program without
30a source listing,
31a mechanism for examining the source text is also included in sdb.
32.PP
33Procedures may be called directly from the debugger.
34This feature is useful both for testing individual procedures
35and for calling user-provided routines which provide formatted
36printout of structured data.
37.AE
38.CS 6 4 10 0 0 2
39.NH
40Introduction
41.PP
42This document describes a symbolic debugger, sdb, as implemented
43for C programs on the UNIX/V32\s-2\u\(dg\d\s+2 Operating System.
44.FS
45\(dg\dUNIX is a trademark of Bell Laboratories
46.FE
47Sdb is useful both for examining core images of aborted programs
48and for providing an environment in which execution of a program
49can be monitored and controlled.
50.NH
51Examining core images
52.PP
53In order to use sdb,
54it is necessary to compile the C program with the `\(mig' flag.
55This causes the compiler to generate additional information
56about the variables and statements of the compiled program.
57When the debug flag is specified,
58sdb can be used to obtain a trace of the called procedures
59at the time of the abort and interactively display the
60values of variables.
61.NH 2
62Invoking sdb
63.PP
64A typical sequence of shell commands for debugging a core image is:
65.DS
66% cc \(mig foo.c \(mio foo
67% foo
68Bus error \(mi core dumped
69% sdb foo
70main:25: x[i] = 0;
71\v'.25m'*\v'-.25m'
72.DE
73.PP
74The program foo was compiled with the `\(mig' flag and then executed.
75An error occurred which caused a core dump.
76Sdb is then invoked to examine the core dump to determine the
77cause of the error.
78It reports that the Bus error occurred in procedure main at line 25
79(line numbers are always relative to the beginning of the file)
80and outputs the source text of the offending line.
81Sdb then prompts the user with a `\v'.25m'*\v'-.25m'' indicating that it awaits
82a command.
83.PP
84It is useful to know that sdb has a notion of current
85procedure and current line.
86In this example, they are initially set to `main' and `25'
87respectively.
88.PP
89In the above example sdb was called with one argument, `foo'.
90In general it takes three arguments on the command line.
91The first is the name of the executable file which is to be
92debugged;
93It defaults to a.out when not specified.
94The second is the name of the core file, defaulting to core
95and the third is the name of the directory containing the source
96of the program being debugged.
97Sdb currently requires all source to reside in a single directory.
98The default is the working directory.
99In the example
100the second and third arguments defaulted to the correct values,
101so only the first was specified.
102.PP
103It is possible that the error occurred in a procedure which was
104not compiled with the debug flag.
105In this case, sdb prints the procedure name and the address at
106which the error occurred.
107The current line and procedure are set to the first line in main.
108Sdb will complain if main was not compiled with `\(mig'
109but debugging can continue for those routines compiled with the
110debug flag.
111.NH 2
112Printing a stack trace
113.PP
114It is often useful to obtain a listing of the procedure calls
115which led to the error.
116This is obtained with the
117.B t
118command.
119For example:
120.DS
121\v'.25m'*\v'-.25m't
122sub(2,3) [foo.c:25]
123inter(16012) [foo.c:96]
124main(1,2147483584, 2147483592) [foo.c:15]
125.DE
126This indicates that the error occurred within the procedure sub
127at line 25 in file foo.c.
128Sub was called with the arguments 2 and 3 for inter at line 96.
129Inter was called from main at line 16.
130Main is always called by the shell with three arguments,
131often referred to as
132.I argc,
133.I argp
134and
135.I envp.
136Arguments in the call trace are always printed in decimal.
137.NH 2
138Examining variables
139.PP
140Sdb can be used to display variables in the stopped program.
141Variables are displayed by typing their name followed by a slash,
142so
143.DS
144\v'.25m'*\v'-.25m'errflg/
145.DE
146causes sdb to display the value of variable errflg.
147Unless otherwise specified,
148variables are assumed to be either local to or accessible from
149the current procedure.
150To specify a different procedure, use the form
151.DS
152\v'.25m'*\v'-.25m'sub:i/
153.DE
154to display variable i in procedure sub.
155Section 3.2 will explain how to change the current procedure.
156.PP
157Sdb normally displays the variable in a format determined by
158its type as declared in the C program.
159To request a different format, a specifier is placed after
160the slash.
161The specifier consists of an optional length specification
162followed by the format.
163The length specifiers are
164.nr PD 0
165.DS
166.IP \fBb\fP
167one byte
168.IP \fBh\fP
169two bytes (half word)
170.IP \fBl\fP
171four bytes (long word)
172.DE
173.nr PD 0.3v
174The lengths are only effective with the formats
175\fBd\fP, \fBo\fP, \fBx\fP and \fBu\fP.
176If no length is specified, the word length of the host machine,
177four for the DEC VAX-11/780\s-2\u\(dg\d\s+2, is used.
178.FS
179\(dg\dDEC and VAX are trademarks of Digital Equipment Corporation
180.FE
181There are a number of format specifiers available:
182.nr PD 0
183.DS
184.DS
185.IP \fBc\fR
186character
187.IP \fBd\fP
188decimal
189.IP \fBu\fP
190decimal unsigned
191.IP \fBo\fP
192octal
193.IP \fBx\fP
194hexadecimal
195.IP \fBf\fP
19632 bit single precision floating point
197.IP \fBg\fP
19864 bit double precision floating point
199.IP \fBs\fP
200Assume variable is a string pointer and print characters until a null is
201reached.
202.IP \fBa\fP
203Print characters starting at the variable's address until a null
204is reached.
205.DE
206.DE
207.nr PD 0.3v
208As an example,
209variable i can be displayed in hexadecimal with the following command
210.DS
211\v'.25m'*\v'-.25m'i/x
212.DE
213.PP
214Sdb also knows about structures, one dimensional arrays and
215pointers
216so that all of the following commands work.
217.DS
218\v'.25m'*\v'-.25m'array[2]/
219\v'.25m'*\v'-.25m'sym.id/
220\v'.25m'*\v'-.25m'psym\(mi>usage/
221\v'.25m'*\v'-.25m'xsym[20].p\(mi>usage/
222.DE
223The only restriction is that array subscripts must be numbers.
224Note that, as a special case
225.DS
226\v'.25m'*\v'-.25m'psym\(mi>/d
227.DE
228displays the location pointed to by psym in decimal.
229.PP
230Core locations can also be displayed by specifying their absolute
231addresses. The command
232.DS
233\v'.25m'*\v'-.25m'1024/
234.DE
235displays location 1024 in decimal.
236As in C,
237numbers may also be specified in octal or hexadecimal so the above
238command is equivalent to both of
239.DS
240\v'.25m'*\v'-.25m'02000/
241\v'.25m'*\v'-.25m'0x400/
242.DE
243It is possible to intermix numbers and variables, so that
244.DS
245\v'.25m'*\v'-.25m'1000.x/
246.DE
247refers to an element of a structure starting at address 1000 and
248.DS
249\v'.25m'*\v'-.25m'1000\(mi>x/
250.DE
251refers to an element of a structure whose address is at 1000.
252.PP
253The address of a variable is printed with the `=' command, so
254.DS
255\v'.25m'*\v'-.25m'i=
256.DE
257displays the address of i.
258Another feature whose usefulness will become apparent later is
259the command
260.DS
261\v'.25m'*\v'-.25m'./
262.DE
263which redisplays the last variable typed.
264.NH
265Source file display and manipulation
266.PP
267Sdb has been designed to make it easy to debug a program without
268constant reference to a current source listing.
269Facilities are provided which perform context searches
270within the source files of the program being debugged
271and to display selected portions of the source files.
272The commands are similar to those of the UNIX editor ed and ex [1].
273Like these editors,
274sdb has a notion of current file and line within the file.
275Sdb also knows how the lines of a file are partitioned into
276procedures, so that it also has a notion of current procedure.
277As noted in other parts of this document,
278the current procedure is used by a number of sdb commands.
279.NH 2
280Displaying the source file
281.PP
282Four command exist for displaying lines in the source file.
283They are useful for perusing through the source program
284and for determining the context of the current line.
285The commands are
286.DS
287.IP \fBw\fP
288Window. Print a window of 10 lines around the current line.
289.IP \fBz\fP
290Print 10 lines starting at the current line.
291Advance the current line by 10.
292.IP
293.ti 0
294\fBcontrol-D\fP
295.br
296.sp -1
297Scroll.
298Print the next 10 lines and advance the current line by 10.
299This command is used to cleanly display longs segments of the program.
300.DE
301.PP
302There is also a
303.B p
304command which prints the current line.
305When a line from a file is printed, it is preceded by its line number.
306This not only gives an indication of its relative position in the file,
307but is also used as input by some sdb commands.
308.NH 2
309Changing the current source file or procedure
310.PP
311The
312.B e
313command is used to change the current source file.
314Either of the forms
315.DS
316\v'.25m'*\v'-.25m'e procedure
317\v'.25m'*\v'-.25m'e file.c
318.DE
319may be used.
320The first causes the file containing the named procedure
321to become the current file
322and the current line becomes the first line of the procedure.
323The other form causes the named file to become current.
324In this case the current line is set to the first line of the named file.
325Finally,
326an
327.B e
328command with no argument causes the current procedure and file named
329to be printed.
330.NH 2
331Changing the current line in the source file
332.PP
333As mentioned in section 3.1,
334the
335.B z
336and
337.B control-D
338commands have a side effect of changing the current line in the
339source file.
340This section describes other commands which change the current line.
341.PP
342There are two commands for searching for regular expressions in
343source files.
344They are
345.DS
346\v'.25m'*\v'-.25m'/regular expression/
347\v'.25m'*\v'-.25m'?regular expression?
348.DE
349The first command searches forward through the file for a line containing
350a string which matches the regular expression and the second searches
351backwards.
352The trailing `/' and `?' may be omitted from these commands.
353Regular expression matching is identical to that of ed.
354.PP
355The
356.B +
357and
358.B \(mi
359commands
360may be used to move the current line forwards or backwards by a
361specified number of lines.
362Typing a newline advances the current line by one
363and
364typing a number causes that line to become the current line
365in the file.
366These commands may be catenated with the display commands so that
367.DS
368\v'.25m'*\v'-.25m'+15z
369.DE
370advances the current line by 15 and then prints 10 lines.
371.NH
372A controlled environment for program testing
373.PP
374One very useful feature of sdb
375is breakpoint debugging.
376After entering the debugger,
377certain lines in the source program may be specified to be
378.I breakpoints.
379The program is then started with a sdb command.
380Execution of the program proceeds as normal until
381it is about to execute one of the lines at which a breakpoint has
382been set.
383The program stops and sdb reports which breakpoint the
384program is stopped at.
385Now, sdb commands may be used to display
386the trace of procedure calls and the values of variables.
387If the user is satisfied that the program is working correctly
388to this point, some breakpoints can be deleted and others set,
389and then program execution may be continued from the point where it stopped.
390.PP
391A useful alternative to setting breakpoints is single stepping.
392Sdb can be requested to execute the next line of the program
393and them stop.
394This feature is especially useful for testing new programs,
395so they can be verified on a statement by statement basis.
396Note that if an attempt is made to single step through a
397procedure which has not been compiled with the `\(mig' flag,
398execution proceeds until a statement in a procedure compiled
399with the debug flag is reached.
400.PP
401The current implementation of single
402stepping is rather slow.
403While this is not a problem when stepping through a single statement,
404it may result in long delays while stepping through procedures not
405compiled with the debug flag.
406This problem is partially alleviated with the
407.B n
408command which quickly single steps until the
409positionally next statement is reached.
410.NH 2
411Setting and deleting breakpoints
412.PP
413Breakpoints can be set at any line in a procedure which contains
414executable code.
415The command format is:
416.DS
417\v'.25m'*\v'-.25m'12b
418\v'.25m'*\v'-.25m'proc:12b
419\v'.25m'*\v'-.25m'proc:b
420.DE
421The first form sets a breakpoint at line 12 in the current procedure.
422The line numbers are relative to the beginning of the file,
423as printed by the source file display commands.
424The second form sets a breakpoint at line 12 of procedure proc and the third
425sets a breakpoint at the first line of proc.
426.PP
427Breakpoints are deleted similarly with the commands:
428.DS
429\v'.25m'*\v'-.25m'12d
430\v'.25m'*\v'-.25m'proc:12d
431\v'.25m'*\v'-.25m'proc:d
432.DE
433In addition,
434if the command
435.B d
436is given alone,
437the breakpoints are deleted interactively.
438Each breakpoint location is printed and a line is read from the
439user.
440If the line begins with a `y' or `d', the breakpoint is deleted.
441.PP
442A list of the current breakpoints is printed in response to
443a
444.B b
445command.
446Beware that breakpoints do strange things if the debugged program
447is being run elsewhere at the same time.
448.NH 2
449Running the program
450.PP
451The
452.B r
453command is used to begin program execution.
454It restarts the program as if it were invoked from the shell.
455The command
456.DS
457\v'.25m'*\v'-.25m'r args
458.DE
459runs the program with the given arguments,
460as if they had been typed on the shell command line.
461.PP
462Execution is continued after a breakpoint with the
463.B c
464command
465and single stepping is accomplished with \fBs\fP.
466The
467.B n
468command is used to run the program until it reaches the positionally
469next statement.
470.PP
471Program execution can also be stopped with the RUBOUT key.
472The debugger is entered as if a breakpoint was encountered
473so that execution may be continued with
474\fBc\fP, \fBs\fP or \fBn\fP.
475.NH 2
476Calling procedures
477.PP
478It is possible to call any of the procedures of the program from
479the debugger.
480This feature is useful both for testing individual procedures
481with different arguments and for calling a procedure which
482prints structured data in a nice way.
483There are two ways to call a procedure:
484.DS
485\v'.25m'*\v'-.25m'proc(arg1, arg2, ...)
486\v'.25m'*\v'-.25m'proc(arg1, arg2, ...)/
487.DE
488The first simply executes the procedure.
489The second is intended for calling functions:
490It executes the procedure and prints the value that it returns.
491The value is printed in decimal unless some other
492format is specified.
493Arguments to procedures may be integer, character or string constants,
494or values of variables which are accessible from the current
495procedure.
496.PP
497An unfortunate bug in the current implementation is that
498if a procedure is called when the program is
499.I not
500stopped at a breakpoint
501(such as when a core image is being debugged),
502static variables are reinitialized before the procedure is
503restarted.
504This makes it impossible to use a procedure which
505formats data from a dump.
506.NH
507Other commands
508.PP
509To exit the debugger, use the
510.B q
511command.
512.PP
513The
514.B !
515command is identical to that in ed and is used to have the shell
516execute a command.
517.PP
518It is possible to change the values of variables when the program
519is stopped at a breakpoint. This is done with the command
520.DS
521\v'.25m'*\v'-.25m'variable!value
522.DE
523which sets the variable to the given value.
524The value may be a number, character constant or the name of
525another variable.
526.SH
527Acknowledgments
528.PP
529I would like to thank Bill Joy and Chuck Haley
530for their comments and constructive criticisms.
531.SH
532Reference
533.IP [1]
534William N. Joy, Ex Reference Manual, Computer Science Division,
535University of California, Berkeley, November 1977.
536.LP
537.SG HO-1353-hpk-sdb
538.bp
539.SH
540Appendix 1. Example of usage.
541.bp
542.SH
543Appendix 2. Manual pages.