Martins ddd changes
[fw/sdcc] / debugger / mcs51 / cmd.c
1 /*-------------------------------------------------------------------------
2     cmd.c - source  file for debugger command execution
3
4               Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1999)
5
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
9    later version.
10    
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.
15    
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.
19    
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 -------------------------------------------------------------------------*/
24
25 #include "sdcdb.h"
26 #include "symtab.h"
27 #include "simi.h"
28 #include "break.h"
29 #include "cmd.h"
30 #include "newalloc.h"
31
32 /* default number of lines to list out */
33 int listLines = 16;
34
35 /* mainly used to retain a reference to the active module being
36    listed.  May be used as a general context for other commands if
37    no better context is available */
38 static module *list_mod = NULL;
39
40 EXTERN_STACK_DCL(callStack,function *,1024);
41
42 #if defined(__APPLE__) && defined(__MACH__)
43 static char *copying=
44 {" GNU GENERAL PUBLIC LICENSE Version 2"};
45 static char *warranty=
46 {" NO WARRANTY"};
47 #else
48 static char *copying=
49 "                   GNU GENERAL PUBLIC LICENSE\n"
50 "                       Version 2, June 1991\n"
51 "\n"
52 " Copyright (C) 1989, 1991 Free Software Foundation, Inc.\n"
53 " 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA\n"
54 " Everyone is permitted to copy and distribute verbatim copies\n"
55 " of this license document, but changing it is not allowed.\n"
56 "\n"
57 "                            Preamble\n"
58 "\n"
59 "  The licenses for most software are designed to take away your\n"
60 "freedom to share and change it.  By contrast, the GNU General Public\n"
61 "License is intended to guarantee your freedom to share and change free\n"
62 "software--to make sure the software is free for all its users.  This\n"
63 "General Public License applies to most of the Free Software\n"
64 "Foundation's software and to any other program whose authors commit to\n"
65 "using it.  (Some other Free Software Foundation software is covered by\n"
66 "the GNU Library General Public License instead.)  You can apply it to\n"
67 "your programs, too.\n"
68 "\n"
69 "  When we speak of free software, we are referring to freedom, not\n"
70 "price.  Our General Public Licenses are designed to make sure that you\n"
71 "have the freedom to distribute copies of free software (and charge for\n"
72 "this service if you wish), that you receive source code or can get it\n"
73 "if you want it, that you can change the software or use pieces of it\n"
74 "in new free programs; and that you know you can do these things.\n"
75 "\n"
76 "  To protect your rights, we need to make restrictions that forbid\n"
77 "anyone to deny you these rights or to ask you to surrender the rights.\n"
78 "These restrictions translate to certain responsibilities for you if you\n"
79 "distribute copies of the software, or if you modify it.\n"
80 "\n"
81 "  For example, if you distribute copies of such a program, whether\n"
82 "gratis or for a fee, you must give the recipients all the rights that\n"
83 "you have.  You must make sure that they, too, receive or can get the\n"
84 "source code.  And you must show them these terms so they know their\n"
85 "rights.\n"
86 "\n"
87 "  We protect your rights with two steps: (1) copyright the software, and\n"
88 "(2) offer you this license which gives you legal permission to copy,\n"
89 "distribute and/or modify the software.\n"
90 "\n"
91 "  Also, for each author's protection and ours, we want to make certain\n"
92 "that everyone understands that there is no warranty for this free\n"
93 "software.  If the software is modified by someone else and passed on, we\n"
94 "want its recipients to know that what they have is not the original, so\n"
95 "that any problems introduced by others will not reflect on the original\n"
96 "authors' reputations.\n"
97 "\n"
98 "  Finally, any free program is threatened constantly by software\n"
99 "patents.  We wish to avoid the danger that redistributors of a free\n"
100 "program will individually obtain patent licenses, in effect making the\n"
101 "program proprietary.  To prevent this, we have made it clear that any\n"
102 "patent must be licensed for everyone's free use or not licensed at all.\n"
103 "\n"
104 "  The precise terms and conditions for copying, distribution and\n"
105 "modification follow.\n"
106 "^L\n"
107 "                    GNU GENERAL PUBLIC LICENSE\n"
108 "   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION\n"
109 "\n"
110 "  0. This License applies to any program or other work which contains\n"
111 "a notice placed by the copyright holder saying it may be distributed\n"
112 "under the terms of this General Public License.  The \"Program\", below,\n"
113 "refers to any such program or work, and a \"work based on the Program\"\n"
114 "means either the Program or any derivative work under copyright law:\n"
115 "that is to say, a work containing the Program or a portion of it,\n"
116 "either verbatim or with modifications and/or translated into another\n"
117 "language.  (Hereinafter, translation is included without limitation in\n"
118 "the term \"modification\".)  Each licensee is addressed as \"you\".\n"
119 "\n"
120 "Activities other than copying, distribution and modification are not\n"
121 "covered by this License; they are outside its scope.  The act of\n"
122 "running the Program is not restricted, and the output from the Program\n"
123 "is covered only if its contents constitute a work based on the\n"
124 "Program (independent of having been made by running the Program).\n"
125 "Whether that is true depends on what the Program does.\n"
126 "\n"
127 "  1. You may copy and distribute verbatim copies of the Program's\n"
128 "source code as you receive it, in any medium, provided that you\n"
129 "conspicuously and appropriately publish on each copy an appropriate\n"
130 "copyright notice and disclaimer of warranty; keep intact all the\n"
131 "notices that refer to this License and to the absence of any warranty;\n"
132 "and give any other recipients of the Program a copy of this License\n"
133 "along with the Program.\n"
134 "\n"
135 "You may charge a fee for the physical act of transferring a copy, and\n"
136 "you may at your option offer warranty protection in exchange for a fee.\n"
137 "\n"
138 "  2. You may modify your copy or copies of the Program or any portion\n"
139 "of it, thus forming a work based on the Program, and copy and\n"
140 "distribute such modifications or work under the terms of Section 1\n"
141 "above, provided that you also meet all of these conditions:\n"
142 "\n"
143 "    a) You must cause the modified files to carry prominent notices\n"
144 "    stating that you changed the files and the date of any change.\n"
145 "\n"
146 "    b) You must cause any work that you distribute or publish, that in\n"
147 "    whole or in part contains or is derived from the Program or any\n"
148 "    part thereof, to be licensed as a whole at no charge to all third\n"
149 "    parties under the terms of this License.\n"
150 "\n"
151 "    c) If the modified program normally reads commands interactively\n"
152 "    when run, you must cause it, when started running for such\n"
153 "    interactive use in the most ordinary way, to print or display an\n"
154 "    announcement including an appropriate copyright notice and a\n"
155 "    notice that there is no warranty (or else, saying that you provide\n"
156 "    a warranty) and that users may redistribute the program under\n"
157 "    these conditions, and telling the user how to view a copy of this\n"
158 "    License.  (Exception: if the Program itself is interactive but\n"
159 "    does not normally print such an announcement, your work based on\n"
160 "    the Program is not required to print an announcement.)\n"
161 "\n"
162 "These requirements apply to the modified work as a whole.  If\n"
163 "identifiable sections of that work are not derived from the Program,\n"
164 "and can be reasonably considered independent and separate works in\n"
165 "themselves, then this License, and its terms, do not apply to those\n"
166 "sections when you distribute them as separate works.  But when you\n"
167 "distribute the same sections as part of a whole which is a work based\n"
168 "on the Program, the distribution of the whole must be on the terms of\n"
169 "this License, whose permissions for other licensees extend to the\n"
170 "entire whole, and thus to each and every part regardless of who wrote it.\n"
171 "\n"
172 "Thus, it is not the intent of this section to claim rights or contest\n"
173 "your rights to work written entirely by you; rather, the intent is to\n"
174 "exercise the right to control the distribution of derivative or\n"
175 "collective works based on the Program.\n"
176 "\n"
177 "In addition, mere aggregation of another work not based on the Program\n"
178 "with the Program (or with a work based on the Program) on a volume of\n"
179 "a storage or distribution medium does not bring the other work under\n"
180 "the scope of this License.\n"
181 "\n"
182 "  3. You may copy and distribute the Program (or a work based on it,\n"
183 "under Section 2) in object code or executable form under the terms of\n"
184 "Sections 1 and 2 above provided that you also do one of the following:\n"
185 "\n"
186 "    a) Accompany it with the complete corresponding machine-readable\n"
187 "    source code, which must be distributed under the terms of Sections\n"
188 "    1 and 2 above on a medium customarily used for software interchange; or,\n"
189 "\n"
190 "    b) Accompany it with a written offer, valid for at least three\n"
191 "    years, to give any third party, for a charge no more than your\n"
192 "    cost of physically performing source distribution, a complete\n"
193 "    machine-readable copy of the corresponding source code, to be\n"
194 "    distributed under the terms of Sections 1 and 2 above on a medium\n"
195 "    customarily used for software interchange; or,\n"
196 "\n"
197 "    c) Accompany it with the information you received as to the offer\n"
198 "    to distribute corresponding source code.  (This alternative is\n"
199 "    allowed only for noncommercial distribution and only if you\n"
200 "    received the program in object code or executable form with such\n"
201 "    an offer, in accord with Subsection b above.)\n"
202 "\n"
203 "The source code for a work means the preferred form of the work for\n"
204 "making modifications to it.  For an executable work, complete source\n"
205 "code means all the source code for all modules it contains, plus any\n"
206 "associated interface definition files, plus the scripts used to\n"
207 "control compilation and installation of the executable.  However, as a\n"
208 "special exception, the source code distributed need not include\n"
209 "anything that is normally distributed (in either source or binary\n"
210 "form) with the major components (compiler, kernel, and so on) of the\n"
211 "operating system on which the executable runs, unless that component\n"
212 "itself accompanies the executable.\n"
213 "\n"
214 "If distribution of executable or object code is made by offering\n"
215 "access to copy from a designated place, then offering equivalent\n"
216 "access to copy the source code from the same place counts as\n"
217 "distribution of the source code, even though third parties are not\n"
218 "compelled to copy the source along with the object code.\n"
219 "^L\n"
220 "  4. You may not copy, modify, sublicense, or distribute the Program\n"
221 "except as expressly provided under this License.  Any attempt\n"
222 "otherwise to copy, modify, sublicense or distribute the Program is\n"
223 "void, and will automatically terminate your rights under this License.\n"
224 "However, parties who have received copies, or rights, from you under\n"
225 "this License will not have their licenses terminated so long as such\n"
226 "parties remain in full compliance.\n"
227 "\n"
228 "  5. You are not required to accept this License, since you have not\n"
229 "signed it.  However, nothing else grants you permission to modify or\n"
230 "distribute the Program or its derivative works.  These actions are\n"
231 "prohibited by law if you do not accept this License.  Therefore, by\n"
232 "modifying or distributing the Program (or any work based on the\n"
233 "Program), you indicate your acceptance of this License to do so, and\n"
234 "all its terms and conditions for copying, distributing or modifying\n"
235 "the Program or works based on it.\n"
236 "\n"
237 "  6. Each time you redistribute the Program (or any work based on the\n"
238 "Program), the recipient automatically receives a license from the\n"
239 "original licensor to copy, distribute or modify the Program subject to\n"
240 "these terms and conditions.  You may not impose any further\n"
241 "restrictions on the recipients' exercise of the rights granted herein.\n"
242 "You are not responsible for enforcing compliance by third parties to\n"
243 "this License.\n"
244 "\n"
245 "  7. If, as a consequence of a court judgment or allegation of patent\n"
246 "infringement or for any other reason (not limited to patent issues),\n"
247 "conditions are imposed on you (whether by court order, agreement or\n"
248 "otherwise) that contradict the conditions of this License, they do not\n"
249 "excuse you from the conditions of this License.  If you cannot\n"
250 "distribute so as to satisfy simultaneously your obligations under this\n"
251 "License and any other pertinent obligations, then as a consequence you\n"
252 "may not distribute the Program at all.  For example, if a patent\n"
253 "license would not permit royalty-free redistribution of the Program by\n"
254 "all those who receive copies directly or indirectly through you, then\n"
255 "the only way you could satisfy both it and this License would be to\n"
256 "refrain entirely from distribution of the Program.\n"
257 "\n"
258 "If any portion of this section is held invalid or unenforceable under\n"
259 "any particular circumstance, the balance of the section is intended to\n"
260 "apply and the section as a whole is intended to apply in other\n"
261 "circumstances.\n"
262 "\n"
263 "It is not the purpose of this section to induce you to infringe any\n"
264 "patents or other property right claims or to contest validity of any\n"
265 "such claims; this section has the sole purpose of protecting the\n"
266 "integrity of the free software distribution system, which is\n"
267 "implemented by public license practices.  Many people have made\n"
268 "generous contributions to the wide range of software distributed\n"
269 "through that system in reliance on consistent application of that\n"
270 "system; it is up to the author/donor to decide if he or she is willing\n"
271 "to distribute software through any other system and a licensee cannot\n"
272 "impose that choice.\n"
273 "\n"
274 "This section is intended to make thoroughly clear what is believed to\n"
275 "be a consequence of the rest of this License.\n"
276 "\n"
277 "  8. If the distribution and/or use of the Program is restricted in\n"
278 "certain countries either by patents or by copyrighted interfaces, the\n"
279 "original copyright holder who places the Program under this License\n"
280 "may add an explicit geographical distribution limitation excluding\n"
281 "those countries, so that distribution is permitted only in or among\n"
282 "countries not thus excluded.  In such case, this License incorporates\n"
283 "the limitation as if written in the body of this License.\n"
284 "\n"
285 "  9. The Free Software Foundation may publish revised and/or new versions\n"
286 "of the General Public License from time to time.  Such new versions will\n"
287 "be similar in spirit to the present version, but may differ in detail to\n"
288 "address new problems or concerns.\n"
289 "\n"
290 "Each version is given a distinguishing version number.  If the Program\n"
291 "specifies a version number of this License which applies to it and \"any\n"
292 "later version\", you have the option of following the terms and conditions\n"
293 "either of that version or of any later version published by the Free\n"
294 "Software Foundation.  If the Program does not specify a version number of\n"
295 "this License, you may choose any version ever published by the Free Software\n"
296 "Foundation.\n"
297 "\n"
298 "  10. If you wish to incorporate parts of the Program into other free\n"
299 "programs whose distribution conditions are different, write to the author\n"
300 "to ask for permission.  For software which is copyrighted by the Free\n"
301 "Software Foundation, write to the Free Software Foundation; we sometimes\n"
302 "make exceptions for this.  Our decision will be guided by the two goals\n"
303 "of preserving the free status of all derivatives of our free software and\n"
304 "of promoting the sharing and reuse of software generally.\n";
305
306 static char *warranty=
307 "                            NO WARRANTY\n"
308 "\n"
309 "  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\n"
310 "FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN\n"
311 "OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\n"
312 "PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\n"
313 "OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\n"
314 "MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS\n"
315 "TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE\n"
316 "PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\n"
317 "REPAIR OR CORRECTION.\n"
318 "\n"
319 "  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\n"
320 "WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\n"
321 "REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\n"
322 "INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\n"
323 "OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\n"
324 "TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\n"
325 "YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\n"
326 "PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\n"
327 "POSSIBILITY OF SUCH DAMAGES.\n";
328 #endif
329
330 static void printTypeInfo(link *);
331 static void printValAggregates (symbol *,link *,char,unsigned int,int);
332 static void printOrSetSymValue (symbol *sym, context *cctxt, 
333                                 int flg, int dnum, int fmt, char *rs, char *val);
334
335 int srcMode = SRC_CMODE ;
336 static set  *dispsymbols = NULL   ; /* set of displayable symbols */
337 static int currentFrame = 0;        /* actual displayed frame     */
338 /*-----------------------------------------------------------------*/
339 /* funcWithName - returns function with name                       */
340 /*-----------------------------------------------------------------*/
341 DEFSETFUNC(funcWithName)
342 {
343     function *func = item;
344     V_ARG(char *,name);
345     V_ARG(function **,funcp);
346
347     if (*funcp)
348         return 0;
349
350     if (strcmp(func->sym->name,name) == 0) {
351         *funcp = func;
352         return 1;
353     }
354     
355     return 0;
356 }
357
358 /*-----------------------------------------------------------------*/
359 /* symWithAddr - look for symbol with sfr / sbit address           */
360 /*-----------------------------------------------------------------*/
361 DEFSETFUNC(symWithAddr)
362 {
363     symbol *sym = item;
364     V_ARG(unsigned long,laddr);
365     V_ARG(int    ,laddrspace);
366     V_ARG(symbol **,rsym);
367
368     if (*rsym)
369         return 0;
370
371     if ( sym->addr == laddr &&
372          sym->addrspace == laddrspace ) 
373     {
374         *rsym = sym;
375         return 1;
376     }
377
378     return 0;
379 }
380
381 /*-----------------------------------------------------------------*/
382 /* setBPatModLine - set break point at the line specified for the  */
383 /*-----------------------------------------------------------------*/
384 static void setBPatModLine (module *mod, int line)
385 {
386   int next_line;
387
388     /* look for the first executable line after the line
389        specified & get the break point there */    
390     if (srcMode == SRC_CMODE && line > mod->ncLines) {
391         fprintf(stderr,"No line %d in file \"%s\".\n",
392                 line,mod->c_name);
393         return ;
394     }
395     
396     if (srcMode == SRC_AMODE && line > mod->nasmLines) {
397         fprintf(stderr,"No line %d in file \"%s\".\n",
398                 line,mod->asm_name);
399         return ;
400     }
401
402     next_line = line;
403     for ( ; next_line < (srcMode == SRC_CMODE ? mod->ncLines : mod->nasmLines ) ; 
404           next_line++ ) {
405         if (srcMode == SRC_CMODE) {
406             if (mod->cLines[next_line]->addr) {
407                 setBreakPoint (mod->cLines[next_line]->addr, CODE, USER, 
408                                userBpCB, mod->c_name, next_line);
409                 return;
410 //              break;
411             }
412         }
413         else {
414            if (mod->asmLines[next_line]->addr) {
415                setBreakPoint (mod->asmLines[next_line]->addr, CODE, USER, 
416                               userBpCB, mod->asm_name, next_line);
417                 return;
418 //             break;
419            } 
420         }
421     }
422
423         fprintf(stderr,"No line %d or after in file \"%s\"..\n",
424                         line,mod->c_name);
425
426     return;
427 }
428
429 /*-----------------------------------------------------------------*/
430 /* clearBPatModLine - clr break point at the line specified        */
431 /*-----------------------------------------------------------------*/
432 static void clearBPatModLine (module *mod, int line)
433 {
434     /* look for the first executable line after the line
435        specified & get the break point there */
436     if (srcMode == SRC_CMODE && line > mod->ncLines) {
437         fprintf(stderr,"No line %d in file \"%s\".\n",
438                 line,mod->c_name);
439         return ;
440     }
441     
442     if (srcMode == SRC_AMODE && line > mod->ncLines) {
443         fprintf(stderr,"No line %d in file \"%s\".\n",
444                 line,mod->c_name);
445         return ;
446     }    
447     
448     for ( ; line < (srcMode == SRC_CMODE ? mod->ncLines : mod->nasmLines ) ; 
449           line++ ) {
450         if (srcMode == SRC_CMODE) 
451             if (mod->cLines[line]->addr) {
452                 clearUSERbp (mod->cLines[line]->addr);                    
453                 break;
454             }
455         else
456             if (mod->asmLines[line]->addr) {
457                 clearUSERbp (mod->asmLines[line]->addr);                          
458                 break;
459             }
460     }
461
462     return;
463 }
464
465 /*-----------------------------------------------------------------*/
466 /* moduleLineWithAddr - finds and returns a line  with a given address */
467 /*-----------------------------------------------------------------*/
468 DEFSETFUNC(moduleLineWithAddr)
469 {
470     module *mod = item;
471     int i;
472
473     V_ARG(unsigned int,addr);
474     V_ARG(module **,rmod);
475     V_ARG(int *,line);
476
477     if (*rmod)
478         return 0;
479
480     for (i=0; i < mod->nasmLines; i++ ) 
481     {
482         if ( mod->asmLines[i]->addr == addr)
483         {
484             *rmod = mod ;
485             if (line )
486                 *line = mod->ncLines;
487             return 1;
488         }
489     }
490
491     return 0;
492 }
493
494 /*-----------------------------------------------------------------*/
495 /* funcWithNameModule - returns functions with a name module combo */
496 /*-----------------------------------------------------------------*/
497 DEFSETFUNC(funcWithNameModule) 
498 {
499     function *func = item;
500     V_ARG(char *,fname);
501     V_ARG(char *,mname);
502     V_ARG(function **,funcp);
503
504     if (*funcp)
505         return 0;
506
507     if (strcmp(func->sym->name,fname) == 0 &&
508         strcmp(func->mod->c_name,mname) == 0) {
509         *funcp = func;
510         return 1;
511     }
512
513     return 0;
514 }
515
516 /*-----------------------------------------------------------------*/
517 /* funcInAddr - given an address returns the function              */
518 /*-----------------------------------------------------------------*/
519 DEFSETFUNC(funcInAddr)
520 {
521     function *func = item;
522     V_ARG(unsigned int,addr);
523     V_ARG(function **,funcp);
524
525     if (*funcp)
526         return 0;
527
528     /* in the address range */
529     if (func->sym->addr <= addr &&
530         func->sym->eaddr >= addr) {
531         
532         *funcp = func;
533         return 1;
534     }
535
536     return 0;       
537 }
538
539 /*-----------------------------------------------------------------*/
540 /* setStepBp - will set STEP Bp @ function entry points            */
541 /*-----------------------------------------------------------------*/
542 DEFSETFUNC(setStepBp)
543 {
544     function *func = item;
545     
546     if (func->sym && func->sym->addr ) {
547         
548         /* set the entry break point */
549         setBreakPoint (func->sym->addr , CODE , STEP , 
550                        stepBpCB ,func->mod->c_name , func->entryline);
551
552         return 1;
553     }
554
555     return 0;
556 }
557
558 /*-----------------------------------------------------------------*/
559 /* setStepEPBp - sets a given type of bp @ the execution point     */
560 /*-----------------------------------------------------------------*/
561 DEFSETFUNC(setStepEPBp)
562 {
563     exePoint *ep = item;
564     V_ARG(int,bptype);
565     V_ARG(char *,mname);
566    
567     setBreakPoint (ep->addr, CODE, bptype, 
568                    stepBpCB, mname, ep->line);
569     return 1;
570 }
571
572 /*-----------------------------------------------------------------*/
573 /* setNextEPBp - sets a given type of bp @ the execution point     */
574 /*-----------------------------------------------------------------*/
575 DEFSETFUNC(setNextEPBp)
576 {
577     exePoint *ep = item;
578     V_ARG(int,bptype);
579     V_ARG(char *,mname);
580    
581     setBreakPoint (ep->addr, CODE, bptype, 
582                    nextBpCB, mname, ep->line);
583     return 1;
584 }
585
586 /*-----------------------------------------------------------------*/
587 /* lineAtAddr - for execution points returns the one with addr     */
588 /*-----------------------------------------------------------------*/
589 DEFSETFUNC(lineAtAddr)
590 {
591     exePoint *ep = item;
592     V_ARG(unsigned int,addr);
593     V_ARG(int *,line);
594     V_ARG(int *,block);
595     V_ARG(int *,level);
596
597     /* address must be an exact match */
598     if (ep->addr == addr) {
599         *line = ep->line;
600         if (block)
601             *block = ep->block ;
602         if (level)
603             *level = ep->level ;
604         return 1;
605     }
606
607     return 0;
608     
609 }
610
611 /*-----------------------------------------------------------------*/
612 /* lineNearAddr - for execution points returns the one with addr   */
613 /*-----------------------------------------------------------------*/
614 DEFSETFUNC(lineNearAddr)
615 {
616     exePoint *ep = item;
617     V_ARG(unsigned int,addr);
618     V_ARG(int *,line);
619     V_ARG(int *,block);
620     V_ARG(int *,level);
621
622     /* the line in which the address is */
623     if (ep->addr <= addr) {
624         *line = ep->line;
625         if (block)
626             *block = ep->block ;
627         if (level)
628             *level = ep->level ;
629         return 1;
630     }
631
632     return 0;
633     
634 }
635
636 /*-----------------------------------------------------------------*/
637 /* discoverContext - find out the current context of the bp        */
638 /*-----------------------------------------------------------------*/
639 context *discoverContext (unsigned addr)
640 {
641     function *func = NULL;
642     module   *mod  = NULL;
643     int line = 0;
644
645     currentFrame = 0; 
646
647     /* find the function we are in */
648     if (!applyToSet(functions,funcInAddr,addr,&func)) {
649         if (!applyToSet(functions,funcWithName,"main") ||
650             !applyToSet(modules,moduleLineWithAddr,addr,&mod,&line))
651         {
652             fprintf(stderr, "Error?:discoverContext: cannot apply to set!\n");
653             return NULL;
654         }
655         currCtxt->func = func;
656         currCtxt->addr = addr;
657         currCtxt->modName = mod->name;
658         currCtxt->cline = func->exitline;
659     }
660     else
661     {
662         currCtxt->func = func;
663         currCtxt->addr = func->laddr = addr;
664         currCtxt->modName = func->modName;
665     
666         /* find the c line number */
667         if(applyToSet(func->cfpoints,lineNearAddr,addr,
668                   &line,&currCtxt->block,&currCtxt->level)) 
669             currCtxt->cline = func->lline = line;
670         else
671             currCtxt->cline = func->exitline;
672     }    
673     /* find the asm line number */
674     line = 0;
675     if (applyToSet(func->afpoints,lineAtAddr,addr,
676                    &line,NULL,NULL))
677         currCtxt->asmline = line;       
678     else
679         currCtxt->asmline = -1;
680         
681     return currCtxt ;
682 }
683
684
685 /*-----------------------------------------------------------------*/
686 /* simGo - send 'go' cmd to simulator and wait till a break occurs */
687 /*-----------------------------------------------------------------*/
688 void simGo (unsigned int gaddr)
689 {   
690     unsigned int addr ;
691     context *ctxt;
692     int rv;
693     static int initial_break_flag = 0;
694
695  top:    
696     if ( userinterrupt )
697     {
698         userinterrupt = 0;
699         return;
700     }
701     addr = simGoTillBp (gaddr);
702
703     /* got the pc for the break point now first
704        discover the program context i.e. module, function 
705        linenumber of the source etc, etc etc */
706     ctxt = discoverContext (addr);
707     
708     /* dispatch all the break point call back functions */
709     rv = dispatchCB (addr,ctxt);    
710  ret:    
711
712     /* the dispatch call back function will return
713        non-zero if an user break point has been hit
714        if not then we continue with the execution 
715        of the program */
716     if (!rv) {
717       if (!initial_break_flag) {
718         initial_break_flag = 1;  // kludge to stop only at first run
719         fprintf(stdout, "Stopping at entry.  You can now list and set breakpoints\n");
720       }
721       else {
722         gaddr = -1;
723         goto top ;
724       }
725
726 // notes: kpb
727 // I took this out, after running "run" it would just keep re-running
728 // even after a lot of break points hit.  For some reason above code
729 // not triggering(dispatchCB).  This seems to be by design, startup adds
730 // a bunch of breakpoints-but they are not USER breakpoints.  Perhaps the
731 // debugger changed with its implementation of "go"("run").  It seems we
732 // need to add a "next" or "step" followed by a "run"...
733 // I added a "step" in simi.c when we want a resume function, this seems
734 // to work.
735
736 // still there is question of how do we stop it initially, since
737 // it must be started before it can get a context.  If so, we would
738 // want it to just run up to an initial entry point you'd think...
739 // I don't see why we can't set breakpoints before an initial run,
740 // this does not seem right to me.
741
742 // line #'s are a bit off too.
743
744 #if 0
745         gaddr = -1;
746         goto top ;
747 #endif
748     }
749     
750 }
751
752 /*-----------------------------------------------------------------*/
753 /* preparePrint - common parse function for                        */
754 /*                output, print and display                        */
755 /*-----------------------------------------------------------------*/
756 static char *preparePrint(char *s, context *cctxt, int *fmt, symbol **sym)
757 {
758     char *bp = s+strlen(s) -1;
759     char save_ch ;
760
761     *fmt = FMT_NON;
762     *sym = NULL;
763
764     while (isspace(*s)) s++;
765     if (!*s) 
766         return (char *)0;
767
768     while (isspace(*bp)) bp--;
769     bp++ ;
770     *bp = '\0';
771
772     if ( *s == '/' )
773     {
774         /* format of printout */
775         switch ( *++s )
776         {
777             case 'x':
778                 *fmt = FMT_HEX ;
779                 break;
780             case 'o':
781                 *fmt = FMT_OCT ;
782                 break;
783             default:
784             case 'd':
785                 *fmt = FMT_DEZ ;
786                 break;
787             case 't':
788                 *fmt = FMT_BIN ;
789                 break;
790         }
791         s++;
792         while (isspace(*s)) s++;
793     }
794     for ( bp = s; *bp && ( isalnum( *bp ) || *bp == '_'); bp++ );
795     save_ch = *bp;
796     if ( *bp )
797         *bp = '\0';
798
799     *sym = symLookup(s,cctxt);
800     *bp = save_ch;
801
802     if ( ! *sym )
803         fprintf(stdout,"No symbol \"%s\" in current context.\n", s);
804     return bp;
805 }
806
807 static int printAsmLine( function *func, module *m, long saddr, long eaddr)
808 {
809     int i,j,delta;
810     int symaddr;
811     int lastaddr = saddr+1;
812     char *symname;
813
814     if ( func )
815     {
816         symaddr = func->sym->addr;
817         symname = func->sym->name;
818     }
819     else
820     {
821         symaddr = saddr;
822         symname = "" ;
823     }
824     for (j=0,i=0; i < m->nasmLines; i++ ) 
825     {
826         if ( saddr >= 0 && m->asmLines[i]->addr < saddr)
827         {
828                 continue;
829         }
830         if ( eaddr >= 0 && m->asmLines[i]->addr > eaddr)
831         {
832                 continue;
833         }
834         if ( func && 
835             (m->asmLines[i]->addr < func->sym->addr ||
836              m->asmLines[i]->addr > func->sym->eaddr ))
837         {
838             continue;
839         } 
840         delta = m->asmLines[i]->addr - symaddr;
841         if ( delta >= 0 )
842         {
843             j++;
844             lastaddr = m->asmLines[i]->addr;
845             printf("0x%08x <%s",lastaddr,symname);
846             if (delta > 0) printf("+%d",delta);
847             printf(">:\t%s",m->asmLines[i]->src);            
848         }
849     }
850     return lastaddr;
851 }
852
853 /*-----------------------------------------------------------------*/
854 /* cmdDisasm - disassemble  asm instruction                        */
855 /*-----------------------------------------------------------------*/
856 static int cmdDisasm (char *s, context *cctxt, int args)
857 {
858     function *func = NULL;
859     long  saddr = -1;
860     long  eaddr = -1;
861     int   found = 0;
862     module *modul;
863     /* white space skip */
864
865     if ( args > 0 )
866     {
867         while (*s && isspace(*s)) s++;
868
869         if ( isdigit(*s))
870         {
871             saddr = strtol(s,&s,0);
872             if ( args > 1 )
873             {
874                 while (*s && isspace(*s)) s++;
875
876                 if ( isdigit(*s))
877                     eaddr = strtol(s,0,0);
878             }
879             else
880                 eaddr = saddr+1;
881         }
882     }
883
884     if ( eaddr == -1 )
885     {       
886         /* no start or only start so dump function */
887         if ( saddr == -1 )
888         {
889             func = cctxt->func;
890         }
891         else
892         {
893             applyToSet(functions,funcInAddr,saddr,&func);
894         }
895         if ( func )
896         {
897             printf("Dump of assembler code for function %s:\n",func->sym->name);
898             printAsmLine(func,func->mod,-1,-1);
899             printf("End of assembler dump.\n");
900             return 0; 
901         }
902         else
903         {
904             if (applyToSet(modules,moduleLineWithAddr,saddr,&modul,NULL))
905             {
906                 eaddr = saddr + 5;
907                 printf("Dump of assembler code:\n");
908                 printAsmLine(NULL,modul,saddr,eaddr);
909                 printf("End of assembler dump.\n");
910                 return 0; 
911             }
912         }
913     }
914     else
915     {
916         if ( args > 1 )
917             printf("Dump of assembler code from 0x%08x to 0x%08x:\n",saddr,eaddr);
918         found = 0;
919         while ( saddr < eaddr )
920         {
921             func = NULL;
922             if (applyToSet(functions,funcInAddr,saddr,&func))
923             {
924                 found = 1;
925                 modul = func->mod;
926             }
927             else
928             {
929                 if ( found )
930                     break;
931                 if (!applyToSet(modules,moduleLineWithAddr,saddr,&modul,NULL))
932                     break;
933             }
934             saddr = printAsmLine(func,modul,saddr,eaddr) + 1;
935         }
936         if( saddr >= eaddr)
937         {
938             if ( args > 1 )
939                 printf("End of assembler dump.\n");
940             return 0; 
941         }
942         
943     }
944     fprintf(stderr,"No function contains specified address.\n");
945     return 0; 
946 }
947 /*-----------------------------------------------------------------*/
948 /* cmdDisasm1 - disassemble one asm instruction                    */
949 /*-----------------------------------------------------------------*/
950 int cmdDisasm1 (char *s, context *cctxt)
951 {
952     return cmdDisasm( s, cctxt, 1);
953 }
954
955 /*-----------------------------------------------------------------*/
956 /* cmdDisasmF - disassemble asm instructions                       */
957 /*-----------------------------------------------------------------*/
958 int cmdDisasmF(char *s, context *cctxt)
959 {
960     return cmdDisasm( s, cctxt, 2);
961 }
962
963 /*-----------------------------------------------------------------*/
964 /* cmdSetUserBp - set break point at the user specified location   */
965 /*-----------------------------------------------------------------*/
966 int cmdSetUserBp (char *s, context *cctxt)
967 {
968     char *bp ;
969     function *func = NULL;
970         
971     /* user break point location specification can be of the following
972        forms
973        a) <nothing>        - break point at current location
974        b) lineno           - number of the current module
975        c) filename:lineno  - line number of the given file
976        e) filename:function- function X in file Y (useful for static functions)
977        f) function         - function entry point
978        g) *addr            - break point at address 
979     */
980
981     if (!cctxt) {
982         fprintf(stdout,"No symbol table is loaded.  Use the \"file\" command.\n");
983         return 0;
984     }
985     /* white space skip */
986     while (*s && isspace(*s)) s++;
987     
988     /* null terminate it after stripping trailing blanks*/
989     bp = s + strlen(s);
990     while (bp != s && isspace(*bp)) bp--;
991     *bp = '\0';
992
993     /* case a) nothing */
994     /* if nothing given then current location : we know
995        the current execution location from the currentContext */
996     if (! *s ) {
997
998         /* if current context is known */
999         if (cctxt->func) {
1000             if (srcMode == SRC_CMODE)
1001                 /* set the break point */
1002                 setBreakPoint ( cctxt->addr , CODE , USER , userBpCB ,
1003                                 cctxt->func->mod->c_name, cctxt->cline);
1004             else
1005                 setBreakPoint ( cctxt->addr , CODE , USER , userBpCB ,
1006                                 cctxt->func->mod->asm_name, cctxt->asmline);
1007                 
1008         }
1009         else
1010             fprintf(stderr,"No default breakpoint address now.\n");
1011                         
1012         goto ret ;
1013     }
1014     /* case g) *addr */
1015     if ( *s == '*' && isdigit(*(s+1)))
1016     {
1017         int  line   = 0;
1018         long braddr = strtol(s+1,0,0);
1019         if (!applyToSet(functions,funcInAddr,braddr,&func))
1020         {
1021             module *modul;
1022             if (!applyToSet(modules,moduleLineWithAddr,braddr,&modul,&line))
1023             {
1024                 fprintf(stderr,"Address 0x%08x not exists in code.\n",braddr); 
1025             }
1026             else
1027             {
1028                 setBreakPoint ( braddr , CODE , USER , userBpCB ,
1029                             modul->c_name,line);
1030             }
1031             goto ret ;
1032         }
1033                 else
1034         {
1035             int line = func->exitline;
1036             applyToSet(func->cfpoints,lineNearAddr,braddr,&line,NULL,NULL);
1037             setBreakPoint ( braddr , CODE , USER , userBpCB ,
1038                             func->mod->c_name,line);
1039         }
1040         goto ret ;
1041     }
1042     /* case b) lineno */
1043     /* check if line number */
1044     if (isdigit(*s)) {
1045         /* get the lineno */
1046         int line = atoi(s);
1047
1048         /* if current context not present then we must get the module
1049            which has main & set the break point @ line number provided
1050            of that module : if current context known then set the bp 
1051            at the line number given for the current module 
1052         */
1053         if (cctxt->func) {
1054             if (!cctxt->func->mod) {
1055                 if (!applyToSet(functions,funcWithName,"main"))
1056                     fprintf(stderr,"Function \"main\" not defined.\n");
1057                 else 
1058                     setBPatModLine(func->mod,line);
1059             } else 
1060                 setBPatModLine(cctxt->func->mod,line);
1061         } else {
1062                 if (list_mod) {
1063                         setBPatModLine(list_mod,line);
1064                 } else {
1065                   fprintf(stdout,"Sdcdb fails to have module symbol context at %d\n", __LINE__);
1066                 }
1067         }
1068         
1069         goto ret;
1070     }
1071
1072     if ((bp = strchr(s,':'))) {
1073         
1074         module *mod = NULL;
1075         *bp = '\0';
1076         
1077         if (srcMode == SRC_CMODE) {
1078             if (!applyToSet(modules,moduleWithCName,s,&mod)) {
1079                 fprintf (stderr,"No source file named %s.\n",s);
1080                 goto ret;
1081             }
1082         } else {
1083             if (!applyToSet(modules,moduleWithAsmName,s,&mod)) {
1084                 fprintf (stderr,"No source file named %s.\n",s);
1085                 goto ret;
1086             }
1087         }
1088                 
1089         /* case c) filename:lineno */
1090         if (isdigit(*(bp +1))) {                    
1091          
1092             setBPatModLine (mod,atoi(bp+1));        
1093             goto ret;
1094             
1095         }
1096         /* case d) filename:function */
1097         if (!applyToSet(functions,funcWithNameModule,bp+1,s,&func)) 
1098             fprintf(stderr,"Function \"%s\" not defined.\n",bp+1); 
1099         else        
1100             setBPatModLine (mod,
1101                             (srcMode == SRC_CMODE ? 
1102                              func->entryline :
1103                              func->aentryline));
1104         
1105         goto ret;
1106     }
1107             
1108     /* case e) function */
1109     if (!applyToSet(functions,funcWithName,s,&func))
1110         fprintf(stderr,"Function \"%s\" not defined.\n",s); 
1111     else
1112         setBPatModLine(func->mod,
1113                        (srcMode == SRC_CMODE ?
1114                         func->entryline :
1115                         func->aentryline));
1116
1117  ret:    
1118     return 0;
1119 }
1120
1121 /*-----------------------------------------------------------------*/
1122 /* cmdListAsm - list assembler source code                         */
1123 /*-----------------------------------------------------------------*/
1124 int cmdListAsm (char *s, context *cctxt)
1125 {
1126     if (  cctxt && cctxt->func) 
1127     {
1128         /* actual line */
1129         if (printAsmLine(cctxt->func,cctxt->func->mod,
1130                          (long)cctxt->addr,(long)cctxt->addr))
1131             return 0; 
1132     }
1133     return 0;
1134 }
1135
1136 /*-----------------------------------------------------------------*/
1137 /* cmdSetOption - set debugger options                             */
1138 /*-----------------------------------------------------------------*/
1139 int cmdSetOption (char *s, context *cctxt)
1140 {
1141     while (*s && isspace(*s)) s++;
1142     if (strncmp(s,"srcmode",7) == 0 ) {
1143         if (srcMode == SRC_CMODE)
1144             srcMode = SRC_AMODE;
1145         else
1146             srcMode = SRC_CMODE;
1147         fprintf(stderr,"source mode set to '%s'\n", 
1148                 (srcMode == SRC_CMODE ? "C" : "asm"));
1149         return 0;
1150     }
1151
1152     if (strncmp(s,"variable ",9) == 0) 
1153     {
1154         symbol *sym ;
1155         int fmt;
1156         char *rs;
1157         s += 9;
1158         if ( !( rs = preparePrint(s, cctxt, &fmt, &sym )))
1159             return 0;
1160         s = rs;
1161         while (*s && *s != '=') s++;
1162         *s++ = '\0';
1163         while (isspace(*s)) *s++ = '\0';
1164         if (*s)
1165         {
1166                 printOrSetSymValue(sym,cctxt,0,0,0,rs,s);
1167                 return 0;
1168         }
1169         else
1170             fprintf(stdout,"No new value for \"%s\".\n",s);
1171         return 0;       
1172     }
1173
1174  
1175     fprintf(stderr,"'set %s' command not yet implemented\n",s);
1176     return 0;
1177 }
1178
1179 /*-----------------------------------------------------------------*/
1180 /* cmdContinue - continue till next break point                    */
1181 /*-----------------------------------------------------------------*/
1182 int cmdContinue (char *s, context *cctxt)
1183 {
1184     if (!cctxt || !cctxt->func) {
1185         fprintf(stdout,"The program is not being run.\n");
1186         return 0;
1187     }
1188
1189     fprintf(stdout,"Continuing.\n");
1190     simGo(-1);
1191     showfull = 1;
1192     return 0;
1193 }
1194
1195 /*-----------------------------------------------------------------*/
1196 /* cmdDelUserBp - delete user break point                          */
1197 /*-----------------------------------------------------------------*/
1198 int cmdDelUserBp (char *s, context *cctxt)
1199 {
1200     int bpnum ;
1201     while (isspace(*s)) s++;
1202     
1203     if (!*s ) {
1204         if (userBpPresent) {
1205             char buffer[10];
1206             fprintf (stdout,"Delete all breakpoints? (y or n) ");
1207             fflush(stdout);
1208             fgets(buffer,sizeof(buffer),stdin);
1209             if (toupper(buffer[0]) == 'Y')
1210                 deleteUSERbp(-1);          
1211         }
1212         return 0;
1213     }
1214     
1215     /* determine the break point number */
1216     if (sscanf(s,"%d",&bpnum) == 1)
1217         deleteUSERbp(bpnum);
1218
1219     return 0;
1220 }
1221
1222 /*-----------------------------------------------------------------*/
1223 /* cmdStepi - single step exactly one instruction                   */
1224 /*-----------------------------------------------------------------*/
1225 int cmdStepi (char *s, context *cctxt)
1226 {
1227
1228     if (!cctxt || !cctxt->func || !cctxt->func->mod) 
1229         fprintf(stdout,"The program is not being run.\n");
1230     else 
1231     {
1232             simGo(2);   
1233         showfull = 1;
1234     }
1235     return 0;
1236 }
1237
1238 /*-----------------------------------------------------------------*/
1239 /* cmdStep - single step thru C source file                        */
1240 /*-----------------------------------------------------------------*/
1241 int cmdStep (char *s, context *cctxt)
1242 {
1243     function *func = NULL;
1244
1245     if (!cctxt || !cctxt->func || !cctxt->func->mod) 
1246         fprintf(stdout,"The program is not being run.\n");
1247     else {
1248         /* if we are @ the end of a function then set
1249            break points at execution points of the
1250            function in the call stack... */
1251         if (cctxt->addr == cctxt->func->sym->eaddr) {
1252             if ((func = STACK_PEEK(callStack))) {
1253                 if (srcMode == SRC_CMODE)
1254                     applyToSet (func->cfpoints,setStepEPBp,STEP,
1255                                 func->mod->c_name);     
1256                 else
1257                     applyToSet (func->afpoints,setStepEPBp,STEP,
1258                                 func->mod->asm_name);
1259             }
1260         } else {
1261             /* set breakpoints at all function entry points
1262                and all exepoints of this functions & for
1263                all functions one up in the call stack */
1264             
1265             /* all function entry points */
1266             applyToSet(functions,setStepBp); 
1267             
1268             if (srcMode == SRC_CMODE) {
1269                 /* for all execution points in this function */
1270                 applyToSet(cctxt->func->cfpoints,setStepEPBp,STEP,
1271                            cctxt->func->mod->c_name);
1272                 
1273                 /* set a break point @ the current function's
1274                    exit */
1275                 setBreakPoint (cctxt->func->sym->eaddr, CODE, STEP , 
1276                                stepBpCB, cctxt->func->mod->c_name, 
1277                                cctxt->func->exitline);
1278                 
1279                 /* now break point @ callers execution points */
1280                 if ((func = STACK_PPEEK(callStack))) {
1281                     applyToSet (func->cfpoints,setStepEPBp,STEP,
1282                                 func->mod->c_name);     
1283                     /* set bp @ callers exit point */
1284                     setBreakPoint (func->sym->eaddr, CODE, STEP , 
1285                                    stepBpCB, func->mod->c_name, 
1286                                    func->exitline);
1287                 }
1288             } else {
1289                 /* for all execution points in this function */
1290                 applyToSet(cctxt->func->afpoints,setStepEPBp,STEP,
1291                            cctxt->func->mod->asm_name);
1292                 
1293                 /* set a break point @ the current function's
1294                    exit */
1295                 setBreakPoint (cctxt->func->sym->eaddr, CODE, STEP , 
1296                                stepBpCB, cctxt->func->mod->asm_name, 
1297                                cctxt->func->aexitline);
1298                 
1299                 /* now break point @ callers execution points */
1300                 if ((func = STACK_PPEEK(callStack))) {
1301                     
1302                     applyToSet (func->afpoints,setStepEPBp,STEP,
1303                                 func->mod->asm_name);   
1304                     
1305                     /* set bp @ callers exit point */
1306                     setBreakPoint (func->sym->eaddr, CODE, STEP , 
1307                                    stepBpCB, func->mod->asm_name, 
1308                                    func->aexitline);
1309                 }
1310             }
1311         }
1312
1313         simGo(-1);
1314     showfull = 1;
1315     }
1316     return 0;
1317 }
1318
1319 /*-----------------------------------------------------------------*/
1320 /* cmdNexti - next instruction but proceed function call           */
1321 /*-----------------------------------------------------------------*/
1322 int cmdNexti (char *s, context *cctxt)
1323 {
1324     if (!cctxt || !cctxt->func || !cctxt->func->mod) 
1325         fprintf(stdout,"The program is not being run.\n");
1326     else 
1327     {
1328             simGo(1);   
1329         showfull = 1;
1330     }   
1331     return 0;
1332 }
1333
1334 /*-----------------------------------------------------------------*/
1335 /* cmdNext - next executable C statement file                      */
1336 /*-----------------------------------------------------------------*/
1337 int cmdNext (char *s, context *cctxt)
1338 {
1339     function *func = NULL;
1340     /* next is almost the same as step except we don't
1341        we don't set break point for all function entry
1342        points */
1343     if (!cctxt || !cctxt->func || !cctxt->func->mod) 
1344         fprintf(stdout,"The program is not being run.\n");
1345     else {
1346         /* if we are @ the end of a function then set
1347            break points at execution points of the
1348            function in the call stack... */
1349         if (cctxt->addr == cctxt->func->sym->eaddr) {
1350             if ((func = STACK_PEEK(callStack))) {
1351                 if (srcMode == SRC_CMODE)
1352                     applyToSet (func->cfpoints,setStepEPBp,STEP,
1353                                 func->mod->c_name);     
1354                 else
1355                     applyToSet (func->afpoints,setStepEPBp,STEP,
1356                                func->mod->asm_name);
1357             }
1358         } else {
1359             if (srcMode == SRC_CMODE) {
1360                 /* for all execution points in this function */
1361                 applyToSet(cctxt->func->cfpoints,setNextEPBp,NEXT,
1362                            cctxt->func->mod->c_name);
1363                 /* set a break point @ the current function's
1364                    exit */
1365                 setBreakPoint (cctxt->func->sym->eaddr, CODE, NEXT , 
1366                                nextBpCB, cctxt->func->mod->c_name, 
1367                                cctxt->func->exitline);
1368                 
1369                 /* now break point @ callers execution points */        
1370                 if ((func = STACK_PPEEK(callStack))) {
1371                     applyToSet (func->cfpoints,setNextEPBp,NEXT ,
1372                                 func->mod->c_name);     
1373                     /* set bp @ callers exit point */
1374                     setBreakPoint (func->sym->eaddr, CODE, NEXT , 
1375                                    stepBpCB, func->mod->c_name, 
1376                                    func->exitline);
1377                 }
1378             } else {
1379                 /* for all execution points in this function */
1380                 applyToSet(cctxt->func->afpoints,setNextEPBp,NEXT,
1381                            cctxt->func->mod->asm_name);
1382                 /* set a break point @ the current function's
1383                    exit */
1384                 setBreakPoint (cctxt->func->sym->eaddr, CODE, NEXT , 
1385                                nextBpCB, cctxt->func->mod->asm_name, 
1386                                cctxt->func->aexitline);
1387                 
1388                 /* now break point @ callers execution points */        
1389                 if ((func = STACK_PPEEK(callStack))) {
1390                     applyToSet (func->cfpoints,setNextEPBp,NEXT ,
1391                                 func->mod->asm_name);   
1392                     /* set bp @ callers exit point */
1393                     setBreakPoint (func->sym->eaddr, CODE, NEXT , 
1394                                    stepBpCB, func->mod->asm_name, 
1395                                    func->aexitline);
1396                 }
1397             }
1398             simGo(-1);  
1399         showfull = 1;
1400         }
1401     }    
1402     return 0;
1403 }
1404
1405 /*-----------------------------------------------------------------*/
1406 /* cmdRun  - run till next break point                             */
1407 /*-----------------------------------------------------------------*/
1408 int cmdRun (char *s, context *cctxt)
1409 {
1410     char buff[10];
1411     if (!cctxt || !cctxt->func || !cctxt->func->mod) {
1412         fprintf(stdout,"Starting program\n");
1413     if ( ! simactive )
1414     {
1415         fprintf(stdout,"No executable file specified.\nUse the \"file\" command.\n");
1416         return 0;
1417     }
1418         simGo(0);
1419     } else {
1420         
1421         fprintf(stdout,
1422                 "The program being debugged has been started already.\n");
1423         fprintf(stdout,"Start it from the beginning? (y or n) ");
1424         fflush(stdout);
1425
1426         fgets(buff,sizeof(buff),stdin);
1427         if (toupper(buff[0]) == 'Y') {
1428             simReset();
1429             simGo(0);
1430         }
1431     }
1432     showfull = 1;
1433     return 0;
1434 }
1435
1436 /*-----------------------------------------------------------------
1437  cmdListSymbols - list symbols
1438 |-----------------------------------------------------------------*/
1439 int cmdListSymbols (char *s, context *cctxt)
1440 {
1441     int our_verbose = 0;
1442     symbol *sy;
1443     int i;
1444
1445     if (strstr(s, "v1")) {
1446       our_verbose = 1;
1447     } else if (strstr(s, "v2")) {
1448       our_verbose = 2;
1449     }
1450
1451     printf("[symbols]\n");
1452     sy = setFirstItem(symbols);
1453     i = 0;
1454     for (;;) {
1455       if (sy == NULL)
1456         break;
1457       if (our_verbose <= 1)
1458         printf("<%s>", sy->name);
1459
1460       if (our_verbose > 1) {
1461         printf("  %d) name:%s, size:%d, level:%d block:%d\n", i,
1462           sy->name, sy->size, sy->level, sy->block);
1463         printf("    isonstack:%d, isfunc:%d, offset:%d addr:%d\n",
1464           sy->isonstack, sy->isfunc, sy->offset, sy->addr);
1465         printf("    eaddr:%d, addr_type:%c, type:%x etype:%x\n",
1466           sy->eaddr, sy->addr_type, sy->type, sy->etype);
1467         printf("    scopetype:%c, sname:%s, rname:%s addrspace:%c\n",
1468           sy->scopetype, sy->sname, sy->rname, sy->addrspace);
1469         printf("    next:%x\n", sy->next);
1470       }
1471       ++i;
1472       sy = setNextItem(symbols);
1473     }
1474     printf("   %d symbols\n", i);
1475     return 0;
1476 }
1477
1478 /*-----------------------------------------------------------------
1479  cmdListFunctions - list functions.
1480 |-----------------------------------------------------------------*/
1481 int cmdListFunctions (char *s, context *cctxt)
1482 {
1483     function *f;
1484     int i;
1485     int our_verbose = 0;
1486
1487     if (strstr(s, "v1")) {
1488       our_verbose = 1;
1489     } else if (strstr(s, "v2")) {
1490       our_verbose = 2;
1491     }
1492
1493     printf("[functions]\n");
1494     f = setFirstItem(functions);
1495     i = 0;
1496     for (;;) {
1497       if (f == NULL)
1498         break;
1499       if (our_verbose) {
1500         printf("  %d) sym:%x, fname:%s, modName:%s, mod:%x\n", i,
1501           f->sym, f->sym->name, f->modName, f->mod);
1502         printf("    entryline:%d, aentryline:%d, exitline:%d, aexitline:%d\n",
1503                 f->entryline, f->aentryline, f->exitline, f->aexitline);
1504         printf("    cfpoints:%x, afpoints:%x, laddr:%x, lline:%d\n",
1505                 f->cfpoints, f->afpoints, f->laddr, f->lline);
1506       }
1507       else {
1508         printf("<%s>", f->modName);
1509       }
1510       ++i;
1511       f = setNextItem(functions);
1512     }
1513     printf("   %d functions\n", i);
1514     return 0;
1515 }
1516
1517 /*-----------------------------------------------------------------
1518  cmdListModules - list functions.
1519 |-----------------------------------------------------------------*/
1520 int cmdListModules (char *s, context *cctxt)
1521 {
1522     module *m;
1523     srcLine *cs, *as;
1524     int i, mi;
1525     int our_verbose = 0;
1526
1527     if (strstr(s, "v1")) {
1528       our_verbose = 1;
1529     } else if (strstr(s, "v2")) {
1530       our_verbose = 2;
1531     }
1532
1533     printf("[modules]\n");
1534     m = setFirstItem(modules);
1535     mi = 0;
1536     for (;;) {
1537       if (m == NULL)
1538         break;
1539
1540       if (our_verbose >= 0) {
1541       printf("  %d) cfullname:%s, afullname:%s, name:%s\n", ++mi,
1542         m->cfullname, m->afullname, m->name);
1543       printf("    c_name:%s, asm_name:%s, ncLines:%d, nasmLines:%d\n",
1544               m->c_name, m->asm_name, m->ncLines, m->nasmLines);
1545       printf("    cLines:%x, asmLines:%x\n",
1546               m->cLines, m->asmLines);
1547       }
1548       if (our_verbose >= 2) {
1549         if (m->ncLines) {
1550           printf("    [cLines] ");
1551           if ( our_verbose)
1552           for (i=0; i<m->ncLines; i++ ) {
1553               cs = m->cLines[i];
1554               printf("   (%d) addr:%x, block:%d, level:%d, src:%s\n",
1555                  i, cs->addr, cs->block, cs->level, cs->src);
1556           }
1557           if (!our_verbose)
1558               printf("%d records", i);
1559         }
1560         if (m->nasmLines) {
1561           printf("    [asmLines] ");
1562           if ( our_verbose)
1563           for (i=0; i<m->nasmLines; i++ ) {
1564               as = m->asmLines[i];
1565               printf("   (%d) addr:%x, block:%d, level:%d, src:%s\n",
1566                  i, as->addr, as->block, as->level, as->src);
1567           }
1568           if (!our_verbose)
1569               printf("%d records", i);
1570         }
1571         printf("\n");
1572       }
1573
1574       m = setNextItem(modules);
1575     }
1576     return 0;
1577 }
1578
1579 /*-----------------------------------------------------------------
1580  infoSymbols - This is really just a tool to dump all these
1581    huge program structures out into human readable form.
1582 |-----------------------------------------------------------------*/
1583 static void infoSymbols(context *ctxt)
1584 {
1585   int our_verbose = 0;
1586
1587   printf("[context:%x] func:%x modName:%s addr:%x\n",
1588     ctxt, ctxt->func, ctxt->modName, ctxt->addr);
1589
1590   printf("  cline:%d asmline:%d block:%d level:%d\n",
1591     ctxt->cline, ctxt->asmline, ctxt->level);
1592
1593   printf("[globals] currCtxt:%x, modules:%x, functions:%x symbols:%x\n",
1594     currCtxt, modules, functions, symbols);
1595   printf("  nStructs:%d, structs:%x, ssdirl:%s\n",
1596     nStructs, structs, ssdirl);
1597
1598   /**************** modules *******************/
1599   {
1600     module *m;
1601     srcLine *cs, *as;
1602     int i, mi;
1603     printf("[modules]\n");
1604     m = setFirstItem(modules);
1605     mi = 0;
1606     for (;;) {
1607       if (m == NULL)
1608         break;
1609       printf("  %d) cfullname:%s, afullname:%s, name:%s\n", ++mi,
1610         m->cfullname, m->afullname, m->name);
1611       printf("    c_name:%s, asm_name:%s, ncLines:%d, nasmLines:%d\n",
1612               m->c_name, m->asm_name, m->ncLines, m->nasmLines);
1613       printf("    cLines:%x, asmLines:%x\n",
1614               m->cLines, m->asmLines);
1615       i = 0;
1616       if (m->cLines) {
1617         cs = m->cLines[i++];
1618         printf("    [cLines] ");
1619         while (cs) {
1620           if (our_verbose)
1621             printf("   (%d) addr:%x, block:%d, level:%d, src:%s\n",
1622                i, cs->addr, cs->block, cs->level, cs->src);
1623           cs = m->cLines[i++];
1624         }
1625         if (!our_verbose)
1626             printf("%d records", i);
1627       }
1628       i = 0;
1629       if (m->asmLines) {
1630         as = m->asmLines[i++];
1631         printf("    [asmLines] ");
1632         while (as) {
1633           if (our_verbose)
1634             printf("   (%d) addr:%x, block:%d, level:%d, src:%s\n",
1635                i, as->addr, as->block, as->level, as->src);
1636           as = m->asmLines[i++];
1637         }
1638         if (!our_verbose)
1639             printf("%d records", i);
1640       }
1641       printf("\n");
1642
1643       m = setNextItem(modules);
1644     }
1645   }
1646
1647   /**************** functions *******************/
1648   {
1649     function *f;
1650     int i;
1651     printf("[functions]\n");
1652     f = setFirstItem(functions);
1653     i = 0;
1654     for (;;) {
1655       if (f == NULL)
1656         break;
1657       if (our_verbose) {
1658         printf("  %d) sym:%x, modName:%s, mod:%x\n", i,
1659           f->sym, f->modName, f->mod);
1660         printf("    entryline:%d, aentryline:%d, exitline:%d, aexitline:%d\n",
1661                 f->entryline, f->aentryline, f->exitline, f->aexitline);
1662         printf("    cfpoints:%x, afpoints:%x, laddr:%x, lline:%d\n",
1663                 f->cfpoints, f->afpoints, f->laddr, f->lline);
1664       }
1665       ++i;
1666       f = setNextItem(functions);
1667     }
1668     if (!our_verbose)
1669       printf("   %d functions\n", i);
1670   }
1671
1672   /**************** symbols *******************/
1673   {
1674     symbol *s;
1675     int i;
1676     printf("[symbols]\n");
1677     s = setFirstItem(symbols);
1678     i = 0;
1679     for (;;) {
1680       if (s == NULL)
1681         break;
1682       if (our_verbose) {
1683         printf("  %d) name:%s, size:%d, level:%d block:%d\n", i,
1684           s->name, s->size, s->level, s->block);
1685         printf("    isonstack:%d, isfunc:%d, offset:%d addr:%d\n",
1686           s->isonstack, s->isfunc, s->offset, s->addr);
1687         printf("    eaddr:%d, addr_type:%c, type:%x etype:%x\n",
1688           s->eaddr, s->addr_type, s->type, s->etype);
1689         printf("    scopetype:%c, sname:%s, rname:%s addrspace:%c\n",
1690           s->scopetype, s->sname, s->rname, s->addrspace);
1691         printf("    next:%x\n", s->next);
1692       }
1693       ++i;
1694       s = setNextItem(symbols);
1695     }
1696     if (!our_verbose)
1697       printf("   %d symbols\n", i);
1698   }
1699
1700 }
1701
1702 /*-----------------------------------------------------------------*/
1703 /* infoStack - print call stack information                        */
1704 /*-----------------------------------------------------------------*/
1705 static void infoStack(context *ctxt)
1706 {
1707     function *func ;
1708     int i = 0 ;
1709
1710     STACK_STARTWALK(callStack) ;
1711     while ((func = STACK_WALK(callStack))) {
1712
1713         fprintf(stdout,"#%d  0x%08x in %s () at %s:%d\n",i++,
1714                 func->laddr,func->sym->name,
1715                 func->mod->c_name,func->lline);
1716     }
1717
1718 }
1719
1720 /*-----------------------------------------------------------------*/
1721 /* cmdWhere -  where command                                       */
1722 /*-----------------------------------------------------------------*/
1723 int cmdWhere(char *s, context *cctxt)
1724 {
1725         infoStack(cctxt);
1726         return 0;
1727 }
1728
1729
1730 static int infomode = 0;
1731 /*-----------------------------------------------------------------*/
1732 /* cmdInfo - info command                                          */
1733 /*-----------------------------------------------------------------*/
1734 int cmdInfo (char *s, context *cctxt)
1735 {
1736     while (isspace(*s)) s++;
1737
1738     /* list all break points */
1739     if (strncmp(s,"break",5) == 0) {
1740         listUSERbp();
1741         return 0;
1742     }
1743
1744     /* info frame same as frame */
1745     if (strcmp(s,"frame") == 0) {
1746         cmdFrame (s,cctxt);
1747         return 0;
1748     }
1749
1750     if (strncmp(s,"line",4) == 0) {
1751     infomode=1;
1752         cmdListSrc (s+4,cctxt);
1753         return 0;
1754     }
1755
1756     /* info stack display call stack */
1757     if (strcmp(s,"stack") == 0) {
1758         infoStack(cctxt);
1759     showfull = 1;
1760         return 0;
1761     }
1762
1763     /* info stack display call stack */
1764     if (strcmp(s,"registers") == 0) {
1765         fprintf(stdout,"%s",simRegs());
1766         return 0;
1767     }
1768
1769     /* info stack display call stack */
1770     if (strcmp(s,"all-registers") == 0) 
1771     {
1772         int i,j;
1773         fprintf(stdout,"%s",simRegs());
1774         fprintf(stdout,"Special Function Registers:\n");
1775         for ( i = 0 ; i < 0x100 ; i++ )
1776         {
1777             symbol *sym = NULL;
1778             unsigned long val;
1779             if (applyToSetFTrue(sfrsymbols,symWithAddr,i,'I',&sym))
1780             {
1781                 val = simGetValue (sym->addr,sym->addrspace,sym->size);
1782                 fprintf(stdout,"%s=0x%02x",sym->name,val);
1783                 if ( !(i & 0x07 ))
1784                 {
1785                     for ( j = 0 ; j < 8 ; j++ )
1786                     {
1787                         sym = NULL;
1788                         if (applyToSetFTrue(sfrsymbols,symWithAddr,i+j,'J',&sym))
1789                         {
1790                             //val = simGetValue (sym->addr,sym->addrspace,sym->size);
1791                             fprintf(stdout," %s=%c",sym->name,(val&1)? '1':'0');
1792                         }
1793                         val >>= 1;
1794                     }
1795                 }
1796                 fprintf(stdout,"\n");
1797             }
1798         }
1799         return 0;
1800     }
1801
1802     /* info stack display call stack */
1803     if (strcmp(s,"symbols") == 0) {
1804       /* dump out symbols we have read in */
1805       fprintf(stdout,"Dumping symbols...\n");
1806       infoSymbols(cctxt);
1807       return 0;
1808     }
1809
1810     if (strcmp(s,"variables") == 0) {
1811       /* dump out symbols we have read in */
1812       fprintf(stdout,"Dumping symbols...\n");
1813       infoSymbols(cctxt);
1814       return 0;
1815     }
1816
1817     fprintf(stdout,"Undefined info command: \"%s\".  Try \"help\n",s);
1818     return 0;
1819
1820 }
1821
1822 /*-----------------------------------------------------------------*/
1823 /* cmdQuit  - quit debugging                                       */
1824 /*-----------------------------------------------------------------*/
1825 int cmdQuit (char *s, context *cctxt)
1826 {   
1827     if (simactive)
1828         closeSimulator();
1829     return 1;
1830 }
1831
1832 /*-----------------------------------------------------------------*/
1833 /* cmdListSrc  - list src                                          */
1834 /*-----------------------------------------------------------------*/
1835 int cmdListSrc (char *s, context *cctxt)
1836 {   
1837     static int currline = 0;
1838     int i =0 ;
1839     int pline = 0;
1840     int llines = listLines;
1841
1842     while (*s && isspace(*s)) s++;
1843     
1844     /* if the user has spcified line numer then the line number
1845        can be of the following formats
1846        LINE          - just line number
1847        FILE:LINE     - filename line number
1848        FUNCTION      - list a function
1849        FILE:FUNCTION - function in file */
1850
1851     if (*s) {
1852         /* case a) LINE */
1853         if (isdigit(*s)) {
1854             sscanf(s,"%d",&pline);
1855             if (!cctxt || !cctxt->func || !cctxt->func->mod) {
1856               if (!list_mod) {
1857                 fprintf(stdout,"Sdcdb fails to have a proper context at %d.\n", __LINE__);
1858                 return 0;
1859               }
1860             }
1861             else
1862               list_mod = cctxt->func->mod;
1863         }
1864         else {
1865             char *bp;
1866             function *func = NULL;
1867             
1868             /* if ':' present then FILE:LINE || FILE:FUNCTION */
1869             if ((bp = strchr(s,':'))) {
1870                 *bp = '\0';
1871                 bp ++;
1872                 if (isdigit(*bp)) {
1873                     /* FILE:LINE */
1874                     list_mod=NULL;  /* bug fix 2-09-02, moduleWithCName expects mod to be null */
1875                     if (srcMode == SRC_CMODE) {
1876                         if (!applyToSet(modules,moduleWithCName,s,&list_mod)) {
1877                             fprintf (stderr,"No c source file named %s.\n",s);
1878                             return 0;
1879                         }
1880                     } else {
1881                         if (!applyToSet(modules,moduleWithAsmName,s,&list_mod)) {
1882                             fprintf (stderr,"No source file named %s.\n",s);
1883                             return 0;
1884                         }
1885                     }
1886                     sscanf(bp,"%d",&pline);
1887                 } else {
1888                     /* FILE:FUCTION */
1889                     if (!applyToSet(functions,funcWithNameModule,bp,s,&func)) {
1890                         fprintf(stdout,"Function \"%s\" not defined.\n",bp);
1891                         return 0;
1892                     }
1893                     list_mod = func->mod;
1894                     if (srcMode == SRC_CMODE) {
1895                         pline = func->entryline;
1896                         llines = func->exitline - func->entryline + 1;
1897                     } else {
1898                         pline = func->aentryline;
1899                         llines = func->aexitline - func->aentryline + 1;
1900                     }
1901                 }
1902             }
1903             else {
1904                 /* FUNCTION */
1905                 if (!applyToSet(functions,funcWithName,s,&func)) {
1906                     fprintf(stderr,"Function \"%s\" not defined.\n",s); 
1907                     return 0;
1908                 }
1909                 else {
1910                     list_mod = func->mod;
1911                     if (srcMode == SRC_CMODE) {
1912                         pline = func->entryline;
1913                         llines = func->exitline - func->entryline + 1; 
1914                     } else {
1915                         pline = func->aentryline;
1916                         llines = func->aexitline - func->aentryline + 1; 
1917                     }
1918                 }
1919             }
1920         }
1921     } else {
1922         /* if no line specified & we had listed
1923            before then continue from that listing */
1924         if (currline)
1925             pline = currline ;
1926         else {
1927             if (!cctxt || !cctxt->func || !cctxt->func->mod) {
1928               fprintf(stdout,"Missing context at %d. Try list filename:lineno\n", __LINE__);
1929               return 0;
1930             }
1931             list_mod = cctxt->func->mod;
1932             if (srcMode == SRC_CMODE)
1933                 pline = cctxt->cline;
1934             else
1935                 pline = cctxt->asmline;
1936         }
1937     }
1938
1939     if (!list_mod) {
1940       fprintf(stdout,"Sdcdb fails to have a valid module context at %d.\n", __LINE__);
1941       return 0;
1942     }
1943     
1944     if (llines > listLines) llines = listLines;
1945
1946     if ( infomode )
1947     {
1948             fprintf(stdout,"Line %d of \"%s\" starts at address 0x%08x.\n",pline,
1949                     list_mod->c_name, list_mod->cLines[pline]->addr);
1950         infomode=0;
1951         return 0;
1952     }
1953     for ( i = 0 ; i < llines ; i++ ) {
1954         if (srcMode == SRC_CMODE) {
1955             if ( (pline + i) >= list_mod->ncLines )
1956                 break;
1957             fprintf(stdout,"%d\t%s",pline + i,
1958                     list_mod->cLines[pline +i]->src);
1959         } else {
1960             if ( (pline + i) >= list_mod->nasmLines )
1961                 break;
1962             fprintf(stdout,"%d\t%s",pline + i,
1963                     list_mod->asmLines[pline +i]->src);
1964         }
1965     }
1966     currline = pline + i ;
1967     return 0;
1968 }
1969
1970 static void setValBasic(symbol *sym, link *type,
1971                         char mem, unsigned addr,int size, char *val)
1972 {
1973     char *s;
1974     union 
1975     {   
1976         float f;     
1977         unsigned long val;
1978         long         sval;
1979         struct {
1980             unsigned short    lo;
1981             unsigned short    hi;
1982         } i;
1983         unsigned char b[4];
1984     }v;
1985
1986     if (IS_FLOAT(type))         
1987         v.f = strtof(val,NULL);    
1988     else
1989         if (IS_PTR(type))
1990             v.val = strtol(val,NULL,0);
1991         else
1992     {
1993             if (IS_INTEGRAL(type)) 
1994         {
1995             link *etype;
1996             if ( type->next )
1997                 etype = type->next;
1998             else
1999                 etype = type;
2000             if (IS_CHAR(etype))
2001             {
2002                 if (( s = strchr(val,'\'')))
2003                 {
2004                     if ( s[1] == '\\' )
2005                         v.b[0] = strtol(s+2,NULL,8);
2006                     else 
2007                         v.b[0] = s[1];
2008                 }
2009                 else
2010                 {
2011                     v.b[0] = strtol(val,NULL,0);
2012                 }
2013             }
2014             else
2015                 if (IS_INT(etype)) 
2016                     if (IS_LONG(etype))
2017                         v.val = strtol(val,NULL,0);
2018                     else
2019                         v.i.lo = strtol(val,NULL,0);
2020                 else
2021                     v.val = strtol(val,NULL,0);
2022             } 
2023         else
2024             v.val = strtol(val,NULL,0);
2025     }
2026     simSetValue(addr,mem,size,v.val);   
2027 }
2028
2029 /*-----------------------------------------------------------------*/
2030 /* printFmtInteger - print value in bin,oct,dez or hex             */
2031 /*-----------------------------------------------------------------*/
2032 static void printFmtInteger(char *deffmt,int fmt, long val, 
2033                             int sign, int size)
2034 {
2035     static char digits[] = 
2036     {
2037         '0' , '1' , '2' , '3' , '4' , '5' ,
2038         '6' , '7' , '8' , '9' , 'a' , 'b' ,
2039         'c' , 'd' , 'e' , 'f' , 'g' , 'h' 
2040     };
2041     static int radixOfFormat[] = { 0 , 2, 8 ,10, 16  };
2042     static int olenOfSize[]    = { 0 , 3, 6 , 8, 11  };
2043         char buf[40];
2044         char negative = 0;
2045         int charPos = 38;
2046     int radix;
2047
2048     if ( fmt == FMT_NON || fmt == FMT_DEZ )
2049     {
2050         fprintf(stdout,deffmt,val);
2051         return;
2052     }
2053     radix = radixOfFormat[fmt];
2054
2055     /*
2056     if ( sign && val < 0 )
2057         negative = 1;
2058     */
2059
2060         if (!negative)
2061             val = -val;
2062
2063         buf[39] = '\0';
2064     while (val <= -radix) 
2065     {
2066             buf[charPos--] = digits[-(val % radix)];
2067             val = val / radix;
2068         }
2069         buf[charPos] = digits[-val];
2070
2071     switch ( fmt )
2072     {
2073         case FMT_OCT:
2074             radix = olenOfSize[size];
2075             break;
2076         case FMT_HEX:
2077             radix = size << 1;
2078             break;
2079         case FMT_BIN:
2080             radix = size << 3;
2081             break;
2082     }
2083
2084     while (charPos > 39 - radix )
2085     {
2086         buf[--charPos] = '0';
2087     } 
2088     switch ( fmt )
2089     {
2090         case FMT_OCT:
2091             if ( buf[charPos] != '0' )
2092                 buf[--charPos] = '0';
2093             break;
2094         case FMT_HEX:
2095             buf[--charPos] = 'x';
2096             buf[--charPos] = '0';
2097             break;
2098     }
2099         if (negative) {
2100             buf[--charPos] = '-';
2101         }
2102     fputs(&buf[charPos],stdout);
2103 }
2104
2105 /*-----------------------------------------------------------------*/
2106 /* printValBasic - print value of basic types                      */
2107 /*-----------------------------------------------------------------*/
2108 static void printValBasic(symbol *sym, link *type,
2109                           char mem, unsigned addr,int size, int fmt)
2110 {
2111     union {     
2112         float f;     
2113         unsigned long val;
2114         long         sval;
2115         struct {
2116             unsigned short    lo;
2117             unsigned short    hi;
2118         } i;
2119         unsigned char b[4];
2120     }v;
2121     union {
2122         unsigned char b[4];
2123     }v1;
2124     
2125     v.val = simGetValue(addr,mem,size);
2126     /* if this a floating point number then */
2127     if (IS_FLOAT(type))         
2128         fprintf(stdout,"%f",v.f);    
2129     else
2130         if (IS_PTR(type))
2131             fprintf(stdout,"0x%*x",size<<1,v.val);
2132         else
2133         if (IS_INTEGRAL(type)) 
2134         {
2135             link *etype;
2136             if ( type->next )
2137                 etype = type->next;
2138             else
2139                 etype = type;
2140             if (IS_CHAR(etype))
2141             { 
2142                 if ( isprint(v.val))
2143                     printFmtInteger("'%c'",fmt,(long)v.val,0,size);
2144                 else
2145                     printFmtInteger("'\\%o'",fmt,(long)v.val,0,size);
2146             }
2147             else
2148             {
2149                 if (IS_INT(etype)) 
2150                     if (IS_LONG(etype))
2151                         if (SPEC_USIGN(etype))
2152                             printFmtInteger("%u",fmt,(long)v.val,0,size);
2153                         else
2154                             printFmtInteger("%d",fmt,(long)v.sval,1,size);
2155                     else
2156                         if (SPEC_USIGN(etype))
2157                             printFmtInteger("%u",fmt,(long)v.i.lo,0,size);
2158                         else
2159                             printFmtInteger("%d",fmt,(long)v.i.lo,1,size);
2160                 else
2161                 {
2162                     if (IS_BITVAR(etype))
2163                         fprintf(stdout,"%c",(v.val?'1':'0'));
2164                     else
2165                         fprintf(stdout,"0x%0*x",size<<1,v.val);
2166                 }
2167             }
2168             } else
2169             fprintf(stdout,"0x%0*x",size<<1,v.val);  
2170 }
2171
2172 /*-----------------------------------------------------------------*/
2173 /* printValFunc  - prints function values                          */
2174 /*-----------------------------------------------------------------*/
2175 static void printValFunc (symbol *sym, int fmt)
2176 {
2177     fprintf(stdout,"print function not yet implemented");
2178 }
2179
2180 /*-----------------------------------------------------------------*/
2181 /* printArrayValue - will print the values of array elements       */
2182 /*-----------------------------------------------------------------*/
2183 static void printArrayValue (symbol *sym,  link *type,
2184                              char space, unsigned int addr, int fmt)
2185 {
2186         link *elem_type = type->next;
2187         int i;
2188         
2189         fprintf(stdout,"{");
2190         for (i = 0 ; i < DCL_ELEM(type) ; i++) {                
2191                 if (IS_AGGREGATE(elem_type)) {
2192                         printValAggregates(sym,elem_type,space,addr,fmt);                      
2193                 } else {
2194                         printValBasic(sym,elem_type,space,addr,getSize(elem_type),fmt);
2195                 }
2196                 addr += getSize(elem_type);
2197                 if (i != DCL_ELEM(type) -1)
2198                         fprintf(stdout,",");
2199         }
2200
2201         fprintf(stdout,"}");            
2202 }
2203
2204 /*-----------------------------------------------------------------*/
2205 /* printStructValue - prints structures elements                   */
2206 /*-----------------------------------------------------------------*/
2207 static void printStructValue (symbol *sym, link *type, 
2208                               char space, unsigned int addr, int fmt) 
2209 {
2210         symbol *fields = SPEC_STRUCT(type)->fields;
2211     int first = 1;
2212         fprintf(stdout," { ");
2213         while (fields) {
2214                 fprintf(stdout,"%s%s = ",(first ? "": ", "),fields->name);
2215                 first = 0;
2216         if (IS_AGGREGATE(fields->type)) {
2217                         printValAggregates(fields,fields->type,space, addr, fmt);
2218                 } else {
2219                         printValBasic(fields,fields->type,space,addr,getSize(fields->type), fmt);
2220                 }
2221                 addr += getSize(fields->type);
2222                 fields = fields->next;
2223         }
2224         fprintf(stdout,"}");
2225 }
2226
2227 /*-----------------------------------------------------------------*/
2228 /* printValAggregates - print value of aggregates                  */
2229 /*-----------------------------------------------------------------*/
2230 static void printValAggregates (symbol *sym, link *type,
2231                                 char space,unsigned int addr, int fmt)
2232 {
2233
2234         if (IS_ARRAY(type)) {
2235                 printArrayValue(sym, type, space, addr, fmt);
2236                 return ;
2237         }
2238
2239         if (IS_STRUCT(type)) { 
2240                 printStructValue(sym, type, space, addr, fmt); 
2241                 return; 
2242         } 
2243 }
2244
2245 /*-----------------------------------------------------------------*/
2246 /* printOrSetSymValue - print or set value of a symbol             */
2247 /*-----------------------------------------------------------------*/
2248 static void printOrSetSymValue (symbol *sym, context *cctxt, 
2249                            int flg, int dnum, int fmt, char *rs, char *val)
2250 {
2251     static char fmtChar[] = " todx ";
2252     static int stack = 1;
2253         symbol *fields;
2254     link *type;
2255     unsigned int  addr; 
2256     int size, n;
2257     char *s, *s2;
2258     char save_ch, save_ch2;
2259
2260     /* if it is on stack then compute address & fall thru */
2261     if (sym->isonstack) 
2262     {
2263         symbol *bp = symLookup("bp",cctxt);
2264         if (!bp) 
2265         {
2266             fprintf(stdout,"cannot determine stack frame\n");
2267             return ;
2268         }
2269
2270         sym->addr = simGetValue(bp->addr,bp->addrspace,bp->size)
2271             + sym->offset ;      
2272     }
2273     
2274     /* get the value from the simulator and
2275        print it */
2276     switch (flg)
2277     {
2278         case 0: 
2279         default:
2280             break;
2281         case 1: 
2282             fprintf(stdout,"$%d = ",stack++);
2283             break;
2284         case 2: 
2285             fprintf(stdout,"%d: ", dnum);
2286             if ( fmt != FMT_NON )
2287                 fprintf(stdout,"/%c ",fmtChar[fmt]);
2288             fprintf(stdout,"%s%s = ",sym->name,rs);
2289             break;
2290     }
2291
2292     addr = sym->addr;
2293     type = sym->type;
2294     size = sym->size;
2295
2296     while ( *rs )
2297     {
2298         if ( *rs == '[' && IS_ARRAY(type))
2299         {
2300             s = rs+1;
2301             while ( *rs && *rs != ']' ) rs++ ;
2302             save_ch = *rs;
2303             *rs = '\0' ;
2304             if ( ! isdigit(*s ))
2305             {
2306                 /* index seems a variable */
2307                 for ( s2 = s; *s2 && ( isalnum( *s2 ) || *s2 == '_'); s2++ );
2308                 save_ch2 = *s2;
2309                 if ( *s2 )
2310                     *s2 = '\0';
2311                 fields = symLookup(s,cctxt);
2312                 *s2 = save_ch2;
2313                 if ( ! fields )
2314                 {
2315                     fprintf(stdout,"Unkown variable \"%s\" for index.\n", s);
2316                     return;                    
2317                 }
2318                 /* arrays & structures first */
2319                 if (! IS_INTEGRAL(fields->type))
2320                 {
2321                     fprintf(stdout,"Wrong type of variable \"%s\" for index \n", s);
2322                     return;                    
2323                 }
2324                 n = simGetValue(fields->addr,fields->addrspace,getSize(fields->type));
2325             }
2326             else
2327             {
2328                 n = strtol(s,0,0);
2329             }
2330             if ( n < 0 || n >= DCL_ELEM(type))
2331             {
2332                 fprintf(stdout,"Wrong index %d.\n", n);
2333                 return;                    
2334             }
2335             type = type->next;
2336             size = getSize(type);
2337             addr += size * n;
2338             *rs++ = save_ch;
2339         }
2340         else if ( *rs == '.' && IS_STRUCT(type))
2341         {
2342             s = rs+1;
2343             /* search structure element */
2344             for ( rs = s; *rs && ( isalnum( *rs ) || *rs == '_'); rs++ );
2345             save_ch = *rs;
2346             if ( *rs )
2347                 *rs = '\0';
2348             for (fields = SPEC_STRUCT(type)->fields; fields; fields = fields->next) 
2349             {
2350                 if (!(strcmp(s,fields->name)))
2351                     break;
2352             }
2353             *rs = save_ch;
2354             if ( ! fields )
2355             {
2356                 fprintf(stdout,"Unknown field \"%s\" of structure\n", s);
2357                 return;                    
2358             }
2359             type = fields->type;
2360             size = getSize(type);
2361             addr += fields->offset;
2362         }
2363         else
2364             break;
2365     }
2366
2367     /* arrays & structures first */
2368     if (IS_AGGREGATE(type))
2369     {
2370             if ( val )
2371             fprintf(stdout,"Cannot set aggregate variable\n");
2372         else
2373             printValAggregates(sym,type,sym->addrspace,addr,fmt);
2374     }
2375     else
2376         /* functions */
2377         if (IS_FUNC(type))
2378     {
2379             if ( !val )
2380             printValFunc(sym,fmt);
2381     }
2382         else
2383     { 
2384             if ( val )
2385             setValBasic  (sym,type,sym->addrspace,addr,size,val);
2386         else
2387             printValBasic(sym,type,sym->addrspace,addr,size,fmt);
2388     }
2389     if ( flg > 0 ) fprintf(stdout,"\n");
2390         
2391 }
2392
2393 /*-----------------------------------------------------------------*/
2394 /* printStructInfo - print out structure information               */
2395 /*-----------------------------------------------------------------*/
2396 static void printStructInfo (structdef *sdef)
2397 {
2398     symbol *field = sdef->fields ;
2399     int i = 0 ;
2400     
2401     while (field) {
2402         i += field->offset;
2403         field = field->next;
2404     }
2405
2406     fprintf(stdout,"%s %s {\n",(i ? "struct" : "union" ), sdef->tag);
2407     field = sdef->fields;
2408     while (field) {
2409         printTypeInfo (field->type);
2410         fprintf(stdout," %s ;\n",field->name);
2411         field = field->next ;
2412     }
2413
2414     fprintf(stdout,"}\n");
2415
2416 }
2417
2418 /*-----------------------------------------------------------------*/
2419 /* printTypeInfo - print out the type information                  */
2420 /*-----------------------------------------------------------------*/
2421 static void printTypeInfo(link *p)
2422 {
2423     if (!p)
2424         return ;
2425
2426     if (IS_DECL(p)) {
2427         switch (DCL_TYPE(p))  {
2428         case FUNCTION:
2429             printTypeInfo (p->next);
2430             fprintf(stdout,"()");
2431             break;
2432         case ARRAY:
2433             printTypeInfo (p->next);
2434             fprintf(stdout,"[%d]",DCL_ELEM(p));
2435             break;
2436         
2437         case IPOINTER:
2438         case PPOINTER:
2439         case POINTER:
2440             printTypeInfo (p->next);
2441             fprintf(stdout,"(_near *)");
2442             break;
2443
2444         case FPOINTER:
2445             printTypeInfo (p->next);
2446             fprintf(stdout,"(_xdata *)");
2447             break;
2448
2449         case CPOINTER:
2450             printTypeInfo( p->next);
2451             fprintf(stdout,"(_code *)");
2452             break;
2453             
2454         case GPOINTER:
2455             printTypeInfo( p->next);
2456             fprintf(stdout,"(_generic *)");
2457             break;                   
2458         }
2459     } else {
2460         switch (SPEC_NOUN(p)) { /* depending on the specifier type */
2461         case V_INT:
2462             (IS_LONG(p) ? fputs("long ",stdout) : 
2463              ( IS_SHORT(p) ? fputs("short ",stdout) : 
2464                fputs("int ",stdout))) ;
2465             break;
2466         case V_FLOAT:
2467              fputs("float ",stdout);
2468              break;
2469
2470         case V_CHAR:
2471             fputs ("char ",stdout);
2472             break;
2473
2474         case V_VOID:
2475             fputs("void ",stdout);
2476             break;
2477
2478         case V_STRUCT:
2479             printStructInfo (SPEC_STRUCT(p));
2480             break;
2481
2482         case V_SBIT:
2483             fputs("sbit ",stdout);
2484             break;
2485
2486         case V_BIT:
2487             fprintf(stdout,": %d" ,SPEC_BLEN(p));       
2488             break;
2489         }
2490     }
2491 }
2492
2493 /*-----------------------------------------------------------------*/
2494 /* cmdPrint - print value of variable                              */
2495 /*-----------------------------------------------------------------*/
2496 int cmdPrint (char *s, context *cctxt)
2497 {   
2498     symbol *sym ;
2499     int fmt;
2500     char *rs;
2501     if ( !( rs = preparePrint(s, cctxt, &fmt, &sym )))
2502         return 0;
2503
2504     if ( sym ) 
2505     {
2506         printOrSetSymValue(sym,cctxt,1,0,fmt,rs,NULL);
2507     } 
2508     return 0;
2509 }
2510
2511 /*-----------------------------------------------------------------*/
2512 /* cmdOutput - print value of variable without number and newline  */
2513 /*-----------------------------------------------------------------*/
2514 int cmdOutput (char *s, context *cctxt)
2515 {   
2516     symbol *sym ;
2517     int fmt;
2518     char *rs;
2519     if ( !( rs = preparePrint(s, cctxt, &fmt, &sym )))
2520         return 0;
2521
2522     if ( sym ) 
2523     {
2524         printOrSetSymValue(sym,cctxt,0,0,fmt,rs,NULL);
2525     } 
2526     return 0;
2527 }
2528
2529 /** find display entry with this number */
2530
2531 DEFSETFUNC(dsymWithNumber)
2532 {
2533     dsymbol *dsym = item;
2534     V_ARG(int , dnum);
2535     V_ARG(dsymbol **,dsymp);
2536
2537     if ( dsym->dnum == dnum )
2538     {
2539         *dsymp = dsym;
2540         return 1;
2541     }
2542     return 0;
2543 }
2544
2545 /*-----------------------------------------------------------------*/
2546 /* displayAll  - display all valid variables                       */
2547 /*-----------------------------------------------------------------*/
2548 void displayAll(context *cctxt)
2549 {
2550     dsymbol *dsym;
2551     symbol  *sym;
2552     if ( !dispsymbols )
2553         return;
2554     for (dsym = setFirstItem(dispsymbols);
2555          dsym ;
2556          dsym = setNextItem(dispsymbols)) 
2557     {
2558         if ( (sym = symLookup(dsym->name,cctxt)))
2559             printOrSetSymValue(sym,cctxt,2,dsym->dnum,dsym->fmt,dsym->rs,NULL);
2560     }
2561 }
2562
2563 /*-----------------------------------------------------------------*/
2564 /* cmdDisplay  - display value of variable                         */
2565 /*-----------------------------------------------------------------*/
2566 int cmdDisplay (char *s, context *cctxt)
2567 {   
2568     static int dnum = 1;
2569     symbol *sym ;
2570     int fmt;
2571     char *rs;
2572     if ( !( rs = preparePrint(s, cctxt, &fmt, &sym )))
2573     {
2574         displayAll(cctxt);
2575         return 0;
2576     }
2577
2578     if ( sym ) 
2579     {
2580         dsymbol *dsym = (dsymbol *)Safe_calloc(1,sizeof(dsymbol));
2581         dsym->dnum = dnum++ ;
2582         dsym->name = sym->name;
2583         dsym->fmt  = fmt;
2584         dsym->rs   = gc_strdup(rs);
2585         addSetHead(&dispsymbols,dsym);
2586     }
2587     return 0;
2588 }
2589
2590 /*-----------------------------------------------------------------*/
2591 /* cmdUnDisplay  - undisplay value of variable                              */
2592 /*-----------------------------------------------------------------*/
2593 int cmdUnDisplay (char *s, context *cctxt)
2594 {   
2595     dsymbol *dsym;
2596     int dnum;
2597
2598     while (isspace(*s)) s++;
2599     if (!*s)
2600     {
2601         for (dsym = setFirstItem(dispsymbols);
2602              dsym;
2603              dsym = setNextItem(dispsymbols))
2604         {
2605             Safe_free(dsym->rs);
2606             Safe_free(dsym); 
2607         }
2608         deleteSet(&dispsymbols);
2609         return 0;
2610     }
2611     while ( s && *s )
2612     {
2613         dnum = strtol(s,&s,10);
2614         if (applyToSetFTrue(dispsymbols,dsymWithNumber,dnum,&dsym)) 
2615         {
2616             deleteSetItem(&dispsymbols,dsym);
2617             Safe_free(dsym->rs);
2618             Safe_free(dsym); 
2619         } 
2620         else
2621         {
2622             fprintf(stdout,"Arguments must be display numbers.\n");    
2623         }
2624     }
2625     return 0;
2626 }
2627
2628 /*-----------------------------------------------------------------*/
2629 /* cmdPrintType - print type of a variable                         */
2630 /*-----------------------------------------------------------------*/
2631 int cmdPrintType (char *s, context *cctxt)
2632 {   
2633         symbol *sym ;
2634     char *bp = s+strlen(s) -1;
2635
2636     while (isspace(*s)) s++;
2637     if (!*s) return 0;
2638     while (isspace(*bp)) bp--;
2639     bp++ ;
2640     *bp = '\0';
2641
2642     if ((sym = symLookup(s,cctxt))) {
2643         printTypeInfo(sym->type);
2644         fprintf(stdout,"\n");
2645     } else {
2646         fprintf(stdout,
2647                 "No symbol \"%s\" in current context.\n",
2648                 s);
2649     }
2650     return 0;   
2651 }
2652
2653 /*-----------------------------------------------------------------*/
2654 /* cmdClrUserBp - clear user break point                           */
2655 /*-----------------------------------------------------------------*/
2656 int cmdClrUserBp (char *s, context *cctxt)
2657 {   
2658     char *bp ;    
2659     function *func = NULL;
2660         
2661     /* clear break point location specification can be of the following
2662        forms
2663        a) <nothing>        - break point at current location
2664        b) lineno           - number of the current module
2665        c) filename:lineno  - line number of the given file
2666        e) filename:function- function X in file Y (useful for static functions)
2667        f) function         - function entry point
2668     */
2669
2670     if (!cctxt) {
2671         fprintf(stdout,"No symbol table is loaded.  Use the \"file\" command.\n");
2672         return 0;
2673     }
2674
2675     /* white space skip */
2676     while (*s && isspace(*s)) s++;
2677     
2678     /* null terminate it after stripping trailing blanks*/
2679     bp = s + strlen(s);
2680     while (bp != s && isspace(*bp)) bp--;
2681     *bp = '\0';
2682
2683     /* case a) nothing */
2684     /* if nothing given then current location : we know
2685        the current execution location from the currentContext */
2686     if (! *s ) {
2687
2688         /* if current context is known */
2689         if (cctxt->func) 
2690             /* clear the break point @ current location */
2691             clearUSERbp (cctxt->addr);
2692         else
2693             fprintf(stderr,"No default breakpoint address now.\n");
2694                         
2695         goto ret ;
2696     }
2697
2698     /* case b) lineno */
2699     /* check if line number */
2700     if (isdigit(*s)) {
2701         /* get the lineno */
2702         int line = atoi(s);
2703
2704         /* if current context not present then we must get the module
2705            which has main & set the break point @ line number provided
2706            of that module : if current context known then set the bp 
2707            at the line number given for the current module 
2708         */
2709         if (cctxt->func) {
2710             if (!cctxt->func->mod) {
2711                 if (!applyToSet(functions,funcWithName,"main"))
2712                     fprintf(stderr,"Function \"main\" not defined.\n");
2713                 else 
2714                     clearBPatModLine(func->mod,line);
2715             } else 
2716                 clearBPatModLine(cctxt->func->mod,line);                        
2717         }
2718         
2719         goto ret;
2720     }
2721
2722     if ((bp = strchr(s,':'))) {
2723         
2724         module *mod = NULL;
2725         *bp = '\0';
2726         
2727         if (!applyToSet(modules,moduleWithCName,s,&mod)) {
2728             fprintf (stderr,"No source file named %s.\n",s);
2729             goto ret;
2730         }
2731
2732         /* case c) filename:lineno */
2733         if (isdigit(*(bp +1))) {                    
2734          
2735             clearBPatModLine (mod,atoi(bp+1));      
2736             goto ret;
2737             
2738         }
2739         /* case d) filename:function */
2740         if (!applyToSet(functions,funcWithNameModule,bp+1,s,&func)) 
2741             fprintf(stderr,"Function \"%s\" not defined.\n",bp+1); 
2742         else
2743             clearBPatModLine (mod,func->entryline);
2744         
2745         goto ret;
2746     }
2747             
2748     /* case e) function */
2749     if (!applyToSet(functions,funcWithName,s,&func))
2750         fprintf(stderr,"Function \"%s\" not defined.\n",s); 
2751     else
2752         clearBPatModLine(func->mod,func->entryline);
2753
2754  ret:    
2755     return 0;        
2756 }
2757
2758
2759 /*-----------------------------------------------------------------*/
2760 /* cmdSimulator - send command to simulator                        */
2761 /*-----------------------------------------------------------------*/
2762 int cmdSimulator (char *s, context *cctxt)
2763 {   
2764   char tmpstr[82];
2765
2766     if (strlen(s) > 80) {
2767       printf("error 3A\n");
2768       exit(1);
2769     }
2770     strcpy(tmpstr, s);
2771     strcat(tmpstr, "\n");
2772     sendSim(tmpstr);
2773     waitForSim(200,NULL);
2774     fprintf(stdout,"%s",simResponse());
2775     return 0;
2776 }
2777
2778 static int printFrame(int framenr, int getlast)
2779 {
2780     int i;
2781     function *func     = NULL;
2782     function *lastfunc = NULL;
2783
2784     STACK_STARTWALK(callStack) ;
2785     if ( framenr < 0 )
2786         framenr = 0;
2787     for ( i = 0; i <= framenr ; i++ )
2788     {
2789         lastfunc = func;
2790         func = STACK_WALK(callStack);
2791         if ( !func )
2792         {
2793             framenr = i-1;
2794             break;
2795         }
2796     }
2797     if (! func && getlast )
2798         func = lastfunc;
2799
2800     if ( func )
2801     {
2802         fprintf(stdout,"#%d  0x%08x in %s () at %s:%d\n",
2803                 framenr,func->laddr,func->sym->name,func->mod->c_name,func->lline);
2804         fprintf(stdout,"\032\032%s:%d:1:beg:0x%08x\n",
2805                 func->mod->cfullname,func->lline,func->laddr);
2806     }
2807     else
2808         fprintf(stdout,"No stack.\n");
2809
2810     return framenr;
2811 }
2812
2813
2814 /*-----------------------------------------------------------------*/
2815 /* cmdUp -  Up command                                             */
2816 /*-----------------------------------------------------------------*/
2817 int cmdUp(char *s, context *cctxt)
2818 {
2819     while (isspace(*s)) s++;
2820     if ( *s )
2821         currentFrame += strtol(s,0,10);
2822     else
2823         currentFrame++ ;
2824
2825     currentFrame = printFrame(currentFrame, 1 );
2826         return 0;
2827 }
2828
2829 /*-----------------------------------------------------------------*/
2830 /* cmdDown - down command                                          */
2831 /*-----------------------------------------------------------------*/
2832 int cmdDown(char *s, context *cctxt)
2833 {
2834     while (isspace(*s)) s++;
2835     if ( *s )
2836         currentFrame -= strtol(s,0,10);
2837     else
2838         currentFrame-- ;
2839
2840     currentFrame = printFrame(currentFrame, 1 );
2841         return 0;
2842 }
2843 /*-----------------------------------------------------------------*/
2844 /* cmdFrame - Frame command                                        */
2845 /*-----------------------------------------------------------------*/
2846 int cmdFrame (char *s, context *cctxt)
2847 {   
2848     function *func = NULL;
2849     int i, framenr = 0;
2850
2851     while (isspace(*s)) s++;
2852     if ( *s )
2853         framenr = strtol(s,0,10);
2854     else
2855         framenr = currentFrame;
2856     printFrame( framenr, 0 );
2857     return 0;
2858 }
2859
2860 /*-----------------------------------------------------------------*/
2861 /* cmdFinish - exec till end of current function                   */
2862 /*-----------------------------------------------------------------*/
2863 int cmdFinish (char *s, context *ctxt)
2864 {
2865     if (!ctxt || ! ctxt->func) {
2866         fprintf(stdout,"The program is not running.\n");
2867         return 0;
2868     }
2869
2870     if (srcMode == SRC_CMODE) {
2871         setBreakPoint (ctxt->func->sym->eaddr, CODE, STEP, 
2872                        stepBpCB, ctxt->func->mod->c_name, 
2873                        ctxt->func->exitline);
2874     } else {
2875         setBreakPoint (ctxt->func->sym->eaddr, CODE, STEP, 
2876                        stepBpCB, ctxt->func->mod->asm_name, 
2877                        ctxt->func->aexitline);
2878     }
2879
2880     simGo(-1);
2881     return 0;
2882     
2883 }
2884
2885
2886 /*-----------------------------------------------------------------*/
2887 /* cmdShow - show command                                          */
2888 /*-----------------------------------------------------------------*/
2889 int cmdShow (char *s, context *cctxt)
2890 {
2891     /* skip white space */
2892     while (*s && isspace(*s)) s++ ;
2893
2894     if (strcmp(s,"copying") == 0) {
2895         fputs(copying,stdout);
2896         return 0;
2897     }
2898     
2899     if (strcmp(s,"warranty") == 0) {
2900         fputs(warranty,stdout);
2901         return 0;
2902     }
2903
2904     return 0;
2905 }
2906