xref: /plan9/sys/doc/acid.ms (revision 7c70c028d2d46a27a61ae88e6df0eb0935d9da7a)
1.HTML "Acid Manual
2.am DS
3.ft I
4..
5.ta 1i 2.3i 4.5i  (optional to set tabs)
6.TL
7Acid Manual
8.AU
9Phil Winterbottom
10philw@plan9.bell-labs.com
11.SH
12Introduction
13.PP
14Acid is a general purpose, source level symbolic debugger.
15The debugger is built around a simple command language.
16The command language, distinct from the language of the program being debugged,
17provides a flexible user interface that allows the debugger
18interface to be customized for a specific application or architecture.
19Moreover, it provides an opportunity to write test and
20verification code independently of a program's source code.
21Acid is able to debug multiple
22processes provided they share a common set of symbols, such as the processes in
23a threaded program.
24.PP
25Like other language-based solutions, Acid presents a poor user interface but
26provides a powerful debugging tool.
27Application of Acid to hard problems is best approached by writing functions off-line
28(perhaps loading them with the
29.CW include
30function or using the support provided by
31.I acme (1)),
32rather than by trying to type intricate Acid operations
33at the interactive prompt.
34.PP
35Acid allows the execution of a program to be controlled by operating on its
36state while it is stopped and by monitoring and controlling its execution
37when it is running. Each program action that causes a change
38of execution state is reflected by the execution
39of an Acid function, which may be user defined.
40A library of default functions provides the functionality of a normal debugger.
41.PP
42A Plan 9 process is controlled by writing messages to a control file in the
43.I proc (3)
44file system. Each control message has a corresponding Acid function, which
45sends the message to the process. These functions take a process id
46.I pid ) (
47as an
48argument. The memory and text file of the program may be manipulated using
49the indirection operators. The symbol table, including source cross reference,
50is available to an Acid program. The combination allows complex operations
51to be performed both in terms of control flow and data manipulation.
52.SH
53Input format and \f(CWwhatis\fP
54.PP
55Comments start with
56.CW //
57and continue to the end of the line.
58Input is a series of statements and expressions separated by semicolons.
59At the top level of the interpreter, the builtin function
60.CW print
61is called automatically to display the result of all expressions except function calls.
62A unary
63.CW +
64may be used as a shorthand to force the result of a function call to be printed.
65.PP
66Also at the top level, newlines are treated as semicolons
67by the parser, so semicolons are unnecessary when evaluating expressions.
68.PP
69When Acid starts, it loads the default program modules,
70enters interactive mode, and prints a prompt. In this state Acid accepts
71either function definitions or statements to be evaluated.
72In this interactive mode
73statements are evaluated immediately, while function definitions are
74stored for later invocation.
75.PP
76The
77.CW whatis
78operator can be used to report the state of identifiers known to the interpreter.
79With no argument,
80.CW whatis
81reports the name of all defined Acid functions; when supplied with an identifier
82as an argument it reports any variable, function, or type definition
83associated with the identifier.
84Because of the way the interpreter handles semicolons,
85the result of a
86.CW whatis
87statement can be returned directly to Acid without adding semicolons.
88A syntax error or interrupt returns Acid to the normal evaluation
89mode; any partially evaluated definitions are lost.
90.SH
91Using the Library Functions
92.PP
93After loading the program binary, Acid loads the portable and architecture-specific
94library functions  that form the standard debugging environment.
95These files are Acid source code and are human-readable.
96The following example uses the standard debugging library to show how
97language and program interact:
98.P1
99% acid /bin/ls
100/bin/ls:mips plan 9 executable
101
102/sys/lib/acid/port
103/sys/lib/acid/mips
104acid: new()
10575721: system call  _main ADD  $-0x14,R29
10675721: breakpoint   main+0x4   MOVW  R31,0x0(R29)
107acid: bpset(ls)
108acid: cont()
10975721: breakpoint   ls    ADD  $-0x16c8,R29
110acid: stk()
111At pc:0x0000141c:ls /sys/src/cmd/ls.c:87
112ls(s=0x0000004d,multi=0x00000000) /sys/src/cmd/ls.c:87
113    called from main+0xf4 /sys/src/cmd/ls.c:79
114main(argc=0x00000000,argv=0x7ffffff0) /sys/src/cmd/ls.c:48
115    called from _main+0x20 /sys/src/libc/mips/main9.s:10
116acid: PC
1170xc0000f60
118acid: *PC
1190x0000141c
120acid: ls
1210x0000141c
122.P2
123The function
124.CW new()
125creates a new process and stops it at the first instruction.
126This change in state is reported by a call to the
127Acid function
128.CW stopped ,
129which is called by the interpreter whenever the debugged program stops.
130.CW Stopped
131prints the status line giving the pid, the reason the program stopped
132and the address and instruction at the current PC.
133The function
134.CW bpset
135makes an entry in the breakpoint table and plants a breakpoint in memory.
136The
137.CW cont
138function continues the process, allowing it to run until some condition
139causes it to stop. In this case the program hits the breakpoint placed on
140the function
141.CW ls
142in the C program. Once again the
143.CW stopped
144routine is called to print the status of the program. The function
145.CW stk
146prints a C stack trace of the current process. It is implemented using
147a builtin Acid function that returns the stack trace as a list; the code
148that formats the information is all written in Acid.
149The Acid variable
150.CW PC
151holds the address of the
152cell where the current value of the processor register
153.CW PC
154is stored. By indirecting through
155the value of
156.CW PC
157the address where the program is stopped can be found.
158All of the processor registers are available by the same mechanism.
159.SH
160Types
161.PP
162An Acid variable has one of four types:
163.I integer ,
164.I float ,
165.I list ,
166or
167.I string .
168The type of a variable is inferred from the type of the right-hand
169side of the assignment expression which last set its value.
170Referencing a variable that has not yet
171been assigned draws a "used but not set" error. Many of the operators may
172be applied to more than
173one type; for these operators the action of the operator is determined by
174the types of its operands. The action of each operator is defined in the
175.I Expressions
176section of this manual.
177.SH
178Variables
179.PP
180Acid has three kinds of variables: variables defined by the symbol table
181of the debugged program, variables that are defined and maintained
182by the interpreter as the debugged program changes state, and variables
183defined and used by Acid programs.
184.PP
185Some examples of variables maintained by the interpreter are the register
186pointers listed by name in the Acid list variable
187.CW registers ,
188and the symbol table listed by name and contents in the Acid variable
189.CW symbols .
190.PP
191The variable
192.CW pid
193is updated by the interpreter to select the most recently created process
194or the process selected by the
195.CW setproc
196builtin function.
197.SH 1
198Formats
199.PP
200In addition to a type, variables have formats. The format is a code
201letter that determines the printing style and the effect of some of the
202operators on that variable. The format codes are derived from the format
203letters used by
204.I db (1).
205By default, symbol table variables and numeric constants
206are assigned the format code
207.CW X ,
208which specifies 32-bit hexadecimal.
209Printing a variable with this code yields the output
210.CW 0x00123456 .
211The format code of a variable may be changed from the default by using the
212builtin function
213.CW fmt .
214This function takes two arguments, an expression and a format code. After
215the expression is evaluated the new format code is attached to the result
216and forms the return value from
217.CW fmt .
218The backslash operator is a short form of
219.CW fmt .
220The format supplied by the backslash operator must be the format character
221rather than an expression.
222If the result is assigned to a variable the new format code is maintained
223in the variable. For example:
224.P1
225acid: x=10
226acid: print(x)
2270x0000000a
228acid: x = fmt(x, 'D')
229acid: print(x, fmt(x, 'X'))
23010 0x0000000a
231acid: x
23210
233acid: x\eo
23412
235.P2
236The supported format characters are:
237.RS
238.IP \f(CWo\fP
239Print two-byte integer in octal.
240.IP \f(CWO\fP
241Print four-byte integer in octal.
242.IP \f(CWq\fP
243Print two-byte integer in signed octal.
244.IP \f(CWQ\fP
245Print four-byte integer in signed octal.
246.IP \f(CWB\fP
247Print four-byte integer in binary.
248.IP \f(CWd\fP
249Print two-byte integer in signed decimal.
250.IP \f(CWD\fP
251Print four-byte integer in signed decimal.
252.IP \f(CWV\fP
253Print eight-byte integer in signed decimal.
254.IP \f(CWZ\fP
255Print eight-byte integer in unsigned decimal.
256.IP \f(CWx\fP
257Print two-byte integer in hexadecimal.
258.IP \f(CWX\fP
259Print four-byte integer in hexadecimal.
260.IP \f(CWY\fP
261Print eight-byte integer in hexadecimal.
262.IP \f(CWu\fP
263Print two-byte integer in unsigned decimal.
264.IP \f(CWU\fP
265Print four-byte integer in unsigned decimal.
266.IP \f(CWf\fP
267Print single-precision floating point number.
268.IP \f(CWF\fP
269Print double-precision floating point number.
270.IP \f(CWg\fP
271Print a single precision floating point number in string format.
272.IP \f(CWG\fP
273Print a double precision floating point number in string format.
274.IP \f(CWb\fP
275Print byte in hexadecimal.
276.IP \f(CWc\fP
277Print byte as an ASCII character.
278.IP \f(CWC\fP
279Like
280.CW c ,
281with
282printable ASCII characters represented normally and
283others printed in the form \f(CW\ex\fInn\fR.
284.IP \f(CWs\fP
285Interpret the addressed bytes as UTF characters
286and print successive characters until a zero byte is reached.
287.IP \f(CWr\fP
288Print a two-byte integer as a rune.
289.IP \f(CWR\fP
290Print successive two-byte integers as runes
291until a zero rune is reached.
292.IP \f(CWi\fP
293Print as machine instructions.
294.IP \f(CWI\fP
295As
296.CW i
297above, but print the machine instructions in
298an alternate form if possible:
299.CW sunsparc
300and
301.CW mipsco
302reproduce the manufacturers' syntax.
303.IP \f(CWa\fP
304Print the value in symbolic form.
305.RE
306.SH
307Complex types
308.PP
309Acid permits the definition of the layout of memory.
310The usual method is to use the
311.CW -a
312flag of the compilers to produce Acid-language descriptions of data structures (see
313.I 2c (1))
314although such definitions can be typed interactively.
315The keywords
316.CW complex ,
317.CW adt ,
318.CW aggr ,
319and
320.CW union
321are all equivalent; the compiler uses the synonyms to document the declarations.
322A complex type is described as a set of members, each containing a format letter,
323an offset in the structure, and a name.  For example, the C structure
324.P1
325struct List {
326	int         type;
327	struct List *next;
328};
329.P2
330is described by the Acid statement
331.P1
332complex List {
333	'D'	0	type;
334	'X'	4	next;
335};
336.P2
337.SH
338Scope
339.PP
340Variables are global unless they are either parameters to functions
341or are declared as
342.CW local
343in a function body. Parameters and local variables are available only in
344the body of the function in which they are instantiated.
345Variables are dynamically bound: if a function declares a local variable
346with the same name as a global variable, the global variable will be hidden
347whenever the function is executing.
348For example, if a function
349.CW f
350has a local called
351.CW main ,
352any function called below
353.CW f
354will see the local version of
355.CW main ,
356not the external symbol.
357.SH 1
358Addressing
359.PP
360Since the symbol table specifies addresses,
361to access the value of program variables
362an extra level of indirection
363is required relative to the source code.
364For consistency, the registers are maintained as pointers as well; Acid variables with the names
365of processor registers point to cells holding the saved registers.
366.PP
367The location in a file or memory image associated with
368an address is calculated from a map
369associated with the file.
370Each map contains one or more quadruples (\c
371.I t ,
372.I b ,
373.I e ,
374.I f \|),
375defining a segment named
376.I t
377(usually
378.CW text ,
379.CW data ,
380.CW regs ,
381or
382.CW fpregs )
383mapping addresses in the range
384.I b
385through
386.I e
387to the part of the file
388beginning at
389offset
390.I f .
391The memory model of a Plan 9 process assumes
392that segments are disjoint.  There
393can be more than one segment of a given type (e.g., a process
394may have more than one text segment) but segments
395may not overlap.
396An address
397.I a
398is translated
399to a file address
400by finding a segment
401for which
402.I b
403+
404.I a
405<
406.I e ;
407the location in the file
408is then
409.I address
410+
411.I f
412\-
413.I b .
414.PP
415Usually,
416the text and initialized data of a program
417are mapped by segments called
418.CW text
419and
420.CW data .
421Since a program file does not contain bss, stack, or register data,
422these data are
423not mapped by the data segment.
424The text segment is mapped similarly in the memory image of
425a normal (i.e., non-kernel) process.
426However, the segment called
427.CW *data
428maps memory from the beginning to the end of the program's data space.
429This region contains the program's static data, the bss, the
430heap and the stack.  A segment
431called
432.CW *regs
433maps the registers;
434.CW *fpregs
435maps the floating point registers.
436.PP
437Sometimes it is useful to define a map with a single segment
438mapping the region from 0 to 0xFFFFFFFF; such a map
439allows the entire file to be examined
440without address translation.  The builtin function
441.CW map
442examines and modifies Acid's map for a process.
443.SH 1
444Name Conflicts
445.PP
446Name conflicts between keywords in the Acid language, symbols in the program,
447and previously defined functions are resolved when the interpreter starts up.
448Each name is made unique by prefixing enough
449.CW $
450characters to the front of the name to make it unique. Acid reports
451a list of each name change at startup. The report looks like this:
452.P1
453/bin/sam: mips plan 9 executable
454/lib/acid/port
455/lib/acid/mips
456Symbol renames:
457	append=$append T/0xa4e40
458acid:
459.P2
460The symbol
461.CW append
462is both a keyword and a text symbol in the program. The message reports
463that the text symbol is now named
464.CW $append .
465.SH
466Expressions
467.PP
468Operators have the same
469binding and precedence as in C.
470For operators of equal precedence, expressions are evaluated from left to right.
471.SH 1
472Boolean expressions
473.PP
474If an expression is evaluated for a boolean condition the test
475performed depends on the type of the result. If the result is of
476.I integer
477or
478.I floating
479type the result is true if the value is non-zero. If the expression is a
480.I list
481the result is true if there are any members in the list.
482If the expression is a
483.I string
484the result is true if there are any characters in the string.
485.DS
486	primary-expression:
487		identifier
488		identifier \f(CW:\fP identifier
489		constant
490		\f(CW(\fP expression \f(CW)\fP
491		\f(CW{\fP elist \f(CW}\fP
492
493	elist:
494		expression
495		elist , expression
496.DE
497An identifier may be any legal Acid variable. The colon operator returns the
498address of parameters or local variables in the current stack of a program.
499For example:
500.P1
501*main:argc
502.P2
503prints the number of arguments passed into main. Local variables and parameters
504can only be referenced after the frame has been established. It may be necessary to
505step a program over the first few instructions of a breakpointed function to properly set
506the frame.
507.PP
508Constants follow the same lexical rules as C.
509A list of expressions delimited by braces forms a list constructor.
510A new list is produced by evaluating each expression when the constructor is executed.
511The empty list is formed from
512.CW {} .
513.P1
514acid: x = 10
515acid: l = { 1, x, 2\eD }
516acid: x = 20
517acid: l
518{0x00000001 , 0x0000000a , 2 }
519.P2
520.SH 1
521Lists
522.PP
523Several operators manipulate lists.
524.DS
525	list-expression:
526		primary-expression
527		\f(CWhead\fP primary-expression
528		\f(CWtail\fP primary-expression
529		\f(CWappend\fP expression \f(CW,\fP primary-expression
530		\f(CWdelete\fP expression \f(CW,\fP primary-expression
531.DE
532The
533.I primary-expression
534for
535.CW head
536and
537.CW tail
538must yield a value of type
539.I list .
540If there are no elements in the list the value of
541.CW head
542or
543.CW tail
544will be the empty list. Otherwise
545.CW head
546evaluates to the first element of the list and
547.CW tail
548evaluates to the rest.
549.P1
550acid: head {}
551{}
552acid: head {1, 2, 3, 4}
5530x00000001
554acid: tail {1, 2, 3, 4}
555{0x00000002 , 0x00000003 , 0x00000004 }
556.P2
557The first operand of
558.CW append
559and
560.CW delete
561must be an expression that yields a
562.I list .
563.CW Append
564places the result of evaluating
565.I primary-expression
566at the end of the list.
567The
568.I primary-expression
569supplied to
570.CW delete
571must evaluate to an integer;
572.CW delete
573removes the
574.I n 'th
575item from the list, where
576.I n
577is integral value of
578.I primary-expression.
579List indices are zero-based.
580.P1
581	acid: append {1, 2}, 3
582	{0x00000001 , 0x00000002 , 0x00000003 }
583	acid: delete {1, 2, 3}, 1
584	{0x00000001 , 0x00000003 }
585.P2
586.PP
587Assigning a list to a variable copies a reference to the list; if a list variable
588is copied it still points at the same list.  To copy a list, the elements must
589be copied piecewise using
590.CW head
591and
592.CW append .
593.SH 1
594Operators
595.PP
596.DS
597	postfix-expression:
598		list-expression
599		postfix-expression \f(CW[\fP expression \f(CW]\fP
600		postfix-expression \f(CW(\fP argument-list \f(CW)\fP
601		postfix-expression \f(CW.\fP tag
602		postfix-expression \f(CW->\fP tag
603		postfix-expression \f(CW++\fP
604		postfix-expression \f(CW--\fP
605
606	argument-list:
607		expression
608		argument-list , expression
609.DE
610The
611.CW [
612.I expression
613.CW ]
614operator performs indexing.
615The indexing expression must result in an expression of
616.I integer
617type, say
618.I n .
619The operation depends on the type of
620.I postfix-expression .
621If the
622.I postfix-expression
623yields an
624.I integer
625it is assumed to be the base address of an array in the memory image.
626The index offsets into this array; the size of the array members is
627determined by the format associated with the
628.I postfix-expression .
629If the
630.I postfix-expression
631yields a
632.I string
633the index operator fetches the
634.I n 'th
635character
636of the string. If the index points beyond the end
637of the string, a zero is returned.
638If the
639.I postfix-expression
640yields a
641.I list
642then the indexing operation returns the
643.I n 'th
644item of the list.
645If the list contains less than
646.I n
647items the empty list
648.CW {}
649is returned.
650.PP
651The
652.CW ++
653and
654.CW --
655operators increment and decrement integer variables.
656The amount of increment or decrement depends on the format code. These postfix
657operators return the value of the variable before the increment or decrement
658has taken place.
659.DS
660	unary-expression:
661		postfix-expression
662		\f(CW++\fP unary-expression
663		\f(CW--\fP unary-expression
664
665	unary-operator: one of
666		\f(CW*\fP \f(CW@\fP \f(CW+\fP \f(CW-\fP ~ \f(CW!\fP
667.DE
668The operators
669.CW *
670and
671.CW @
672are the indirection operators.
673.CW @
674references a value from the text file of the program being debugged.
675The size of the value depends on the format code. The
676.CW *
677operator fetches a value from the memory image of a process. If either
678operator appears on the left-hand side of an assignment statement, either the file
679or memory will be written. The file can only be modified when Acid is invoked
680with the
681.CW -w
682option.
683The prefix
684.CW ++
685and
686.CW --
687operators perform the same operation as their postfix counterparts but
688return the value after the increment or decrement has been performed. Since the
689.CW ++
690and
691.CW *
692operators fetch and increment the correct amount for the specified format,
693the following function prints correct machine instructions on a machine with
694variable length instructions, such as the 68020 or 386:
695.P1
696	defn asm(addr)
697	{
698		addr = fmt(addr, 'i');
699		loop 1, 10 do
700			print(*addr++, "\en");
701	}
702.P2
703The operators
704.CW ~
705and
706.CW !
707perform bitwise and logical negation respectively. Their operands must be of
708.I integer
709type.
710.DS
711	cast-expression:
712		unary-expression
713		unary-expression \f(CW\e\fP format-char
714		\f(CW(\fP complex-name \f(CW)\fP unary-expression
715.DE
716A unary expression may be preceded by a cast. The cast has the effect of
717associating the value of
718.I unary-expression
719with a complex type structure.
720The result may then be dereferenced using the
721.CW .
722and
723.CW ->
724operators.
725.PP
726An Acid variable may be associated with a complex type
727to enable accessing the type's members:
728.P1
729acid: complex List {
730	'D'	0	type;
731	'X'	4	next;
732};
733acid: complex List lhead
734acid: lhead.type
73510
736acid: lhead = ((List)lhead).next
737acid: lhead.type
738-46
739.P2
740Note that the
741.CW next
742field cannot be given a complex type automatically.
743.PP
744When entered at the top level of the interpreter,
745an expression of complex type
746is treated specially.
747If the type is called
748.CW T
749and an Acid function also called
750.CW T
751exists,
752then that function will be called with the expression as its argument.
753The compiler options
754.CW -a
755and
756.CW -aa
757will generate Acid source code defining such complex types and functions; see
758.I 2c (1).
759.PP
760A
761.I unary-expression
762may be qualified with a format specifier using the
763.CW \e
764operator. This has the same effect as passing the expression to the
765.CW fmt
766builtin function.
767.DS
768	multiplicative-expression:
769		cast-expression
770		multiplicative-expression \f(CW*\fP multiplicative-expression
771		multiplicative-expression \f(CW/\fP multiplicative-expression
772		multiplicative-expression \f(CW%\fP multiplicative-expression
773.DE
774These operate on
775.I integer
776and
777.I float
778types and perform the expected operations:
779.CW *
780multiplication,
781.CW /
782division,
783.CW %
784modulus.
785.DS
786	additive-expression:
787		multiplicative-expression
788		additive-expression \f(CW+\fP multiplicative-expression
789		additive-expression \f(CW-\fP multiplicative-expression
790.DE
791These operators perform as expected for
792.I integer
793and
794.I float
795operands.
796Unlike in C,
797.CW +
798and
799.CW -
800do not scale the addition based on the format of the expression.
801This means that
802.CW i=i+1
803will always add 1 but
804.CW i++
805will add the size corresponding to the format stored with
806.CW i .
807If both operands are of either
808.I string
809or
810.I list
811type then addition is defined as concatenation.
812Adding a string and an integer is treated as concatenation
813with the Unicode character corresponding to the integer.
814Subtraction is undefined for strings and lists.
815.DS
816	shift-expression:
817		additive-expression
818		shift-expression \f(CW<<\fP additive-expression
819		shift-expression \f(CW>>\fP additive-expression
820.DE
821The
822.CW >>
823and
824.CW <<
825operators perform bitwise right and left shifts respectively. Both
826require operands of
827.I integer
828type.
829.DS
830	relational-expression:
831		relational-expression \f(CW<\fP shift-expression
832		relational-expression \f(CW>\fP shift-expression
833		relational-expression \f(CW<=\fP shift-expression
834		relational-expression \f(CW>=\fP shift-expression
835
836	equality-expression:
837		relational-expression
838		relational-expression \f(CW==\fP equality-expression
839		relational-expression \f(CW!=\fP equality-expression
840.DE
841The comparison operators are
842.CW <
843(less than),
844.CW >
845(greater than),
846.CW <=
847(less than or equal to),
848.CW >=
849(greater than or equal to),
850.CW ==
851(equal to) and
852.CW !=
853(not equal to). The result of a comparison is 0
854if the condition is false, otherwise 1. The relational operators can only be
855applied to operands of
856.I integer
857and
858.I float
859type. The equality operators apply to all types.  Comparing mixed types is legal.
860Mixed integer and float compare on the integral value.  Other mixtures are always unequal.
861Two lists are equal if they
862have the same number of members and a pairwise comparison of the members results
863in equality.
864.DS
865	AND-expression:
866		equality-expression
867		AND-expression \f(CW&\fP equality-expression
868
869	XOR-expression:
870		AND-expression
871		XOR-expression \f(CW^\fP AND-expression
872
873	OR-expression:
874		XOR-expression
875		OR-expression \f(CW|\fP XOR-expression
876.DE
877These operators perform bitwise logical operations and apply only to the
878.I integer
879type.
880The operators are
881.CW &
882(logical and),
883.CW ^
884(exclusive or) and
885.CW |
886(inclusive or).
887.DS
888	logical-AND-expression:
889		OR-expression
890		logical-AND-expression \f(CW&&\fP OR-expression
891
892	logical-OR-expression:
893		logical-AND-expression
894		logical-OR-expression \f(CW||\fP logical-AND-expression
895.DE
896The
897.CW &&
898operator returns 1 if both of its operands evaluate to boolean true, otherwise 0.
899The
900.CW ||
901operator returns 1 if either of its operands evaluates to boolean true,
902otherwise 0.
903.SH
904Statements
905.PP
906.DS
907	\f(CWif\fP expression \f(CWthen\fP statement \f(CWelse\fP statement
908	\f(CWif\fP expression \f(CWthen\fP statement
909.DE
910The
911.I expression
912is evaluated as a boolean. If its value is true the statement after
913the
914.CW then
915is executed, otherwise the statement after the
916.CW else
917is executed. The
918.CW else
919portion may be omitted.
920.DS
921	\f(CWwhile\fP expression \f(CWdo\fP statement
922.DE
923In a while loop, the
924.I statement
925is executed while the boolean
926.I expression
927evaluates
928true.
929.DS
930	\f(CWloop\fP startexpr, endexpr \f(CWdo\fP statement
931.DE
932The two expressions
933.I startexpr
934and
935.I endexpr
936are evaluated prior to loop entry.
937.I Statement
938is evaluated while the value of
939.I startexpr
940is less than or equal to
941.I endexpr .
942Both expressions must yield
943.I integer
944values. The value of
945.I startexpr
946is
947incremented by one for each loop iteration.
948Note that there is no explicit loop variable; the
949.I expressions
950are just values.
951.DS
952	\f(CWreturn\fP expression
953.DE
954.CW return
955terminates execution of the current function and returns to its caller.
956The value of the function is given by expression. Since
957.CW return
958requires an argument, nil-valued functions should return the empty list
959.CW {} .
960.DS
961	\f(CWlocal\fP variable
962.DE
963The
964.CW local
965statement creates a local instance of
966.I variable ,
967which exists for the duration
968of the instance of the function in which it is declared. Binding is dynamic: the local variable,
969rather than the previous value of
970.I variable ,
971is visible to called functions.
972After a return from the current function the previous value of
973.I variable
974is
975restored.
976.PP
977If Acid is interrupted, the values of all local variables are lost,
978as if the function returned.
979.DS
980	\f(CWdefn\fP function-name \f(CW(\fP parameter-list \f(CW)\fP body
981
982	parameter-list:
983		variable
984		parameter-list , variable
985
986	body:
987		\f(CW{\fP statement \f(CW}\fP
988.DE
989Functions are introduced by the
990.CW defn
991statement. The definition of parameter names suppresses any variables
992of the same name until the function returns. The body of a function is a list
993of statements enclosed by braces.
994.SH
995Code variables
996.PP
997Acid permits the delayed evaluation of a parameter to a function.  The parameter
998may then be evaluated at any time with the
999.CW eval
1000operator.  Such parameters are called
1001.I "code variables
1002and are defined by prefixing their name with an asterisk in their declaration.
1003.PP
1004For example, this function wraps up an expression for later evaluation:
1005.P1
1006acid: defn code(*e) { return e; }
1007acid: x = code(v+atoi("100")\eD)
1008acid: print(x)
1009(v+atoi("100"))\eD;
1010acid: eval x
1011<stdin>:5: (error) v used but not set
1012acid: v=5
1013acid: eval x
1014105
1015.P2
1016.SH
1017Source Code Management
1018.PP
1019Acid provides the means to examine source code. Source code is
1020represented by lists of strings. Builtin functions provide mapping
1021from address to lines and vice-versa. The default debugging environment
1022has the means to load and display source files.
1023.SH
1024Builtin Functions
1025.PP
1026The Acid interpreter has a number of builtin functions, which cannot be redefined.
1027These functions perform machine- or operating system-specific functions such as
1028symbol table and process management.
1029The following section presents a description of each builtin function.
1030The notation
1031.CW {}
1032is used to denote the empty list, which is the default value of a function that
1033does not execute a
1034.CW return
1035statement.
1036The type and number of parameters for each function are specified in the
1037description; where a parameter can be of any type it is specified as type
1038.I item .
1039.de Ip
1040.KS
1041.in 0
1042.LP
1043.ie h \&\f2\\$1\fP\ \ \f(CW\\$2(\f2\\$3\f(CW)\f1\ \ \ \ \ \ \ \ \\$4
1044.el .tl '\f2\\$1\fP\ \ \f(CW\\$2(\f2\\$3\f(CW)\f1''\\$4'
1045.IP
1046..
1047.de Ex
1048.KE
1049.KS
1050.IP
1051.ft CW
1052.ta 4n +4n +4n +4n +4n +4n +4n +4n +4n +4n +4n +4n +4n +4n +4n +4n
1053.nf
1054.in +4n
1055.br
1056..
1057.de Ee
1058.fi
1059.ft 1
1060.br
1061.KE
1062..
1063.\"
1064.\"
1065.\"
1066.Ip integer access string "Check if a file can be read
1067.CW Access
1068returns the integer 1 if the file name in
1069.I string
1070can be read by the builtin functions
1071.CW file ,
1072.CW readfile ,
1073or
1074.CW include ,
1075otherwise 0. A typical use of this function is to follow
1076a search path looking for a source file; it is used by
1077.CW findsrc .
1078.Ex
1079if access("main.c") then
1080	return file("main.c");
1081.Ee
1082.\"
1083.\"
1084.\"
1085.Ip float atof string "Convert a string to float
1086.CW atof
1087converts the string supplied as its argument into a floating point
1088number. The function accepts strings in the same format as the C
1089function of the same name. The value returned has the format code
1090.CW f .
1091.CW atof
1092returns the value 0.0 if it is unable to perform the conversion.
1093.Ex
1094acid: +atof("10.4e6")
10951.04e+07
1096.Ee
1097.\"
1098.\"
1099.\"
1100.Ip integer atoi string "Convert a string to an integer
1101.CW atoi
1102converts the argument
1103.i string
1104to an integer value.
1105The function accepts strings in the same format as the C function of the
1106same name. The value returned has the format code
1107.CW D .
1108.CW atoi
1109returns the integer 0 if it is unable to perform a conversion.
1110.Ex
1111acid: +atoi("-1255")
1112-1255
1113.Ee
1114.\"
1115.\"
1116.\"
1117.Ip \f(CW{}\fP error string "Generate an interpreter error
1118.CW error
1119generates an error message and returns the interpreter to interactive
1120mode. If an Acid program is running, it is aborted.
1121Processes being debugged are not affected. The values of all local variables are lost.
1122.CW error
1123is commonly used to stop the debugger when some interesting condition arises
1124in the debugged program.
1125.Ex
1126while 1 do {
1127	step();
1128	if *main != @main then
1129		error("memory corrupted");
1130}
1131.Ee
1132.\"
1133.\"
1134.\"
1135.Ip list file string "Read the contents of a file into a list
1136.CW file
1137reads the contents of the file specified by
1138.I string
1139into a list.
1140Each element in the list is a string corresponding to a line in the file.
1141.CW file
1142breaks lines at the newline character, but the newline
1143characters are not returned as part each string.
1144.CW file
1145returns the empty list if it encounters an error opening or reading the data.
1146.Ex
1147acid: print(file("main.c")[0])
1148#include	<u.h>
1149.Ee
1150.\"
1151.\"
1152.\"
1153.Ip integer filepc string "Convert source address to text address
1154.CW filepc
1155interprets its
1156.I string
1157argument as a source file address in the form of a file name and line offset.
1158.CW filepc
1159uses the symbol table to map the source address into a text address
1160in the debugged program. The
1161.I integer
1162return value has the format
1163.CW X .
1164.CW filepc
1165returns an address of -1 if the source address is invalid.
1166The source file address uses the same format as
1167.I acme (1).
1168This function is commonly used to set breakpoints from the source text.
1169.Ex
1170acid: bpset(filepc("main:10"))
1171acid: bptab()
1172	0x00001020 usage  ADD	$-0xc,R29
1173.Ee
1174.\"
1175.\"
1176.\"
1177.Ip item fmt item,fmt "Set print, \f(CW@\fP and \f(CW*\fP formats
1178.CW fmt
1179evaluates the expression
1180.I item
1181and sets the format of the result to
1182.I fmt .
1183The format of a value determines how it will be printed and
1184what kind of object will be fetched by the
1185.CW *
1186and
1187.CW @
1188operators. The
1189.CW \e
1190operator is a short-hand form of the
1191.CW fmt
1192builtin function. The
1193.CW fmt
1194function leaves the format of the
1195.I item
1196unchanged.
1197.Ex
1198acid: main=fmt(main, 'i') // as instructions
1199acid: print(main\eX, "\et", *main)
12000x00001020 ADD	$-64,R29
1201.Ee
1202.\"
1203.\"
1204.\"
1205.Ip fmt fmtof item "Get format
1206.CW fmtof
1207evaluates the expression
1208.I item
1209and returns the format of the result.
1210.Ex
1211acid: +fmtof(33)
1212W
1213acid: +fmtof("string")
1214s
1215.Ee
1216.\"
1217.\"
1218.\"
1219.Ip integer fmtsize item "Get format size
1220.CW fmtsize
1221evaluates the expression
1222.I item
1223and returns the size in bytes of a single element of result's format.
1224.Ex
1225acid: +fmtsize('c')
12268
1227acid: +fmtsize('c'\ec)
12281
1229acid: +fmtsize(0\eX)
12304
1231acid: +fmtsize('c'\e3)
123210
1233.Ee
1234.\"
1235.\"
1236.\"
1237.Ip list fnbound integer "Find start and end address of a function
1238.CW fnbound
1239interprets its
1240.I integer
1241argument as an address in the text of the debugged program.
1242.CW fnbound
1243returns a list containing two integers corresponding to
1244the start and end addresses of the function containing the supplied address.
1245If the
1246.I integer
1247address is not in the text segment of the program then the empty list is returned.
1248.CW fnbound
1249is used by
1250.CW next
1251to detect stepping into new functions.
1252.Ex
1253acid: print(fnbound(main))
1254{0x00001050, 0x000014b8}
1255.Ee
1256.\"
1257.\"
1258.\"
1259.Ip \f(CW{}\fP follow integer "Compute follow set
1260The follow set is defined as the set of program counter values that could result
1261from executing an instruction.
1262.CW follow
1263interprets its
1264.I integer
1265argument as a text address, decodes the instruction at
1266that address and, with the current register set, builds a list of possible
1267next program counter values. If the instruction at the specified address
1268cannot be decoded
1269.CW follow
1270raises an error.
1271.CW follow
1272is used to plant breakpoints on
1273all potential paths of execution. The following code fragment
1274plants breakpoints on top of all potential following instructions.
1275.Ex
1276lst = follow(*PC);
1277while lst do
1278{
1279	*head lst = bpinst;
1280	lst = tail lst;
1281}
1282.Ee
1283.\"
1284.\"
1285.\"
1286.Ip \f(CW{}\fP include string "Take input from a new file
1287.CW include
1288opens the file specified by
1289.I string
1290and uses its contents as command input to the interpreter.
1291The interpreter restores input to its previous source when it encounters
1292either an end of file or an error.
1293.CW include
1294can be used to incrementally load symbol table information without
1295leaving the interpreter.
1296.Ex
1297acid: include("/sys/src/cmd/acme/syms")
1298.Ee
1299.\"
1300.\"
1301.\"
1302.Ip \f(CW{}\fP interpret string "Take input from a string
1303.CW interpret
1304evaluates the
1305.I string
1306expression and uses its result as command input for the interpreter.
1307The interpreter restores input to its previous source when it encounters
1308either the end of string or an error. The
1309.CW interpret
1310function allows Acid programs to write Acid code for later evaluation.
1311.Ex
1312acid: interpret("main+10;")
13130x0000102a
1314.Ee
1315.\"
1316.\"
1317.\"
1318.Ip string itoa integer[,string] "Convert integer to string
1319.CW itoa
1320takes an integer argument and converts it into an ASCII string
1321in the
1322.CW D
1323format.
1324an alternate format string
1325may be provided in the
1326.CW %
1327style of
1328.I print (2).
1329This function is commonly used to build
1330.CW rc
1331command lines.
1332.Ex
1333acid: rc("cat /proc/"+itoa(pid)+"/segment")
1334Stack    7fc00000 80000000    1
1335Data     00001000 00009000    1
1336Data     00009000 0000a000    1
1337Bss      0000a000 0000c000    1
1338.Ee
1339.\"
1340.\"
1341.\"
1342.Ip \f(CW{}\fP kill integer "Kill a process
1343.CW kill
1344writes a kill control message into the control file of the process
1345specified by the
1346.I integer
1347pid.
1348If the process was previously installed by
1349.CW setproc
1350it will be removed from the list of active processes.
1351If the
1352.I integer
1353has the same value as
1354.CW pid ,
1355then
1356.CW pid
1357will be set to 0.
1358To continue debugging, a new process must be selected using
1359.CW setproc .
1360For example, to kill all the active processes:
1361.Ex
1362while proclist do {
1363	kill(head proclist);
1364	proclist = tail proclist;
1365}
1366.Ee
1367.\"
1368.\"
1369.\"
1370.Ip list map list "Set or retrieve process memory map
1371.CW map
1372either retrieves all the mappings associated with a process or sets a single
1373map entry to a new value.
1374If the
1375.I list
1376argument is omitted then
1377.CW map
1378returns a list of lists. Each sublist has four values and describes a
1379single region of contiguous addresses in the
1380memory or file image of the debugged program. The first entry is the name of the
1381mapping. If the name begins with
1382.CW *
1383it denotes a map into the memory of an active process.
1384The second and third values specify the base and end
1385address of the region and the fourth number specifies the offset in the file
1386corresponding to the first location of the region.
1387A map entry may be set by supplying a list in the same format as the sublist
1388described above. The name of the mapping must match a region already defined
1389by the current map.
1390Maps are set automatically for Plan 9 processes and some kernels; they may
1391need to be set by hand for other kernels and programs that run on bare hardware.
1392.Ex
1393acid: map({"text", _start, end, 0x30})
1394.Ee
1395.\"
1396.\"
1397.\"
1398.Ip integer match item,list "Search list for matching value
1399.CW match
1400compares each item in
1401.I list
1402using the equality operator
1403.CW ==
1404with
1405.I item .
1406The
1407.I item
1408can be of any type. If the match succeeds the result is the integer index
1409of the matching value, otherwise -1.
1410.Ex
1411acid: list={8,9,10,11}
1412acid: print(list[match(10, list)]\eD)
141310
1414.Ee
1415.\"
1416.\"
1417.\"
1418.Ip \f(CW{}\fP newproc string "Create a new process
1419.CW newproc
1420starts a new process with an argument vector constructed from
1421.I string .
1422The argument vector excludes the name of the program to execute and
1423each argument in
1424.I string
1425must be space separated. A new process can accept no more
1426than 512 arguments. The internal variable
1427.CW pid
1428is set to the pid of the newly created process. The new pid
1429is also appended to the list of active processes stored in the variable
1430.CW proclist .
1431The new process is created then halted at the first instruction, causing
1432the debugger to call
1433.CW stopped .
1434The library functions
1435.CW new
1436and
1437.CW win
1438should be used to start processes when using the standard debugging
1439environment.
1440.Ex
1441acid: newproc("-l .")
144256720: system call	_main	ADD	$-0x14,R29
1443.Ee
1444.\"
1445.\"
1446.\"
1447.Ip string pcfile integer "Convert text address to source file name
1448.CW pcfile
1449interprets its
1450.I integer
1451argument as a text address in the debugged program. The address and symbol table
1452are used to generate a string containing the name of the source file
1453corresponding to the text address. If the address does not lie within the
1454program the string
1455.CW ?file?
1456is returned.
1457.Ex
1458acid: print("Now at ", pcfile(*PC), ":", pcline(*PC))
1459Now at ls.c:46
1460.Ee
1461.\"
1462.\"
1463.\"
1464.Ip integer pcline integer "Convert text address to source line number
1465.CW pcline
1466interprets its
1467.I integer
1468argument as a text address in the debugged program. The address and symbol table
1469are used to generate an integer containing the line number in the source file
1470corresponding to the text address. If the address does not lie within the
1471program the integer 0 is returned.
1472.Ex
1473acid: +file("main.c")[pcline(main)]
1474main(int argc, char *argv[])
1475.Ee
1476.\"
1477.\"
1478.\"
1479.Ip \f(CW{}\fP print item,item,... "Print expressions
1480.CW print
1481evaluates each
1482.I item
1483supplied in its argument list and prints it to standard output. Each
1484argument will be printed according to its associated format character.
1485When the interpreter is executing, output is buffered and flushed every
14865000 statements or when the interpreter returns to interactive mode.
1487.CW print
1488accepts a maximum of 512 arguments.
1489.Ex
1490acid: print(10, "decimal ", 10\eD, "octal ", 10\eo)
14910x0000000a decimal 10 octal 000000000012
1492acid: print({1, 2, 3})
1493{0x00000001 , 0x00000002 , 0x00000003 }
1494acid: print(main, main\ea, "\et", @main\ei)
14950x00001020 main	ADD	$-64,R29
1496.Ee
1497.\"
1498.\"
1499.\"
1500.Ip \f(CW{}\fP printto string,item,item,... "Print expressions to file
1501.CW printto
1502offers a limited form of output redirection. The first
1503.I string
1504argument is used as the path name of a new file to create.
1505Each
1506.I item
1507is then evaluated and printed to the newly created file. When all items
1508have been printed the file is closed.
1509.CW printto
1510accepts a maximum of 512 arguments.
1511.Ex
1512acid: printto("/env/foo", "hello")
1513acid: rc("echo -n $foo")
1514hello
1515.Ee
1516.\"
1517.\"
1518.\"
1519.Ip string rc string "Execute a shell command
1520.CW rc
1521evaluates
1522.I string
1523to form a shell command. A new command interpreter is started
1524to execute the command. The Acid interpreter blocks until the command
1525completes. The return value is the empty string
1526if the command succeeds, otherwise the exit status of the failed command.
1527.Ex
1528acid: rc("B "+itoa(-pcline(addr))+" "+pcfile(addr));
1529.Ee
1530.\"
1531.\"
1532.\"
1533.Ip string readfile string "Read file contents into a string
1534.CW readfile
1535takes the contents of the file specified by
1536.I string
1537and returns its contents as a new string.
1538If
1539.CW readfile
1540encounters a zero byte in the file, it terminates.
1541If
1542.CW readfile
1543encounters an error opening or reading the file then the empty list
1544is returned.
1545.CW readfile
1546can be used to read the contents of device files whose lines are not
1547terminated with newline characters.
1548.Ex
1549acid: ""+readfile("/dev/label")
1550helix
1551.Ee
1552.\"
1553.\"
1554.\"
1555.Ip string reason integer "Print cause of program stoppage
1556.CW reason
1557uses machine-dependent information to generate a string explaining
1558why a process has stopped. The
1559.I integer
1560argument is the value of an architecture dependent status register,
1561for example
1562.CW CAUSE
1563on the MIPS.
1564.Ex
1565acid: print(reason(*CAUSE))
1566system call
1567.Ee
1568.\"
1569.\"
1570.\"
1571.Ip integer regexp pattern,string "Regular expression match
1572.CW regexp
1573matches the
1574.I pattern
1575string supplied as its first argument with the
1576.I string
1577supplied as its second.
1578If the pattern matches the result is the value 1, otherwise 0.
1579.Ex
1580acid: print(regexp(".*bar", "foobar"))
15811
1582.Ee
1583.\"
1584.\"
1585.\"
1586.Ip \f(CW{}\fP setproc integer "Set debugger focus
1587.CW setproc
1588selects the default process used for memory and control operations. It effectively
1589shifts the focus of control between processes. The
1590.I integer
1591argument specifies the pid of the process to look at.
1592The variable
1593.CW pid
1594is set to the pid of the selected process. If the process is being
1595selected for the first time its pid is added to the list of active
1596processes
1597.CW proclist .
1598.Ex
1599acid: setproc(68382)
1600acid: procs()
1601>68382: Stopped at main+0x4 setproc(68382)
1602.Ee
1603.\"
1604.\"
1605.\"
1606.Ip \f(CW{}\fP start integer "Restart execution
1607.CW start
1608writes a
1609.CW start
1610message to the control file of the process specified by the pid
1611supplied as its
1612.I integer
1613argument.
1614.CW start
1615draws an error if the process is not in the
1616.CW Stopped
1617state.
1618.Ex
1619acid: start(68382)
1620acid: procs()
1621>68382: Running at main+0x4 setproc(68382)
1622.Ee
1623.\"
1624.\"
1625.\"
1626.Ip \f(CW{}\fP startstop integer "Restart execution, block until stopped
1627.CW startstop
1628performs the same actions as a call to
1629.CW start
1630followed by a call to
1631.CW stop .
1632The
1633.I integer
1634argument specifies the pid of the process to control. The process
1635must be in the
1636.CW Stopped
1637state.
1638Execution is restarted, the debugger then waits for the process to
1639return to the
1640.CW Stopped
1641state. A process will stop if a startstop message has been written to its control
1642file and any of the following conditions becomes true: the process executes or returns from
1643a system call, the process generates a trap or the process receives a note.
1644.CW startstop
1645is used to implement single stepping.
1646.Ex
1647acid: startstop(pid)
164875374: breakpoint	ls	ADD	$-0x16c8,R29
1649.Ee
1650.\"
1651.\"
1652.\"
1653.Ip string status integer "Return process state
1654.CW status
1655uses the pid supplied by its
1656.I integer
1657argument to generate a string describing the state of the process.
1658The string corresponds to the state returned by the
1659sixth column of the
1660.I ps (1)
1661command.
1662A process must be in the
1663.CW Stopped
1664state to modify its memory or registers.
1665.Ex
1666acid: ""+status(pid)
1667Stopped
1668.Ee
1669.\"
1670.\"
1671.\"
1672.Ip \f(CW{}\fP stop integer "Wait for a process to stop
1673.CW stop
1674writes a
1675.CW stop
1676message to the control file of the process specified by the
1677pid supplied as its
1678.I integer
1679argument.
1680The interpreter blocks until the debugged process enters the
1681.CW Stopped
1682state.
1683A process will stop if a stop message has been written to its control
1684file and any of the following conditions becomes true: the process executes or returns from
1685a system call, the process generates a trap, the process is scheduled or the
1686process receives a note.
1687.CW stop
1688is used to wait for a process to halt before planting a breakpoint since Plan 9
1689only allows a process's memory to be written while it is in the
1690.CW Stopped
1691state.
1692.Ex
1693defn bpset(addr) {
1694	if (status(pid)!="Stopped") then {
1695		print("Waiting...\en");
1696		stop(pid);
1697	}
1698	...
1699}
1700.Ee
1701.\"
1702.\"
1703.\"
1704.Ip list strace pc,sp,linkreg "Stack trace
1705.CW strace
1706generates a list of lists corresponding to procedures called by the debugged
1707program. Each sublist describes a single stack frame in the active process.
1708The first element is an
1709.I integer
1710of format
1711.CW X
1712specifying the address of the called function. The second element is the value
1713of the program counter when the function was called. The third and fourth elements
1714contain lists of parameter and automatic variables respectively.
1715Each element of these lists
1716contains a string with the name of the variable and an
1717.I integer
1718value of format
1719.CW X
1720containing the current value of the variable.
1721The arguments to
1722.CW strace
1723are the current value of the program counter, the current value of the
1724stack pointer, and the address of the link register. All three parameters
1725must be integers.
1726The setting of
1727.I linkreg
1728is architecture dependent. On the MIPS linkreg is set to the address of saved
1729.CW R31 ,
1730on the SPARC to the address of saved
1731.CW R15 .
1732For the other architectures
1733.I linkreg
1734is not used, but must point to valid memory.
1735.Ex
1736acid: print(strace(*PC, *SP, linkreg))
1737{{0x0000141c, 0xc0000f74,
1738{{"s", 0x0000004d}, {"multi", 0x00000000}},
1739{{"db", 0x00000000}, {"fd", 0x000010a4},
1740{"n", 0x00000001}, {"i", 0x00009824}}}}
1741.Ee
1742.\"
1743.\"
1744.\"
1745.Ip \f(CW{}\fP waitstop integer "Wait for a process to stop
1746.CW waitstop
1747writes a waitstop message to the control file of the process specified by the
1748pid supplied as its
1749.I integer
1750argument.
1751The interpreter will remain blocked until the debugged process enters the
1752.CW Stopped
1753state.
1754A process will stop if a waitstop message has been written to its control
1755file and any of the following conditions becomes true: the process generates a trap
1756or receives a note. Unlike
1757.CW stop ,
1758the
1759.CW waitstop
1760function is passive; it does not itself cause the program to stop.
1761.Ex
1762acid: waitstop(pid)
176375374: breakpoint	ls	ADD	$-0x16c8,R29
1764.Ee
1765.\"
1766.\"
1767.\"
1768.SH
1769Library Functions
1770.PP
1771A standard debugging environment is provided by modules automatically
1772loaded when
1773Acid is started.
1774These modules are located in the directory
1775.CW /sys/lib/acid .
1776These functions may be overridden, personalized, or added to by code defined in
1777.CW $home/lib/acid .
1778The implementation of these functions can be examined using the
1779.CW whatis
1780operator and then modified during debugging sessions.
1781.\"
1782.\"
1783.\"
1784.Ip \f(CW{}\fP Bsrc integer "Load editor with source
1785.CW Bsrc
1786interprets the
1787.I integer
1788argument as a text address. The text address is used to produce a pathname
1789and line number suitable for the
1790.CW B
1791command
1792to send to the text editor
1793.I sam (1)
1794or
1795.I acme (1).
1796.CW Bsrc
1797builds an
1798.I rc (1)
1799command to invoke
1800.CW B ,
1801which either selects an existing source file or loads a new source file into the editor.
1802The line of source corresponding to the text address is then selected.
1803In the following example
1804.CW stopped
1805is redefined so that the editor
1806follows and displays the source line currently being executed.
1807.Ex
1808defn stopped(pid) {
1809	pstop(pid);
1810	Bsrc(*PC);
1811}
1812.Ee
1813.\"
1814.\"
1815.\"
1816.Ip \f(CW{}\fP Fpr "" "Display double precision floating registers
1817For machines equipped with floating point,
1818.CW Fpr
1819displays the contents of the floating point registers as double precision
1820values.
1821.Ex
1822acid: Fpr()
1823F0   0.	F2   0.
1824F4   0.	F6   0.
1825F8   0.	F10  0.
1826\&...
1827.Ee
1828.\"
1829.\"
1830.\"
1831.Ip \f(CW{}\fP Ureg integer "Display contents of Ureg structure
1832.CW Ureg
1833interprets the integer passed as its first argument as the address of a
1834kernel
1835.CW Ureg
1836structure. Each element of the structure is retrieved and printed.
1837The size and contents of the
1838.CW Ureg
1839structure are architecture dependent.
1840This function can be used to decode the first argument passed to a
1841.I notify (2)
1842function after a process has received a note.
1843.Ex
1844acid: Ureg(*notehandler:ur)
1845	status	0x3000f000
1846	pc	0x1020
1847	sp	0x7ffffe00
1848	cause	0x00004002
1849\&...
1850.Ee
1851.\"
1852.\"
1853.\"
1854.Ip \f(CW{}\fP acidinit "" "Interpreter startup
1855.CW acidinit
1856is called by the interpreter after all
1857modules have been loaded at initialization time.
1858It is used to set up machine specific variables and the default source path.
1859.CW acidinit
1860should not be called by user code.
1861.KE
1862.\"
1863.\"
1864.\"
1865.Ip \f(CW{}\fP addsrcdir string "Add element to source search path
1866.CW addsrcdir
1867interprets its string argument as a new directory
1868.CW findsrc
1869should search when looking for source code files.
1870.CW addsrcdir
1871draws an error if the directory is already in the source search path. The search
1872path may be examined by looking at the variable
1873.CW srcpath .
1874.Ex
1875acid: rc("9fs fornax")
1876acid: addsrcpath("/n/fornax/sys/src/cmd")
1877.Ee
1878.\"
1879.\"
1880.\"
1881.Ip \f(CW{}\fP asm integer "Disassemble machine instructions
1882.CW asm
1883interprets its integer argument as a text address from which to disassemble
1884machine instructions.
1885.CW asm
1886prints the instruction address in symbolic and hexadecimal form, then prints
1887the instructions with addressing modes. Up to twenty instructions will
1888be disassembled.
1889.CW asm
1890stops disassembling when it reaches the end of the current function.
1891Instructions are read from the file image using the
1892.CW @
1893operator.
1894.Ex
1895acid: asm(main)
1896main     0x00001020 ADD    $-0x64,R29
1897main+0x4 0x00001024 MOVW   R31,0x0(R29)
1898main+0x8 0x00001028 MOVW   R1,argc+4(FP)
1899main+0xc 0x0000102c MOVW   $bin(SB),R1
1900.Ee
1901.\"
1902.\"
1903.\"
1904.Ip \f(CW{}\fP bpdel integer "Delete breakpoint
1905.CW bpdel
1906removes a previously set breakpoint from memory.
1907The
1908.I integer
1909supplied as its argument must be the address of a previously set breakpoint.
1910The breakpoint address is deleted from the active breakpoint list
1911.CW bplist ,
1912then the original instruction is copied from the file image to the memory
1913image so that the breakpoint is removed.
1914.Ex
1915acid: bpdel(main+4)
1916.Ee
1917.\"
1918.\"
1919.\"
1920.Ip \f(CW{}\fP bpset integer "Set a breakpoint
1921.CW bpset
1922places a breakpoint instruction at the address specified
1923by its
1924.I integer
1925argument, which must be in the text segment.
1926.CW bpset
1927draws an error if a breakpoint has already been set at the specified address.
1928A list of current breakpoints is maintained in the variable
1929.CW bplist .
1930Unlike in
1931.I db (1),
1932breakpoints are left in memory even when a process is stopped, and
1933the process must exist, perhaps by being
1934created by either
1935.CW new
1936or
1937.CW win ,
1938in order to place a breakpoint.
1939.CW Db "" (
1940accepts breakpoint commands before the process is started.)
1941On the
1942MIPS and SPARC architectures,
1943breakpoints at function entry points should be set 4 bytes into the function
1944because the
1945instruction scheduler may fill
1946.CW JAL
1947branch delay slots with the first instruction of the function.
1948.Ex
1949acid: bpset(main+4)
1950.Ee
1951.\"
1952.\"
1953.\"
1954.Ip \f(CW{}\fP bptab "" "List active breakpoints
1955.CW bptab
1956prints a list of currently installed breakpoints. The list contains the
1957breakpoint address in symbolic and hexadecimal form as well as the instruction
1958the breakpoint replaced. Breakpoints are not maintained across process creation
1959using
1960.CW new
1961and
1962.CW win .
1963They are maintained across a fork, but care must be taken to keep control of
1964the child process.
1965.Ex
1966acid: bpset(ls+4)
1967acid: bptab()
1968	0x00001420 ls+0x4  MOVW	R31,0x0(R29)
1969.Ee
1970.\"
1971.\"
1972.\"
1973.Ip \f(CW{}\fP casm "" "Continue disassembly
1974.CW casm
1975continues to disassemble instructions from where the last
1976.CW asm
1977or
1978.CW casm
1979command stopped. Like
1980.CW asm ,
1981this command stops disassembling at function boundaries.
1982.Ex
1983acid: casm()
1984main+0x10 0x00001030	MOVW	$0x1,R3
1985main+0x14 0x00001034	MOVW	R3,0x8(R29)
1986main+0x18 0x00001038	MOVW	$0x1,R5
1987main+0x1c 0x0000103c	JAL	Binit(SB)
1988.Ee
1989.\"
1990.\"
1991.\"
1992.Ip \f(CW{}\fP cont "" "Continue program execution
1993.CW cont
1994restarts execution of the currently active process.
1995If the process is stopped on a breakpoint, the breakpoint is first removed,
1996the program is single stepped, the breakpoint is replaced and the program
1997is then set executing. This may cause
1998.CW stopped()
1999to be called twice.
2000.CW cont
2001causes the interpreter to block until the process enters the
2002.CW Stopped
2003state.
2004.Ex
2005acid: cont()
200695197: breakpoint	ls+0x4	MOVW	R31,0x0(R29)
2007.Ee
2008.\"
2009.\"
2010.\"
2011.Ip \f(CW{}\fP dump integer,integer,string "Formatted memory dump
2012.CW dump
2013interprets its first argument as an address, its second argument as a
2014count and its third as a format string.
2015.CW dump
2016fetches an object from memory at the current address and prints it according
2017to the format. The address is incremented by the number of bytes specified by
2018the format and the process is repeated count times. The format string is any
2019combination of format characters, each preceded by an optional count.
2020For each object,
2021.CW dump
2022prints the address in hexadecimal, a colon, the object and then a newline.
2023.CW dump
2024uses
2025.CW mem
2026to fetch each object.
2027.Ex
2028acid: dump(main+35, 4, "X2bi")
20290x00001043: 0x0c8fa700 108 143 lwc2 r0,0x528f(R4)
20300x0000104d: 0xa9006811   0   0 swc3 r0,0x0(R24)
20310x00001057: 0x2724e800   4  37 ADD  $-0x51,R23,R31
20320x00001061: 0xa200688d   6   0 NOOP
20330x0000106b: 0x2710c000   7   0 BREAK
2034.Ee
2035.\"
2036.\"
2037.\"
2038.Ip \f(CW{}\fP findsrc string "Use source path to load source file
2039.CW findsrc
2040interprets its
2041.I string
2042argument as a source file. Each directory in the source path is searched
2043in turn for the file. If the file is found, the source text is loaded using
2044.CW file
2045and stored in the list of active source files called
2046.CW srctext .
2047The name of the file is added to the source file name list
2048.CW srcfiles .
2049Users are unlikely to call
2050.CW findsrc
2051from the command line, but may use it from scripts to preload source files
2052for a debugging session. This function is used by
2053.CW src
2054and
2055.CW line
2056to locate and load source code. The default search path for the MIPS
2057is
2058.CW ./ ,
2059.CW /sys/src/libc/port ,
2060.CW /sys/src/libc/9sys ,
2061.CW /sys/src/libc/mips .
2062.Ex
2063acid: findsrc(pcfile(main));
2064.Ee
2065.\"
2066.\"
2067.\"
2068.Ip \f(CW{}\fP fpr "" "Display single precision floating registers
2069For machines equipped with floating point,
2070.CW fpr
2071displays the contents of the floating point registers as single precision
2072values. When the interpreter stores or manipulates floating point values
2073it converts into double precision values.
2074.Ex
2075acid: fpr()
2076F0   0.	F1   0.
2077F2   0.	F3   0.
2078F4   0.	F5   0.
2079\&...
2080.Ee
2081.\"
2082.\"
2083.\"
2084.Ip \f(CW{}\fP func "" "Step while in function
2085.CW func
2086single steps the active process until it leaves the current function
2087by either calling another function or returning to its caller.
2088.CW func
2089will execute a single instruction after leaving the current function.
2090.Ex
2091acid: func()
209295197: breakpoint	ls+0x8	MOVW	R1,R8
209395197: breakpoint	ls+0xc	MOVW	R8,R1
209495197: breakpoint	ls+0x10	MOVW	R8,s+4(FP)
209595197: breakpoint	ls+0x14	MOVW	$0x2f,R5
209695197: breakpoint	ls+0x18	JAL	utfrrune(SB)
209795197: breakpoint	utfrrune	ADD	$-0x18,R29
2098.Ee
2099.\"
2100.\"
2101.\"
2102.Ip \f(CW{}\fP gpr "" "Display general purpose registers
2103.CW gpr
2104prints the values of the general purpose processor registers.
2105.Ex
2106acid: gpr()
2107R1	0x00009562 R2	0x000010a4 R3	0x00005d08
2108R4	0x0000000a R5	0x0000002f R6	0x00000008
2109\&...
2110.Ee
2111.\"
2112.\"
2113.\"
2114.Ip \f(CW{}\fP labstk integer "Print stack trace from label
2115.CW labstk
2116performs a stack trace from a Plan 9
2117.I label.
2118The kernel,
2119C compilers store continuations in a common format. Since the
2120compilers all use caller save conventions a continuation may be saved by
2121storing a
2122.CW PC
2123and
2124.CW SP
2125pair. This data structure is called a label and is used by the
2126the C function
2127.CW longjmp
2128and the kernel to schedule threads and processes.
2129.CW labstk
2130interprets its
2131.I integer
2132argument as the address of a label and produces a stack trace for
2133the thread of execution. The value of the function
2134.CW ALEF_tid
2135is a suitable argument for
2136.CW labstk .
2137.Ex
2138acid: labstk(*mousetid)
2139At pc:0x00021a70:Rendez_Sleep+0x178 rendez.l:44
2140Rendez_Sleep(r=0xcd7d8,bool=0xcd7e0,t=0x0) rendez.l:5
2141	called from ALEF_rcvmem+0x198 recvmem.l:45
2142ALEF_rcvmem(c=0x000cd764,l=0x00000010) recvmem.l:6
2143\&...
2144.Ee
2145.\"
2146.\"
2147.\"
2148.Ip \f(CW{}\fP lstk "" "Stack trace with local variables
2149.CW lstk
2150produces a long format stack trace.
2151The stack trace includes each function in the stack,
2152where it was called from, and the value of the parameters and automatic
2153variables for each function.
2154.CW lstk
2155displays the value rather than the address of each variable and all
2156variables are assumed to be an integer in format
2157.CW X .
2158To print a variable in its correct format use the
2159.CW :
2160operator to find the address and apply the appropriate format before indirection
2161with the
2162.CW *
2163operator. It may be necessary to single step a couple of instructions into
2164a function to get a correct stack trace because the frame pointer adjustment
2165instruction may get scheduled down into the body of the function.
2166.Ex
2167acid: lstk()
2168At pc:0x00001024:main+0x4 ls.c:48
2169main(argc=0x00000001,argv=0x7fffefec) ls.c:48
2170	called from _main+0x20 main9.s:10
2171	_argc=0x00000000
2172	_args=0x00000000
2173	fd=0x00000000
2174	buf=0x00000000
2175	i=0x00000000
2176.Ee
2177.\"
2178.\"
2179.\"
2180.Ip \f(CW{}\fP mem integer,string "Print memory object
2181.CW mem
2182interprets its first
2183.I integer
2184argument as the address of an object to be printed according to the
2185format supplied in its second
2186.I string
2187argument.
2188The format string can be any combination of format characters, each preceded
2189by an optional count.
2190.Ex
2191acid: mem(bdata+0x326, "2c2Xb")
2192P = 0xa94bc464 0x3e5ae44d  19
2193.Ee
2194.\"
2195.\"
2196.\"
2197.Ip \f(CW{}\fP new "" "Create new process
2198.CW new
2199starts a new copy of the debugged program. The new program is started
2200with the program arguments set by the variable
2201.CW progargs .
2202The new program is stopped in the second instruction of
2203.CW main .
2204The breakpoint list is reinitialized.
2205.CW new
2206may be used several times to instantiate several copies of a program
2207simultaneously. The user can rotate between the copies using
2208.CW setproc .
2209.Ex
2210acid: progargs="-l"
2211acid: new()
221260: external interrupt	_main	ADD	$-0x14,R29
221360: breakpoint	main+0x4	MOVW	R31,0x0(R29)
2214.Ee
2215.\"
2216.\"
2217.\"
2218.Ip \f(CW{}\fP next "" "Step through language statement
2219.CW next
2220steps through a single language level statement without tracing down
2221through each statement in a called function. For each statement,
2222.CW next
2223prints the machine instructions executed as part of the statement. After
2224the statement has executed, source lines around the current program
2225counter are displayed.
2226.Ex
2227acid: next()
222860: breakpoint	Binit+0x4 MOVW	R31,0x0(R29)
222960: breakpoint	Binit+0x8 MOVW	f+8(FP),R4
2230binit.c:93
2231 88
2232 89	int
2233 90	Binit(Biobuf *bp, int f, int mode)
2234 91	{
2235>92		return Binits(bp, f, mode, bp->b, BSIZE);
2236 93	}
2237.Ee
2238.\"
2239.\"
2240.\"
2241.Ip \f(CW{}\fP notestk integer "Stack trace after receiving a note
2242.CW notestk
2243interprets its
2244.I integer
2245argument as the address of a
2246.CW Ureg
2247structure passed by the kernel to a
2248.I notify (2)
2249function during note processing.
2250.CW notestk
2251uses the
2252.CW PC ,
2253.CW SP ,
2254and link register from the
2255.CW Ureg
2256to print a stack trace corresponding to the point in the program where the note
2257was received.
2258To get a valid stack trace on the MIPS and SPARC architectures from a notify
2259routine, the program must stop in a new function called from the notify routine
2260so that the link register is valid and the notify routine's parameters are
2261addressable.
2262.Ex
2263acid: notestk(*notify:ur)
2264Note pc:0x00001024:main+0x4 ls.c:48
2265main(argc=0x00000001,argv=0x7fffefec) ls.c:48
2266	called from _main+0x20 main9.s:10
2267	_argc=0x00000000
2268	_args=0x00000000
2269.Ee
2270.\"
2271.\"
2272.\"
2273.Ip \f(CW{}\fP pfl integer "Print source file and line
2274.CW pfl
2275interprets its argument as a text address and uses it to print
2276the source file and line number corresponding to the address. The output
2277has the same format as file addresses in
2278.I acme (1).
2279.Ex
2280acid: pfl(main)
2281ls.c:48
2282.Ee
2283.\"
2284.\"
2285.\"
2286.Ip \f(CW{}\fP procs "" "Print active process list
2287.CW procs
2288prints a list of active process attached to the debugger. Each process
2289produces a single line of output giving the pid, process state, the address
2290the process is currently executing, and the
2291.CW setproc
2292command required to make that process current.
2293The current process is marked in the first column with a
2294.CW >
2295character. The debugger maintains a list of processes in the variable
2296.CW proclist .
2297.Ex
2298acid: procs()
2299>62: Stopped at main+0x4 setproc(62)
2300 60: Stopped at Binit+0x8 setproc(60)
2301.Ee
2302.\"
2303.\"
2304.\"
2305.Ip \f(CW{}\fP pstop integer "Print reason process stopped
2306.CW pstop
2307prints the status of the process specified by the
2308.I integer
2309pid supplied as its argument.
2310.CW pstop
2311is usually called from
2312.CW stopped
2313every time a process enters the
2314.CW Stopped
2315state.
2316.Ex
2317acid: pstop(62)
23180x0000003e: breakpoint	main+0x4	MOVW	R31,0x0(R29)
2319.Ee
2320.\"
2321.\"
2322.\"
2323.Ip \f(CW{}\fP regs "" "Print registers
2324.CW regs
2325prints the contents of both the general and special purpose registers.
2326.CW regs
2327calls
2328.CW spr
2329then
2330.CW gpr
2331to display the contents of the registers.
2332.KE
2333.\"
2334.\"
2335.\"
2336.Ip \f(CW{}\fP source "" "Summarize source data base
2337.CW source
2338prints the directory search path followed by a list of currently loaded
2339source files. The source management functions
2340.CW src
2341and
2342.CW findsrc
2343use the search path to locate and load source files. Source files are
2344loaded incrementally into a source data base during debugging. A list
2345of loaded files is stored in the variable
2346.CW srcfiles
2347and the contents of each source file in the variable
2348.CW srctext .
2349.Ex
2350acid: source()
2351/n/bootes/sys/src/libbio/
2352./
2353/sys/src/libc/port/
2354/sys/src/libc/9sys/
2355/sys/src/libc/mips/
2356	binit.c
2357.Ee
2358.\"
2359.\"
2360.\"
2361.Ip \f(CW{}\fP spr "" "Print special purpose registers
2362.CW spr
2363prints the contents of the processor control and memory management
2364registers. Where possible, the contents of the registers are decoded
2365to provide extra information; for example the
2366.CW CAUSE
2367register on the MIPS is
2368printed both in hexadecimal and using the
2369.CW reason
2370function.
2371.Ex
2372acid: spr()
2373PC	0x00001024 main+0x4  ls.c:48
2374SP	0x7fffef68 LINK	0x00006264 _main+0x28 main9.s:12
2375STATUS	0x0000ff33 CAUSE	0x00000024 breakpoint
2376TLBVIR	0x000000d3 BADVADR	0x00001020
2377HI	0x00000004 LO		0x00001ff7
2378.Ee
2379.\"
2380.\"
2381.\"
2382.Ip \f(CW{}\fP src integer "Print lines of source
2383.CW src
2384interprets its
2385.I integer
2386argument as a text address and uses this address to print 5 lines
2387of source before and after the address. The current line is marked with a
2388.CW >
2389character.
2390.CW src
2391uses the source search path maintained by
2392.CW source
2393and
2394.CW addsrcdir
2395to locate the required source files.
2396.Ex
2397acid: src(*PC)
2398ls.c:47
2399 42	Biobuf	bin;
2400 43
2401 44	#define		HUNK	50
2402 45
2403 46	void
2404>47	main(int argc, char *argv[])
2405 48	{
2406 49		int i, fd;
2407 50		char buf[64];
2408 51
2409 52		Binit(&bin, 1, OWRITE);
2410.Ee
2411.\"
2412.\"
2413.\"
2414.Ip \f(CW{}\fP step "" "Single step process
2415.CW step
2416causes the debugged process to execute a single machine level instruction.
2417If the program is stopped on a breakpoint set by
2418.CW bpset
2419it is first removed, the single step executed, and the breakpoint replaced.
2420.CW step
2421uses
2422.CW follow
2423to predict the address of the program counter after the current instruction
2424has been executed. A breakpoint is placed at each of these predicted addresses
2425and the process is started. When the process stops the breakpoints are removed.
2426.Ex
2427acid: step()
242862: breakpoint	main+0x8	MOVW	R1,argc+4(FP)
2429.Ee
2430.\"
2431.\"
2432.\"
2433.Ip \f(CW{}\fP stk "" "Stack trace
2434.CW stk
2435produces a short format stack trace. The stack trace includes each function
2436in the stack, where it was called from, and the value of the parameters.
2437The short format omits the values of automatic variables.
2438Parameters are assumed to be integer values in the format
2439.CW X ;
2440to print a parameter in the correct format use the
2441.CW :
2442to obtain its address, apply the correct format, and use the
2443.CW *
2444indirection operator to find its value.
2445It may be necessary to single step a couple of instructions into
2446a function to get a correct stack trace because the frame pointer adjustment
2447instruction may get scheduled down into the body of the function.
2448.Ex
2449acid: stk()
2450At pc:0x00001028:main+0x8 ls.c:48
2451main(argc=0x00000002,argv=0x7fffefe4) ls.c:48
2452	called from _main+0x20 main9.s:10
2453.Ee
2454.\"
2455.\"
2456.\"
2457.Ip \f(CW{}\fP stmnt "" "Execute a single statement
2458.CW stmnt
2459executes a single language level statement.
2460.CW stmnt
2461displays each machine level instruction as it is executed. When the executed
2462statement is completed the source for the next statement is displayed.
2463Unlike
2464.CW next ,
2465the
2466.CW stmnt
2467function will trace down through function calls.
2468.Ex
2469acid: stmnt()
247062: breakpoint	main+0x18 MOVW	R5,0xc(R29)
247162: breakpoint	main+0x1c JAL	Binit(SB)
247262: breakpoint	Binit     ADD	$-0x18,R29
2473binit.c:91
2474 89	int
2475 90	Binit(Biobuf *bp, int f, int mode)
2476>91	{
2477.Ee
2478.\"
2479.\"
2480.\"
2481.Ip \f(CW{}\fP stopped integer "Report status of stopped process
2482.CW stopped
2483is called automatically by the interpreter
2484every time a process enters the
2485.CW Stopped
2486state, such as when it hits a breakpoint.
2487The pid is passed as the
2488.I integer
2489argument.  The default implementation just calls
2490.CW pstop ,
2491but the function may be changed to provide more information or perform fine control
2492of execution.  Note that
2493.CW stopped
2494should return; for example, calling
2495.CW step
2496in
2497.CW stopped
2498will recur until the interpreter runs out of stack space.
2499.Ex
2500acid: defn stopped(pid) {
2501	if *lflag != 0 then error("lflag modified");
2502	}
2503acid: progargs = "-l"
2504acid: new();
2505acid: while 1 do step();
2506<stdin>:7: (error) lflag modified
2507acid: stk()
2508At pc:0x00001220:main+0x200 ls.c:54
2509main(argc=0x00000001,argv=0x7fffffe8) ls.c:48
2510	called from _main+0x20 main9.s:10
2511.Ee
2512.\"
2513.\"
2514.\"
2515.Ip \f(CW{}\fP symbols string "Search symbol table
2516.CW symbols
2517uses the regular expression supplied by
2518.I string
2519to search the symbol table for symbols whose name matches the
2520regular expression.
2521.Ex
2522acid: symbols("main")
2523main	T	0x00001020
2524_main	T	0x0000623c
2525.Ee
2526.\"
2527.\"
2528.\"
2529.Ip \f(CW{}\fP win "" "Start new process in a window
2530.CW win
2531performs exactly the same function as
2532.CW new
2533but uses the window system to create a new window for the debugged process.
2534The variable
2535.CW progargs
2536supplies arguments to the new process.
2537The environment variable
2538.CW $8½srv
2539must be set to allow the interpreter to locate the mount channel for the
2540window system.
2541The window is created in the top left corner of the screen and is
2542400x600 pixels in size. The
2543.CW win
2544function may be modified to alter the geometry.
2545The window system will not be able to deliver notes in the new window
2546since the pid of the created process is not passed when the server is
2547mounted to create a new window.
2548.Ex
2549acid: win()
2550.Ee
2551