Martins ddd changes
[fw/sdcc] / debugger / README
1 sdcc/debugger
2
3 SDCDB debugger - Development notes.
4 ======================
5
6
7 ddd - Notes from Martin Helmling, April 28, 2003
8 ========================
9
10 To fullfill the requirements of 'ddd' ( Data Display Debugger ) to use it at
11 graphical frontend to 'sdcdb' some new commands must be implemented.
12
13 Now you can run the ddd with the commandline
14
15 ddd -debugger 'sdcdb -cpu 8032 <file>'
16
17 Following commands are additional implemented:
18
19 1. execution:
20
21 stepi - Step one instruction exactly using simulator 'step' command.
22 nexti - Step one instruction, but proceed through subroutine calls using simulator 'next' command.
23 break *<addr> - Set breakpoint at program address
24
25 log examples:
26 -> "stepi\n"
27 <- "Simulator stopped at Address 0x0051\n"
28    "\032\032x.c:31:1:beg:0x00000051\n"
29    "(sdcdb) "
30 -> "nexti\n"
31 <- "Simulator stopped at Address 0x0055\n"
32    "\032\032x.c:33:1:beg:0x00000055\n"
33    "(sdcdb) "
34
35 2. show machine code window:
36
37 x /i  - disassemble one asm command
38 x disassemble asm commands
39
40 log examples:
41 -> "x /i 0x00000055\n"
42 <- "0x00000055 <main+6>:\t\tmov\tr0,#_l\n"
43    "(sdcdb) "
44 -> "disassemble 0x00000055\n"
45 <- "Dump of assembler code for function main:\n"
46    "0x0000004f <main>:\t\tmov\tr0,#_ttt\n"
47    "0x00000051 <main+2>:\t\tmov\t@r0,#0x01\n"
48 <- "0x00000053 <main+4>:\t\tmov\tr2,#0x09\n"
49    "0x00000055 <main+6>:\t\tmov\tr0,#_l\n"
50 ...
51    "0x000000c1 <main+114>:\t\tret\n"
52    "End of assembler dump.\n"
53    "(sdcdb) "
54 -> "disassemble 0x40 0x45\n"
55 <- "Dump of assembler code from 0x00000040 to 0x00000045:\n"
56    "0x00000040 <subfunc1+8>:\t\tinc\tr0\n"
57    "0x00000041 <subfunc1+9>:\t\tmov\t@r0,ar3\n"
58    "0x00000043 <subfunc1+11>:\t\tmov\ta,r3\n"
59    "0x00000044 <subfunc1+12>:\t\trlc\ta\n"
60    "0x00000045 <subfunc1+13>:\t\tsubb\ta,acc\n"
61    "End of assembler dump.\n"
62    "(sdcdb) "
63 -> "break x.c:23\n"
64 <- "Breakpoint 1 at 0x4b: file x.c, line 23.\n"
65    "(sdcdb) "
66 -> "info breakpoints\n"
67 <- "Num Type           Disp Enb Address    What\n"
68    "1   breakpoint     keep y   0x0000004b at x.c:23\n"
69    "(sdcdb) "
70 ->
71
72 3. show registers and stack
73
74 frame - print information about the current Stack
75 where - print stack
76 up    - Select and print stack frame that called this one
77 down  - Select and print stack frame that called this one
78 info registers - show registers
79 info all-registers - show also special these function registers,
80                      which are symbolical defined.
81 help <cmd> show short help for ddd tooltip info
82 log examples:
83
84 -> "where\n"
85 <- "#0  0x0000004b in subfunc1 () at x.c:23\n"
86    "#1  0x0000004f in main () at x.c:31\n"
87    "(sdcdb) "
88 -> "frame\n"
89 <- "#0  0x0000004b in subfunc1 () at x.c:23\n"
90    "\032\032x.c:23:1:beg:0x0000004b\n"
91    "(sdcdb) "
92 -> "up\n"
93 <- "#1  0x0000004f in main () at x.c:31\n"
94    "\032\032x.c:31:1:beg:0x0000004f\n"
95    "(sdcdb) "
96 -> "frame\n"
97 <- "#1  0x0000004f in main () at x.c:31\n"
98    "\032\032x.c:31:1:beg:0x0000004f\n"
99    "(sdcdb) "
100 -> "down\n"
101 <- "#0  0x0000004b in subfunc1 () at x.c:23\n"
102    "\032\032x.c:23:1:beg:0x0000004b\n"
103    "(sdcdb) "
104 -> "frame\n"
105 <- "#0  0x0000004b in subfunc1 () at x.c:23\n"
106    "\032\032x.c:23:1:beg:0x0000004b\n"
107    "(sdcdb) "
108 -> "info registers\n"
109 <- "R0  : 0x87 135 .\n"
110    "R1  : 0x00 0 .\n"
111    "R2  : 0x05 5 .\n"
112    "R3  : 0x00 0 .\n"
113    "R4  : 0x00 0 .\n"
114    "R5  : 0x08 8 .\n"
115    "R6  : 0x00 0 .\n"
116    "ACC : 0x00 0 .\n"
117    "B   : 0x00 0 .\n"
118    "DPTR: 0x0005 5\n"
119    "@DPTR: 0x00 0 .\n"
120    "PSW  : 0x00 | CY : 0 | AC : 0 | OV : 0 | P : 0\n"
121    "(sdcdb) "
122 #  Showing all registers.
123 -> "info all-registers\n"
124 <- "R0  : 0x87 135 .\n"
125    "R1  : 0x00 0 .\n"
126    "R2  : 0x05 5 .\n"
127    "R3  : 0x00 0 .\n"
128    "R4  : 0x00 0 .\n"
129    "R5  : 0x08 8 .\n"
130    "R6  : 0x00 0 .\n"
131    "ACC : 0x00 0 .\n"
132    "B   : 0x00 0 .\n"
133    "DPTR: 0x0005 5\n"
134    "@DPTR: 0x00 0 .\n"
135    "PSW  : 0x00 | CY : 0 | AC : 0 | OV : 0 | P : 0\n"
136    "Special Function Registers:\n"
137 <- "P0=0xff P0_0=1 P0_1=1 P0_2=1 P0_3=1\n"
138    "SP=0x13\n"
139    "(sdcdb) "
140 -> "help step\n"
141 <- "Step program until it reaches a different source line.\n"
142    "(sdcdb) "
143 -> "help stepi\n"
144 <- "Step one instruction exactly.\n"
145    "(sdcdb) "
146
147 4. show and set variables:
148 output  [/<fmt>] <expr1> print value of <expr1> without newline for tooltip of ddd
149 print   [/<fmt>] <expr1> print value of <expr1> with newline
150 display [/<fmt>] <expr1> print value of <expr1> every time the program is stopped 
151                           or 'display' command without args is used.
152
153 set variable <expr1> = <value> 
154
155
156 <fmt> is the format for the basic type which is print out. 
157 If no format is set the default format is used.
158 format is set by ddd  'x' as hex 'o' as octal, 'd' as dezimal and 't' as binary.
159
160 <expr1> can by a variable , a array element , a structure element ( [] and . is parsed ).
161 indices of arrays may be constants or variables.
162 ( dereference of pointers and casting today not implemented !! )
163
164 log examples:
165 code:
166
167 typedef unsigned char uchar;
168 idata long  l;
169 typedef struct _txstate
170 {
171     uchar dstatus[4];
172     uchar avalue [8 ];
173 } txstate_t;
174
175 xdata txstate_t txstates[8];
176
177 -> "output l\n"
178 <- "5(sdcdb) "
179 -> "display l\n"
180 <- "(sdcdb) "
181 -> "display /x l\n"
182 <- "(sdcdb) "
183 -> "display\n"
184 <- "2: /x l = 0x00000005\n"
185    "1: l = 5\n"
186    "(sdcdb) "
187 -> "display /o l\n"
188 <- "(sdcdb) "
189 -> "display\n"
190 <- "3: /o l = 00000000005\n"
191    "2: /x l = 0x00000005\n"
192    "1: l = 5\n"
193    "(sdcdb) "
194 -> "display /d l\n"
195 <- "(sdcdb) "
196 -> "display\n"
197 <- "4: /d l = 5\n"
198    "3: /o l = 00000000005\n"
199    "2: /x l = 0x00000005\n"
200    "1: l = 5\n"
201    "(sdcdb) "
202 -> "display /t l\n"
203 <- "(sdcdb) "
204 -> "display\n"
205 <- "5: /t l = 00000000000000000000000000000101\n"
206    "4: /d l = 5\n"
207    "3: /o l = 00000000005\n"
208    "2: /x l = 0x00000005\n"
209    "1: l = 5\n"
210    "(sdcdb) "
211 -> "output l\n"
212 <- "5(sdcdb) "
213 -> "print txstates\n"
214 <- "$2 = { { dstatus = {\'\\1\',\'\\0\',\'\\0\',\'\\0\'},
215  avalue = {\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\'}},
216  { dstatus = {\'\\0\',\'\\2\',\'\\0\',\'\\0\'},
217  avalue = {\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\'}},
218  { dstatus = {\'\\0\',\'\\0\',\'\\375\',\'\\0\'},
219  avalue = {\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\'}},
220  { dstatus = {\'\\0\',\'\\0\',\'\\0\',\'\\0\'},
221  avalue = {\'\\0\',\'\\0\',\'\\0\',\'\\4\',\'\\0\',\'\\0\',\'\\0\',\
222 '\\0\'}}, { dstatus = {\'\\0\',\'\\0\',\'\\0\',\'\\0\'},
223  avalue = {\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\'}},
224  { dstatus = {\'\\0\',\'\\0\',\'\\0\',\'\\0\'},
225  avalue = {\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\'}},
226  { dstatus = {\'\\0\',\'\\0\',\'\\0\',\'\\0\'},
227  avalue = {\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\'}},
228  { dstatus = {\'\\0\',\'\\0\',\'\\0\',\'\\0\'},
229  avalue = {\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\0\',\'\\
230 0\'}}}\n"
231    "(sdcdb) "
232 -> "print txstates[1].dstatus[1]\n"
233 <- "$5 = \'\\2\'\n"
234    "(sdcdb) "
235 -> "print /x txstates[1].dstatus[1]\n"
236 <- "$7 = 0x02\n"
237 -> "output txstates[1].dstatus[1]\n"
238 <- "\'\\2\'(sdcdb) "
239 -> "set variable txstates[1].dstatus[1] = 0x10\n"
240 <- "(sdcdb) "
241 -> "print /t txstates[1].dstatus[1]\n"
242 <- "$9 = 00010000\n"
243 -> "print /x txstates[1].dstatus[1]\n"
244 <- "$10 = 0x10\n"
245
246 -> "output l\n"
247 <- "5(sdcdb) "
248 -> "set variable l = 2\n"
249 <- "(sdcdb) "
250 -> "print l\n"
251 <- "$15 = 2\n"
252 -> "print /x txstates[l].dstatus[2]\n"
253 <- "$16 = 0xfd\n"
254
255
256 ---------------------------------------------------
257
258 Implementation Notes:
259
260 IMEM and XMEM are cached to optimize the printaut of structures and arrays
261
262
263
264 Notes April 23, 2003
265 ========================
266 Martin Helmling added support for ddd GUI debugger.
267 Code added to display assembly, set variables, and other commands
268 to interface to ddd.
269
270 From Martins email:
271
272 in the attachment is my actual patch.
273
274 The 'up' and 'down' stack commands are not implemented.
275 But the features for 'ddd' to see bopth the c source and the assembler 
276 works,
277 also the display of variables.
278 Set variables are only implemented for basic types.
279 Register variables can also displayed, but here I think a problem in the 
280 *.cdb file exists.
281 Sometimes the register name not exists:
282
283 S:LcacheTxAnalogValue$offset$1$1({1}SC:U),E,0,0
284 S:LcacheTxAnalogValue$i$1$1({1}SC:S),R,0,0,[]
285 S:LcacheTxAnalogValue$val$1$1({4}ST__00020000:S),E,0,0
286 S:LcacheTxAnalogValue$value$1$1({1}SC:U),R,0,0,[]
287 S:LcacheTxAnalogValue$sloc0$1$0({4}SL:U),E,0,0
288 S:LsetRelays$addr$1$1({1}SC:U),R,0,0,[r2]
289 S:LsetRelays$state$1$1({1}SC:U),R,0,0,[r3]
290 S:LsetRelays$value$1$1({1}SC:U),R,0,0,[r5]
291 S:L_main$i$1$1({1}SC:U),R,0,0,[]
292 S:L_main$j$1$1({1}SC:U),R,0,0,[r3]
293 S:L_main$ok$1$1({1}SC:U),R,0,0,[r3]
294
295 the empty [] I mean !!
296
297 Is this a known bug  in sdcc ?
298
299 Also sometimes not the correct breakpoints for stepping found  in sdcdb. 
300 I try to fix this tomorrow.
301
302 Additional patches and notes from Martin(4-24-03):
303 in the attachment you found three patches:
304 debugpatch: Only for debugging, you can switch on and off the 4 different
305              debug and you can grep the debug by the prefix.
306 signalpatch:Now the simulator stops if the user sends an interrupt
307              ( or 'ddd' ) and also the simulator does not die by another
308              user signal.
309
310 sympatch:   This is not a feature, THIS is the fix of a BUG !!!
311              In some circumstances in the *.cdb file the Symbol record
312              comes before the function record. So the symbol was
313              generated TWO TIMES in the symbol list. The L:G and L:XG 
314
315              records then insert entry and exit address to the wrong
316              symbol. So before a new symbol is generated for the
317              function, a lookup in the symboltable is done.
318
319 Example of a *.cdb:
320
321 S:G$ZpuInitialize$0$0({2}DF,SV:S),C,0,0
322 S:G$ZpuInitialize$0$0({2}DF,SV:S),C,0,0
323 F:G$ZpuInitialize$0$0({2}DF,SV:S),Z,0,0,0,0,0
324 S:LZpuInitialize$i$1$1({2}SI:S),R,0,0,[]
325 L:G$ZpuInitialize$0$0:ADF
326 L:XG$ZpuInitialize$0$0:BBB
327
328
329 Notes Feb 10, 2002 - Karl Bongers
330 ========================
331
332 SDCDB is a debugger for SDCC compiler.  It works as a front
333 end to the ucSim simulator/disassembler program.
334
335 WARNING: SDCDB is EXPERIMENTAL and NOT A FULLY FUNCTIONING TOOL.
336
337 SDCDB does hold out promise for a nice debugger tool that
338 could integrate well with ucSim and other SDCC tools.  It has
339 some nice functionality that could augment ucSim nicely.
340 It reads in a lot of symbolic information from the .CDB
341 debug files as well as the source code and assembly
342 listings referred to in these CDB files.
343 This can be used to display your program data, list
344 c or asm source code and set breakpoints.
345
346 Unfortunately, SDCDB is not quite finished and does not
347 function well enough yet to recommend for general use.
348
349
350 Usage Notes
351 ======================
352 Compile with --debug option:
353 sdcc --debug hi.c
354 This should generate .cdb symbolic debug files.
355
356 Load sdcdb, like this:
357 sdcdb hi.ihx
358
359 Then you need to do an initial "run" command.  It should hopefully
360 stop somewhere at the main() entrypoint.
361
362 Now you should be able to list code, and set breakpoints.  Type
363 "c" to continue running to a breakpoint, or type "s" to single
364 step or "n" to skip function calls.  Tpye "help" for a summary
365 of commands.
366
367 Use a preceeding bang("!") to do s51 simulator commands.  So for
368 example:
369 !pc
370 will send the "pc" command to ucSim and
371 give you the program counter and list the current line of assembly.
372 !help
373 will give you the brief command line help listing of ucSim.  But
374 this help will scroll of the screen.  So to get a list, I redirect
375 stdout to a text file to examine like this:
376 sdcdb hi.ihx 2>&1 | tee sdcdb.log
377
378 Type '.' to toggle to a full time ucSim prompt.
379
380
381 SDCDB problem areas.
382 ======================
383
384 Ok, so what are some of its problems?  Lets try to itemize
385 them so we can fix them:
386
387 * It hangs sometimes when trying to do step or next.
388
389    This probably has something to do with the context
390    sdcdb tries to build dynamically.  Sdcdb tries to
391    trace program operation by placing hidden breakpoints
392    and stepping threw the simulation between these.
393
394 * Does not print variables properly.  Seems to use the
395   wrong address for the variable. (fixed)
396
397 * ASM listing not implemented.
398   This should be easy enough to add.  This feature
399   really needs to be implemented before this is a useful
400   tool.  That or addresses of code functions need to be
401   readily available for raw ucSim commands.
402
403 * No way to browse symbols, files names.
404   Preferably, it is easy to learn the addresses of variables
405   and functions too.
406   (working on this... see new ls,lm,lf commands)
407
408 * Does not allow listing, setting breakpoints or viewing variables
409   before starting the simulation.
410   (I am fixing now, partly fixed)
411
412 * p codestr - print on code char * broken, could be CDB info invalid.
413
414
415 Changes Made
416 ======================
417
418 Added a -z option.  Any options after -z on sdcdb invocation line
419  are passed directly to ucSim.
420
421 Fixed print basic variable command(was not parsing or expecting
422   "0x" in data coming from ucSim).  Dump bit command changed from
423   "db" to "dump".
424
425 Add support for the following alternative ucSim binaries:
426  -mz80 - use "uz80" simulator.
427  -mavr - use "uavr" simulator.
428  -mxa - use "uxa" simulator.
429
430   Note that Z80(and probably avr/xa) does not produce enough
431   CDB debugging information to
432   make SDCDB useful.  Hopefully CDB support will be
433   added to these other linkers and code generators.
434
435 Added lf,lm,ls commands(list functions, list modules, list symbols).
436  This allows browsing some the CDB information SDCDB pulls in
437  to do the wonderful things it does.
438
439 Added '.' prefix/command, used alone switches back and forth
440  from ucSim/SDCDB command mode).  Also when at the (ucsim) prompt,
441  if a command is prefixed with '.' it will route the command to
442  the SDCDB command processor.  (This could obsolete the '!' prefix).
443
444
445 ======================
446
447 Fix any remaining shortcomings above and make sdcdb integrate better with
448 ucSim.
449
450 Concerning the hanging on next/step instructions:  This is critical
451 to fix.  I beleive the problem is related to SDCDB's attempt to
452 dynamically track context.  To do this, it sets many temporary break
453 points and uses these to progress step by step through the source code.
454 It does not seem reliable.  It might be an idea to offer a simplified
455 mode of stepping and breakpoints.  Need to study this more.
456
457 Make SDCDB integrate better with ucSim.
458 I think if sdcdb can act more transparently in regard to ucSim
459 it would be used more.  So stress adding value and enhancing
460 ucSim operation, and not block or hide the functionality of ucSim.
461
462 * combine print "p" & "pt" together.  We have room on the screen,
463 might as well print its type, address and value in one command.
464
465 * "fr" command prints the current position in the C code.
466 Should also print the raw code address for dumping ucSim code.
467
468
469
470 Developer Notes:
471 ======================
472 To debug, it might be helpful to turn on verbose debug dumps
473 by uncommenting #define SDCDB_DEBUG in sdcdb.h.
474
475 Some basic layout:
476 sdcdb.c - main shell of the program.
477 cmd.c - process user commands typed in.
478 simi.c - handle talking to the simulator via a socket connection.
479 symtab.c - Misc. functions to translate and process linked list
480   structures(modules, functions, symbols, etc) and CDB file
481   parsing.
482 break.c - track and implement break points.
483
484 Understanding the structures and access mechanisms of SDCDB
485 is a bit of work.  The structures include generic linked list
486 and function access.  These lists are accessed in part by the
487 functions from ../../src/SDCCset.c and SDCChash.c.  See the
488 cmd.c:infoSymbols() routine to gain some insight into these
489 main program structures.  Or type "info symbols" to dump
490 out some of these.  The new ls,lm,lf commands are also based
491 on showing information about these lists of structures.
492
493 Generic functions are used to act on these linked lists of
494 structures.  For example, in cmd.c, DEFSETFUNC(funcWithName)
495 function will find a function structure with a given name.
496 So the linked list of function structures(with the head
497 "functions") is traversed and when one is found with a matching
498 name, a pointer to the function structure is returned.
499
500 The src/SDCCset.c,SDCCset.h contain much of the core helper
501 routines to allowed to implement these linked list access
502 functions.
503
504 The dynamic context tracking is difficult to understand.
505 It has some concept of stack use, as in
506 cmd.c:printSymValue(), where it can print variables relative
507 to the stack pointer.
508