📄 tut
字号:
the message.P1 a.out:running.P2.PPThe UNIX quit and interrupt signalsact on ADB itself rather than on the program being debugged.If such a signal occurs then the program being debugged is stopped and control is returned to ADB.The signal is saved by ADB and is passed on to the test program if:.P1 :c.P2is typed.This can be useful when testing interrupthandling routines.The signal is not passed on to the test program if:.P1 :c 0.P2is typed..PPNow let us reset the breakpoint at.ulsettaband display the instructions located there when we reach the breakpoint.This is accomplished by:.P1 settab+4:b settab,5?ia \fR*.P2.FS* Owing to a bug in early versions of ADB (including theversion distributed in Generic 3 UNIX) these statementsmust be written as:.br.in 1i\fBsettab+4:b settab,5?ia;0\fR.ft B.brgetc+4,3:b main.c?C;0.brsettab+4:b settab,5?ia; ptab/o;0.br.ft R.in -1iNote that \fB;0\fR will set dot to zero and stop at the breakpoint..FEIt is also possible to execute the ADB requests for each occurrence of the breakpoint butonly stop after the third occurrence by typing:.P1 getc+4,3:b main.c?C \fR*.P2This request will print the local variable .ulcin the function .ulmainat each occurrence of the breakpoint.The semicolon is used to separate multiple ADB requests on a single line..PPWarning:setting a breakpoint causes the value of dot to be changed;executing the program under ADB does not change dot.Therefore:.P1 settab+4:b .,5?ia fopen+4:b.P2will print the last thing dot was set to(in the example \fIfopen+4\fP).ulnotthe current location (\fIsettab+4\fP)at which the program is executing..PPA breakpoint can be overwritten without first deleting the old breakpoint.For example:.P1 settab+4:b settab,5?ia; ptab/o \fR*.P2could be entered after typing the above requests..PPNow the display of breakpoints:.P1 $b.P2shows the above request for the.ulsettabbreakpoint.When the breakpoint at.ulsettabis encountered the ADB requests are executed.Note that the location at.ulsettab+4has been changed to plant the breakpoint;all the other locations match their original value..PPUsing the functions,.ulf, gand .ulhshown in Figure 3,we can follow the execution of each function by planting non-stoppingbreakpoints.We call ADB with the executable program of Figure 3 as follows:.P1 adb ex3 \-.P2Suppose we enter the following breakpoints:.P1 h+4:b hcnt/d; h.hi/; h.hr/ g+4:b gcnt/d; g.gi/; g.gr/ f+4:b fcnt/d; f.fi/; f.fr/ :r.P2Each request line indicates that the variables are printed in decimal(by the specification \fBd\fR).Since the format is not changed, the \fBd\fR can be left off all butthe first request..PPThe output in Figure 7 illustrates two points.First, the ADB requests in the breakpoint line are notexamined until the program undertest is run.That means any errors in those ADB requests is not detected until run time.At the location of the error ADB stops running the program..PPThe second point is the way ADB handles register variables.ADB uses the symbol table to address variables.Register variables, like \fIf.fr\fR above, have pointers to uninitializedplaces on the stack.Therefore the message "symbol not found"..PPAnother way of getting at the data in this example is to printthe variables used in the call as:.P1 f+4:b fcnt/d; f.a/; f.b/; f.fi/ g+4:b gcnt/d; g.p/; g.q/; g.gi/ :c.P2The operator / was used instead of ?to read values from the \fIcore\fP file.The output for each function, as shown in Figure 7, has the same format.For the function \fIf\fP, for example, it shows the name and value of the.ulexternalvariable.ulfcnt.It also shows the address on the stack and value of thevariables.ula, band.ulfi..PPNotice that the addresses on the stack will continue to decreaseuntil no address space is left for program executionat which time (after many pages of output)the program under test aborts.A display with names would be produced by requests like the following:.P1 f+4:b fcnt/d; f.a/"a="d; f.b/"b="d; f.fi/"fi="d.P2In this format the quoted string is printed literally and the \fBd\fPproduces a decimal display of the variables.The results are shown in Figure 7..NH 2Other Breakpoint Facilities.LP.IP \(bu 4Arguments and change of standard input and output are passed to a program as:.P1 :r arg1 arg2 ... <infile >outfile.P2This requestkills any existing program under test andstarts the.ula.outafresh..IP \(buThe program being debugged can be single steppedby:.P1 :s.P2If necessary, this request will start up the program beingdebugged and stop after executingthe first instruction..IP \(buADB allows a program to be entered at a specific addressby typing:.P1 address:r.P2.IP \(buThe count field can be used to skip the first \fIn\fR breakpoints as:.P1 ,n:r.P2The request:.P1 ,n:c.P2may also be used for skipping the first \fIn\fR breakpointswhen continuing a program..sp.IP \(buA program can be continued at an address different from the breakpoint by:.P1 address:c.P2.IP \(buThe program being debugged runs as a separate process and can be killed by:.P1 :k.P2.LP.NHMaps.PPUNIX supports several executable file formats. These are used to tellthe loader how to load the program file. File type 407is the most common and is generated by a C compiler invocation such as\fBcc pgm.c\fP.A 410 file is produced by a C compiler command of the form \fBcc -n pgm.c\fP,whereas a 411 file is produced by \fBcc -i pgm.c\fP.ADB interprets these different file formats andprovides access to the different segments through a set of maps (see Figure 8).To print the maps type:.P1 $m.P2.PPIn 407 files, both text (instructions) and data are intermixed.This makes it impossible for ADB to differentiate data frominstructions and some of the printed symbolic addresses look incorrect;for example, printing data addresses as offsets from routines..PPIn 410 files (shared text), the instructions are separated from data and\fB?*\fR accesses the data part of the \fIa.out\fP file.The \fB?* \fP request tells ADB to use the second part of themap in the.ula.outfile.Accessing data in the \fIcore\fP file showsthe data after it was modified by the execution of the program.Notice also that the data segment may have grown duringprogram execution..PPIn 411 files (separated I & D space), theinstructions and data are also separated.However, in thiscase, since data is mapped through a separate set of segmentationregisters, the base of the data segment is also relative to address zero.In this case since the addresses overlap it is necessary to usethe \fB?*\fR operator to access the data space of the \fIa.out\fP file.In both 410 and 411 files the correspondingcore file does not contain the program text..PPFigure 9 shows the display of three mapsfor the same program linked as a 407, 410, 411 respectively.The b, e, and f fields are used by ADB to mapaddresses into file addresses.The "f1" field is thelength of the header at the beginning of the file (020 bytesfor an \fIa.out\fP file and 02000 bytes for a \fIcore\fP file).The "f2" field is the displacement from the beginning of the file to the data.For a 407 file with mixed text and data this is thesame as the length of the header; for 410 and 411 files thisis the length of the header plus the size of the text portion..PPThe "b" and "e" fields are the starting and ending locationsfor a segment.Given an address, A, the location inthe file (either \fIa.out\fP or \fIcore\fP) is calculated as:.P1 b1\(<=A\(<=e1 =\h'-.5m'> file address = (A\-b1)+f1 b2\(<=A\(<=e2 =\h'-.5m'> file address = (A\-b2)+f2.P2A user can access locations by using the ADB defined variables.The \fB$v\fR request prints the variables initialized by ADB:.P1 b base address of data segment d length of the data segment s length of the stack t length of the text m execution type (407,410,411).P2.PPIn Figure 9 those variables not present are zero.Use can be made of these variables by expressions such as:.P1 <b.P2in the address field.Similarly the value of the variable can be changed by an assignment requestsuch as:.P1 02000>b.P2that sets \fBb\fP to octal 2000.These variables are useful to know if the file under examinationis an executable or \fIcore\fP image file..PPADB reads the header of the \fIcore\fP image file to find thevalues for these variables.If the second file specified does notseem to be a \fIcore\fP file, or if it is missing then the header ofthe executable file is used instead..NHAdvanced Usage.PPIt is possible with ADB to combine formatting requeststo provide elaborate displays.Below are several examples..NH 2Formatted dump.PPThe line:.P1 <b,\-1/4o4^8Cn.P2prints 4 octal words followed by their ASCII interpretationfrom the data space of the core image file.Broken down, the various request pieces mean:.sp.in 1.7i.ta .7i.ti -.7i<b The base address of the data segment..sp.ti -.7i<b,\-1 Print from the base address to the end of file.A negative count is used here and elsewhere to loop indefinitelyor until some error condition (like end of file) is detected..sp.ti -1.7iThe format \fB4o4^8Cn\fR is broken down as follows:.sp.ti -.7i4o Print 4 octal locations..sp.ti -.7i4^ Backup the current address 4 locations (to the original start of the field)..sp.ti -.7i8C Print 8 consecutive characters using an escape convention;each character in the range 0 to 037 is printed as @ followed by the corresponding character in the range 0140 to 0177.An @ is printed as @@..sp.ti -.7in Print a newline..in -1.7i.fi.sp.PPThe request:.P1 <b,<d/4o4^8Cn.P2could have been used instead to allow the printing to stopat the end of the data segment (<d provides the data segment size in bytes)..PPThe formatting requests can be combined with ADB's abilityto read in a script to produce a core image dump script.ADB is invoked as:.P1 adb a.out core < dump.P2to read in a script file,.uldump,of requests.An example of such a script is:.P1 120$w 4095$s $v =3n $m =3n"C Stack Backtrace" $C =3n"C External Variables" $e =3n"Registers" $r 0$s =3n"Data Segment" <b,\-1/8ona.P2.PPThe request \fB120$w\fP sets the width of the output to120 characters(normally, the width is 80 characters).ADB attempts to print addresses as:.P1 symbol + offset.P2The request \fB4095$s\fP increases the maximum permissible offsetto the nearest symbolic address from 255 (default) to 4095.The request \fB=\fP can be used to print literal strings.Thus,headings are provided in this.uldumpprogramwith requests of the form:.P1 =3n"C Stack Backtrace".P2that spaces three lines and prints the literalstring.The request \fB$v\fP prints all non-zero ADB variables (see Figure 8).The request\fB0$s\fPsets the maximum offset for symbol matches to zero thussuppressing the printing of symbolic labels in favorof octal values.Note that this is only done for the printing of the data segment.The request:.P1 <b,\-1/8ona.P2prints a dump from the base of the data segment to the end of filewith an octal address field and eight octal numbers per line..PPFigure 11 shows the results of some formatting requestson the C program of Figure 10..NH 2Directory Dump.PPAs another illustration (Figure 12) consider a set of requests to dumpthe contents of a directory (which is made upof an integer \fIinumber\fP followed by a 14 character name):.P1 adb dir \- =n8t"Inum"8t"Name" 0,\-1? u8t14cn.P2In this example, the \fBu\fP prints the \fIinumber\fP as an unsigned decimal integer,the \fB8t\fP means that ADB will space to the nextmultiple of 8 on the output line, and the \fB14c\fP prints the 14 character file name..NH 2Ilist Dump.PPSimilarly the contents of the \fIilist\fP of a file system, (e.g. /dev/src,on UNIX systems distributed by the UNIX Support Group;see UNIX Programmer'sManual Section V) could be dumped with the following set of requests:.P1 adb /dev/src \- 02000>b ?m <b <b,\-1?"flags"8ton"links,uid,gid"8t3bn",size"8tbrdn"addr"8t8un"times"8t2Y2na.P2In this example the value of the base for the map was changed to 02000 (by saying \fB?m<b\fR) since that is the start of an \fIilist\fP within a file system.An artifice (\fBbrd\fP above) was used to print the 24 bit size fieldas a byte, a space, and a decimal integer.The last access time and last modify time are printed with the\fB2Y\fRoperator.Figure 12 shows portions of these requests as applied to a directoryand file system..NH 2Converting values.PPADB may be used to convert values from one representation toanother.For example:.P1 072 = odx.P2will print.P1 072 58 #3a.P2which is the octal, decimal and hexadecimal representationsof 072 (octal).The format is remembered so that typingsubsequent numbers will print them in the given formats.Character values may be converted similarly, for example:.P1 'a' = co.P2prints.P1 a 0141.P2It may also be used to evaluate expressions but bewarned that all binary operators havethe same precedence which is lower than that for unary operators..NHPatching.PPPatching files with ADB is accomplished with the .ulwrite,\fBw\fP or \fBW\fP, request (which is not like the \fIed\fP editor write command).This is often used in conjunction with the .ullocate,\fBl\fP or \fBL\fPrequest.In general, the request syntax for \fBl\fP and \fBw\fP are similar as follows:.P1 ?l value.P2The request \fBl\fP is used to match on two bytes, \fBL\fP is used forfour bytes.The request \fBw\fP is used to write two bytes, whereas\fBW\fP writes four bytes.The \fBvalue\fP field in either .ullocateor.ulwriterequestsis an expression.Therefore, decimal and octal numbers, or character strings are supported..PPIn order to modify a file, ADB must be called as:.P1 adb \-w file1 file2.P2When called with this option, .ulfile1and .ulfile2are created if necessary and opened for both reading and writing..PPFor example, consider the C program shown in Figure 10.We can change the word "This" to "The " in the executable filefor this program, \fIex7\fP, by using the following requests:.P1 adb \-w ex7 \- ?l 'Th' ?W 'The '.P2The request \fB?l\fP starts at dot and stops at the first match of "Th"having set dot to the address of the location found.Note the use of \fB?\fP to write to the .ula.outfile.The form \fB?*\fP would have been used for a 411 file..PPMore frequently the request will be typed as:.P1 ?l 'Th'; ?s.P2and locates the first occurrence of "Th" and print the entire string.Execution of this ADB request will set dot to the address of the "Th" characters..PPAs another example of the utility of the patching facility,consider a C program that has an internal logic flag.The flag could be set by the user through ADB and the program run.For example:.P1 adb a.out \- :s arg1 arg2 flag/w 1 :c.P2The \fB:s\fR request is normally used to single step through a processor start a process in single step mode.In this case it starts.ula.outas a subprocesswith arguments \fBarg1\fP and \fBarg2\fP.If there is a subprocess running ADB writes to it rather than to the fileso the \fBw\fP request causes \fIflag\fP to be changed in the memory of the subprocess..NHAnomalies.PPBelow is a list of some strange things that usersshould be aware of..IP 1.Function calls and arguments are put on the stack by the Csave routine.Putting breakpoints at the entry point to routinesmeans that the function appears not to have been calledwhen thebreakpoint occurs..IP 2.When printing addresses, ADB useseither text or data symbols from the \fIa.out\fP file.This sometimes causes unexpected symbol names to be printed with data (e.g. \fIsavr5+022\fP).This does not happen if\fB?\fR is used for text (instructions)and \fB/\fP for data..IP 3.ADB cannot handle C register variablesin the most recently activated function..LP.NHAcknowledgements.PPThe authors are grateful for the thoughtful commentson how to organize this documentfrom R. B. Brandt, E. N. Pinson and B. A. Tague.D. M. Ritchie made the system changes necessary to accommodatetracing within ADB. He also participated in discussions during the writing of ADB.His earlier work with DB and CDB led to many of the features found in ADB..SG MH-8234-JFM/1273-SRB-unix.NHReferences.LP.IP 1.D. M. Ritchie and K. Thompson,``The UNIX Time-Sharing System,''CACM, July, 1974..IP 2.B. W. Kernighan and D. M. Ritchie,.ulThe C Programming Language,Prentice-Hall, 1978..IP 3.K. Thompson and D. M. Ritchie,UNIX Programmer's Manual - 7th Edition,1978..IP 4.B. W. Kernighan and P. J. Plauger,.ulSoftware Tools,Addison-Wesley, 1976.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -