Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / debugger / mcs51 / cmd.c
blob6b3c6700b4cb07b108a7aba5825d73eb2cd8a7ba
1 /*-------------------------------------------------------------------------
2 cmd.c - source file for debugger command execution
3 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
8 later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 In other words, you are welcome to use, share and improve this program.
20 You are forbidden to forbid anyone else to use, share and improve
21 what you give them. Help stamp out software-hoarding!
22 -------------------------------------------------------------------------*/
24 #include "sdcdb.h"
25 #undef DATADIR
26 #include "symtab.h"
27 #include "simi.h"
28 #include "break.h"
29 #include "cmd.h"
30 #include "newalloc.h"
32 /* default number of lines to list out */
33 #define LISTLINES 10
34 static int listlines = LISTLINES;
36 /* mainly used to retain a reference to the active module being
37 listed. May be used as a general context for other commands if
38 no better context is available */
39 static module *list_mod = NULL;
41 EXTERN_STACK_DCL(callStack,function *,1024)
43 #if defined(__APPLE__) && defined(__MACH__)
44 static char *copying=
45 {" GNU GENERAL PUBLIC LICENSE Version 2"};
46 static char *warranty=
47 {" NO WARRANTY"};
48 #else
49 static char *copying=
50 " GNU GENERAL PUBLIC LICENSE\n"
51 " Version 2, June 1991\n"
52 "\n"
53 " Copyright (C) 1989, 1991 Free Software Foundation, Inc.\n"
54 " 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA\n"
55 " Everyone is permitted to copy and distribute verbatim copies\n"
56 " of this license document, but changing it is not allowed.\n"
57 "\n"
58 " Preamble\n"
59 "\n"
60 " The licenses for most software are designed to take away your\n"
61 "freedom to share and change it. By contrast, the GNU General Public\n"
62 "License is intended to guarantee your freedom to share and change free\n"
63 "software--to make sure the software is free for all its users. This\n"
64 "General Public License applies to most of the Free Software\n"
65 "Foundation's software and to any other program whose authors commit to\n"
66 "using it. (Some other Free Software Foundation software is covered by\n"
67 "the GNU Library General Public License instead.) You can apply it to\n"
68 "your programs, too.\n"
69 "\n"
70 " When we speak of free software, we are referring to freedom, not\n"
71 "price. Our General Public Licenses are designed to make sure that you\n"
72 "have the freedom to distribute copies of free software (and charge for\n"
73 "this service if you wish), that you receive source code or can get it\n"
74 "if you want it, that you can change the software or use pieces of it\n"
75 "in new free programs; and that you know you can do these things.\n"
76 "\n"
77 " To protect your rights, we need to make restrictions that forbid\n"
78 "anyone to deny you these rights or to ask you to surrender the rights.\n"
79 "These restrictions translate to certain responsibilities for you if you\n"
80 "distribute copies of the software, or if you modify it.\n"
81 "\n"
82 " For example, if you distribute copies of such a program, whether\n"
83 "gratis or for a fee, you must give the recipients all the rights that\n"
84 "you have. You must make sure that they, too, receive or can get the\n"
85 "source code. And you must show them these terms so they know their\n"
86 "rights.\n"
87 "\n"
88 " We protect your rights with two steps: (1) copyright the software, and\n"
89 "(2) offer you this license which gives you legal permission to copy,\n"
90 "distribute and/or modify the software.\n"
91 "\n"
92 " Also, for each author's protection and ours, we want to make certain\n"
93 "that everyone understands that there is no warranty for this free\n"
94 "software. If the software is modified by someone else and passed on, we\n"
95 "want its recipients to know that what they have is not the original, so\n"
96 "that any problems introduced by others will not reflect on the original\n"
97 "authors' reputations.\n"
98 "\n"
99 " Finally, any free program is threatened constantly by software\n"
100 "patents. We wish to avoid the danger that redistributors of a free\n"
101 "program will individually obtain patent licenses, in effect making the\n"
102 "program proprietary. To prevent this, we have made it clear that any\n"
103 "patent must be licensed for everyone's free use or not licensed at all.\n"
104 "\n"
105 " The precise terms and conditions for copying, distribution and\n"
106 "modification follow.\n"
107 "^L\n"
108 " GNU GENERAL PUBLIC LICENSE\n"
109 " TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION\n"
110 "\n"
111 " 0. This License applies to any program or other work which contains\n"
112 "a notice placed by the copyright holder saying it may be distributed\n"
113 "under the terms of this General Public License. The \"Program\", below,\n"
114 "refers to any such program or work, and a \"work based on the Program\"\n"
115 "means either the Program or any derivative work under copyright law:\n"
116 "that is to say, a work containing the Program or a portion of it,\n"
117 "either verbatim or with modifications and/or translated into another\n"
118 "language. (Hereinafter, translation is included without limitation in\n"
119 "the term \"modification\".) Each licensee is addressed as \"you\".\n"
120 "\n"
121 "Activities other than copying, distribution and modification are not\n"
122 "covered by this License; they are outside its scope. The act of\n"
123 "running the Program is not restricted, and the output from the Program\n"
124 "is covered only if its contents constitute a work based on the\n"
125 "Program (independent of having been made by running the Program).\n"
126 "Whether that is true depends on what the Program does.\n"
127 "\n"
128 " 1. You may copy and distribute verbatim copies of the Program's\n"
129 "source code as you receive it, in any medium, provided that you\n"
130 "conspicuously and appropriately publish on each copy an appropriate\n"
131 "copyright notice and disclaimer of warranty; keep intact all the\n"
132 "notices that refer to this License and to the absence of any warranty;\n"
133 "and give any other recipients of the Program a copy of this License\n"
134 "along with the Program.\n"
135 "\n"
136 "You may charge a fee for the physical act of transferring a copy, and\n"
137 "you may at your option offer warranty protection in exchange for a fee.\n"
138 "\n"
139 " 2. You may modify your copy or copies of the Program or any portion\n"
140 "of it, thus forming a work based on the Program, and copy and\n"
141 "distribute such modifications or work under the terms of Section 1\n"
142 "above, provided that you also meet all of these conditions:\n"
143 "\n"
144 " a) You must cause the modified files to carry prominent notices\n"
145 " stating that you changed the files and the date of any change.\n"
146 "\n"
147 " b) You must cause any work that you distribute or publish, that in\n"
148 " whole or in part contains or is derived from the Program or any\n"
149 " part thereof, to be licensed as a whole at no charge to all third\n"
150 " parties under the terms of this License.\n"
151 "\n"
152 " c) If the modified program normally reads commands interactively\n"
153 " when run, you must cause it, when started running for such\n"
154 " interactive use in the most ordinary way, to print or display an\n"
155 " announcement including an appropriate copyright notice and a\n"
156 " notice that there is no warranty (or else, saying that you provide\n"
157 " a warranty) and that users may redistribute the program under\n"
158 " these conditions, and telling the user how to view a copy of this\n"
159 " License. (Exception: if the Program itself is interactive but\n"
160 " does not normally print such an announcement, your work based on\n"
161 " the Program is not required to print an announcement.)\n"
162 "\n"
163 "These requirements apply to the modified work as a whole. If\n"
164 "identifiable sections of that work are not derived from the Program,\n"
165 "and can be reasonably considered independent and separate works in\n"
166 "themselves, then this License, and its terms, do not apply to those\n"
167 "sections when you distribute them as separate works. But when you\n"
168 "distribute the same sections as part of a whole which is a work based\n"
169 "on the Program, the distribution of the whole must be on the terms of\n"
170 "this License, whose permissions for other licensees extend to the\n"
171 "entire whole, and thus to each and every part regardless of who wrote it.\n"
172 "\n"
173 "Thus, it is not the intent of this section to claim rights or contest\n"
174 "your rights to work written entirely by you; rather, the intent is to\n"
175 "exercise the right to control the distribution of derivative or\n"
176 "collective works based on the Program.\n"
177 "\n"
178 "In addition, mere aggregation of another work not based on the Program\n"
179 "with the Program (or with a work based on the Program) on a volume of\n"
180 "a storage or distribution medium does not bring the other work under\n"
181 "the scope of this License.\n"
182 "\n"
183 " 3. You may copy and distribute the Program (or a work based on it,\n"
184 "under Section 2) in object code or executable form under the terms of\n"
185 "Sections 1 and 2 above provided that you also do one of the following:\n"
186 "\n"
187 " a) Accompany it with the complete corresponding machine-readable\n"
188 " source code, which must be distributed under the terms of Sections\n"
189 " 1 and 2 above on a medium customarily used for software interchange; or,\n"
190 "\n"
191 " b) Accompany it with a written offer, valid for at least three\n"
192 " years, to give any third party, for a charge no more than your\n"
193 " cost of physically performing source distribution, a complete\n"
194 " machine-readable copy of the corresponding source code, to be\n"
195 " distributed under the terms of Sections 1 and 2 above on a medium\n"
196 " customarily used for software interchange; or,\n"
197 "\n"
198 " c) Accompany it with the information you received as to the offer\n"
199 " to distribute corresponding source code. (This alternative is\n"
200 " allowed only for noncommercial distribution and only if you\n"
201 " received the program in object code or executable form with such\n"
202 " an offer, in accord with Subsection b above.)\n"
203 "\n"
204 "The source code for a work means the preferred form of the work for\n"
205 "making modifications to it. For an executable work, complete source\n"
206 "code means all the source code for all modules it contains, plus any\n"
207 "associated interface definition files, plus the scripts used to\n"
208 "control compilation and installation of the executable. However, as a\n"
209 "special exception, the source code distributed need not include\n"
210 "anything that is normally distributed (in either source or binary\n"
211 "form) with the major components (compiler, kernel, and so on) of the\n"
212 "operating system on which the executable runs, unless that component\n"
213 "itself accompanies the executable.\n"
214 "\n"
215 "If distribution of executable or object code is made by offering\n"
216 "access to copy from a designated place, then offering equivalent\n"
217 "access to copy the source code from the same place counts as\n"
218 "distribution of the source code, even though third parties are not\n"
219 "compelled to copy the source along with the object code.\n"
220 "^L\n"
221 " 4. You may not copy, modify, sublicense, or distribute the Program\n"
222 "except as expressly provided under this License. Any attempt\n"
223 "otherwise to copy, modify, sublicense or distribute the Program is\n"
224 "void, and will automatically terminate your rights under this License.\n"
225 "However, parties who have received copies, or rights, from you under\n"
226 "this License will not have their licenses terminated so long as such\n"
227 "parties remain in full compliance.\n"
228 "\n"
229 " 5. You are not required to accept this License, since you have not\n"
230 "signed it. However, nothing else grants you permission to modify or\n"
231 "distribute the Program or its derivative works. These actions are\n"
232 "prohibited by law if you do not accept this License. Therefore, by\n"
233 "modifying or distributing the Program (or any work based on the\n"
234 "Program), you indicate your acceptance of this License to do so, and\n"
235 "all its terms and conditions for copying, distributing or modifying\n"
236 "the Program or works based on it.\n"
237 "\n"
238 " 6. Each time you redistribute the Program (or any work based on the\n"
239 "Program), the recipient automatically receives a license from the\n"
240 "original licensor to copy, distribute or modify the Program subject to\n"
241 "these terms and conditions. You may not impose any further\n"
242 "restrictions on the recipients' exercise of the rights granted herein.\n"
243 "You are not responsible for enforcing compliance by third parties to\n"
244 "this License.\n"
245 "\n"
246 " 7. If, as a consequence of a court judgment or allegation of patent\n"
247 "infringement or for any other reason (not limited to patent issues),\n"
248 "conditions are imposed on you (whether by court order, agreement or\n"
249 "otherwise) that contradict the conditions of this License, they do not\n"
250 "excuse you from the conditions of this License. If you cannot\n"
251 "distribute so as to satisfy simultaneously your obligations under this\n"
252 "License and any other pertinent obligations, then as a consequence you\n"
253 "may not distribute the Program at all. For example, if a patent\n"
254 "license would not permit royalty-free redistribution of the Program by\n"
255 "all those who receive copies directly or indirectly through you, then\n"
256 "the only way you could satisfy both it and this License would be to\n"
257 "refrain entirely from distribution of the Program.\n"
258 "\n"
259 "If any portion of this section is held invalid or unenforceable under\n"
260 "any particular circumstance, the balance of the section is intended to\n"
261 "apply and the section as a whole is intended to apply in other\n"
262 "circumstances.\n"
263 "\n"
264 "It is not the purpose of this section to induce you to infringe any\n"
265 "patents or other property right claims or to contest validity of any\n"
266 "such claims; this section has the sole purpose of protecting the\n"
267 "integrity of the free software distribution system, which is\n"
268 "implemented by public license practices. Many people have made\n"
269 "generous contributions to the wide range of software distributed\n"
270 "through that system in reliance on consistent application of that\n"
271 "system; it is up to the author/donor to decide if he or she is willing\n"
272 "to distribute software through any other system and a licensee cannot\n"
273 "impose that choice.\n"
274 "\n"
275 "This section is intended to make thoroughly clear what is believed to\n"
276 "be a consequence of the rest of this License.\n"
277 "\n"
278 " 8. If the distribution and/or use of the Program is restricted in\n"
279 "certain countries either by patents or by copyrighted interfaces, the\n"
280 "original copyright holder who places the Program under this License\n"
281 "may add an explicit geographical distribution limitation excluding\n"
282 "those countries, so that distribution is permitted only in or among\n"
283 "countries not thus excluded. In such case, this License incorporates\n"
284 "the limitation as if written in the body of this License.\n"
285 "\n"
286 " 9. The Free Software Foundation may publish revised and/or new versions\n"
287 "of the General Public License from time to time. Such new versions will\n"
288 "be similar in spirit to the present version, but may differ in detail to\n"
289 "address new problems or concerns.\n"
290 "\n"
291 "Each version is given a distinguishing version number. If the Program\n"
292 "specifies a version number of this License which applies to it and \"any\n"
293 "later version\", you have the option of following the terms and conditions\n"
294 "either of that version or of any later version published by the Free\n"
295 "Software Foundation. If the Program does not specify a version number of\n"
296 "this License, you may choose any version ever published by the Free Software\n"
297 "Foundation.\n"
298 "\n"
299 " 10. If you wish to incorporate parts of the Program into other free\n"
300 "programs whose distribution conditions are different, write to the author\n"
301 "to ask for permission. For software which is copyrighted by the Free\n"
302 "Software Foundation, write to the Free Software Foundation; we sometimes\n"
303 "make exceptions for this. Our decision will be guided by the two goals\n"
304 "of preserving the free status of all derivatives of our free software and\n"
305 "of promoting the sharing and reuse of software generally.\n";
307 static char *warranty=
308 " NO WARRANTY\n"
309 "\n"
310 " 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n"
311 "FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN\n"
312 "OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\n"
313 "PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\n"
314 "OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n"
315 "MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS\n"
316 "TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE\n"
317 "PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\n"
318 "REPAIR OR CORRECTION.\n"
319 "\n"
320 " 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n"
321 "WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\n"
322 "REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\n"
323 "INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\n"
324 "OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\n"
325 "TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\n"
326 "YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\n"
327 "PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\n"
328 "POSSIBILITY OF SUCH DAMAGES.\n";
329 #endif
331 static void printTypeInfo(st_link *);
332 static void printValAggregates (symbol *,st_link *,char,unsigned int,int);
333 static int printOrSetSymValue (symbol *sym, context *cctxt,
334 int flg, int dnum, int fmt,
335 char *rs, char *val, char cmp);
337 int srcMode = SRC_CMODE ;
338 set *dispsymbols = NULL ; /* set of displayable symbols */
339 static int currentFrame = 0; /* actual displayed frame */
340 /*-----------------------------------------------------------------*/
341 /* funcWithName - returns function with name */
342 /*-----------------------------------------------------------------*/
343 DEFSETFUNC(funcWithName)
345 function *func = item;
346 V_ARG(char *, name);
347 V_ARG(function **, funcp);
349 if (*funcp)
350 return 0;
352 if (strcmp(func->sym->name, name) == 0)
354 *funcp = func;
355 return 1;
358 return 0;
361 /*-----------------------------------------------------------------*/
362 /* symWithAddr - look for symbol with sfr / sbit address */
363 /*-----------------------------------------------------------------*/
364 DEFSETFUNC(symWithAddr)
366 symbol *sym = item;
367 V_ARG(unsigned long, laddr);
368 V_ARG(int, laddrspace);
369 V_ARG(symbol **, rsym);
371 if (*rsym)
372 return 0;
374 if (sym->addr == laddr && sym->addrspace == laddrspace)
376 *rsym = sym;
377 return 1;
380 return 0;
383 /*-----------------------------------------------------------------*/
384 /* setBPatModLine - set break point at the line specified for the */
385 /*-----------------------------------------------------------------*/
386 static void setBPatModLine (module *mod, int line, char bpType)
388 int next_line;
390 /* look for the first executable line after the line
391 specified & get the break point there */
393 if (line < 0)
394 return;
396 if(!mod)
398 fprintf(stderr, "Internal error: NULL module\n");
399 return;
402 if (srcMode == SRC_CMODE && line > mod->ncLines)
404 fprintf(stderr,"No line %d in file \"%s\".\n",
405 line,mod->c_name);
406 return ;
409 if (srcMode == SRC_AMODE && line > mod->nasmLines)
411 fprintf(stderr,"No line %d in file \"%s\".\n",
412 line,mod->asm_name);
413 return ;
416 next_line = line;
417 for ( ;
418 next_line < (srcMode == SRC_CMODE ? mod->ncLines : mod->nasmLines) ;
419 next_line++)
421 if (srcMode == SRC_CMODE)
423 if (mod->cLines[next_line]->addr != INT_MAX)
425 setBreakPoint (mod->cLines[next_line]->addr, CODE, bpType,
426 userBpCB, mod->c_name, next_line);
427 return;
428 // break;
431 else
433 if (mod->asmLines[next_line]->addr != INT_MAX)
435 setBreakPoint (mod->asmLines[next_line]->addr, CODE, bpType,
436 userBpCB, mod->asm_name, next_line);
437 return;
438 // break;
443 fprintf(stderr, "No line %d or after in file \"%s\"..\n",
444 line, mod->c_name);
446 return;
449 /*-----------------------------------------------------------------*/
450 /* clearBPatModLine - clr break point at the line specified */
451 /*-----------------------------------------------------------------*/
452 static void clearBPatModLine (module *mod, int line)
454 /* look for the first executable line after the line
455 specified & get the break point there */
456 if (srcMode == SRC_CMODE && line > mod->ncLines)
458 fprintf(stderr, "No line %d in file \"%s\".\n",
459 line, mod->c_name);
460 return;
463 if (srcMode == SRC_AMODE && line > mod->ncLines)
465 fprintf(stderr, "No line %d in file \"%s\".\n",
466 line, mod->c_name);
467 return;
470 for ( ;
471 line < (srcMode == SRC_CMODE ? mod->ncLines : mod->nasmLines) ;
472 line++)
474 if (srcMode == SRC_CMODE)
476 if (mod->cLines[line]->addr)
478 clearUSERbp (mod->cLines[line]->addr);
479 break;
482 else
484 if (mod->asmLines[line]->addr)
486 clearUSERbp (mod->asmLines[line]->addr);
487 break;
492 return;
495 /*-----------------------------------------------------------------*/
496 /* moduleLineWithAddr - finds and returns a line with a given address */
497 /*-----------------------------------------------------------------*/
498 DEFSETFUNC(moduleLineWithAddr)
500 module *mod = item;
501 int i;
503 V_ARG(unsigned int, addr);
504 V_ARG(module **, rmod);
505 V_ARG(int *, line);
507 if (*rmod)
508 return 0;
510 for (i = 0; i < mod->nasmLines; i++)
512 if (mod->asmLines[i]->addr == addr)
514 *rmod = mod;
515 if (line)
517 *line = 0;
518 for (i = 0; i < mod->ncLines; i++)
520 if (mod->cLines[i]->addr > addr)
521 break;
522 *line = i;
524 return 1;
529 return 0;
532 /*-----------------------------------------------------------------*/
533 /* funcWithNameModule - returns functions with a name module combo */
534 /*-----------------------------------------------------------------*/
535 DEFSETFUNC(funcWithNameModule)
537 function *func = item;
538 V_ARG(char *, fname);
539 V_ARG(char *, mname);
540 V_ARG(function **, funcp);
542 if (*funcp)
543 return 0;
545 if (strcmp(func->sym->name, fname) == 0 &&
546 strcmp(func->mod->c_name, mname) == 0)
548 *funcp = func;
549 return 1;
552 return 0;
555 /*-----------------------------------------------------------------*/
556 /* funcInAddr - given an address returns the function */
557 /*-----------------------------------------------------------------*/
558 DEFSETFUNC(funcInAddr)
560 function *func = item;
561 V_ARG(unsigned int, addr);
562 V_ARG(function **, funcp);
564 if (*funcp)
565 return 0;
567 /* in the address range */
568 if (func->sym->addr <= addr && func->sym->eaddr >= addr)
570 *funcp = func;
571 return 1;
574 return 0;
577 /*-----------------------------------------------------------------*/
578 /* setStepBp - will set STEP Bp @ function entry points */
579 /*-----------------------------------------------------------------*/
580 DEFSETFUNC(setStepBp)
582 function *func = item;
584 if (func->sym && func->sym->addr)
586 /* set the entry break point */
587 setBreakPoint (func->sym->addr , CODE , STEP ,
588 stepBpCB ,func->mod->c_name , func->entryline);
590 return 1;
593 return 0;
596 /*-----------------------------------------------------------------*/
597 /* setStepEPBp - sets a given type of bp @ the execution point */
598 /*-----------------------------------------------------------------*/
599 DEFSETFUNC(setStepEPBp)
601 exePoint *ep = item;
602 V_ARG(int, bptype);
603 V_ARG(char *, mname);
605 setBreakPoint (ep->addr, CODE, bptype, stepBpCB, mname, ep->line);
606 return 1;
609 /*-----------------------------------------------------------------*/
610 /* setNextEPBp - sets a given type of bp @ the execution point */
611 /*-----------------------------------------------------------------*/
612 DEFSETFUNC(setNextEPBp)
614 exePoint *ep = item;
615 V_ARG(int, bptype);
616 V_ARG(char *, mname);
618 setBreakPoint (ep->addr, CODE, bptype, nextBpCB, mname, ep->line);
619 return 1;
622 /*-----------------------------------------------------------------*/
623 /* lineAtAddr - for execution points returns the one with addr */
624 /*-----------------------------------------------------------------*/
625 DEFSETFUNC(lineAtAddr)
627 exePoint *ep = item;
628 V_ARG(unsigned int, addr);
629 V_ARG(int *, line);
630 V_ARG(int *, block);
631 V_ARG(int *, level);
633 /* address must be an exact match */
634 if (ep->addr == addr)
636 *line = ep->line;
637 if (block)
638 *block = ep->block;
639 if (level)
640 *level = ep->level;
641 return 1;
644 return 0;
647 /*-----------------------------------------------------------------*/
648 /* lineNearAddr - for execution points returns the one with addr */
649 /*-----------------------------------------------------------------*/
650 DEFSETFUNC(lineNearAddr)
652 exePoint *ep = item;
653 V_ARG(unsigned int, addr);
654 V_ARG(int *, line);
655 V_ARG(int *, block);
656 V_ARG(int *, level);
658 /* the line in which the address is */
659 if (ep->addr <= addr)
661 *line = ep->line;
662 if (block)
663 *block = ep->block;
664 if (level)
665 *level = ep->level;
666 return 1;
669 return 0;
672 /*-----------------------------------------------------------------*/
673 /* discoverContext - find out the current context of the bp */
674 /*-----------------------------------------------------------------*/
675 context *discoverContext (unsigned addr, function *func)
677 module *mod = NULL;
678 int line = 0;
680 /* find the function we are in */
681 if (!func && !applyToSet(functions, funcInAddr, addr, &func))
683 if (!applyToSet(functions, funcWithName, "_main", &func) ||
684 !applyToSet(modules, moduleLineWithAddr, addr, &mod, NULL))
686 fprintf (stderr, "addr 0x%x in no module/function (runtime env?)\n",addr);
687 return NULL;
689 currCtxt->func = func;
690 currCtxt->addr = addr;
691 currCtxt->modName = mod->name;
692 currCtxt->cline = func->exitline;
694 else
696 currCtxt->func = func;
697 currCtxt->addr = func->laddr = addr;
698 currCtxt->modName = func->modName;
700 /* find the c line number */
701 if (applyToSet(func->cfpoints, lineAtAddr, addr, &line,
702 &currCtxt->block, &currCtxt->level))
703 currCtxt->cline = func->lline = line;
704 else if (applyToSet(func->cfpoints, lineNearAddr, addr, &line,
705 &currCtxt->block, &currCtxt->level))
706 currCtxt->cline = func->lline = line;
707 else
708 currCtxt->cline = -1;
710 /* find the asm line number */
711 line = 0;
712 if (applyToSet(func->afpoints, lineAtAddr, addr, &line, NULL, NULL))
713 currCtxt->asmline = line;
714 else
715 currCtxt->asmline = -1;
717 return currCtxt;
720 /*-----------------------------------------------------------------*/
721 /* simGo - send 'go' cmd to simulator and wait till a break occurs */
722 /*-----------------------------------------------------------------*/
723 void simGo (unsigned int gaddr)
725 unsigned int addr ;
726 context *ctxt;
727 int rv;
728 stopCommandList();
730 top:
731 if ( userinterrupt )
733 userinterrupt = 0;
734 return;
736 if ( gaddr == 0 )
738 function *func = NULL;
739 if (applyToSet(functions, funcInAddr, gaddr, &func))
740 STACK_PUSH(callStack, func);
742 addr = simGoTillBp (gaddr);
744 /* got the pc for the break point now first
745 discover the program context i.e. module, function
746 linenumber of the source etc, etc etc */
747 currentFrame = 0;
748 ctxt = discoverContext (addr, NULL);
750 /* dispatch all the break point call back functions */
751 rv = dispatchCB (addr, ctxt);
753 /* the dispatch call back function will return
754 non-zero if an user break point has been hit
755 if not then we continue with the execution
756 of the program */
757 if (!rv)
759 if ( gaddr == 0 )
760 gaddr = -1;
761 if ( gaddr == -1 || doingSteps == 1 )
762 goto top ;
766 /*-----------------------------------------------------------------*/
767 /* preparePrint - common parse function for set variable, */
768 /* output, print and display */
769 /*-----------------------------------------------------------------*/
770 static char *preparePrint (char *s, context *cctxt, int *fmt, symbol **sym)
772 char *bp;
773 char save_ch;
775 *fmt = FMT_NON;
776 *sym = NULL;
778 s = trim(s);
779 if (!*s)
780 return (char *)0;
782 if (*s == '/')
784 /* format of printout */
785 switch (*++s)
787 case 'x':
788 *fmt = FMT_HEX;
789 break;
790 case 'o':
791 *fmt = FMT_OCT;
792 break;
793 default:
794 case 'd':
795 *fmt = FMT_DEZ;
796 break;
797 case 't':
798 *fmt = FMT_BIN;
799 break;
801 s++;
802 s = trim_left(s);
804 for ( bp = s; *bp && ( isalnum( *bp ) || *bp == '_' || *bp == '$'); bp++ );
805 save_ch = *bp;
806 if ( *bp )
807 *bp = '\0';
809 if ( *s )
810 *sym = symLookup(s, cctxt);
811 *bp = save_ch;
813 if ( ! *sym )
814 fprintf(stdout,"No symbol \"%s\" in current context.\n", s);
815 return bp;
818 static int printAsmLine (function *func, module *m, unsigned saddr, unsigned eaddr)
820 int i, j, delta;
821 unsigned symaddr;
822 unsigned lastaddr = saddr+1;
823 char *symname;
825 if ( func )
827 symaddr = func->sym->addr;
828 symname = func->sym->name;
830 else
832 symaddr = saddr;
833 symname = "";
835 for (j=0, i=0; i < m->nasmLines; i++)
837 if (saddr >= 0 && m->asmLines[i]->addr < saddr)
839 continue;
841 if (eaddr >= 0 && m->asmLines[i]->addr > eaddr)
843 continue;
845 if (func &&
846 (m->asmLines[i]->addr < func->sym->addr ||
847 m->asmLines[i]->addr > func->sym->eaddr))
849 continue;
851 delta = m->asmLines[i]->addr - symaddr;
852 if ( delta >= 0 )
854 j++;
855 lastaddr = m->asmLines[i]->addr;
856 printf("0x%08x <%s", lastaddr, symname);
857 if (delta > 0)
858 printf("+%d", delta);
859 printf(">:\t%s", m->asmLines[i]->src);
862 return lastaddr;
865 /*-----------------------------------------------------------------*/
866 /* cmdDisasm - disassemble asm instruction */
867 /*-----------------------------------------------------------------*/
868 static int cmdDisasm (char *s, context *cctxt, int args)
870 function *func = NULL;
871 long saddr = -1;
872 long eaddr = -1;
873 int found = 0;
874 module *modul;
875 /* white space skip */
877 if ( args > 0 )
879 s = trim_left(s);
881 if (isdigit(*s))
883 saddr = strtol(s, &s, 0);
884 if ( args > 1 )
886 s = trim_left(s);
888 if (isdigit(*s))
889 eaddr = strtol(s, 0, 0);
891 else
893 eaddr = saddr+1;
898 if ( eaddr == -1 )
900 /* no start or only start so dump function */
901 if ( saddr == -1 )
903 func = cctxt->func;
905 else
907 applyToSet(functions, funcInAddr, saddr, &func);
909 if ( func )
911 printf("Dump of assembler code for function %s:\n", func->sym->name);
912 printAsmLine(func, func->mod, -1, -1);
913 printf("End of assembler dump.\n");
914 return 0;
916 else
918 if (applyToSet(modules, moduleLineWithAddr, saddr, &modul, NULL))
920 eaddr = saddr+5;
921 printf("Dump of assembler code:\n");
922 printAsmLine(NULL, modul, saddr, eaddr);
923 printf("End of assembler dump.\n");
924 return 0;
928 else
930 if ( args > 1 )
931 printf("Dump of assembler code from 0x%08lx to 0x%08lx:\n", saddr, eaddr);
932 found = 0;
933 while ( saddr < eaddr )
935 func = NULL;
936 if (applyToSet(functions, funcInAddr, saddr, &func))
938 found = 1;
939 modul = func->mod;
941 else
943 if ( found )
944 break;
945 if (!applyToSet(modules, moduleLineWithAddr, saddr, &modul, NULL))
946 break;
948 saddr = printAsmLine(func, modul, saddr, eaddr) + 1;
950 if (saddr >= eaddr)
952 if ( args > 1 )
953 printf("End of assembler dump.\n");
954 return 0;
957 fprintf(stderr,"No function contains specified address.\n");
958 if (saddr >= 0)
960 char lbuf[64];
961 sprintf(lbuf, "dis 0x%lx 0 %ld\n", saddr, (eaddr == -1) ? 1L : eaddr-saddr);
962 sendSim(lbuf);
963 waitForSim(1000, NULL);
964 fputs(simResponse(), stdout);
966 return 0;
968 /*-----------------------------------------------------------------*/
969 /* cmdDisasm1 - disassemble one asm instruction */
970 /*-----------------------------------------------------------------*/
971 int cmdDisasm1 (char *s, context *cctxt)
973 return cmdDisasm(s, cctxt, 1);
976 /*-----------------------------------------------------------------*/
977 /* cmdDisasmF - disassemble asm instructions */
978 /*-----------------------------------------------------------------*/
979 int cmdDisasmF(char *s, context *cctxt)
981 return cmdDisasm(s, cctxt, 2);
984 static int commonSetUserBp(char *s, context *cctxt, char bpType)
986 char *bp ;
987 function *func = NULL;
989 /* user break point location specification can be of the following
990 forms
991 a) <nothing> - break point at current location
992 b) lineno - number of the current module
993 c) filename:lineno - line number of the given file
994 e) filename:function- function X in file Y (useful for static functions)
995 f) function - function entry point
996 g) *addr - break point at address
999 if (!cctxt)
1001 fprintf (stdout, "No symbol table is loaded. Use the \"file\" command.\n");
1002 return 0;
1004 /* trim left and right */
1005 s = trim(s);
1007 /* case a) nothing */
1008 /* if nothing given then current location : we know
1009 the current execution location from the currentContext */
1010 if (! *s )
1012 /* if current context is known */
1013 if (cctxt->func)
1015 Dprintf (D_break, ("commonSetUserBp: a) cctxtaddr:%x \n", cctxt->addr));
1016 if (srcMode == SRC_CMODE)
1018 /* set the break point */
1019 setBreakPoint (cctxt->addr, CODE, bpType, userBpCB,
1020 cctxt->func->mod->c_name, cctxt->cline);
1022 else
1024 setBreakPoint (cctxt->addr, CODE, bpType, userBpCB,
1025 cctxt->func->mod->asm_name, cctxt->asmline);
1028 else
1030 fprintf(stderr, "No default breakpoint address now.\n");
1033 goto ret;
1035 /* case g) *addr */
1036 if (*s == '*' && isdigit(*(s+1)))
1038 int line = 0;
1039 long braddr = strtol(s+1, 0, 0);
1040 if (!applyToSet (functions, funcInAddr, braddr, &func))
1042 module *modul;
1043 if (!applyToSet (modules, moduleLineWithAddr, braddr, &modul, &line))
1045 fprintf (stderr, "Address 0x%08lx not exists in code.\n", braddr);
1047 else
1049 Dprintf (D_break, ("commonSetUserBp: g) addr:%lx \n", braddr));
1050 setBreakPoint (braddr, CODE, bpType, userBpCB, modul->c_name, line);
1052 goto ret;
1054 else
1056 int line = func->exitline;
1057 if (!applyToSet (func->cfpoints, lineAtAddr, braddr, &line, NULL, NULL))
1059 applyToSet (func->cfpoints, lineNearAddr, braddr, &line, NULL, NULL);
1061 setBreakPoint (braddr, CODE, bpType, userBpCB, func->mod->c_name, line);
1063 goto ret;
1065 /* case b) lineno */
1066 /* check if line number */
1067 if (!strchr(s, ':') && isdigit(*s))
1069 /* get the lineno */
1070 int line = atoi(s) -1;
1071 Dprintf (D_break, ("commonSetUserBp: b) line:%d \n", line));
1072 if (line < 0)
1074 fprintf(stdout, "linenumber <= 0\n");
1075 goto ret;
1077 /* if current context not present then we must get the module
1078 which has main & set the break point @ line number provided
1079 of that module : if current context known then set the bp
1080 at the line number given for the current module
1082 if (cctxt->func)
1084 if (!cctxt->func->mod)
1086 if (!applyToSet (functions, funcWithName, "main", &func))
1088 fprintf (stderr, "Function \"main\" not defined.\n");
1090 else
1092 setBPatModLine (func->mod, line, bpType);
1095 else
1097 setBPatModLine(cctxt->func->mod,line, bpType);
1100 else
1102 if (list_mod)
1104 setBPatModLine (list_mod, line, bpType);
1106 else
1108 fprintf (stdout, "Sdcdb fails to have module symbol context at %d\n", __LINE__);
1112 goto ret;
1115 if ((bp = strchr(s, ':')))
1117 module *mod = NULL;
1118 *bp = '\0';
1120 if (srcMode == SRC_CMODE)
1122 if (!applyToSet (modules, moduleWithCName, s, &mod))
1124 fprintf (stderr, "No source file named %s.\n", s);
1125 goto ret;
1128 else
1130 if (!applyToSet (modules, moduleWithAsmName, s, &mod))
1132 fprintf (stderr, "No source file named %s.\n", s);
1133 goto ret;
1137 /* case c) filename:lineno */
1138 if (isdigit(*(bp+1)))
1140 Dprintf (D_break, ("commonSetUserBp: c) line:%d \n", atoi(bp+1)));
1141 setBPatModLine (mod, atoi(bp+1)-1, bpType);
1142 goto ret;
1144 /* case d) filename:function */
1145 if (!applyToSet (functions, funcWithNameModule, bp+1, s, &func))
1147 fprintf(stderr, "Function \"%s\" not defined.\n", bp+1);
1149 else
1151 Dprintf (D_break, ("commonSetUserBp: d) \n"));
1152 setBPatModLine (mod,
1153 (srcMode == SRC_CMODE ?
1154 func->entryline :
1155 func->aentryline), bpType);
1157 goto ret;
1160 /* case e) function */
1161 Dprintf (D_break, ("commonSetUserBp: e) \n"));
1162 if (!applyToSet (functions, funcWithName, s, &func))
1164 fprintf(stderr, "Function \"%s\" not defined.\n", s);
1166 else
1168 setBPatModLine (func->mod,
1169 (srcMode == SRC_CMODE ?
1170 func->entryline :
1171 func->aentryline), bpType);
1174 ret:
1175 return 0;
1178 /*-----------------------------------------------------------------*/
1179 /* cmdSetTmpUserBp - settempory break point at the user specified location */
1180 /*-----------------------------------------------------------------*/
1181 int cmdSetTmpUserBp (char *s, context *cctxt)
1183 return commonSetUserBp (s, cctxt, TMPUSER);
1186 /*-----------------------------------------------------------------*/
1187 /* cmdSetUserBp - set break point at the user specified location */
1188 /*-----------------------------------------------------------------*/
1189 int cmdSetUserBp (char *s, context *cctxt)
1191 return commonSetUserBp (s, cctxt, USER);
1194 /*-----------------------------------------------------------------*/
1195 /* cmdJump - set program counter */
1196 /*-----------------------------------------------------------------*/
1197 int cmdJump (char *s, context *cctxt)
1199 char *bp;
1200 if (STACK_EMPTY(callStack))
1202 fprintf (stdout, "The program is not running.\n");
1203 return 0;
1206 /* trim left and right */
1207 s = trim(s);
1209 if (! *s)
1211 fprintf (stdout, "No argument: need line or *addr.\n");
1212 return 0;
1214 if (*s == '*' && isdigit(*(s+1)))
1216 unsigned int addr = atoi(s);
1217 if (cctxt && cctxt->func &&
1218 cctxt->func->sym->addr <= addr &&
1219 cctxt->func->sym->eaddr >= addr)
1221 simSetPC (addr);
1222 return 0;
1224 fprintf (stdout, "Warning addr 0x%x outside actual function.\n", addr);
1225 simSetPC (addr);
1226 return 0;
1228 if (isdigit(*s))
1230 /* get the lineno */
1231 int line = atoi(s) -1;
1232 if (!cctxt || !cctxt->func || !cctxt->func->mod)
1234 fprintf (stderr, "Function not defined.\n");
1235 return 0;
1237 if (line >= cctxt->func->entryline &&
1238 line <= cctxt->func->exitline )
1240 simSetPC (cctxt->func->mod->cLines[line]->addr);
1241 return 0;
1243 if (line >= cctxt->func->mod->ncLines )
1245 fprintf (stderr, "line not in module.\n");
1246 return 0;
1248 fprintf (stdout, "Warning line %d outside actual function.\n", line+1);
1249 simSetPC (cctxt->func->mod->cLines[line]->addr);
1250 return 0;
1252 if ((bp = strchr(s,':')))
1254 int line;
1255 module *mod = NULL;
1256 *bp++ = '\0';
1257 if (!applyToSet (modules, moduleWithCName, s, &mod))
1259 fprintf (stderr, "No source file named %s.\n", s);
1260 return 0;
1262 if (!isdigit(*bp))
1264 fprintf (stderr, "No line number.\n");
1265 return 0;
1267 line = atoi(bp) -1;
1268 if (line >= mod->ncLines )
1270 fprintf (stderr, "line not in module.\n");
1271 return 0;
1273 if ( mod != cctxt->func->mod ||
1274 line < cctxt->func->entryline ||
1275 line > cctxt->func->exitline )
1277 fprintf (stdout, "Warning line %d outside actual function.\n", line+1);
1279 simSetPC (mod->cLines[line]->addr);
1281 return 0;
1284 /*-----------------------------------------------------------------*/
1285 /* cmdListAsm - list assembler source code */
1286 /*-----------------------------------------------------------------*/
1287 int cmdListAsm (char *s, context *cctxt)
1289 if (cctxt && cctxt->func)
1291 /* actual line */
1292 if (cctxt->addr != INT_MAX)
1294 if (printAsmLine (cctxt->func, cctxt->func->mod,
1295 cctxt->addr, cctxt->addr))
1296 return 0;
1299 return 0;
1302 /*-----------------------------------------------------------------*/
1303 /* cmdSetOption - set debugger options */
1304 /*-----------------------------------------------------------------*/
1305 int cmdSetOption (char *s, context *cctxt)
1307 s = trim_left(s);
1308 if (strncmp (s, "srcmode", 7) == 0)
1310 if (srcMode == SRC_CMODE)
1311 srcMode = SRC_AMODE;
1312 else
1313 srcMode = SRC_CMODE;
1314 fprintf (stderr,"source mode set to '%s'\n",
1315 (srcMode == SRC_CMODE ? "C" : "asm"));
1316 return 0;
1319 if (strncmp (s, "listsize ", 9) == 0)
1321 listlines = strtol(s+9, 0, 0);
1322 if (listlines < LISTLINES)
1323 listlines = LISTLINES;
1324 return 0;
1327 #ifdef SDCDB_DEBUG
1328 if (strncmp(s,"debug ",6) == 0)
1330 sdcdbDebug = strtol(s+6, 0, 0);
1331 return 0;
1333 #endif
1334 if (strncmp (s, "variable ", 9) == 0)
1336 symbol *sym ;
1337 int fmt;
1338 char *rs;
1339 s += 9;
1340 if (!(rs = preparePrint (s, cctxt, &fmt, &sym)))
1341 return 0;
1342 s = rs;
1343 while (*s && *s != '=')
1344 s++;
1345 *s++ = '\0';
1346 s = trim_left(s);
1347 if (*s && sym)
1349 printOrSetSymValue (sym, cctxt, 0, 0, 0, rs, s, '\0');
1350 return 0;
1352 else
1354 fprintf (stdout, "No new value for \"%s\".\n", s);
1356 return 0;
1359 fprintf (stderr, "'set %s' command not yet implemented\n", s);
1360 return 0;
1363 /*-----------------------------------------------------------------*/
1364 /* cmdContinue - continue till next break point */
1365 /*-----------------------------------------------------------------*/
1366 int cmdContinue (char *s, context *cctxt)
1368 if (STACK_EMPTY(callStack))
1370 fprintf(stdout, "The program is not being run.\n");
1371 return 0;
1374 fprintf(stdout, "Continuing.\n");
1375 simGo(-1);
1376 showfull = 1;
1377 return 0;
1380 /*-----------------------------------------------------------------*/
1381 /* cmdIgnore - set ignorecount for breakpoint */
1382 /*-----------------------------------------------------------------*/
1383 int cmdIgnore (char *s, context *cctxt)
1385 int bpnum, cnt ;
1386 s = trim_left(s);
1387 if (!*s )
1389 fprintf(stdout, "Argument required (breakpoint number).\n");
1390 return 0;
1392 bpnum = strtol (s, &s, 10);
1393 s = trim_left(s);
1394 if (!*s )
1396 fprintf (stdout, "Second argument (specified ignore-count) is missing.");
1397 return 0;
1399 cnt = strtol (s, 0, 10);
1400 setUserbpIgnCount (bpnum, cnt);
1401 return 0;
1404 /*-----------------------------------------------------------------*/
1405 /* cmdCondition - set condition for breakpoint */
1406 /*-----------------------------------------------------------------*/
1407 int cmdCondition (char *s, context *cctxt)
1409 int bpnum ;
1410 s = trim_left(s);
1411 if (!*s )
1413 fprintf (stdout, "Argument required (breakpoint number).\n");
1414 return 0;
1416 bpnum = strtol (s, &s, 10);
1417 s = trim_left(s);
1418 if (*s)
1419 s = Safe_strdup(s);
1420 else
1421 s = NULL;
1422 setUserbpCondition (bpnum, s);
1423 return 0;
1426 /*-----------------------------------------------------------------*/
1427 /* cmdCommands - set commands for breakpoint */
1428 /*-----------------------------------------------------------------*/
1429 int cmdCommands (char *s, context *cctxt)
1431 int bpnum ;
1432 char *cmds, *line;
1433 s = trim_left(s);
1435 if (!*s )
1436 bpnum = getLastBreakptNumber();
1437 else
1438 bpnum = strtol (s, 0, 10);
1440 cmds = NULL;
1441 while ((line = getNextCmdLine()))
1443 line = trim_left (line);
1444 if (!strncmp (line, "end", 3))
1445 break;
1446 if (! cmds )
1448 cmds = Safe_strdup (line);
1450 else
1452 cmds = Safe_realloc (cmds, strlen(cmds) + 1 + strlen(line));
1453 strcat (cmds, line);
1456 setUserbpCommand (bpnum, cmds);
1457 return 0;
1460 /*-----------------------------------------------------------------*/
1461 /* cmdDelUserBp - delete user break point */
1462 /*-----------------------------------------------------------------*/
1463 int cmdDelUserBp (char *s, context *cctxt)
1465 int bpnum ;
1466 s = trim_left(s);
1468 if (!*s )
1470 if (userBpPresent)
1472 char buffer[10];
1473 fprintf (stdout, "Delete all breakpoints? (y or n) ");
1474 fflush (stdout);
1475 if (fgets (buffer, sizeof(buffer), stdin) && toupper(buffer[0]) == 'Y')
1476 deleteUSERbp (-1);
1478 return 0;
1481 /* determine the break point number */
1482 if (sscanf (s, "%d",&bpnum) == 1)
1483 deleteUSERbp (bpnum);
1485 return 0;
1488 /*-----------------------------------------------------------------*/
1489 /* cmdStepi - single step exactly one instruction */
1490 /*-----------------------------------------------------------------*/
1491 int cmdStepi (char *s, context *cctxt)
1493 if (0 /*STACK_EMPTY(callStack)*/)
1495 fprintf (stdout, "The program is not being run.\n");
1497 else
1499 doingSteps = 2;
1500 simGo(2);
1501 doingSteps = 0;
1502 showfull = 1;
1504 return 0;
1507 /*-----------------------------------------------------------------*/
1508 /* cmdStep - single step thru C source file */
1509 /*-----------------------------------------------------------------*/
1510 int cmdStep (char *s, context *cctxt)
1512 function *func = NULL;
1514 if (STACK_EMPTY (callStack))
1516 fprintf (stdout, "The program is not being run.\n");
1518 else
1520 /* if we are @ the end of a function then set
1521 break points at execution points of the
1522 function in the call stack... */
1523 if (cctxt->addr == cctxt->func->sym->eaddr)
1525 if ((func = STACK_PEEK (callStack)))
1527 if (srcMode == SRC_CMODE)
1528 applyToSet (func->cfpoints, setStepEPBp, STEP,
1529 func->mod->c_name);
1530 else
1531 applyToSet (func->afpoints, setStepEPBp, STEP,
1532 func->mod->asm_name);
1535 else
1537 /* set breakpoints at all function entry points
1538 and all exepoints of this functions & for
1539 all functions one up in the call stack */
1541 /* all function entry points */
1542 applyToSet (functions, setStepBp);
1544 if (srcMode == SRC_CMODE)
1546 /* for all execution points in this function */
1547 applyToSet (cctxt->func->cfpoints, setStepEPBp, STEP,
1548 cctxt->func->mod->c_name);
1550 /* set a break point @ the current function's
1551 exit */
1552 setBreakPoint (cctxt->func->sym->eaddr, CODE, STEP ,
1553 stepBpCB, cctxt->func->mod->c_name,
1554 cctxt->func->exitline);
1556 /* now break point @ callers execution points */
1557 if ((func = STACK_PPEEK (callStack)))
1559 applyToSet (func->cfpoints, setStepEPBp, STEP,
1560 func->mod->c_name);
1561 /* set bp @ callers exit point */
1562 setBreakPoint (func->sym->eaddr, CODE, STEP ,
1563 stepBpCB, func->mod->c_name,
1564 func->exitline);
1567 else
1569 /* for all execution points in this function */
1570 applyToSet (cctxt->func->afpoints, setStepEPBp, STEP,
1571 cctxt->func->mod->asm_name);
1573 /* set a break point @ the current function's
1574 exit */
1575 setBreakPoint (cctxt->func->sym->eaddr, CODE, STEP ,
1576 stepBpCB, cctxt->func->mod->asm_name,
1577 cctxt->func->aexitline);
1579 /* now break point @ callers execution points */
1580 if ((func = STACK_PPEEK (callStack)))
1582 applyToSet (func->afpoints, setStepEPBp, STEP,
1583 func->mod->asm_name);
1585 /* set bp @ callers exit point */
1586 setBreakPoint (func->sym->eaddr, CODE, STEP ,
1587 stepBpCB, func->mod->asm_name,
1588 func->aexitline);
1593 doingSteps = 1;
1594 simGo(2);
1595 doingSteps = 0;
1596 showfull = 1;
1598 return 0;
1601 /*-----------------------------------------------------------------*/
1602 /* cmdNexti - next instruction but proceed function call */
1603 /*-----------------------------------------------------------------*/
1604 int cmdNexti (char *s, context *cctxt)
1606 if (STACK_EMPTY (callStack))
1608 fprintf (stdout, "The program is not being run.\n");
1610 else
1612 doingSteps = 2;
1613 simGo(1);
1614 doingSteps = 0;
1615 showfull = 1;
1617 return 0;
1620 /*-----------------------------------------------------------------*/
1621 /* cmdNext - next executable C statement file */
1622 /*-----------------------------------------------------------------*/
1623 int cmdNext (char *s, context *cctxt)
1625 function *func = NULL;
1626 /* next is almost the same as step except we don't
1627 we don't set break point for all function entry
1628 points */
1629 if (STACK_EMPTY (callStack))
1631 fprintf (stdout, "The program is not being run.\n");
1633 else
1635 /* if we are @ the end of a function then set
1636 break points at execution points of the
1637 function in the call stack... */
1638 if (cctxt->addr == cctxt->func->sym->eaddr)
1640 if ((func = STACK_PEEK (callStack)))
1642 if (srcMode == SRC_CMODE)
1643 applyToSet (func->cfpoints, setStepEPBp, NEXT,
1644 func->mod->c_name);
1645 else
1646 applyToSet (func->afpoints, setStepEPBp, NEXT,
1647 func->mod->asm_name);
1650 else
1652 if (srcMode == SRC_CMODE)
1654 /* for all execution points in this function */
1655 applyToSet (cctxt->func->cfpoints, setNextEPBp, NEXT,
1656 cctxt->func->mod->c_name);
1657 /* set a break point @ the current function's
1658 exit */
1659 setBreakPoint (cctxt->func->sym->eaddr, CODE, NEXT ,
1660 nextBpCB, cctxt->func->mod->c_name,
1661 cctxt->func->exitline);
1663 /* now break point @ callers execution points */
1664 if ((func = STACK_PPEEK(callStack)))
1666 applyToSet (func->cfpoints, setNextEPBp, NEXT ,
1667 func->mod->c_name);
1668 /* set bp @ callers exit point */
1669 setBreakPoint (func->sym->eaddr, CODE, NEXT ,
1670 stepBpCB, func->mod->c_name,
1671 func->exitline);
1674 else
1676 /* for all execution points in this function */
1677 applyToSet (cctxt->func->afpoints, setNextEPBp, NEXT,
1678 cctxt->func->mod->asm_name);
1679 /* set a break point @ the current function's
1680 exit */
1681 setBreakPoint (cctxt->func->sym->eaddr, CODE, NEXT ,
1682 nextBpCB, cctxt->func->mod->asm_name,
1683 cctxt->func->aexitline);
1685 /* now break point @ callers execution points */
1686 if ((func = STACK_PPEEK (callStack)))
1688 applyToSet (func->cfpoints, setNextEPBp, NEXT ,
1689 func->mod->asm_name);
1690 /* set bp @ callers exit point */
1691 setBreakPoint (func->sym->eaddr, CODE, NEXT ,
1692 stepBpCB, func->mod->asm_name,
1693 func->aexitline);
1697 doingSteps = 1;
1698 simGo(1);
1699 doingSteps = 0;
1700 showfull = 1;
1702 return 0;
1705 /*-----------------------------------------------------------------*/
1706 /* cmdRun - run till next break point */
1707 /*-----------------------------------------------------------------*/
1708 int cmdRun (char *s, context *cctxt)
1710 char buff[10];
1711 if (STACK_EMPTY (callStack))
1713 fprintf (stdout, "Starting program\n");
1714 if ( ! simactive )
1716 fprintf (stdout, "No executable file specified.\nUse the \"file\" command.\n");
1717 return 0;
1719 resetHitCount();
1720 simGo(0);
1722 else
1724 fprintf (stdout, "The program being debugged has been started already.\n");
1725 fprintf (stdout, "Start it from the beginning? (y or n) ");
1726 fflush (stdout);
1728 if (fgets (buff, sizeof(buff), stdin) && toupper(buff[0]) == 'Y')
1730 simReset();
1731 resetHitCount();
1732 simGo(0);
1735 showfull = 1;
1736 return 0;
1739 /*-----------------------------------------------------------------
1740 cmdListSymbols - list symbols
1741 |-----------------------------------------------------------------*/
1742 int cmdListSymbols (char *s, context *cctxt)
1744 int our_verbose = 0;
1745 symbol *sy;
1746 int i;
1748 if (strstr (s, "v1"))
1750 our_verbose = 1;
1752 else if (strstr (s, "v2"))
1754 our_verbose = 2;
1757 printf ("[symbols]\n");
1758 sy = setFirstItem (symbols);
1759 i = 0;
1760 for (;;)
1762 if (sy == NULL)
1763 break;
1764 if (our_verbose <= 1)
1765 printf ("<%s>", sy->name);
1767 if (our_verbose > 1)
1769 printf (" %d) name:%s, size:%d, level:%d block:%d\n", i,
1770 sy->name, sy->size, sy->level, sy->block);
1771 printf (" isonstack:%d, isfunc:%d, offset:%d addr:%d\n",
1772 sy->isonstack, sy->isfunc, sy->offset, sy->addr);
1773 printf (" eaddr:%d, addr_type:%c, type:%p etype:%p\n",
1774 sy->eaddr, sy->addr_type, sy->type, sy->etype);
1775 printf (" scopetype:%c, sname:%s, rname:%s addrspace:%c\n",
1776 sy->scopetype, sy->sname, sy->rname, sy->addrspace);
1777 printf (" next:%p\n", sy->next);
1779 ++i;
1780 sy = setNextItem (symbols);
1782 printf (" %d symbols\n", i);
1783 return 0;
1786 /*-----------------------------------------------------------------
1787 cmdListFunctions - list functions.
1788 |-----------------------------------------------------------------*/
1789 int cmdListFunctions (char *s, context *cctxt)
1791 function *f;
1792 int i;
1793 int our_verbose = 0;
1795 if (strstr (s, "v1"))
1797 our_verbose = 1;
1799 else if (strstr (s, "v2"))
1801 our_verbose = 2;
1804 printf ("[functions]\n");
1805 f = setFirstItem (functions);
1806 i = 0;
1807 while (f != NULL)
1809 if (our_verbose)
1811 printf (" %d) sym:%p, fname:%s, modName:%s, mod:%p\n", i,
1812 f->sym, f->sym->name, f->modName, f->mod);
1813 printf (" entryline:%d, aentryline:%d, exitline:%d, aexitline:%d\n",
1814 f->entryline, f->aentryline, f->exitline, f->aexitline);
1815 printf (" cfpoints:%p, afpoints:%p, laddr:%x, lline:%d\n",
1816 f->cfpoints, f->afpoints, f->laddr, f->lline);
1818 else
1820 printf ("<%s>", f->sym->name);
1822 ++i;
1823 f = setNextItem (functions);
1825 printf (" %d functions\n", i);
1826 return 0;
1829 /*-----------------------------------------------------------------
1830 cmdListModules - list modules.
1831 |-----------------------------------------------------------------*/
1832 int cmdListModules (char *s, context *cctxt)
1834 module *m;
1835 srcLine *cs, *as;
1836 int i, mi;
1837 int our_verbose = 0;
1839 if (strstr (s, "v1"))
1841 our_verbose = 1;
1843 else if (strstr (s, "v2"))
1845 our_verbose = 2;
1848 printf ("[modules]\n");
1849 m = setFirstItem (modules);
1850 mi = 0;
1851 for (; ; )
1853 if (m == NULL)
1854 break;
1856 if (our_verbose >= 0)
1858 printf (" %d) cfullname:%s, afullname:%s, name:%s\n", ++mi,
1859 m->cfullname, m->afullname, m->name);
1860 printf (" c_name:%s, asm_name:%s, ncLines:%d, nasmLines:%d\n",
1861 m->c_name, m->asm_name, m->ncLines, m->nasmLines);
1862 printf (" cLines:%p, asmLines:%p\n",
1863 m->cLines, m->asmLines);
1865 if (our_verbose >= 2)
1867 if (m->ncLines)
1869 printf (" [cLines] ");
1870 if (our_verbose)
1872 for (i = 0; i < m->ncLines; i++)
1874 cs = m->cLines[i];
1875 printf (" (%d) addr:%x, block:%d, level:%d, src:%s\n",
1876 i, cs->addr, cs->block, cs->level, cs->src);
1880 if (m->nasmLines)
1882 printf (" [asmLines] ");
1883 if (our_verbose)
1885 for (i = 0; i < m->nasmLines; i++)
1887 as = m->asmLines[i];
1888 printf (" (%d) addr:%x, block:%d, level:%d, src:%s\n",
1889 i, as->addr, as->block, as->level, as->src);
1893 printf ("\n");
1895 m = setNextItem (modules);
1897 return 0;
1900 /*-----------------------------------------------------------------
1901 infoSymbols - This is really just a tool to dump all these
1902 huge program structures out into human readable form.
1903 |-----------------------------------------------------------------*/
1904 static void infoSymbols (context *ctxt)
1906 int our_verbose = 0;
1908 printf ("[context:%p] func:%p modName:%s addr:%x\n",
1909 ctxt, ctxt->func, ctxt->modName, ctxt->addr);
1911 printf (" cline:%d asmline:%d block:%d level:%d\n",
1912 ctxt->cline, ctxt->asmline, ctxt->block, ctxt->level);
1914 printf ("[globals] currCtxt:%p, modules:%p, functions:%p symbols:%p\n",
1915 currCtxt, modules, functions, symbols);
1916 printf (" nStructs:%d, structs:%p, ssdirl:%s\n",
1917 nStructs, structs, ssdirl);
1919 /**************** modules *******************/
1921 module *m;
1922 srcLine *cs, *as;
1923 int i, mi;
1924 printf ("[modules]\n");
1925 m = setFirstItem (modules);
1926 mi = 0;
1927 for (;;)
1929 if (m == NULL)
1930 break;
1931 printf (" %d) cfullname:%s, afullname:%s, name:%s\n", ++mi,
1932 m->cfullname, m->afullname, m->name);
1933 printf (" c_name:%s, asm_name:%s, ncLines:%d, nasmLines:%d\n",
1934 m->c_name, m->asm_name, m->ncLines, m->nasmLines);
1935 printf (" cLines:%p, asmLines:%p\n",
1936 m->cLines, m->asmLines);
1937 i = 0;
1938 if (m->cLines)
1940 cs = m->cLines[i++];
1941 printf (" [cLines] ");
1942 while (cs)
1944 if (our_verbose)
1945 printf (" (%d) addr:%x, block:%d, level:%d, src:%s\n",
1946 i, cs->addr, cs->block, cs->level, cs->src);
1947 cs = m->cLines[i++];
1949 if (!our_verbose)
1950 printf ("%d records", i);
1952 i = 0;
1953 if (m->asmLines)
1955 as = m->asmLines[i++];
1956 printf (" [asmLines] ");
1957 while (as)
1959 if (our_verbose)
1960 printf (" (%d) addr:%x, block:%d, level:%d, src:%s\n",
1961 i, as->addr, as->block, as->level, as->src);
1962 as = m->asmLines[i++];
1964 if (!our_verbose)
1965 printf ("%d records", i);
1967 printf ("\n");
1969 m = setNextItem (modules);
1973 /**************** functions *******************/
1975 function *f;
1976 int i;
1977 printf ("[functions]\n");
1978 f = setFirstItem (functions);
1979 i = 0;
1980 for (;;)
1982 if (f == NULL)
1983 break;
1984 if (our_verbose)
1986 printf (" %d) sym:%p, modName:%s, mod:%p\n", i,
1987 f->sym, f->modName, f->mod);
1988 printf (" entryline:%d, aentryline:%d, exitline:%d, aexitline:%d\n",
1989 f->entryline, f->aentryline, f->exitline, f->aexitline);
1990 printf (" cfpoints:%p, afpoints:%p, laddr:%x, lline:%d\n",
1991 f->cfpoints, f->afpoints, f->laddr, f->lline);
1993 ++i;
1994 f = setNextItem (functions);
1996 if (!our_verbose)
1997 printf (" %d functions\n", i);
2000 /**************** symbols *******************/
2002 symbol *s;
2003 int i;
2004 printf ("[symbols]\n");
2005 s = setFirstItem (symbols);
2006 i = 0;
2007 for (;;)
2009 if (s == NULL)
2010 break;
2011 if (our_verbose)
2013 printf (" %d) name:%s, size:%d, level:%d block:%d\n", i,
2014 s->name, s->size, s->level, s->block);
2015 printf (" isonstack:%d, isfunc:%d, offset:%d addr:%d\n",
2016 s->isonstack, s->isfunc, s->offset, s->addr);
2017 printf (" eaddr:%d, addr_type:%c, type:%p etype:%p\n",
2018 s->eaddr, s->addr_type, s->type, s->etype);
2019 printf (" scopetype:%c, sname:%s, rname:%s addrspace:%c\n",
2020 s->scopetype, s->sname, s->rname, s->addrspace);
2021 printf (" next:%p\n", s->next);
2023 ++i;
2024 s = setNextItem (symbols);
2026 if (!our_verbose)
2027 printf (" %d symbols\n", i);
2032 /*-----------------------------------------------------------------*/
2033 /* infoRegisters - print register information */
2034 /*-----------------------------------------------------------------*/
2035 static void infoRegisters (int all, context *ctxt)
2037 static int regaddrs[] = {0x81,0x82,0x83,0xb8,0xd0,0xe0,0xf0,0};
2038 unsigned long val;
2039 int i, j, *r;
2041 i = simGetValue (0xd0, 'I', 1);
2042 fprintf (stdout, "IP : 0x%04X RegisterBank %d:\nR0-7:", ctxt->addr, (i>>3)&3);
2043 for (j = 0; j < 8 ; j++)
2045 val = simGetValue (j, 'R', 1);
2046 fprintf(stdout," 0x%02lX", val);
2048 fprintf(stdout, "\n");
2049 val = simGetValue (0xe0, 'I', 1);
2050 fprintf (stdout, "ACC : 0x%02lX %lu %c\n", val, val, isprint(val) ? (char)val : '.');
2051 val = simGetValue (0xf0, 'I', 1);
2052 fprintf (stdout, "B : 0x%02lX %lu %c\n", val, val, isprint(val) ? (char)val : '.');
2053 val = simGetValue (0x82, 'I', 2);
2054 fprintf (stdout, "DPTR: 0x%04lX %lu\n", val, val);
2055 val = simGetValue (0x81, 'I', 1);
2056 fprintf (stdout, "SP : 0x%02lX (0x%04lX)\n", val, val ? simGetValue (val-1, 'B', 2) : 0);
2057 fprintf (stdout, "PSW : 0x%02X | CY : %c | AC : %c | OV : %c | P : %c\n",
2058 i, (i&0x80)?'1':'0', (i&0x40)?'1':'0', (i&4)?'1':'0', (i&1)?'1':'0');
2059 if (all)
2061 fprintf (stdout, "Special Function Registers:\n");
2062 r = regaddrs;
2063 for (i = 0x80 ; i < 0x100 ; i++)
2065 symbol *sym = NULL;
2066 if ( *r && *r == i )
2068 /* skip normal registers */
2069 r++ ;
2070 continue;
2072 if (applyToSetFTrue (sfrsymbols, symWithAddr, i, 'I', &sym))
2074 val = simGetValue (sym->addr, sym->addrspace, sym->size);
2075 fprintf (stdout,"%s : 0x%02lx", sym->name, val);
2076 if (!(i & 0x07))
2078 for (j = 0 ; j < 8 ; j++)
2080 sym = NULL;
2081 if (applyToSetFTrue (sfrsymbols, symWithAddr, i+j, 'J', &sym))
2083 //val = simGetValue (sym->addr, sym->addrspace, sym->size);
2084 fprintf (stdout," %s=%c", sym->name, (val&1)? '1':'0');
2086 val >>= 1;
2089 fprintf (stdout,"\n");
2095 /*-----------------------------------------------------------------*/
2096 /* infoStack - print call stack information */
2097 /*-----------------------------------------------------------------*/
2098 static void infoStack(context *ctxt)
2100 function *func ;
2101 int i = 0 ;
2103 STACK_STARTWALK(callStack) ;
2104 while ((func = STACK_WALK(callStack)))
2106 Dprintf (D_break, ("break: infoStack: %s %p (%p)\n", func->sym->name, w_callStack, p_callStack));
2108 fprintf (stdout, "#%d 0x%08x in %s () at %s:%d\n", i++,
2109 func->laddr, func->sym->name,
2110 func->mod->c_name, func->lline+1);
2112 if ( !i )
2113 fprintf (stdout, "no stack.\n");
2116 /*-----------------------------------------------------------------*/
2117 /* cmdWhere - where command */
2118 /*-----------------------------------------------------------------*/
2119 int cmdWhere (char *s, context *cctxt)
2121 infoStack (cctxt);
2122 return 0;
2125 static int infomode = 0;
2126 /*-----------------------------------------------------------------*/
2127 /* cmdInfo - info command */
2128 /*-----------------------------------------------------------------*/
2129 int cmdInfo (char *s, context *cctxt)
2131 if (!simactive)
2133 fprintf (stdout, "No executable file specified.\nUse the \"file\" command.\n");
2134 return 0;
2137 /* trim left and_right*/
2138 s = trim(s);
2140 /* list all break points */
2141 if (strncmp (s, "break", 5) == 0)
2143 listUSERbp();
2144 return 0;
2147 /* info frame same as frame */
2148 if (strncmp (s, "frame", 5) == 0)
2150 cmdFrame (s+5, cctxt);
2151 return 0;
2154 if (strncmp (s, "line", 4) == 0)
2156 infomode=1;
2157 cmdListSrc (s+4, cctxt);
2158 return 0;
2160 if (strncmp (s, "source", 6) == 0)
2162 module *m;
2163 if ( s[6] == 's' )
2165 int k = 0;
2166 fprintf (stdout, "Source files for which symbols have been read in:\n\n");
2167 for (m = setFirstItem(modules); m ; m = setNextItem(modules))
2169 fprintf (stdout, "%s%s, %s", k ? ", ":"", m->cfullname, m->afullname);
2170 k = 1;
2172 fprintf(stdout,"\n");
2174 else
2176 if (!cctxt || !cctxt->func || !cctxt->func->mod)
2178 fprintf (stdout, "No source file loaded\n");
2179 return 0;
2181 m = cctxt->func->mod;
2182 fprintf (stdout, "Current source file is %s\n", m->c_name);
2183 fprintf (stdout, "Located in %s\n", m->cfullname);
2184 fprintf (stdout, "Contains %d lines.\nSource language is c.\n",
2185 m->ncLines);
2187 return 0;
2189 if (strcmp (s, "functions") == 0)
2191 function *f;
2192 module *m = NULL;
2193 fprintf(stdout, "All defined functions:\n");
2194 for ( f = setFirstItem(functions); f ; f = setNextItem(functions))
2196 if ( f->mod != m )
2198 m = f->mod;
2199 fprintf (stdout, "\nFile %s\n", m->c_name);
2201 fprintf (stdout, "%s();\n", f->sym->name);
2203 return 0;
2205 /* info stack display call stack */
2206 if (strcmp (s, "stack") == 0)
2208 infoStack (cctxt);
2209 showfull = 1;
2210 return 0;
2213 /* info stack display call stack */
2214 if (strcmp (s, "registers") == 0)
2216 infoRegisters (0, cctxt);
2217 return 0;
2220 /* info stack display call stack */
2221 if (strcmp (s, "all-registers") == 0)
2223 infoRegisters (1, cctxt);
2224 return 0;
2227 /* info stack display call stack */
2228 if (strcmp (s, "symbols") == 0)
2230 /* dump out symbols we have read in */
2231 fprintf (stdout, "Dumping symbols...\n");
2232 infoSymbols (cctxt);
2233 return 0;
2236 if (strcmp (s, "variables") == 0)
2238 /* dump out symbols we have read in */
2239 fprintf (stdout, "Dumping symbols...\n");
2240 infoSymbols (cctxt);
2241 return 0;
2244 fprintf (stdout, "Undefined info command: \"%s\". Try \"help\n", s);
2245 return 0;
2248 /*-----------------------------------------------------------------*/
2249 /* cmdQuit - quit debugging */
2250 /*-----------------------------------------------------------------*/
2251 int cmdQuit (char *s, context *cctxt)
2253 if (simactive)
2254 closeSimulator();
2255 return 1;
2258 /*-----------------------------------------------------------------*/
2259 /* cmdListSrc - list src */
2260 /*-----------------------------------------------------------------*/
2261 int cmdListSrc (char *s, context *cctxt)
2263 static int currline = 0;
2264 int i =0 ;
2265 int pline = 0;
2266 int llines = listlines;
2267 function *func = NULL;
2269 s = trim_left(s);
2271 /* if the user has spcified line numer then the line number
2272 can be of the following formats
2273 LINE - just line number
2274 FILE:LINE - filename line number
2275 FILE:LINE,LASTLINE + last line
2276 FUNCTION - list a function
2277 FILE:FUNCTION - function in file */
2279 if (*s)
2281 /* case a) LINE */
2282 if (isdigit(*s))
2284 if (!cctxt || !cctxt->func || !cctxt->func->mod)
2286 if (!list_mod)
2288 fprintf (stdout, "Sdcdb fails to have a proper context at %d.\n", __LINE__);
2289 return 0;
2292 else
2294 list_mod = cctxt->func->mod;
2296 pline = strtol (s, &s, 10) - 1;
2297 if (s && (s = strchr(s,',')))
2299 /* LINE,LASTLINE */
2300 llines = strtol (s+1, 0, 10);
2301 if ( llines > 0 )
2302 llines -= pline+1;
2303 else
2304 llines = listlines;
2307 else
2309 char *bp;
2311 /* if ':' present then FILE:LINE || FILE:FUNCTION */
2312 if ((bp = strchr(s,':')))
2314 *bp = '\0';
2315 bp ++;
2316 if (isdigit(*bp))
2318 /* FILE:LINE */
2319 list_mod=NULL; /* bug fix 2-09-02, moduleWithCName expects mod to be null */
2320 if (srcMode == SRC_CMODE)
2322 if (!applyToSet (modules, moduleWithCName, s, &list_mod))
2324 fprintf (stderr,"No c source file named %s.\n",s);
2325 return 0;
2328 else
2330 if (!applyToSet (modules, moduleWithAsmName, s, &list_mod))
2332 fprintf (stderr, "No source file named %s.\n", s);
2333 return 0;
2336 pline = strtol (bp, &bp, 10) - 1;
2337 if (bp && (bp = strchr (bp, ',')))
2339 /* FILE:LINE,LASTLINE */
2340 llines = strtol (bp+1, 0, 10);
2341 if ( llines > 0 )
2342 llines -= pline+1;
2343 else
2344 llines = listlines;
2347 else
2349 /* FILE:FUCTION */
2350 if (!applyToSet (functions, funcWithNameModule, bp, s, &func))
2352 fprintf (stdout, "Function \"%s\" not defined.\n", bp);
2353 return 0;
2355 list_mod = func->mod;
2356 if (srcMode == SRC_CMODE)
2358 pline = func->entryline;
2359 llines = func->exitline - func->entryline + 1;
2361 else
2363 pline = func->aentryline;
2364 llines = func->aexitline - func->aentryline + 1;
2368 else
2370 /* FUNCTION */
2371 if (*s == '\'')
2373 /* 'FUNCTION' */
2374 s++;
2375 if ((bp = strrchr(s,'\'')))
2377 *bp = '\0';
2380 if (!applyToSet (functions, funcWithName, s, &func))
2382 fprintf (stderr, "Function \"%s\" not defined.\n", s);
2383 return 0;
2385 else
2387 list_mod = func->mod;
2388 if (srcMode == SRC_CMODE)
2390 pline = func->entryline;
2391 llines = func->exitline - func->entryline + 1;
2393 else
2395 pline = func->aentryline;
2396 llines = func->aexitline - func->aentryline + 1;
2402 else
2404 /* if no line specified & we had listed
2405 before then continue from that listing */
2406 if (currline)
2407 pline = currline;
2408 else
2410 if (!cctxt || !cctxt->func || !cctxt->func->mod)
2412 fprintf (stdout, "Missing context at %d. Try list filename:lineno\n", __LINE__);
2413 return 0;
2415 list_mod = cctxt->func->mod;
2416 if (srcMode == SRC_CMODE)
2417 pline = cctxt->cline;
2418 else
2419 pline = cctxt->asmline;
2423 if (!list_mod)
2425 fprintf (stdout, "Sdcdb fails to have a valid module context at %d.\n", __LINE__);
2426 return 0;
2429 if ( pline < 0 )
2430 return 0;
2431 if ( infomode )
2433 unsigned firstaddr, lastaddr;
2435 if (pline >= list_mod->ncLines)
2437 if (!cctxt)
2439 fprintf (stdout, "Missing context at %d. Try list filename:lineno\n", __LINE__);
2440 return 0;
2442 else
2444 pline = cctxt->cline;
2448 firstaddr = lastaddr = list_mod->cLines[pline]->addr;
2449 if (!func && cctxt && cctxt->func )
2450 func = cctxt->func;
2451 fprintf (stdout, "Line %d of \"%s\" starts at address 0x%08x <%s+%d>",
2452 pline+1,
2453 list_mod->c_name, lastaddr,
2454 func ? func->sym->name : "?",
2455 func ? lastaddr -func->sym->addr : 0);
2456 llines = pline+1;
2457 for ( ; pline < list_mod->ncLines; pline++)
2459 if (list_mod->cLines[pline]->addr > lastaddr)
2461 lastaddr = list_mod->cLines[pline]->addr -1;
2462 break;
2465 fprintf (stdout, " and ends at 0x%08x <%s+%d>.\n",
2466 lastaddr,
2467 func ? func->sym->name : "?",
2468 func ? lastaddr -func->sym->addr : 0);
2469 infomode=0;
2470 if (func)
2471 fprintf (stdout, "\032\032%s:%d:1:beg:0x%08x\n",
2472 func->mod->cfullname,
2473 llines,firstaddr);
2474 else
2475 showfull=1;
2476 return 0;
2478 for ( i = 0 ; i < llines ; i++ )
2480 if (srcMode == SRC_CMODE)
2482 if ( (pline+i) >= list_mod->ncLines )
2483 break;
2484 fprintf (stdout, "%d\t%s", pline+i,
2485 list_mod->cLines[pline+i]->src);
2487 else
2489 if ( (pline+i) >= list_mod->nasmLines )
2490 break;
2491 fprintf (stdout, "%d\t%s", pline+i,
2492 list_mod->asmLines[pline+i]->src);
2495 currline = pline+i ;
2496 return 0;
2499 static unsigned long getValBasic (symbol *sym, st_link *type, char *val)
2501 char *s;
2502 union
2504 float f;
2505 unsigned long val;
2506 long sval;
2507 struct
2509 unsigned short lo;
2510 unsigned short hi;
2511 } i;
2512 unsigned char b[4];
2513 } v;
2515 if (IS_FLOAT(type))
2517 v.f = (float)strtod (val, NULL);
2519 else if (IS_PTR (type))
2521 v.val = strtol (val, NULL, 0);
2523 else if (IS_INTEGRAL (type))
2525 st_link *etype;
2526 if ( type->next )
2527 etype = type->next;
2528 else
2529 etype = type;
2531 if (IS_CHAR (etype))
2533 if (( s = strchr (val, '\'')))
2535 if ( s[1] == '\\' )
2536 v.b[0] = (unsigned char)strtol (s+2, NULL, 8);
2537 else
2538 v.b[0] = s[1];
2540 else
2542 v.b[0] = (unsigned char)strtol(val,NULL,0);
2545 else if (IS_INT (etype))
2547 if (IS_LONG (etype))
2548 v.val = strtol (val, NULL, 0);
2549 else
2550 v.i.lo = (unsigned short)strtol (val, NULL, 0);
2552 else
2554 v.val = strtol (val, NULL, 0);
2557 else
2559 v.val = strtol (val, NULL, 0);
2561 return v.val;
2564 /*-----------------------------------------------------------------*/
2565 /* printFmtInteger - print value in bin,oct,dec or hex */
2566 /*-----------------------------------------------------------------*/
2567 static void printFmtInteger (char *deffmt,int fmt, long val,
2568 int sign, int size)
2570 static char digits[] =
2572 '0' , '1' , '2' , '3' , '4' , '5' ,
2573 '6' , '7' , '8' , '9' , 'a' , 'b' ,
2574 'c' , 'd' , 'e' , 'f' , 'g' , 'h'
2576 static int radixOfFormat[] = { 0 , 2, 8 ,10, 16 };
2577 static int olenOfSize[] = { 0 , 3, 6 , 8, 11 };
2578 char buf[40];
2579 char negative = 0;
2580 int charPos = 38;
2581 int radix;
2583 if ( fmt == FMT_NON || fmt == FMT_DEZ )
2585 fprintf (stdout, deffmt, val);
2586 return;
2588 radix = radixOfFormat[fmt];
2591 if ( sign && val < 0 )
2592 negative = 1;
2595 if (!negative)
2596 val = -val;
2598 buf[39] = '\0';
2599 while (val <= -radix)
2601 buf[charPos--] = digits[-(val % radix)];
2602 val = val / radix;
2604 buf[charPos] = digits[-val];
2606 switch ( fmt )
2608 case FMT_OCT:
2609 radix = olenOfSize[size];
2610 break;
2611 case FMT_HEX:
2612 radix = size << 1;
2613 break;
2614 case FMT_BIN:
2615 radix = size << 3;
2616 break;
2619 while (charPos > 39 - radix )
2621 buf[--charPos] = '0';
2623 switch ( fmt )
2625 case FMT_OCT:
2626 if ( buf[charPos] != '0' )
2627 buf[--charPos] = '0';
2628 break;
2629 case FMT_HEX:
2630 buf[--charPos] = 'x';
2631 buf[--charPos] = '0';
2632 break;
2634 if (negative)
2636 buf[--charPos] = '-';
2638 fputs (&buf[charPos], stdout);
2641 /*-----------------------------------------------------------------*/
2642 /* printValBasic - print value of basic types */
2643 /*-----------------------------------------------------------------*/
2644 static void printValBasic (symbol *sym, st_link *type,
2645 char mem, unsigned addr,int size, int fmt)
2647 union
2649 float f;
2650 unsigned long val;
2651 long sval;
2652 struct
2654 unsigned short lo;
2655 unsigned short hi;
2656 } i;
2657 unsigned char b[4];
2658 } v;
2660 v.val = simGetValue (addr, mem, size);
2661 /* if this a floating point number then */
2662 if (IS_FLOAT (type))
2664 fprintf (stdout, "%f", v.f);
2666 else if (IS_PTR (type))
2668 fprintf (stdout, "0x%*lx", size<<1, v.val);
2670 else if (IS_INTEGRAL (type))
2672 st_link *etype;
2673 if ( type->next )
2674 etype = type->next;
2675 else
2676 etype = type;
2677 if (IS_CHAR (etype))
2679 if (isprint (v.val))
2680 printFmtInteger ((SPEC_USIGN(etype)?"0x%02x":"'%c'"),
2681 fmt, (long)v.val, 0, size);
2682 else
2683 printFmtInteger ((SPEC_USIGN(etype)?"0x%02x":"'\\%o'"),
2684 fmt, (long)v.val, 0, size);
2686 else if (IS_INT (etype))
2688 if (IS_LONG (etype))
2689 if (SPEC_USIGN (etype))
2690 printFmtInteger ("%u", fmt, (long)v.val, 0, size);
2691 else
2692 printFmtInteger ("%d", fmt, (long)v.sval, 1, size);
2693 else
2694 if (SPEC_USIGN (etype))
2695 printFmtInteger ("%u", fmt, (long)v.i.lo, 0, size);
2696 else
2697 printFmtInteger ("%d", fmt, (long)v.i.lo, 1, size);
2699 else if (IS_BITVAR (etype))
2701 fprintf (stdout, "%c", (v.val?'1':'0'));
2703 else
2705 fprintf (stdout, "0x%0*lx", size<<1, v.val);
2708 else
2710 fprintf (stdout, "0x%0*lx", size<<1, v.val);
2714 /*-----------------------------------------------------------------*/
2715 /* printValFunc - prints function values */
2716 /*-----------------------------------------------------------------*/
2717 static void printValFunc (symbol *sym, int fmt)
2719 fprintf (stdout, "print function not yet implemented");
2722 /*-----------------------------------------------------------------*/
2723 /* printArrayValue - will print the values of array elements */
2724 /*-----------------------------------------------------------------*/
2725 static void printArrayValue (symbol *sym, st_link *type,
2726 char space, unsigned int addr, int fmt)
2728 st_link *elem_type = type->next;
2729 int i;
2731 fprintf (stdout, "{");
2732 for (i = 0 ; i < DCL_ELEM (type) ; i++)
2734 if (IS_AGGREGATE (elem_type))
2736 printValAggregates (sym, elem_type, space, addr, fmt);
2738 else
2740 printValBasic (sym, elem_type, space, addr, getSize (elem_type), fmt);
2742 addr += getSize (elem_type);
2743 if (i != DCL_ELEM (type) -1)
2744 fprintf (stdout, ",");
2747 fprintf (stdout, "}");
2750 /*-----------------------------------------------------------------*/
2751 /* printStructValue - prints structures elements */
2752 /*-----------------------------------------------------------------*/
2753 static void printStructValue (symbol *sym, st_link *type,
2754 char space, unsigned int addr, int fmt)
2756 symbol *fields = SPEC_STRUCT(type)->fields;
2757 int first = 1;
2759 fprintf (stdout, " { ");
2760 while (fields)
2762 fprintf (stdout, "%s%s = ",(first ? "": ", "), fields->name);
2763 first = 0;
2764 if (IS_AGGREGATE (fields->type))
2766 printValAggregates (fields, fields->type, space, addr, fmt);
2768 else
2770 printValBasic (fields, fields->type, space, addr, getSize(fields->type), fmt);
2772 addr += getSize (fields->type);
2773 fields = fields->next;
2775 fprintf (stdout, "}");
2778 /*-----------------------------------------------------------------*/
2779 /* printValAggregates - print value of aggregates */
2780 /*-----------------------------------------------------------------*/
2781 static void printValAggregates (symbol *sym, st_link *type,
2782 char space,unsigned int addr, int fmt)
2784 if (IS_ARRAY (type))
2786 printArrayValue (sym, type, space, addr, fmt);
2787 return;
2790 if (IS_STRUCT (type))
2792 printStructValue (sym, type, space, addr, fmt);
2793 return;
2797 /*-----------------------------------------------------------------*/
2798 /* printOrSetSymValue - print or set value of a symbol */
2799 /*-----------------------------------------------------------------*/
2800 static int printOrSetSymValue (symbol *sym, context *cctxt,
2801 int flg, int dnum, int fmt, char *rs,
2802 char *val, char cmp )
2804 static char fmtChar[] = " todx ";
2805 static int stack = 1;
2806 symbol *fields;
2807 st_link *type;
2808 unsigned int addr, size;
2809 int n;
2810 char *s, *s2;
2811 char save_ch, save_ch2;
2813 /* if it is on stack then compute address & fall thru */
2814 if (sym->isonstack)
2816 symbol *bp = symLookup ("bp", cctxt);
2817 if (!bp)
2819 fprintf (stdout, "cannot determine stack frame\n");
2820 return 1;
2823 sym->addr = simGetValue (bp->addr, bp->addrspace, bp->size) + sym->offset;
2826 /* get the value from the simulator and print it */
2827 switch (flg)
2829 case 0:
2830 default:
2831 break;
2832 case 1:
2833 fprintf (stdout, "$%d = ", stack++);
2834 break;
2835 case 2:
2836 fprintf (stdout, "%d: ", dnum);
2837 if ( fmt != FMT_NON )
2838 fprintf (stdout, "/%c ", fmtChar[fmt]);
2839 fprintf (stdout, "%s%s = ", sym->name, rs);
2840 break;
2843 addr = sym->addr;
2844 type = sym->type;
2845 size = sym->size;
2847 while (*rs)
2849 if (*rs == '[' && IS_ARRAY(type))
2851 s = rs+1;
2852 while (*rs && *rs != ']')
2853 rs++;
2854 save_ch = *rs;
2855 *rs = '\0' ;
2856 if ( ! isdigit (*s))
2858 /* index seems a variable */
2859 for (s2 = s; *s2 && (isalnum (*s2) || *s2 == '_'); s2++);
2860 save_ch2 = *s2;
2861 if ( *s2 )
2862 *s2 = '\0';
2863 fields = symLookup (s, cctxt);
2864 *s2 = save_ch2;
2865 if ( ! fields )
2867 fprintf (stdout, "Unknown variable \"%s\" for index.\n", s);
2868 return 1;
2870 /* arrays & structures first */
2871 if (! IS_INTEGRAL (fields->type))
2873 fprintf (stdout, "Wrong type of variable \"%s\" for index \n", s);
2874 return 1;
2876 n = simGetValue (fields->addr, fields->addrspace, getSize (fields->type));
2878 else
2880 n = strtol (s, 0, 0);
2882 if (n < 0 || n >= DCL_ELEM (type))
2884 fprintf (stdout, "Wrong index %d.\n", n);
2885 return 1;
2887 type = type->next;
2888 size = getSize (type);
2889 addr += size * n;
2890 *rs++ = save_ch;
2892 else if (*rs == '.' && IS_STRUCT (type))
2894 s = rs+1;
2895 /* search structure element */
2896 for (rs = s; *rs && (isalnum (*rs) || *rs == '_'); rs++);
2897 save_ch = *rs;
2898 if ( *rs )
2899 *rs = '\0';
2900 for (fields = SPEC_STRUCT (type)->fields; fields; fields = fields->next)
2902 if (!(strcmp (s, fields->name)))
2903 break;
2905 *rs = save_ch;
2906 if ( ! fields )
2908 fprintf (stdout, "Unknown field \"%s\" of structure\n", s);
2909 return 1;
2911 type = fields->type;
2912 size = getSize (type);
2913 addr += fields->offset;
2915 else
2917 break;
2921 if (IS_AGGREGATE (type)) /* arrays & structures first */
2923 if ( val )
2925 fprintf (stdout, "Cannot set/compare aggregate variable\n");
2926 return 1;
2928 else
2930 printValAggregates (sym, type, sym->addrspace, addr, fmt);
2933 else if (IS_FUNC (type)) /* functions */
2935 if ( !val )
2936 printValFunc (sym, fmt);
2937 else
2938 return 1;
2940 else
2942 if ( val )
2944 unsigned long newval;
2945 newval = getValBasic (sym, type, val);
2947 if ( cmp )
2949 unsigned long lval;
2950 lval = simGetValue (addr, sym->addrspace, size);
2951 switch ( cmp )
2953 case '<' : return ( lval < newval ? 1:0 ); break;
2954 case '>' : return ( lval > newval ? 1:0 ); break;
2955 case 'l' : return ( lval <= newval ? 1:0 ); break;
2956 case 'g' : return ( lval >= newval ? 1:0 ); break;
2957 case '=' : return ( lval == newval ? 1:0 ); break;
2958 case '!' : return ( lval != newval ? 1:0 ); break;
2961 else
2963 if ( sym->addrspace == 'I' && addr == 0xb8 )
2965 /* Symbol with address of IP */
2966 if ( cctxt )
2967 cctxt->addr = newval;
2968 simSetPC (cctxt->addr);
2970 else
2972 simSetValue (addr, sym->addrspace, size, newval);
2974 return 1;
2977 else
2979 printValBasic (sym, type, sym->addrspace, addr, size, fmt);
2982 if ( flg > 0 )
2983 fprintf (stdout, "\n");
2984 return 0;
2987 /*-----------------------------------------------------------------*/
2988 /* printStructInfo - print out structure information */
2989 /*-----------------------------------------------------------------*/
2990 static void printStructInfo (structdef *sdef)
2992 symbol *field = sdef->fields ;
2993 int i = 0 ;
2995 while (field)
2997 i += field->offset;
2998 field = field->next;
3001 fprintf (stdout, "%s %s {\n",(i ? "struct" : "union" ), sdef->tag);
3002 field = sdef->fields;
3003 while (field)
3005 printTypeInfo (field->type);
3006 fprintf (stdout, " %s ;\n", field->name);
3007 field = field->next ;
3010 fprintf (stdout, "}\n");
3013 /*-----------------------------------------------------------------*/
3014 /* printTypeInfo - print out the type information */
3015 /*-----------------------------------------------------------------*/
3016 static void printTypeInfo (st_link *p)
3018 if (!p)
3019 return;
3021 if (IS_DECL (p))
3023 switch (DCL_TYPE (p))
3025 case FUNCTION:
3026 printTypeInfo (p->next);
3027 fprintf (stdout, "()");
3028 break;
3029 case ARRAY:
3030 printTypeInfo (p->next);
3031 fprintf (stdout, "[%d]", DCL_ELEM (p));
3032 break;
3034 case IPOINTER:
3035 case PPOINTER:
3036 case POINTER:
3037 printTypeInfo (p->next);
3038 fprintf (stdout, "(_near *)");
3039 break;
3041 case FPOINTER:
3042 printTypeInfo (p->next);
3043 fprintf (stdout, "(_xdata *)");
3044 break;
3046 case CPOINTER:
3047 printTypeInfo (p->next);
3048 fprintf (stdout, "(_code *)");
3049 break;
3051 case GPOINTER:
3052 printTypeInfo (p->next);
3053 fprintf (stdout, "(_generic *)");
3054 break;
3057 else
3059 switch (SPEC_NOUN (p)) /* depending on the specifier type */
3061 case V_INT:
3062 (IS_LONG (p) ? fputs ("long ", stdout) :
3063 IS_SHORT(p) ? fputs ("short ", stdout) :
3064 fputs ("int ", stdout));
3065 break;
3066 case V_FLOAT:
3067 fputs ("float ", stdout);
3068 break;
3070 case V_CHAR:
3071 fputs ("char ", stdout);
3072 break;
3074 case V_VOID:
3075 fputs ("void ", stdout);
3076 break;
3078 case V_STRUCT:
3079 printStructInfo (SPEC_STRUCT (p));
3080 break;
3082 case V_SBIT:
3083 fputs ("sbit ", stdout);
3084 break;
3086 case V_BIT:
3087 fprintf (stdout, ": %d", SPEC_BLEN(p));
3088 break;
3093 /*-----------------------------------------------------------------*/
3094 /* conditionIsTrue - compare variable with constant value */
3095 /*-----------------------------------------------------------------*/
3096 int conditionIsTrue (char *s, context *cctxt)
3098 symbol *sym = NULL;
3099 int fmt;
3100 char *rs, *dup, cmp_char;
3101 dup = s = Safe_strdup (s);
3102 if ( !( rs = preparePrint (s, cctxt, &fmt, &sym )) || !sym)
3103 fmt = 1;
3104 else if (!( s = strpbrk (rs,"<>=!")))
3105 fmt = 1;
3106 else
3108 cmp_char = *s;
3109 *s++ = '\0';
3110 if ( *s == '=' )
3112 /* if <= or >= an other char is used
3113 * == or != not checked in switch
3115 switch( cmp_char )
3117 case '>': cmp_char = 'g' ; break;
3118 case '<': cmp_char = 'l' ; break;
3120 s++;
3122 s = trim_left (s);
3123 fmt = printOrSetSymValue (sym, cctxt, 0, 0, 0, rs, s, cmp_char);
3125 Safe_free (dup);
3126 return fmt;
3129 /*-----------------------------------------------------------------*/
3130 /* cmdPrint - print value of variable */
3131 /*-----------------------------------------------------------------*/
3132 int cmdPrint (char *s, context *cctxt)
3134 symbol *sym ;
3135 int fmt;
3136 char *rs;
3137 if ( !(rs = preparePrint (s, cctxt, &fmt, &sym)))
3138 return 0;
3140 if ( sym )
3142 printOrSetSymValue (sym, cctxt, 1, 0, fmt, rs, NULL, '\0');
3144 return 0;
3147 /*-----------------------------------------------------------------*/
3148 /* cmdOutput - print value of variable without number and newline */
3149 /*-----------------------------------------------------------------*/
3150 int cmdOutput (char *s, context *cctxt)
3152 symbol *sym ;
3153 int fmt;
3154 char *rs;
3155 if ( !(rs = preparePrint (s, cctxt, &fmt, &sym)))
3156 return 0;
3158 if ( sym )
3160 printOrSetSymValue (sym, cctxt, 0, 0, fmt, rs, NULL, '\0');
3162 return 0;
3165 /** find display entry with this number */
3167 DEFSETFUNC (dsymWithNumber)
3169 dsymbol *dsym = item;
3170 V_ARG (int, dnum);
3171 V_ARG (dsymbol **, dsymp);
3173 if ( dsym->dnum == dnum )
3175 *dsymp = dsym;
3176 return 1;
3178 return 0;
3181 /*-----------------------------------------------------------------*/
3182 /* displayAll - display all valid variables */
3183 /*-----------------------------------------------------------------*/
3184 void displayAll (context *cctxt)
3186 dsymbol *dsym;
3187 if ( !dispsymbols )
3188 return;
3189 for (dsym = setFirstItem (dispsymbols);
3190 dsym;
3191 dsym = setNextItem (dispsymbols))
3193 symbol *sym = symLookup (dsym->name, cctxt);
3194 if (sym)
3195 printOrSetSymValue (sym, cctxt, 2, dsym->dnum, dsym->fmt,
3196 dsym->rs, NULL, '\0');
3200 /*-----------------------------------------------------------------*/
3201 /* cmdDisplay - display value of variable */
3202 /*-----------------------------------------------------------------*/
3203 int cmdDisplay (char *s, context *cctxt)
3205 static int dnum = 1;
3206 symbol *sym ;
3207 int fmt;
3208 char *rs;
3209 if ( !(rs = preparePrint (s, cctxt, &fmt, &sym)))
3211 displayAll (cctxt);
3212 return 0;
3215 if ( sym )
3217 dsymbol *dsym = (dsymbol *)Safe_calloc (1, sizeof(dsymbol));
3218 dsym->dnum = dnum++ ;
3219 dsym->name = sym->name;
3220 dsym->fmt = fmt;
3221 dsym->rs = gc_strdup (rs);
3222 addSetHead (&dispsymbols, dsym);
3224 return 0;
3227 /*-----------------------------------------------------------------*/
3228 /* cmdUnDisplay - undisplay value of variable */
3229 /*-----------------------------------------------------------------*/
3230 int cmdUnDisplay (char *s, context *cctxt)
3232 dsymbol *dsym;
3233 int dnum;
3235 s = trim_left (s);
3236 if (!*s)
3238 for (dsym = setFirstItem (dispsymbols);
3239 dsym;
3240 dsym = setNextItem (dispsymbols))
3242 Safe_free (dsym->rs);
3243 Safe_free (dsym);
3245 deleteSet (&dispsymbols);
3246 return 0;
3248 while ( s && *s )
3250 dnum = strtol (s, &s, 10);
3251 if (applyToSetFTrue (dispsymbols, dsymWithNumber, dnum, &dsym))
3253 deleteSetItem (&dispsymbols, dsym);
3254 Safe_free (dsym->rs);
3255 Safe_free (dsym);
3257 else
3259 fprintf (stdout, "Arguments must be display numbers.\n");
3262 return 0;
3265 /*-----------------------------------------------------------------*/
3266 /* cmdPrintType - print type of a variable */
3267 /*-----------------------------------------------------------------*/
3268 int cmdPrintType (char *s, context *cctxt)
3270 symbol *sym;
3272 /* trim left and right */
3273 s = trim (s);
3274 if (!*s)
3275 return 0;
3277 if ((sym = symLookup (s, cctxt)))
3279 printTypeInfo (sym->type);
3280 fprintf (stdout, "\n");
3282 else
3284 fprintf (stdout, "No symbol \"%s\" in current context.\n", s);
3286 return 0;
3289 /*-----------------------------------------------------------------*/
3290 /* cmdClrUserBp - clear user break point */
3291 /*-----------------------------------------------------------------*/
3292 int cmdClrUserBp (char *s, context *cctxt)
3294 char *bp ;
3295 function *func = NULL;
3297 /* clear break point location specification can be of the following
3298 forms
3299 a) <nothing> - break point at current location
3300 b) lineno - number of the current module
3301 c) filename:lineno - line number of the given file
3302 e) filename:function- function X in file Y (useful for static functions)
3303 f) function - function entry point
3306 if (!cctxt)
3308 fprintf (stdout, "No symbol table is loaded. Use the \"file\" command.\n");
3309 return 0;
3312 /* trim left and right */
3313 s = trim (s);
3315 /* case a) nothing */
3316 /* if nothing given then current location : we know
3317 the current execution location from the currentContext */
3318 if (! *s )
3320 /* if current context is known */
3321 if (cctxt->func)
3322 /* clear the break point @ current location */
3323 clearUSERbp (cctxt->addr);
3324 else
3325 fprintf (stderr, "No default breakpoint address now.\n");
3327 goto ret;
3330 /* case b) lineno */
3331 /* check if line number */
3332 if (isdigit (*s))
3334 /* get the lineno */
3335 int line = atoi (s);
3337 /* if current context not present then we must get the module
3338 which has main & set the break point @ line number provided
3339 of that module : if current context known then set the bp
3340 at the line number given for the current module
3342 if (cctxt->func)
3344 if (!cctxt->func->mod)
3346 if (!applyToSet (functions, funcWithName, "main", &func))
3347 fprintf (stderr, "Function \"main\" not defined.\n");
3348 else
3349 clearBPatModLine (func->mod, line);
3351 else
3353 clearBPatModLine (cctxt->func->mod, line);
3357 goto ret;
3360 if ((bp = strchr (s, ':')))
3362 module *mod = NULL;
3363 *bp = '\0';
3365 if (!applyToSet (modules, moduleWithCName, s, &mod))
3367 fprintf (stderr, "No source file named %s.\n", s);
3368 goto ret;
3371 /* case c) filename:lineno */
3372 if (isdigit (*(bp+1)))
3374 clearBPatModLine (mod, atoi (bp+1));
3375 goto ret;
3377 /* case d) filename:function */
3378 if (!applyToSet (functions, funcWithNameModule, bp+1, s, &func))
3379 fprintf (stderr, "Function \"%s\" not defined.\n", bp+1);
3380 else
3381 clearBPatModLine (mod, func->entryline);
3383 goto ret;
3386 /* case e) function */
3387 if (!applyToSet (functions, funcWithName, s, &func))
3388 fprintf (stderr, "Function \"%s\" not defined.\n", s);
3389 else
3390 clearBPatModLine (func->mod, func->entryline);
3392 ret:
3393 return 0;
3396 /*-----------------------------------------------------------------*/
3397 /* cmdSimulator - send command to simulator */
3398 /*-----------------------------------------------------------------*/
3399 int cmdSimulator (char *s, context *cctxt)
3401 char tmpstr[82];
3403 if (strlen (s) > 80)
3405 printf ("error 3A\n");
3406 exit (1);
3408 strcpy (tmpstr, s);
3409 strcat (tmpstr, "\n");
3410 sendSim (tmpstr);
3411 waitForSim (200, NULL);
3412 fprintf (stdout, "%s", simResponse());
3413 return 0;
3416 void setMainContext()
3418 function *func = NULL;
3419 currentFrame = 0;
3420 if (!applyToSet (functions, funcWithName, "_main", &func) &&
3421 !applyToSet (functions, funcWithName, "main", &func))
3423 return;
3425 discoverContext (func->sym->addr, func);
3428 function *needExtraMainFunction (void)
3430 function *func = NULL;
3431 if (!applyToSet (functions, funcWithName, "_main", &func))
3433 if (applyToSet (functions, funcWithName, "main", &func))
3435 return func;
3438 return NULL;
3441 static void printFrame (void)
3443 int i;
3444 function *func = NULL;
3446 if ( currentFrame < 0 )
3448 currentFrame = 0;
3449 fprintf (stdout, "Bottom (i.e., innermost) frame selected; you cannot go down.\n");
3450 return;
3452 STACK_STARTWALK (callStack) ;
3453 for ( i = 0; i <= currentFrame ; i++ )
3455 func = STACK_WALK (callStack);
3456 if ( !func )
3458 currentFrame = i-1;
3459 fprintf (stdout, "Initial frame selected; you cannot go up.\n");
3460 return;
3463 fprintf (stdout, "#%d 0x%08x in %s () at %s:%d\n",
3464 currentFrame, func->laddr, func->sym->name, func->mod->c_name, func->lline+1);
3465 fprintf (stdout, "\032\032%s:%d:1:beg:0x%08x\n",
3466 func->mod->cfullname, func->lline+1, func->laddr);
3468 discoverContext (func->laddr, func);
3472 /*-----------------------------------------------------------------*/
3473 /* cmdUp - Up command */
3474 /*-----------------------------------------------------------------*/
3475 int cmdUp (char *s, context *cctxt)
3477 s = trim_left (s);
3478 if ( *s )
3479 currentFrame += strtol (s, 0, 10);
3480 else
3481 currentFrame++ ;
3483 printFrame();
3484 return 0;
3487 /*-----------------------------------------------------------------*/
3488 /* cmdDown - down command */
3489 /*-----------------------------------------------------------------*/
3490 int cmdDown (char *s, context *cctxt)
3492 s = trim_left (s);
3493 if ( *s )
3494 currentFrame -= strtol (s, 0, 10);
3495 else
3496 currentFrame-- ;
3498 printFrame();
3499 return 0;
3502 /*-----------------------------------------------------------------*/
3503 /* cmdFrame - Frame command */
3504 /*-----------------------------------------------------------------*/
3505 int cmdFrame (char *s, context *cctxt)
3507 s = trim_left (s);
3508 if ( *s )
3509 currentFrame = strtol (s, 0, 10);
3510 printFrame();
3511 return 0;
3514 /*-----------------------------------------------------------------*/
3515 /* cmdFinish - exec till end of current function */
3516 /*-----------------------------------------------------------------*/
3517 int cmdFinish (char *s, context *ctxt)
3519 if (STACK_EMPTY (callStack))
3521 fprintf (stdout, "The program is not running.\n");
3522 return 0;
3525 if (srcMode == SRC_CMODE)
3527 setBreakPoint (ctxt->func->sym->eaddr, CODE, STEP,
3528 stepBpCB, ctxt->func->mod->c_name,
3529 ctxt->func->exitline);
3531 else
3533 setBreakPoint (ctxt->func->sym->eaddr, CODE, STEP,
3534 stepBpCB, ctxt->func->mod->asm_name,
3535 ctxt->func->aexitline);
3538 simGo (-1);
3539 showfull = 1;
3540 return 0;
3543 /*-----------------------------------------------------------------*/
3544 /* cmdShow - show command */
3545 /*-----------------------------------------------------------------*/
3546 int cmdShow (char *s, context *cctxt)
3548 /* skip white space */
3549 s = trim_left (s);
3551 if (strcmp (s, "copying") == 0)
3553 fputs (copying, stdout);
3554 return 0;
3557 if (strcmp (s, "warranty") == 0)
3559 fputs (warranty, stdout);
3560 return 0;
3563 return 0;