1 /*-------------------------------------------------------------------------
2 cmd.c - source file for debugger command execution
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
31 /* default number of lines to list out */
34 /* mainly used to retain a reference to the active module being
35 listed. May be used as a general context for other commands if
36 no better context is available */
37 static module *list_mod = NULL;
39 EXTERN_STACK_DCL(callStack,function *,1024);
41 #if defined(__APPLE__) && defined(__MACH__)
43 {" GNU GENERAL PUBLIC LICENSE Version 2"};
44 static char *warranty=
48 " GNU GENERAL PUBLIC LICENSE\n"
49 " Version 2, June 1991\n"
51 " Copyright (C) 1989, 1991 Free Software Foundation, Inc.\n"
52 " 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA\n"
53 " Everyone is permitted to copy and distribute verbatim copies\n"
54 " of this license document, but changing it is not allowed.\n"
58 " The licenses for most software are designed to take away your\n"
59 "freedom to share and change it. By contrast, the GNU General Public\n"
60 "License is intended to guarantee your freedom to share and change free\n"
61 "software--to make sure the software is free for all its users. This\n"
62 "General Public License applies to most of the Free Software\n"
63 "Foundation's software and to any other program whose authors commit to\n"
64 "using it. (Some other Free Software Foundation software is covered by\n"
65 "the GNU Library General Public License instead.) You can apply it to\n"
66 "your programs, too.\n"
68 " When we speak of free software, we are referring to freedom, not\n"
69 "price. Our General Public Licenses are designed to make sure that you\n"
70 "have the freedom to distribute copies of free software (and charge for\n"
71 "this service if you wish), that you receive source code or can get it\n"
72 "if you want it, that you can change the software or use pieces of it\n"
73 "in new free programs; and that you know you can do these things.\n"
75 " To protect your rights, we need to make restrictions that forbid\n"
76 "anyone to deny you these rights or to ask you to surrender the rights.\n"
77 "These restrictions translate to certain responsibilities for you if you\n"
78 "distribute copies of the software, or if you modify it.\n"
80 " For example, if you distribute copies of such a program, whether\n"
81 "gratis or for a fee, you must give the recipients all the rights that\n"
82 "you have. You must make sure that they, too, receive or can get the\n"
83 "source code. And you must show them these terms so they know their\n"
86 " We protect your rights with two steps: (1) copyright the software, and\n"
87 "(2) offer you this license which gives you legal permission to copy,\n"
88 "distribute and/or modify the software.\n"
90 " Also, for each author's protection and ours, we want to make certain\n"
91 "that everyone understands that there is no warranty for this free\n"
92 "software. If the software is modified by someone else and passed on, we\n"
93 "want its recipients to know that what they have is not the original, so\n"
94 "that any problems introduced by others will not reflect on the original\n"
95 "authors' reputations.\n"
97 " Finally, any free program is threatened constantly by software\n"
98 "patents. We wish to avoid the danger that redistributors of a free\n"
99 "program will individually obtain patent licenses, in effect making the\n"
100 "program proprietary. To prevent this, we have made it clear that any\n"
101 "patent must be licensed for everyone's free use or not licensed at all.\n"
103 " The precise terms and conditions for copying, distribution and\n"
104 "modification follow.\n"
106 " GNU GENERAL PUBLIC LICENSE\n"
107 " TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION\n"
109 " 0. This License applies to any program or other work which contains\n"
110 "a notice placed by the copyright holder saying it may be distributed\n"
111 "under the terms of this General Public License. The \"Program\", below,\n"
112 "refers to any such program or work, and a \"work based on the Program\"\n"
113 "means either the Program or any derivative work under copyright law:\n"
114 "that is to say, a work containing the Program or a portion of it,\n"
115 "either verbatim or with modifications and/or translated into another\n"
116 "language. (Hereinafter, translation is included without limitation in\n"
117 "the term \"modification\".) Each licensee is addressed as \"you\".\n"
119 "Activities other than copying, distribution and modification are not\n"
120 "covered by this License; they are outside its scope. The act of\n"
121 "running the Program is not restricted, and the output from the Program\n"
122 "is covered only if its contents constitute a work based on the\n"
123 "Program (independent of having been made by running the Program).\n"
124 "Whether that is true depends on what the Program does.\n"
126 " 1. You may copy and distribute verbatim copies of the Program's\n"
127 "source code as you receive it, in any medium, provided that you\n"
128 "conspicuously and appropriately publish on each copy an appropriate\n"
129 "copyright notice and disclaimer of warranty; keep intact all the\n"
130 "notices that refer to this License and to the absence of any warranty;\n"
131 "and give any other recipients of the Program a copy of this License\n"
132 "along with the Program.\n"
134 "You may charge a fee for the physical act of transferring a copy, and\n"
135 "you may at your option offer warranty protection in exchange for a fee.\n"
137 " 2. You may modify your copy or copies of the Program or any portion\n"
138 "of it, thus forming a work based on the Program, and copy and\n"
139 "distribute such modifications or work under the terms of Section 1\n"
140 "above, provided that you also meet all of these conditions:\n"
142 " a) You must cause the modified files to carry prominent notices\n"
143 " stating that you changed the files and the date of any change.\n"
145 " b) You must cause any work that you distribute or publish, that in\n"
146 " whole or in part contains or is derived from the Program or any\n"
147 " part thereof, to be licensed as a whole at no charge to all third\n"
148 " parties under the terms of this License.\n"
150 " c) If the modified program normally reads commands interactively\n"
151 " when run, you must cause it, when started running for such\n"
152 " interactive use in the most ordinary way, to print or display an\n"
153 " announcement including an appropriate copyright notice and a\n"
154 " notice that there is no warranty (or else, saying that you provide\n"
155 " a warranty) and that users may redistribute the program under\n"
156 " these conditions, and telling the user how to view a copy of this\n"
157 " License. (Exception: if the Program itself is interactive but\n"
158 " does not normally print such an announcement, your work based on\n"
159 " the Program is not required to print an announcement.)\n"
161 "These requirements apply to the modified work as a whole. If\n"
162 "identifiable sections of that work are not derived from the Program,\n"
163 "and can be reasonably considered independent and separate works in\n"
164 "themselves, then this License, and its terms, do not apply to those\n"
165 "sections when you distribute them as separate works. But when you\n"
166 "distribute the same sections as part of a whole which is a work based\n"
167 "on the Program, the distribution of the whole must be on the terms of\n"
168 "this License, whose permissions for other licensees extend to the\n"
169 "entire whole, and thus to each and every part regardless of who wrote it.\n"
171 "Thus, it is not the intent of this section to claim rights or contest\n"
172 "your rights to work written entirely by you; rather, the intent is to\n"
173 "exercise the right to control the distribution of derivative or\n"
174 "collective works based on the Program.\n"
176 "In addition, mere aggregation of another work not based on the Program\n"
177 "with the Program (or with a work based on the Program) on a volume of\n"
178 "a storage or distribution medium does not bring the other work under\n"
179 "the scope of this License.\n"
181 " 3. You may copy and distribute the Program (or a work based on it,\n"
182 "under Section 2) in object code or executable form under the terms of\n"
183 "Sections 1 and 2 above provided that you also do one of the following:\n"
185 " a) Accompany it with the complete corresponding machine-readable\n"
186 " source code, which must be distributed under the terms of Sections\n"
187 " 1 and 2 above on a medium customarily used for software interchange; or,\n"
189 " b) Accompany it with a written offer, valid for at least three\n"
190 " years, to give any third party, for a charge no more than your\n"
191 " cost of physically performing source distribution, a complete\n"
192 " machine-readable copy of the corresponding source code, to be\n"
193 " distributed under the terms of Sections 1 and 2 above on a medium\n"
194 " customarily used for software interchange; or,\n"
196 " c) Accompany it with the information you received as to the offer\n"
197 " to distribute corresponding source code. (This alternative is\n"
198 " allowed only for noncommercial distribution and only if you\n"
199 " received the program in object code or executable form with such\n"
200 " an offer, in accord with Subsection b above.)\n"
202 "The source code for a work means the preferred form of the work for\n"
203 "making modifications to it. For an executable work, complete source\n"
204 "code means all the source code for all modules it contains, plus any\n"
205 "associated interface definition files, plus the scripts used to\n"
206 "control compilation and installation of the executable. However, as a\n"
207 "special exception, the source code distributed need not include\n"
208 "anything that is normally distributed (in either source or binary\n"
209 "form) with the major components (compiler, kernel, and so on) of the\n"
210 "operating system on which the executable runs, unless that component\n"
211 "itself accompanies the executable.\n"
213 "If distribution of executable or object code is made by offering\n"
214 "access to copy from a designated place, then offering equivalent\n"
215 "access to copy the source code from the same place counts as\n"
216 "distribution of the source code, even though third parties are not\n"
217 "compelled to copy the source along with the object code.\n"
219 " 4. You may not copy, modify, sublicense, or distribute the Program\n"
220 "except as expressly provided under this License. Any attempt\n"
221 "otherwise to copy, modify, sublicense or distribute the Program is\n"
222 "void, and will automatically terminate your rights under this License.\n"
223 "However, parties who have received copies, or rights, from you under\n"
224 "this License will not have their licenses terminated so long as such\n"
225 "parties remain in full compliance.\n"
227 " 5. You are not required to accept this License, since you have not\n"
228 "signed it. However, nothing else grants you permission to modify or\n"
229 "distribute the Program or its derivative works. These actions are\n"
230 "prohibited by law if you do not accept this License. Therefore, by\n"
231 "modifying or distributing the Program (or any work based on the\n"
232 "Program), you indicate your acceptance of this License to do so, and\n"
233 "all its terms and conditions for copying, distributing or modifying\n"
234 "the Program or works based on it.\n"
236 " 6. Each time you redistribute the Program (or any work based on the\n"
237 "Program), the recipient automatically receives a license from the\n"
238 "original licensor to copy, distribute or modify the Program subject to\n"
239 "these terms and conditions. You may not impose any further\n"
240 "restrictions on the recipients' exercise of the rights granted herein.\n"
241 "You are not responsible for enforcing compliance by third parties to\n"
244 " 7. If, as a consequence of a court judgment or allegation of patent\n"
245 "infringement or for any other reason (not limited to patent issues),\n"
246 "conditions are imposed on you (whether by court order, agreement or\n"
247 "otherwise) that contradict the conditions of this License, they do not\n"
248 "excuse you from the conditions of this License. If you cannot\n"
249 "distribute so as to satisfy simultaneously your obligations under this\n"
250 "License and any other pertinent obligations, then as a consequence you\n"
251 "may not distribute the Program at all. For example, if a patent\n"
252 "license would not permit royalty-free redistribution of the Program by\n"
253 "all those who receive copies directly or indirectly through you, then\n"
254 "the only way you could satisfy both it and this License would be to\n"
255 "refrain entirely from distribution of the Program.\n"
257 "If any portion of this section is held invalid or unenforceable under\n"
258 "any particular circumstance, the balance of the section is intended to\n"
259 "apply and the section as a whole is intended to apply in other\n"
262 "It is not the purpose of this section to induce you to infringe any\n"
263 "patents or other property right claims or to contest validity of any\n"
264 "such claims; this section has the sole purpose of protecting the\n"
265 "integrity of the free software distribution system, which is\n"
266 "implemented by public license practices. Many people have made\n"
267 "generous contributions to the wide range of software distributed\n"
268 "through that system in reliance on consistent application of that\n"
269 "system; it is up to the author/donor to decide if he or she is willing\n"
270 "to distribute software through any other system and a licensee cannot\n"
271 "impose that choice.\n"
273 "This section is intended to make thoroughly clear what is believed to\n"
274 "be a consequence of the rest of this License.\n"
276 " 8. If the distribution and/or use of the Program is restricted in\n"
277 "certain countries either by patents or by copyrighted interfaces, the\n"
278 "original copyright holder who places the Program under this License\n"
279 "may add an explicit geographical distribution limitation excluding\n"
280 "those countries, so that distribution is permitted only in or among\n"
281 "countries not thus excluded. In such case, this License incorporates\n"
282 "the limitation as if written in the body of this License.\n"
284 " 9. The Free Software Foundation may publish revised and/or new versions\n"
285 "of the General Public License from time to time. Such new versions will\n"
286 "be similar in spirit to the present version, but may differ in detail to\n"
287 "address new problems or concerns.\n"
289 "Each version is given a distinguishing version number. If the Program\n"
290 "specifies a version number of this License which applies to it and \"any\n"
291 "later version\", you have the option of following the terms and conditions\n"
292 "either of that version or of any later version published by the Free\n"
293 "Software Foundation. If the Program does not specify a version number of\n"
294 "this License, you may choose any version ever published by the Free Software\n"
297 " 10. If you wish to incorporate parts of the Program into other free\n"
298 "programs whose distribution conditions are different, write to the author\n"
299 "to ask for permission. For software which is copyrighted by the Free\n"
300 "Software Foundation, write to the Free Software Foundation; we sometimes\n"
301 "make exceptions for this. Our decision will be guided by the two goals\n"
302 "of preserving the free status of all derivatives of our free software and\n"
303 "of promoting the sharing and reuse of software generally.\n";
305 static char *warranty=
308 " 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n"
309 "FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN\n"
310 "OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\n"
311 "PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\n"
312 "OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n"
313 "MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS\n"
314 "TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE\n"
315 "PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\n"
316 "REPAIR OR CORRECTION.\n"
318 " 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n"
319 "WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\n"
320 "REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\n"
321 "INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\n"
322 "OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\n"
323 "TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\n"
324 "YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\n"
325 "PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\n"
326 "POSSIBILITY OF SUCH DAMAGES.\n";
329 static void printTypeInfo(link *);
330 static void printValAggregates (symbol *,link *,char,unsigned int);
331 static void setSymValue(symbol *sym, char *val, context *cctxt);
333 int srcMode = SRC_CMODE ;
334 static set *dispsymbols = NULL ; /* set of displayable symbols */
336 /*-----------------------------------------------------------------*/
337 /* funcWithName - returns function with name */
338 /*-----------------------------------------------------------------*/
339 DEFSETFUNC(funcWithName)
341 function *func = item;
343 V_ARG(function **,funcp);
348 if (strcmp(func->sym->name,name) == 0) {
356 /*-----------------------------------------------------------------*/
357 /* setBPatModLine - set break point at the line specified for the */
358 /*-----------------------------------------------------------------*/
359 static void setBPatModLine (module *mod, int line)
363 /* look for the first executable line after the line
364 specified & get the break point there */
365 if (srcMode == SRC_CMODE && line > mod->ncLines) {
366 fprintf(stderr,"No line %d in file \"%s\".\n",
371 if (srcMode == SRC_AMODE && line > mod->nasmLines) {
372 fprintf(stderr,"No line %d in file \"%s\".\n",
378 for ( ; next_line < (srcMode == SRC_CMODE ? mod->ncLines : mod->nasmLines ) ;
380 if (srcMode == SRC_CMODE) {
381 if (mod->cLines[next_line]->addr) {
382 setBreakPoint (mod->cLines[next_line]->addr, CODE, USER,
383 userBpCB, mod->c_name, next_line);
389 if (mod->asmLines[next_line]->addr) {
390 setBreakPoint (mod->asmLines[next_line]->addr, CODE, USER,
391 userBpCB, mod->asm_name, next_line);
398 fprintf(stderr,"No line %d or after in file \"%s\"..\n",
404 /*-----------------------------------------------------------------*/
405 /* clearBPatModLine - clr break point at the line specified */
406 /*-----------------------------------------------------------------*/
407 static void clearBPatModLine (module *mod, int line)
409 /* look for the first executable line after the line
410 specified & get the break point there */
411 if (srcMode == SRC_CMODE && line > mod->ncLines) {
412 fprintf(stderr,"No line %d in file \"%s\".\n",
417 if (srcMode == SRC_AMODE && line > mod->ncLines) {
418 fprintf(stderr,"No line %d in file \"%s\".\n",
423 for ( ; line < (srcMode == SRC_CMODE ? mod->ncLines : mod->nasmLines ) ;
425 if (srcMode == SRC_CMODE)
426 if (mod->cLines[line]->addr) {
427 clearUSERbp (mod->cLines[line]->addr);
431 if (mod->asmLines[line]->addr) {
432 clearUSERbp (mod->asmLines[line]->addr);
440 /*-----------------------------------------------------------------*/
441 /* moduleLineWithAddr - finds and returns a line with a given address */
442 /*-----------------------------------------------------------------*/
443 DEFSETFUNC(moduleLineWithAddr)
448 V_ARG(unsigned int,addr);
449 V_ARG(module **,rmod);
455 for (i=0; i < mod->nasmLines; i++ )
457 if ( mod->asmLines[i]->addr == addr)
461 *line = mod->ncLines;
469 /*-----------------------------------------------------------------*/
470 /* funcWithNameModule - returns functions with a name module combo */
471 /*-----------------------------------------------------------------*/
472 DEFSETFUNC(funcWithNameModule)
474 function *func = item;
477 V_ARG(function **,funcp);
482 if (strcmp(func->sym->name,fname) == 0 &&
483 strcmp(func->mod->c_name,mname) == 0) {
491 /*-----------------------------------------------------------------*/
492 /* funcInAddr - given an address returns the function */
493 /*-----------------------------------------------------------------*/
494 DEFSETFUNC(funcInAddr)
496 function *func = item;
497 V_ARG(unsigned int,addr);
498 V_ARG(function **,funcp);
503 /* in the address range */
504 if (func->sym->addr <= addr &&
505 func->sym->eaddr >= addr) {
514 /*-----------------------------------------------------------------*/
515 /* setStepBp - will set STEP Bp @ function entry points */
516 /*-----------------------------------------------------------------*/
517 DEFSETFUNC(setStepBp)
519 function *func = item;
521 if (func->sym && func->sym->addr ) {
523 /* set the entry break point */
524 setBreakPoint (func->sym->addr , CODE , STEP ,
525 stepBpCB ,func->mod->c_name , func->entryline);
533 /*-----------------------------------------------------------------*/
534 /* setStepEPBp - sets a given type of bp @ the execution point */
535 /*-----------------------------------------------------------------*/
536 DEFSETFUNC(setStepEPBp)
542 setBreakPoint (ep->addr, CODE, bptype,
543 stepBpCB, mname, ep->line);
547 /*-----------------------------------------------------------------*/
548 /* setNextEPBp - sets a given type of bp @ the execution point */
549 /*-----------------------------------------------------------------*/
550 DEFSETFUNC(setNextEPBp)
556 setBreakPoint (ep->addr, CODE, bptype,
557 nextBpCB, mname, ep->line);
561 /*-----------------------------------------------------------------*/
562 /* lineAtAddr - for execution points returns the one with addr */
563 /*-----------------------------------------------------------------*/
564 DEFSETFUNC(lineAtAddr)
567 V_ARG(unsigned int,addr);
572 /* address must be an exact match */
573 if (ep->addr == addr) {
586 /*-----------------------------------------------------------------*/
587 /* lineNearAddr - for execution points returns the one with addr */
588 /*-----------------------------------------------------------------*/
589 DEFSETFUNC(lineNearAddr)
592 V_ARG(unsigned int,addr);
597 /* the line in which the address is */
598 if (ep->addr <= addr) {
611 /*-----------------------------------------------------------------*/
612 /* discoverContext - find out the current context of the bp */
613 /*-----------------------------------------------------------------*/
614 context *discoverContext (unsigned addr)
616 function *func = NULL;
620 /* find the function we are in */
621 if (!applyToSet(functions,funcInAddr,addr,&func)) {
622 if (!applyToSet(functions,funcWithName,"main") ||
623 !applyToSet(modules,moduleLineWithAddr,addr,&mod,&line))
625 fprintf(stderr, "Error?:discoverContext: cannot apply to set!\n");
628 currCtxt->func = func;
629 currCtxt->addr = addr;
630 currCtxt->modName = mod->name;
631 currCtxt->cline = func->exitline;
635 currCtxt->func = func;
636 currCtxt->addr = func->laddr = addr;
637 currCtxt->modName = func->modName;
639 /* find the c line number */
640 if(applyToSet(func->cfpoints,lineNearAddr,addr,
641 &line,&currCtxt->block,&currCtxt->level))
642 currCtxt->cline = func->lline = line;
644 currCtxt->cline = func->exitline;
646 /* find the asm line number */
648 if (applyToSet(func->afpoints,lineAtAddr,addr,
650 currCtxt->asmline = line;
652 currCtxt->asmline = -1;
658 /*-----------------------------------------------------------------*/
659 /* simGo - send 'go' cmd to simulator and wait till a break occurs */
660 /*-----------------------------------------------------------------*/
661 void simGo (unsigned int gaddr)
666 static int initial_break_flag = 0;
674 addr = simGoTillBp (gaddr);
676 /* got the pc for the break point now first
677 discover the program context i.e. module, function
678 linenumber of the source etc, etc etc */
679 ctxt = discoverContext (addr);
681 /* dispatch all the break point call back functions */
682 rv = dispatchCB (addr,ctxt);
685 /* the dispatch call back function will return
686 non-zero if an user break point has been hit
687 if not then we continue with the execution
690 if (!initial_break_flag) {
691 initial_break_flag = 1; // kludge to stop only at first run
692 fprintf(stdout, "Stopping at entry. You can now list and set breakpoints\n");
700 // I took this out, after running "run" it would just keep re-running
701 // even after a lot of break points hit. For some reason above code
702 // not triggering(dispatchCB). This seems to be by design, startup adds
703 // a bunch of breakpoints-but they are not USER breakpoints. Perhaps the
704 // debugger changed with its implementation of "go"("run"). It seems we
705 // need to add a "next" or "step" followed by a "run"...
706 // I added a "step" in simi.c when we want a resume function, this seems
709 // still there is question of how do we stop it initially, since
710 // it must be started before it can get a context. If so, we would
711 // want it to just run up to an initial entry point you'd think...
712 // I don't see why we can't set breakpoints before an initial run,
713 // this does not seem right to me.
715 // line #'s are a bit off too.
725 static int printAsmLine( function *func, module *m, long saddr, long eaddr)
729 int lastaddr = saddr+1;
734 symaddr = func->sym->addr;
735 symname = func->sym->name;
742 for (j=0,i=0; i < m->nasmLines; i++ )
744 if ( saddr >= 0 && m->asmLines[i]->addr < saddr)
748 if ( eaddr >= 0 && m->asmLines[i]->addr > eaddr)
753 (m->asmLines[i]->addr < func->sym->addr ||
754 m->asmLines[i]->addr > func->sym->eaddr ))
758 delta = m->asmLines[i]->addr - symaddr;
762 lastaddr = m->asmLines[i]->addr;
763 printf("0x%08x <%s",lastaddr,symname);
764 if (delta > 0) printf("+%d",delta);
765 printf(">:\t%s",m->asmLines[i]->src);
771 /*-----------------------------------------------------------------*/
772 /* cmdDisasm - disassemble asm instruction */
773 /*-----------------------------------------------------------------*/
774 static int cmdDisasm (char *s, context *cctxt, int args)
776 function *func = NULL;
781 /* white space skip */
785 while (*s && isspace(*s)) s++;
789 saddr = strtol(s,&s,0);
792 while (*s && isspace(*s)) s++;
795 eaddr = strtol(s,0,0);
804 /* no start or only start so dump function */
811 applyToSet(functions,funcInAddr,saddr,&func);
815 printf("Dump of assembler code for function %s:\n",func->sym->name);
816 printAsmLine(func,func->mod,-1,-1);
817 printf("End of assembler dump.\n");
822 if (applyToSet(modules,moduleLineWithAddr,saddr,&modul,NULL))
825 printf("Dump of assembler code:\n");
826 printAsmLine(NULL,modul,saddr,eaddr);
827 printf("End of assembler dump.\n");
835 printf("Dump of assembler code from 0x%08x to 0x%08x:\n",saddr,eaddr);
837 while ( saddr < eaddr )
840 if (applyToSet(functions,funcInAddr,saddr,&func))
849 if (!applyToSet(modules,moduleLineWithAddr,saddr,&modul,NULL))
852 saddr = printAsmLine(func,modul,saddr,eaddr) + 1;
857 printf("End of assembler dump.\n");
862 fprintf(stderr,"No function contains specified address.\n");
865 /*-----------------------------------------------------------------*/
866 /* cmdDisasm1 - disassemble one asm instruction */
867 /*-----------------------------------------------------------------*/
868 int cmdDisasm1 (char *s, context *cctxt)
870 return cmdDisasm( s, cctxt, 1);
873 /*-----------------------------------------------------------------*/
874 /* cmdDisasmF - disassemble asm instructions */
875 /*-----------------------------------------------------------------*/
876 int cmdDisasmF(char *s, context *cctxt)
878 return cmdDisasm( s, cctxt, 2);
881 /*-----------------------------------------------------------------*/
882 /* cmdSetUserBp - set break point at the user specified location */
883 /*-----------------------------------------------------------------*/
884 int cmdSetUserBp (char *s, context *cctxt)
887 function *func = NULL;
889 /* user break point location specification can be of the following
891 a) <nothing> - break point at current location
892 b) lineno - number of the current module
893 c) filename:lineno - line number of the given file
894 e) filename:function- function X in file Y (useful for static functions)
895 f) function - function entry point
896 g) *addr - break point at address
900 fprintf(stdout,"No symbol table is loaded. Use the \"file\" command.\n");
903 /* white space skip */
904 while (*s && isspace(*s)) s++;
906 /* null terminate it after stripping trailing blanks*/
908 while (bp != s && isspace(*bp)) bp--;
911 /* case a) nothing */
912 /* if nothing given then current location : we know
913 the current execution location from the currentContext */
916 /* if current context is known */
918 if (srcMode == SRC_CMODE)
919 /* set the break point */
920 setBreakPoint ( cctxt->addr , CODE , USER , userBpCB ,
921 cctxt->func->mod->c_name, cctxt->cline);
923 setBreakPoint ( cctxt->addr , CODE , USER , userBpCB ,
924 cctxt->func->mod->asm_name, cctxt->asmline);
928 fprintf(stderr,"No default breakpoint address now.\n");
933 if ( *s == '*' && isdigit(*(s+1)))
936 long braddr = strtol(s+1,0,0);
937 if (!applyToSet(functions,funcInAddr,braddr,&func))
940 if (!applyToSet(modules,moduleLineWithAddr,braddr,&modul,&line))
942 fprintf(stderr,"Address 0x%08x not exists in code.\n",braddr);
946 setBreakPoint ( braddr , CODE , USER , userBpCB ,
953 int line = func->exitline;
954 applyToSet(func->cfpoints,lineNearAddr,braddr,&line,NULL,NULL);
955 setBreakPoint ( braddr , CODE , USER , userBpCB ,
956 func->mod->c_name,line);
961 /* check if line number */
966 /* if current context not present then we must get the module
967 which has main & set the break point @ line number provided
968 of that module : if current context known then set the bp
969 at the line number given for the current module
972 if (!cctxt->func->mod) {
973 if (!applyToSet(functions,funcWithName,"main"))
974 fprintf(stderr,"Function \"main\" not defined.\n");
976 setBPatModLine(func->mod,line);
978 setBPatModLine(cctxt->func->mod,line);
981 setBPatModLine(list_mod,line);
983 fprintf(stdout,"Sdcdb fails to have module symbol context at %d\n", __LINE__);
990 if ((bp = strchr(s,':'))) {
995 if (srcMode == SRC_CMODE) {
996 if (!applyToSet(modules,moduleWithCName,s,&mod)) {
997 fprintf (stderr,"No source file named %s.\n",s);
1001 if (!applyToSet(modules,moduleWithAsmName,s,&mod)) {
1002 fprintf (stderr,"No source file named %s.\n",s);
1007 /* case c) filename:lineno */
1008 if (isdigit(*(bp +1))) {
1010 setBPatModLine (mod,atoi(bp+1));
1014 /* case d) filename:function */
1015 if (!applyToSet(functions,funcWithNameModule,bp+1,s,&func))
1016 fprintf(stderr,"Function \"%s\" not defined.\n",bp+1);
1018 setBPatModLine (mod,
1019 (srcMode == SRC_CMODE ?
1026 /* case e) function */
1027 if (!applyToSet(functions,funcWithName,s,&func))
1028 fprintf(stderr,"Function \"%s\" not defined.\n",s);
1030 setBPatModLine(func->mod,
1031 (srcMode == SRC_CMODE ?
1039 /*-----------------------------------------------------------------*/
1040 /* cmdListAsm - list assembler source code */
1041 /*-----------------------------------------------------------------*/
1042 int cmdListAsm (char *s, context *cctxt)
1044 if ( cctxt && cctxt->func)
1047 if (printAsmLine(cctxt->func,cctxt->func->mod,
1048 (long)cctxt->addr,(long)cctxt->addr))
1054 /*-----------------------------------------------------------------*/
1055 /* cmdSetOption - set debugger options */
1056 /*-----------------------------------------------------------------*/
1057 int cmdSetOption (char *s, context *cctxt)
1059 while (*s && isspace(*s)) s++;
1060 if (strncmp(s,"srcmode",7) == 0 ) {
1061 if (srcMode == SRC_CMODE)
1062 srcMode = SRC_AMODE;
1064 srcMode = SRC_CMODE;
1065 fprintf(stderr,"source mode set to '%s'\n",
1066 (srcMode == SRC_CMODE ? "C" : "asm"));
1070 if (strncmp(s,"variable ",9) == 0)
1075 while (isspace(*s)) s++;
1079 while (*val && !isspace(*val) && *val != '=') val++;
1080 while (isspace(*val)) *val++ = '\0';
1081 if (*val) *val++ = '\0';
1084 if ((sym = symLookup(s,cctxt)))
1086 setSymValue(sym,val,cctxt);
1089 fprintf(stdout,"No symbol \"%s\" in current context.\n",s);
1092 fprintf(stdout,"No new value for \"%s\".\n",s);
1097 fprintf(stderr,"'set %s' command not yet implemented\n",s);
1101 /*-----------------------------------------------------------------*/
1102 /* cmdContinue - continue till next break point */
1103 /*-----------------------------------------------------------------*/
1104 int cmdContinue (char *s, context *cctxt)
1106 if (!cctxt || !cctxt->func) {
1107 fprintf(stdout,"The program is not being run.\n");
1111 fprintf(stdout,"Continuing.\n");
1117 /*-----------------------------------------------------------------*/
1118 /* cmdDelUserBp - delete user break point */
1119 /*-----------------------------------------------------------------*/
1120 int cmdDelUserBp (char *s, context *cctxt)
1123 while (isspace(*s)) s++;
1126 if (userBpPresent) {
1128 fprintf (stdout,"Delete all breakpoints? (y or n) ");
1130 fgets(buffer,sizeof(buffer),stdin);
1131 if (toupper(buffer[0]) == 'Y')
1137 /* determine the break point number */
1138 if (sscanf(s,"%d",&bpnum) == 1)
1139 deleteUSERbp(bpnum);
1144 /*-----------------------------------------------------------------*/
1145 /* cmdStep - single step thru C source file */
1146 /*-----------------------------------------------------------------*/
1147 int cmdStep (char *s, context *cctxt)
1149 function *func = NULL;
1151 if (!cctxt || !cctxt->func || !cctxt->func->mod)
1152 fprintf(stdout,"The program is not being run.\n");
1154 int origSrcMode = srcMode;
1156 srcMode = SRC_AMODE;
1157 /* if we are @ the end of a function then set
1158 break points at execution points of the
1159 function in the call stack... */
1160 if (cctxt->addr == cctxt->func->sym->eaddr) {
1161 if ((func = STACK_PEEK(callStack))) {
1162 if (srcMode == SRC_CMODE)
1163 applyToSet (func->cfpoints,setStepEPBp,STEP,
1166 applyToSet (func->afpoints,setStepEPBp,STEP,
1167 func->mod->asm_name);
1170 /* set breakpoints at all function entry points
1171 and all exepoints of this functions & for
1172 all functions one up in the call stack */
1174 /* all function entry points */
1175 applyToSet(functions,setStepBp);
1177 if (srcMode == SRC_CMODE) {
1178 /* for all execution points in this function */
1179 applyToSet(cctxt->func->cfpoints,setStepEPBp,STEP,
1180 cctxt->func->mod->c_name);
1182 /* set a break point @ the current function's
1184 setBreakPoint (cctxt->func->sym->eaddr, CODE, STEP ,
1185 stepBpCB, cctxt->func->mod->c_name,
1186 cctxt->func->exitline);
1188 /* now break point @ callers execution points */
1189 if ((func = STACK_PPEEK(callStack))) {
1190 applyToSet (func->cfpoints,setStepEPBp,STEP,
1192 /* set bp @ callers exit point */
1193 setBreakPoint (func->sym->eaddr, CODE, STEP ,
1194 stepBpCB, func->mod->c_name,
1198 /* for all execution points in this function */
1199 applyToSet(cctxt->func->afpoints,setStepEPBp,STEP,
1200 cctxt->func->mod->asm_name);
1202 /* set a break point @ the current function's
1204 setBreakPoint (cctxt->func->sym->eaddr, CODE, STEP ,
1205 stepBpCB, cctxt->func->mod->asm_name,
1206 cctxt->func->aexitline);
1208 /* now break point @ callers execution points */
1209 if ((func = STACK_PPEEK(callStack))) {
1211 applyToSet (func->afpoints,setStepEPBp,STEP,
1212 func->mod->asm_name);
1214 /* set bp @ callers exit point */
1215 setBreakPoint (func->sym->eaddr, CODE, STEP ,
1216 stepBpCB, func->mod->asm_name,
1222 srcMode = origSrcMode;
1229 /*-----------------------------------------------------------------*/
1230 /* cmdNext - next executable C statement file */
1231 /*-----------------------------------------------------------------*/
1232 int cmdNext (char *s, context *cctxt)
1234 function *func = NULL;
1235 /* next is almost the same as step except we don't
1236 we don't set break point for all function entry
1238 if (!cctxt || !cctxt->func || !cctxt->func->mod)
1239 fprintf(stdout,"The program is not being run.\n");
1241 int origSrcMode = srcMode;
1243 srcMode = SRC_AMODE;
1245 /* if we are @ the end of a function then set
1246 break points at execution points of the
1247 function in the call stack... */
1248 if (cctxt->addr == cctxt->func->sym->eaddr) {
1249 if ((func = STACK_PEEK(callStack))) {
1250 if (srcMode == SRC_CMODE)
1251 applyToSet (func->cfpoints,setStepEPBp,STEP,
1254 applyToSet (func->afpoints,setStepEPBp,STEP,
1255 func->mod->asm_name);
1258 if (srcMode == SRC_CMODE) {
1259 /* for all execution points in this function */
1260 applyToSet(cctxt->func->cfpoints,setNextEPBp,NEXT,
1261 cctxt->func->mod->c_name);
1262 /* set a break point @ the current function's
1264 setBreakPoint (cctxt->func->sym->eaddr, CODE, NEXT ,
1265 nextBpCB, cctxt->func->mod->c_name,
1266 cctxt->func->exitline);
1268 /* now break point @ callers execution points */
1269 if ((func = STACK_PPEEK(callStack))) {
1270 applyToSet (func->cfpoints,setNextEPBp,NEXT ,
1272 /* set bp @ callers exit point */
1273 setBreakPoint (func->sym->eaddr, CODE, NEXT ,
1274 stepBpCB, func->mod->c_name,
1278 /* for all execution points in this function */
1279 applyToSet(cctxt->func->afpoints,setNextEPBp,NEXT,
1280 cctxt->func->mod->asm_name);
1281 /* set a break point @ the current function's
1283 setBreakPoint (cctxt->func->sym->eaddr, CODE, NEXT ,
1284 nextBpCB, cctxt->func->mod->asm_name,
1285 cctxt->func->aexitline);
1287 /* now break point @ callers execution points */
1288 if ((func = STACK_PPEEK(callStack))) {
1289 applyToSet (func->cfpoints,setNextEPBp,NEXT ,
1290 func->mod->asm_name);
1291 /* set bp @ callers exit point */
1292 setBreakPoint (func->sym->eaddr, CODE, NEXT ,
1293 stepBpCB, func->mod->asm_name,
1297 srcMode = origSrcMode;
1301 srcMode = origSrcMode;
1306 /*-----------------------------------------------------------------*/
1307 /* cmdRun - run till next break point */
1308 /*-----------------------------------------------------------------*/
1309 int cmdRun (char *s, context *cctxt)
1312 if (!cctxt || !cctxt->func || !cctxt->func->mod) {
1313 fprintf(stdout,"Starting program\n");
1316 fprintf(stdout,"No executable file specified.\nUse the \"file\" command.\n");
1323 "The program being debugged has been started already.\n");
1324 fprintf(stdout,"Start it from the beginning? (y or n) ");
1327 fgets(buff,sizeof(buff),stdin);
1328 if (toupper(buff[0]) == 'Y') {
1337 /*-----------------------------------------------------------------
1338 cmdListSymbols - list symbols
1339 |-----------------------------------------------------------------*/
1340 int cmdListSymbols (char *s, context *cctxt)
1342 int our_verbose = 0;
1346 if (strstr(s, "v1")) {
1348 } else if (strstr(s, "v2")) {
1352 printf("[symbols]\n");
1353 sy = setFirstItem(symbols);
1358 if (our_verbose <= 1)
1359 printf("<%s>", sy->name);
1361 if (our_verbose > 1) {
1362 printf(" %d) name:%s, size:%d, level:%d block:%d\n", i,
1363 sy->name, sy->size, sy->level, sy->block);
1364 printf(" isonstack:%d, isfunc:%d, offset:%d addr:%d\n",
1365 sy->isonstack, sy->isfunc, sy->offset, sy->addr);
1366 printf(" eaddr:%d, addr_type:%c, type:%x etype:%x\n",
1367 sy->eaddr, sy->addr_type, sy->type, sy->etype);
1368 printf(" scopetype:%c, sname:%s, rname:%s addrspace:%c\n",
1369 sy->scopetype, sy->sname, sy->rname, sy->addrspace);
1370 printf(" next:%x\n", sy->next);
1373 sy = setNextItem(symbols);
1375 printf(" %d symbols\n", i);
1379 /*-----------------------------------------------------------------
1380 cmdListFunctions - list functions.
1381 |-----------------------------------------------------------------*/
1382 int cmdListFunctions (char *s, context *cctxt)
1386 int our_verbose = 0;
1388 if (strstr(s, "v1")) {
1390 } else if (strstr(s, "v2")) {
1394 printf("[functions]\n");
1395 f = setFirstItem(functions);
1401 printf(" %d) sym:%x, fname:%s, modName:%s, mod:%x\n", i,
1402 f->sym, f->sym->name, f->modName, f->mod);
1403 printf(" entryline:%d, aentryline:%d, exitline:%d, aexitline:%d\n",
1404 f->entryline, f->aentryline, f->exitline, f->aexitline);
1405 printf(" cfpoints:%x, afpoints:%x, laddr:%x, lline:%d\n",
1406 f->cfpoints, f->afpoints, f->laddr, f->lline);
1409 printf("<%s>", f->modName);
1412 f = setNextItem(functions);
1414 printf(" %d functions\n", i);
1418 /*-----------------------------------------------------------------
1419 cmdListModules - list functions.
1420 |-----------------------------------------------------------------*/
1421 int cmdListModules (char *s, context *cctxt)
1426 int our_verbose = 0;
1428 if (strstr(s, "v1")) {
1430 } else if (strstr(s, "v2")) {
1434 printf("[modules]\n");
1435 m = setFirstItem(modules);
1441 if (our_verbose >= 0) {
1442 printf(" %d) cfullname:%s, afullname:%s, name:%s\n", ++mi,
1443 m->cfullname, m->afullname, m->name);
1444 printf(" c_name:%s, asm_name:%s, ncLines:%d, nasmLines:%d\n",
1445 m->c_name, m->asm_name, m->ncLines, m->nasmLines);
1446 printf(" cLines:%x, asmLines:%x\n",
1447 m->cLines, m->asmLines);
1449 if (our_verbose >= 2) {
1451 printf(" [cLines] ");
1453 for (i=0; i<m->ncLines; i++ ) {
1455 printf(" (%d) addr:%x, block:%d, level:%d, src:%s\n",
1456 i, cs->addr, cs->block, cs->level, cs->src);
1459 printf("%d records", i);
1462 printf(" [asmLines] ");
1464 for (i=0; i<m->nasmLines; i++ ) {
1465 as = m->asmLines[i];
1466 printf(" (%d) addr:%x, block:%d, level:%d, src:%s\n",
1467 i, as->addr, as->block, as->level, as->src);
1470 printf("%d records", i);
1475 m = setNextItem(modules);
1480 /*-----------------------------------------------------------------
1481 infoSymbols - This is really just a tool to dump all these
1482 huge program structures out into human readable form.
1483 |-----------------------------------------------------------------*/
1484 static void infoSymbols(context *ctxt)
1486 int our_verbose = 0;
1488 printf("[context:%x] func:%x modName:%s addr:%x\n",
1489 ctxt, ctxt->func, ctxt->modName, ctxt->addr);
1491 printf(" cline:%d asmline:%d block:%d level:%d\n",
1492 ctxt->cline, ctxt->asmline, ctxt->level);
1494 printf("[globals] currCtxt:%x, modules:%x, functions:%x symbols:%x\n",
1495 currCtxt, modules, functions, symbols);
1496 printf(" nStructs:%d, structs:%x, ssdirl:%s\n",
1497 nStructs, structs, ssdirl);
1499 /**************** modules *******************/
1504 printf("[modules]\n");
1505 m = setFirstItem(modules);
1510 printf(" %d) cfullname:%s, afullname:%s, name:%s\n", ++mi,
1511 m->cfullname, m->afullname, m->name);
1512 printf(" c_name:%s, asm_name:%s, ncLines:%d, nasmLines:%d\n",
1513 m->c_name, m->asm_name, m->ncLines, m->nasmLines);
1514 printf(" cLines:%x, asmLines:%x\n",
1515 m->cLines, m->asmLines);
1518 cs = m->cLines[i++];
1519 printf(" [cLines] ");
1522 printf(" (%d) addr:%x, block:%d, level:%d, src:%s\n",
1523 i, cs->addr, cs->block, cs->level, cs->src);
1524 cs = m->cLines[i++];
1527 printf("%d records", i);
1531 as = m->asmLines[i++];
1532 printf(" [asmLines] ");
1535 printf(" (%d) addr:%x, block:%d, level:%d, src:%s\n",
1536 i, as->addr, as->block, as->level, as->src);
1537 as = m->asmLines[i++];
1540 printf("%d records", i);
1544 m = setNextItem(modules);
1548 /**************** functions *******************/
1552 printf("[functions]\n");
1553 f = setFirstItem(functions);
1559 printf(" %d) sym:%x, modName:%s, mod:%x\n", i,
1560 f->sym, f->modName, f->mod);
1561 printf(" entryline:%d, aentryline:%d, exitline:%d, aexitline:%d\n",
1562 f->entryline, f->aentryline, f->exitline, f->aexitline);
1563 printf(" cfpoints:%x, afpoints:%x, laddr:%x, lline:%d\n",
1564 f->cfpoints, f->afpoints, f->laddr, f->lline);
1567 f = setNextItem(functions);
1570 printf(" %d functions\n", i);
1573 /**************** symbols *******************/
1577 printf("[symbols]\n");
1578 s = setFirstItem(symbols);
1584 printf(" %d) name:%s, size:%d, level:%d block:%d\n", i,
1585 s->name, s->size, s->level, s->block);
1586 printf(" isonstack:%d, isfunc:%d, offset:%d addr:%d\n",
1587 s->isonstack, s->isfunc, s->offset, s->addr);
1588 printf(" eaddr:%d, addr_type:%c, type:%x etype:%x\n",
1589 s->eaddr, s->addr_type, s->type, s->etype);
1590 printf(" scopetype:%c, sname:%s, rname:%s addrspace:%c\n",
1591 s->scopetype, s->sname, s->rname, s->addrspace);
1592 printf(" next:%x\n", s->next);
1595 s = setNextItem(symbols);
1598 printf(" %d symbols\n", i);
1603 /*-----------------------------------------------------------------*/
1604 /* infoStack - print call stack information */
1605 /*-----------------------------------------------------------------*/
1606 static void infoStack(context *ctxt)
1611 STACK_STARTWALK(callStack) ;
1612 while ((func = STACK_WALK(callStack))) {
1614 fprintf(stdout,"#%d 0x%04x %s () at %s:%d\n",i++,
1615 func->laddr,func->sym->name,
1616 func->mod->c_name,func->lline);
1621 /*-----------------------------------------------------------------*/
1622 /* cmdWhere - where command */
1623 /*-----------------------------------------------------------------*/
1624 int cmdWhere(char *s, context *cctxt)
1631 /*-----------------------------------------------------------------*/
1632 /* cmdUp - Up command */
1633 /*-----------------------------------------------------------------*/
1634 int cmdUp(char *s, context *cctxt)
1639 /*-----------------------------------------------------------------*/
1640 /* cmdDown - down command */
1641 /*-----------------------------------------------------------------*/
1642 int cmdDown(char *s, context *cctxt)
1647 static int infomode = 0;
1648 /*-----------------------------------------------------------------*/
1649 /* cmdInfo - info command */
1650 /*-----------------------------------------------------------------*/
1651 int cmdInfo (char *s, context *cctxt)
1653 while (isspace(*s)) s++;
1655 /* list all break points */
1656 if (strncmp(s,"break",5) == 0) {
1661 /* info frame same as frame */
1662 if (strcmp(s,"frame") == 0) {
1668 if (strncmp(s,"line",4) == 0) {
1670 cmdListSrc (s+4,cctxt);
1674 /* info stack display call stack */
1675 if (strcmp(s,"stack") == 0) {
1681 /* info stack display call stack */
1682 if (strcmp(s,"registers") == 0) {
1683 fprintf(stdout,"%s",simRegs());
1687 /* info stack display call stack */
1688 if (strcmp(s,"all-registers") == 0) {
1689 fprintf(stdout,"%s",simRegs());
1690 fprintf(stdout,"Special Function Registers:\n");
1691 sendSim("ds 0x80 0x100\n");
1692 waitForSim(100,NULL);
1693 fprintf(stdout,simResponse());
1697 /* info stack display call stack */
1698 if (strcmp(s,"symbols") == 0) {
1699 /* dump out symbols we have read in */
1700 fprintf(stdout,"Dumping symbols...\n");
1705 if (strcmp(s,"variables") == 0) {
1706 /* dump out symbols we have read in */
1707 fprintf(stdout,"Dumping symbols...\n");
1712 fprintf(stdout,"Undefined info command: \"%s\". Try \"help\n",s);
1717 /*-----------------------------------------------------------------*/
1718 /* cmdQuit - quit debugging */
1719 /*-----------------------------------------------------------------*/
1720 int cmdQuit (char *s, context *cctxt)
1727 /*-----------------------------------------------------------------*/
1728 /* cmdListSrc - list src */
1729 /*-----------------------------------------------------------------*/
1730 int cmdListSrc (char *s, context *cctxt)
1732 static int currline = 0;
1735 int llines = listLines;
1737 while (*s && isspace(*s)) s++;
1739 /* if the user has spcified line numer then the line number
1740 can be of the following formats
1741 LINE - just line number
1742 FILE:LINE - filename line number
1743 FUNCTION - list a function
1744 FILE:FUNCTION - function in file */
1749 sscanf(s,"%d",&pline);
1750 if (!cctxt || !cctxt->func || !cctxt->func->mod) {
1752 fprintf(stdout,"Sdcdb fails to have a proper context at %d.\n", __LINE__);
1757 list_mod = cctxt->func->mod;
1761 function *func = NULL;
1763 /* if ':' present then FILE:LINE || FILE:FUNCTION */
1764 if ((bp = strchr(s,':'))) {
1769 list_mod=NULL; /* bug fix 2-09-02, moduleWithCName expects mod to be null */
1770 if (srcMode == SRC_CMODE) {
1771 if (!applyToSet(modules,moduleWithCName,s,&list_mod)) {
1772 fprintf (stderr,"No c source file named %s.\n",s);
1776 if (!applyToSet(modules,moduleWithAsmName,s,&list_mod)) {
1777 fprintf (stderr,"No source file named %s.\n",s);
1781 sscanf(bp,"%d",&pline);
1784 if (!applyToSet(functions,funcWithNameModule,bp,s,&func)) {
1785 fprintf(stdout,"Function \"%s\" not defined.\n",bp);
1788 list_mod = func->mod;
1789 if (srcMode == SRC_CMODE) {
1790 pline = func->entryline;
1791 llines = func->exitline - func->entryline + 1;
1793 pline = func->aentryline;
1794 llines = func->aexitline - func->aentryline + 1;
1800 if (!applyToSet(functions,funcWithName,s,&func)) {
1801 fprintf(stderr,"Function \"%s\" not defined.\n",s);
1805 list_mod = func->mod;
1806 if (srcMode == SRC_CMODE) {
1807 pline = func->entryline;
1808 llines = func->exitline - func->entryline + 1;
1810 pline = func->aentryline;
1811 llines = func->aexitline - func->aentryline + 1;
1817 /* if no line specified & we had listed
1818 before then continue from that listing */
1822 if (!cctxt || !cctxt->func || !cctxt->func->mod) {
1823 fprintf(stdout,"Missing context at %d. Try list filename:lineno\n", __LINE__);
1826 list_mod = cctxt->func->mod;
1827 if (srcMode == SRC_CMODE)
1828 pline = cctxt->cline;
1830 pline = cctxt->asmline;
1835 fprintf(stdout,"Sdcdb fails to have a valid module context at %d.\n", __LINE__);
1839 if (llines > listLines) llines = listLines;
1843 fprintf(stdout,"Line %d of \"%s\" starts at address 0x%08x.\n",pline,
1844 list_mod->c_name, list_mod->cLines[pline]->addr);
1848 for ( i = 0 ; i < llines ; i++ ) {
1849 if (srcMode == SRC_CMODE) {
1850 if ( (pline + i) >= list_mod->ncLines )
1852 fprintf(stdout,"%d\t%s",pline + i,
1853 list_mod->cLines[pline +i]->src);
1855 if ( (pline + i) >= list_mod->nasmLines )
1857 fprintf(stdout,"%d\t%s",pline + i,
1858 list_mod->asmLines[pline +i]->src);
1861 currline = pline + i ;
1865 static void setValBasic(symbol *sym, char *val)
1879 if (IS_FLOAT(sym->type))
1880 v.f = strtof(val,NULL);
1882 if (IS_PTR(sym->type))
1883 v.sval = strtol(val,NULL,0);
1886 if (IS_SPEC(sym->type) && IS_INTEGRAL(sym->type))
1888 if (IS_CHAR(sym->etype))
1893 if (IS_INT(sym->etype))
1894 if (IS_LONG(sym->etype))
1895 if (SPEC_USIGN(sym->etype))
1896 v.val = strtol(val,NULL,0);
1898 v.sval = strtol(val,NULL,0);
1900 v.i.lo = strtol(val,NULL,0);
1902 v.sval = strtol(val,NULL,0);
1905 v.sval = strtol(val,NULL,0);
1907 simSetValue(sym->addr,sym->addrspace,sym->size,v.sval);
1910 /*-----------------------------------------------------------------*/
1911 /* printValBasic - print value of basic types */
1912 /*-----------------------------------------------------------------*/
1913 static void printValBasic(symbol *sym,unsigned addr,char mem, int size)
1929 v.val = simGetValue(addr,mem,size);
1930 /* if this a floating point number then */
1931 if (IS_FLOAT(sym->type))
1932 fprintf(stdout,"%f",v.f);
1934 if (IS_PTR(sym->type))
1935 fprintf(stdout,"0x%x",v.val);
1937 if (IS_SPEC(sym->type) && IS_INTEGRAL(sym->type)) {
1938 if (IS_CHAR(sym->etype))
1940 if ( isprint(v.val))
1941 fprintf(stdout,"%d 0x%x '%c'",v.val,v.val,v.val);
1943 fprintf(stdout,"%d 0x%x '\\%o'",v.val,v.val,v.val);
1946 if (IS_INT(sym->etype))
1947 if (IS_LONG(sym->etype))
1948 if (SPEC_USIGN(sym->etype))
1949 fprintf(stdout,"%d 0x%x",v.val,v.val);
1951 fprintf(stdout,"%d 0x%x",v.sval,v.sval);
1953 fprintf(stdout,"%d 0x%x",v.i.lo,v.i.lo);
1955 fprintf(stdout,"0x%x",v.val);
1957 fprintf(stdout,"0x%x",v.val);
1962 /*-----------------------------------------------------------------*/
1963 /* printValFunc - prints function values */
1964 /*-----------------------------------------------------------------*/
1965 static void printValFunc (symbol *sym)
1967 fprintf(stdout,"print function not yet implemented");
1970 /*-----------------------------------------------------------------*/
1971 /* printArrayValue - will print the values of array elements */
1972 /*-----------------------------------------------------------------*/
1973 static void printArrayValue (symbol *sym, char space, unsigned int addr)
1975 link *elem_type = sym->type->next;
1978 fprintf(stdout," { ");
1979 for (i = 0 ; i < DCL_ELEM(sym->type) ; i++) {
1980 if (IS_AGGREGATE(elem_type)) {
1981 printValAggregates(sym,elem_type,space,addr);
1983 printValBasic(sym,addr,space,getSize(elem_type));
1985 addr += getSize(elem_type);
1986 if (i != DCL_ELEM(sym->type) -1)
1987 fprintf(stdout,",");
1990 fprintf(stdout,"}");
1993 /*-----------------------------------------------------------------*/
1994 /* printStructValue - prints structures elements */
1995 /*-----------------------------------------------------------------*/
1996 static void printStructValue (symbol *sym,link *type, char space, unsigned int addr)
1998 symbol *fields = SPEC_STRUCT(type)->fields;
2000 fprintf(stdout," { ");
2002 fprintf(stdout,"%s = ",fields->name);
2003 if (IS_AGGREGATE(fields->type)) {
2004 printValAggregates(fields,fields->type,space, addr);
2006 printValBasic(fields,addr,space,getSize(fields->type));
2008 addr += getSize(fields->type);
2009 fields = fields->next;
2011 fprintf(stdout,"}");
2014 /*-----------------------------------------------------------------*/
2015 /* printValAggregates - print value of aggregates */
2016 /*-----------------------------------------------------------------*/
2017 static void printValAggregates (symbol *sym, link *type,char space,unsigned int addr)
2020 if (IS_ARRAY(type)) {
2021 printArrayValue(sym, space, addr);
2025 if (IS_STRUCT(type)) {
2026 printStructValue(sym,sym->type,space, addr);
2031 /*-----------------------------------------------------------------*/
2032 /* setSymValue - set value of a symbol */
2033 /*-----------------------------------------------------------------*/
2034 static void setSymValue(symbol *sym, char *val, context *cctxt)
2038 symbol *bp = symLookup("bp",cctxt);
2041 fprintf(stdout,"cannot determine stack frame\n");
2045 sym->addr = simGetValue(bp->addr,bp->addrspace,bp->size)
2048 /* arrays & structures first */
2049 if (IS_AGGREGATE(sym->type))
2054 if (IS_FUNC(sym->type))
2059 setValBasic(sym,val);
2063 /*-----------------------------------------------------------------*/
2064 /* printSymValue - print value of a symbol */
2065 /*-----------------------------------------------------------------*/
2066 static void printSymValue (symbol *sym, context *cctxt, int flg, int dnum)
2068 static int stack = 1;
2070 /* if it is on stack then compute address & fall thru */
2071 if (sym->isonstack) {
2072 symbol *bp = symLookup("bp",cctxt);
2074 fprintf(stdout,"cannot determine stack frame\n");
2078 sym->addr = simGetValue(bp->addr,bp->addrspace,bp->size)
2082 /* get the value from the simulator and
2090 fprintf(stdout,"$%d = ",stack++);
2093 fprintf(stdout,"%d: %s = ",dnum,sym->name);
2096 /* arrays & structures first */
2097 if (IS_AGGREGATE(sym->type))
2098 printValAggregates(sym,sym->type,sym->addrspace,sym->addr);
2101 if (IS_FUNC(sym->type))
2104 printValBasic(sym,sym->addr,sym->addrspace,sym->size);
2105 if ( flg > 0 ) fprintf(stdout,"\n");
2106 //fprintf(stdout,"(BASIC %x,%c,%d)\n",sym->addr,sym->addrspace,sym->size);
2110 /*-----------------------------------------------------------------*/
2111 /* printStructInfo - print out structure information */
2112 /*-----------------------------------------------------------------*/
2113 static void printStructInfo (structdef *sdef)
2115 symbol *field = sdef->fields ;
2120 field = field->next;
2123 fprintf(stdout,"%s %s {\n",(i ? "struct" : "union" ), sdef->tag);
2124 field = sdef->fields;
2126 printTypeInfo (field->type);
2127 fprintf(stdout," %s ;\n",field->name);
2128 field = field->next ;
2131 fprintf(stdout,"}\n");
2135 /*-----------------------------------------------------------------*/
2136 /* printTypeInfo - print out the type information */
2137 /*-----------------------------------------------------------------*/
2138 static void printTypeInfo(link *p)
2144 switch (DCL_TYPE(p)) {
2146 printTypeInfo (p->next);
2147 fprintf(stdout,"()");
2150 printTypeInfo (p->next);
2151 fprintf(stdout,"[%d]",DCL_ELEM(p));
2157 printTypeInfo (p->next);
2158 fprintf(stdout,"(_near *)");
2162 printTypeInfo (p->next);
2163 fprintf(stdout,"(_xdata *)");
2167 printTypeInfo( p->next);
2168 fprintf(stdout,"(_code *)");
2172 printTypeInfo( p->next);
2173 fprintf(stdout,"(_generic *)");
2177 switch (SPEC_NOUN(p)) { /* depending on the specifier type */
2179 (IS_LONG(p) ? fputs("long ",stdout) :
2180 ( IS_SHORT(p) ? fputs("short ",stdout) :
2181 fputs("int ",stdout))) ;
2184 fputs("float ",stdout);
2188 fputs ("char ",stdout);
2192 fputs("void ",stdout);
2196 printStructInfo (SPEC_STRUCT(p));
2200 fputs("sbit ",stdout);
2204 fprintf(stdout,": %d" ,SPEC_BLEN(p));
2210 /*-----------------------------------------------------------------*/
2211 /* cmdPrint - print value of variable */
2212 /*-----------------------------------------------------------------*/
2213 int cmdPrint (char *s, context *cctxt)
2216 char *bp = s+strlen(s) -1;
2218 while (isspace(*s)) s++;
2220 while (isspace(*bp)) bp--;
2224 if ((sym = symLookup(s,cctxt))) {
2225 printSymValue(sym,cctxt,1,0);
2228 "No symbol \"%s\" in current context.\n",
2234 /*-----------------------------------------------------------------*/
2235 /* cmdOutput - print value of variable without number and newline */
2236 /*-----------------------------------------------------------------*/
2237 int cmdOutput (char *s, context *cctxt)
2240 char *bp = s+strlen(s) -1;
2242 while (isspace(*s)) s++;
2244 while (isspace(*bp)) bp--;
2248 if ((sym = symLookup(s,cctxt))) {
2249 printSymValue(sym,cctxt,0,0);
2252 "No symbol \"%s\" in current context.",
2258 typedef struct _dsymbol
2264 /** find display entry with this number */
2266 DEFSETFUNC(dsymWithNumber)
2268 dsymbol *dsym = item;
2270 V_ARG(dsymbol **,dsymp);
2272 if ( dsym->dnum == dnum )
2280 /*-----------------------------------------------------------------*/
2281 /* displayAll - display all valid variables */
2282 /*-----------------------------------------------------------------*/
2283 void displayAll(context *cctxt)
2289 for (dsym = setFirstItem(dispsymbols);
2291 dsym = setNextItem(dispsymbols))
2293 if ( (sym = symLookup(dsym->name,cctxt)))
2294 printSymValue(sym,cctxt,2,dsym->dnum);
2298 /*-----------------------------------------------------------------*/
2299 /* cmdDisplay - display value of variable */
2300 /*-----------------------------------------------------------------*/
2301 int cmdDisplay (char *s, context *cctxt)
2304 char *bp = s+strlen(s) -1;
2305 static int dnum = 1;
2307 while (isspace(*s)) s++;
2313 while (isspace(*bp)) bp--;
2317 if ((sym = symLookup(s,cctxt)))
2319 dsymbol *dsym = (dsymbol *)Safe_calloc(1,sizeof(dsymbol));
2320 dsym->dnum = dnum++ ;
2321 dsym->name = sym->name;
2322 addSetHead(&dispsymbols,dsym);
2326 fprintf(stdout,"No symbol \"%s\" in current context.\n",
2332 /*-----------------------------------------------------------------*/
2333 /* cmdUnDisplay - undisplay value of variable */
2334 /*-----------------------------------------------------------------*/
2335 int cmdUnDisplay (char *s, context *cctxt)
2340 while (isspace(*s)) s++;
2343 deleteSet(&dispsymbols);
2348 dnum = strtol(s,&s,10);
2349 if (applyToSetFTrue(dispsymbols,dsymWithNumber,dnum,&dsym))
2351 deleteSetItem(&dispsymbols,dsym);
2355 fprintf(stdout,"Arguments must be display numbers.\n");
2361 /*-----------------------------------------------------------------*/
2362 /* cmdPrintType - print type of a variable */
2363 /*-----------------------------------------------------------------*/
2364 int cmdPrintType (char *s, context *cctxt)
2367 char *bp = s+strlen(s) -1;
2369 while (isspace(*s)) s++;
2371 while (isspace(*bp)) bp--;
2375 if ((sym = symLookup(s,cctxt))) {
2376 printTypeInfo(sym->type);
2377 fprintf(stdout,"\n");
2380 "No symbol \"%s\" in current context.\n",
2386 /*-----------------------------------------------------------------*/
2387 /* cmdClrUserBp - clear user break point */
2388 /*-----------------------------------------------------------------*/
2389 int cmdClrUserBp (char *s, context *cctxt)
2392 function *func = NULL;
2394 /* clear break point location specification can be of the following
2396 a) <nothing> - break point at current location
2397 b) lineno - number of the current module
2398 c) filename:lineno - line number of the given file
2399 e) filename:function- function X in file Y (useful for static functions)
2400 f) function - function entry point
2404 fprintf(stdout,"No symbol table is loaded. Use the \"file\" command.\n");
2408 /* white space skip */
2409 while (*s && isspace(*s)) s++;
2411 /* null terminate it after stripping trailing blanks*/
2413 while (bp != s && isspace(*bp)) bp--;
2416 /* case a) nothing */
2417 /* if nothing given then current location : we know
2418 the current execution location from the currentContext */
2421 /* if current context is known */
2423 /* clear the break point @ current location */
2424 clearUSERbp (cctxt->addr);
2426 fprintf(stderr,"No default breakpoint address now.\n");
2431 /* case b) lineno */
2432 /* check if line number */
2434 /* get the lineno */
2437 /* if current context not present then we must get the module
2438 which has main & set the break point @ line number provided
2439 of that module : if current context known then set the bp
2440 at the line number given for the current module
2443 if (!cctxt->func->mod) {
2444 if (!applyToSet(functions,funcWithName,"main"))
2445 fprintf(stderr,"Function \"main\" not defined.\n");
2447 clearBPatModLine(func->mod,line);
2449 clearBPatModLine(cctxt->func->mod,line);
2455 if ((bp = strchr(s,':'))) {
2460 if (!applyToSet(modules,moduleWithCName,s,&mod)) {
2461 fprintf (stderr,"No source file named %s.\n",s);
2465 /* case c) filename:lineno */
2466 if (isdigit(*(bp +1))) {
2468 clearBPatModLine (mod,atoi(bp+1));
2472 /* case d) filename:function */
2473 if (!applyToSet(functions,funcWithNameModule,bp+1,s,&func))
2474 fprintf(stderr,"Function \"%s\" not defined.\n",bp+1);
2476 clearBPatModLine (mod,func->entryline);
2481 /* case e) function */
2482 if (!applyToSet(functions,funcWithName,s,&func))
2483 fprintf(stderr,"Function \"%s\" not defined.\n",s);
2485 clearBPatModLine(func->mod,func->entryline);
2492 /*-----------------------------------------------------------------*/
2493 /* cmdSimulator - send command to simulator */
2494 /*-----------------------------------------------------------------*/
2495 int cmdSimulator (char *s, context *cctxt)
2499 if (strlen(s) > 80) {
2500 printf("error 3A\n");
2504 strcat(tmpstr, "\n");
2506 waitForSim(200,NULL);
2507 fprintf(stdout,"%s",simResponse());
2511 /*-----------------------------------------------------------------*/
2512 /* cmdFrame - Frame command */
2513 /*-----------------------------------------------------------------*/
2514 int cmdFrame (char *s, context *cctxt)
2518 if ((func = STACK_PEEK(callStack))) {
2519 fprintf(stdout,"#0 %s () at %s:%d\n",
2520 func->sym->name,func->mod->c_name,cctxt->cline);
2522 if (cctxt->cline < func->mod->ncLines)
2523 fprintf(stdout,"%d\t%s",
2525 func->mod->cLines[cctxt->cline]->src);
2527 fprintf(stdout,"No stack.\n");
2531 /*-----------------------------------------------------------------*/
2532 /* cmdFinish - exec till end of current function */
2533 /*-----------------------------------------------------------------*/
2534 int cmdFinish (char *s, context *ctxt)
2536 if (!ctxt || ! ctxt->func) {
2537 fprintf(stdout,"The program is not running.\n");
2541 if (srcMode == SRC_CMODE) {
2542 setBreakPoint (ctxt->func->sym->eaddr, CODE, STEP,
2543 stepBpCB, ctxt->func->mod->c_name,
2544 ctxt->func->exitline);
2546 setBreakPoint (ctxt->func->sym->eaddr, CODE, STEP,
2547 stepBpCB, ctxt->func->mod->asm_name,
2548 ctxt->func->aexitline);
2557 /*-----------------------------------------------------------------*/
2558 /* cmdShow - show command */
2559 /*-----------------------------------------------------------------*/
2560 int cmdShow (char *s, context *cctxt)
2562 /* skip white space */
2563 while (*s && isspace(*s)) s++ ;
2565 if (strcmp(s,"copying") == 0) {
2566 fputs(copying,stdout);
2570 if (strcmp(s,"warranty") == 0) {
2571 fputs(warranty,stdout);