fix some things
[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
31 /* default number of lines to list out */
32 int listLines = 16;
33
34 /* mainly used to retain a reference to the active module being
35    listed.  May be used as a general context for other commands if
36    no better context is available */
37 static module *list_mod = NULL;
38
39 EXTERN_STACK_DCL(callStack,function *,1024);
40
41 #if defined(__APPLE__) && defined(__MACH__)
42 static char *copying=
43 {" GNU GENERAL PUBLIC LICENSE Version 2"};
44 static char *warranty=
45 {" NO WARRANTY"};
46 #else
47 static char *copying=
48 "                   GNU GENERAL PUBLIC LICENSE
49                        Version 2, June 1991
50
51  Copyright (C) 1989, 1991 Free Software Foundation, Inc.
52  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
53  Everyone is permitted to copy and distribute verbatim copies
54  of this license document, but changing it is not allowed.
55
56                             Preamble
57
58   The licenses for most software are designed to take away your
59 freedom to share and change it.  By contrast, the GNU General Public
60 License is intended to guarantee your freedom to share and change free
61 software--to make sure the software is free for all its users.  This
62 General Public License applies to most of the Free Software
63 Foundation's software and to any other program whose authors commit to
64 using it.  (Some other Free Software Foundation software is covered by
65 the GNU Library General Public License instead.)  You can apply it to
66 your programs, too.
67
68   When we speak of free software, we are referring to freedom, not
69 price.  Our General Public Licenses are designed to make sure that you
70 have the freedom to distribute copies of free software (and charge for
71 this service if you wish), that you receive source code or can get it
72 if you want it, that you can change the software or use pieces of it
73 in new free programs; and that you know you can do these things.
74
75   To protect your rights, we need to make restrictions that forbid
76 anyone to deny you these rights or to ask you to surrender the rights.
77 These restrictions translate to certain responsibilities for you if you
78 distribute copies of the software, or if you modify it.
79
80   For example, if you distribute copies of such a program, whether
81 gratis or for a fee, you must give the recipients all the rights that
82 you have.  You must make sure that they, too, receive or can get the
83 source code.  And you must show them these terms so they know their
84 rights.
85
86   We protect your rights with two steps: (1) copyright the software, and
87 (2) offer you this license which gives you legal permission to copy,
88 distribute and/or modify the software.
89
90   Also, for each author's protection and ours, we want to make certain
91 that everyone understands that there is no warranty for this free
92 software.  If the software is modified by someone else and passed on, we
93 want its recipients to know that what they have is not the original, so
94 that any problems introduced by others will not reflect on the original
95 authors' reputations.
96
97   Finally, any free program is threatened constantly by software
98 patents.  We wish to avoid the danger that redistributors of a free
99 program will individually obtain patent licenses, in effect making the
100 program proprietary.  To prevent this, we have made it clear that any
101 patent must be licensed for everyone's free use or not licensed at all.
102
103   The precise terms and conditions for copying, distribution and
104 modification follow.
105 ^L
106                     GNU GENERAL PUBLIC LICENSE
107    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
108
109   0. This License applies to any program or other work which contains
110 a notice placed by the copyright holder saying it may be distributed
111 under the terms of this General Public License.  The \"Program\", below,
112 refers to any such program or work, and a \"work based on the Program\"
113 means either the Program or any derivative work under copyright law:
114 that is to say, a work containing the Program or a portion of it,
115 either verbatim or with modifications and/or translated into another
116 language.  (Hereinafter, translation is included without limitation in
117 the term \"modification\".)  Each licensee is addressed as \"you\".
118
119 Activities other than copying, distribution and modification are not
120 covered by this License; they are outside its scope.  The act of
121 running the Program is not restricted, and the output from the Program
122 is covered only if its contents constitute a work based on the
123 Program (independent of having been made by running the Program).
124 Whether that is true depends on what the Program does.
125
126   1. You may copy and distribute verbatim copies of the Program's
127 source code as you receive it, in any medium, provided that you
128 conspicuously and appropriately publish on each copy an appropriate
129 copyright notice and disclaimer of warranty; keep intact all the
130 notices that refer to this License and to the absence of any warranty;
131 and give any other recipients of the Program a copy of this License
132 along with the Program.
133
134 You may charge a fee for the physical act of transferring a copy, and
135 you may at your option offer warranty protection in exchange for a fee.
136
137   2. You may modify your copy or copies of the Program or any portion
138 of it, thus forming a work based on the Program, and copy and
139 distribute such modifications or work under the terms of Section 1
140 above, provided that you also meet all of these conditions:
141
142     a) You must cause the modified files to carry prominent notices
143     stating that you changed the files and the date of any change.
144
145     b) You must cause any work that you distribute or publish, that in
146     whole or in part contains or is derived from the Program or any
147     part thereof, to be licensed as a whole at no charge to all third
148     parties under the terms of this License.
149
150     c) If the modified program normally reads commands interactively
151     when run, you must cause it, when started running for such
152     interactive use in the most ordinary way, to print or display an
153     announcement including an appropriate copyright notice and a
154     notice that there is no warranty (or else, saying that you provide
155     a warranty) and that users may redistribute the program under
156     these conditions, and telling the user how to view a copy of this
157     License.  (Exception: if the Program itself is interactive but
158     does not normally print such an announcement, your work based on
159     the Program is not required to print an announcement.)
160
161 These requirements apply to the modified work as a whole.  If
162 identifiable sections of that work are not derived from the Program,
163 and can be reasonably considered independent and separate works in
164 themselves, then this License, and its terms, do not apply to those
165 sections when you distribute them as separate works.  But when you
166 distribute the same sections as part of a whole which is a work based
167 on the Program, the distribution of the whole must be on the terms of
168 this License, whose permissions for other licensees extend to the
169 entire whole, and thus to each and every part regardless of who wrote it.
170
171 Thus, it is not the intent of this section to claim rights or contest
172 your rights to work written entirely by you; rather, the intent is to
173 exercise the right to control the distribution of derivative or
174 collective works based on the Program.
175
176 In addition, mere aggregation of another work not based on the Program
177 with the Program (or with a work based on the Program) on a volume of
178 a storage or distribution medium does not bring the other work under
179 the scope of this License.
180
181   3. You may copy and distribute the Program (or a work based on it,
182 under Section 2) in object code or executable form under the terms of
183 Sections 1 and 2 above provided that you also do one of the following:
184
185     a) Accompany it with the complete corresponding machine-readable
186     source code, which must be distributed under the terms of Sections
187     1 and 2 above on a medium customarily used for software interchange; or,
188
189     b) Accompany it with a written offer, valid for at least three
190     years, to give any third party, for a charge no more than your
191     cost of physically performing source distribution, a complete
192     machine-readable copy of the corresponding source code, to be
193     distributed under the terms of Sections 1 and 2 above on a medium
194     customarily used for software interchange; or,
195
196     c) Accompany it with the information you received as to the offer
197     to distribute corresponding source code.  (This alternative is
198     allowed only for noncommercial distribution and only if you
199     received the program in object code or executable form with such
200     an offer, in accord with Subsection b above.)
201
202 The source code for a work means the preferred form of the work for
203 making modifications to it.  For an executable work, complete source
204 code means all the source code for all modules it contains, plus any
205 associated interface definition files, plus the scripts used to
206 control compilation and installation of the executable.  However, as a
207 special exception, the source code distributed need not include
208 anything that is normally distributed (in either source or binary
209 form) with the major components (compiler, kernel, and so on) of the
210 operating system on which the executable runs, unless that component
211 itself accompanies the executable.
212
213 If distribution of executable or object code is made by offering
214 access to copy from a designated place, then offering equivalent
215 access to copy the source code from the same place counts as
216 distribution of the source code, even though third parties are not
217 compelled to copy the source along with the object code.
218 ^L
219   4. You may not copy, modify, sublicense, or distribute the Program
220 except as expressly provided under this License.  Any attempt
221 otherwise to copy, modify, sublicense or distribute the Program is
222 void, and will automatically terminate your rights under this License.
223 However, parties who have received copies, or rights, from you under
224 this License will not have their licenses terminated so long as such
225 parties remain in full compliance.
226
227   5. You are not required to accept this License, since you have not
228 signed it.  However, nothing else grants you permission to modify or
229 distribute the Program or its derivative works.  These actions are
230 prohibited by law if you do not accept this License.  Therefore, by
231 modifying or distributing the Program (or any work based on the
232 Program), you indicate your acceptance of this License to do so, and
233 all its terms and conditions for copying, distributing or modifying
234 the Program or works based on it.
235
236   6. Each time you redistribute the Program (or any work based on the
237 Program), the recipient automatically receives a license from the
238 original licensor to copy, distribute or modify the Program subject to
239 these terms and conditions.  You may not impose any further
240 restrictions on the recipients' exercise of the rights granted herein.
241 You are not responsible for enforcing compliance by third parties to
242 this License.
243
244   7. If, as a consequence of a court judgment or allegation of patent
245 infringement or for any other reason (not limited to patent issues),
246 conditions are imposed on you (whether by court order, agreement or
247 otherwise) that contradict the conditions of this License, they do not
248 excuse you from the conditions of this License.  If you cannot
249 distribute so as to satisfy simultaneously your obligations under this
250 License and any other pertinent obligations, then as a consequence you
251 may not distribute the Program at all.  For example, if a patent
252 license would not permit royalty-free redistribution of the Program by
253 all those who receive copies directly or indirectly through you, then
254 the only way you could satisfy both it and this License would be to
255 refrain entirely from distribution of the Program.
256
257 If any portion of this section is held invalid or unenforceable under
258 any particular circumstance, the balance of the section is intended to
259 apply and the section as a whole is intended to apply in other
260 circumstances.
261
262 It is not the purpose of this section to induce you to infringe any
263 patents or other property right claims or to contest validity of any
264 such claims; this section has the sole purpose of protecting the
265 integrity of the free software distribution system, which is
266 implemented by public license practices.  Many people have made
267 generous contributions to the wide range of software distributed
268 through that system in reliance on consistent application of that
269 system; it is up to the author/donor to decide if he or she is willing
270 to distribute software through any other system and a licensee cannot
271 impose that choice.
272
273 This section is intended to make thoroughly clear what is believed to
274 be a consequence of the rest of this License.
275
276   8. If the distribution and/or use of the Program is restricted in
277 certain countries either by patents or by copyrighted interfaces, the
278 original copyright holder who places the Program under this License
279 may add an explicit geographical distribution limitation excluding
280 those countries, so that distribution is permitted only in or among
281 countries not thus excluded.  In such case, this License incorporates
282 the limitation as if written in the body of this License.
283
284   9. The Free Software Foundation may publish revised and/or new versions
285 of the General Public License from time to time.  Such new versions will
286 be similar in spirit to the present version, but may differ in detail to
287 address new problems or concerns.
288
289 Each version is given a distinguishing version number.  If the Program
290 specifies a version number of this License which applies to it and \"any
291 later version\", you have the option of following the terms and conditions
292 either of that version or of any later version published by the Free
293 Software Foundation.  If the Program does not specify a version number of
294 this License, you may choose any version ever published by the Free Software
295 Foundation.
296
297   10. If you wish to incorporate parts of the Program into other free
298 programs whose distribution conditions are different, write to the author
299 to ask for permission.  For software which is copyrighted by the Free
300 Software Foundation, write to the Free Software Foundation; we sometimes
301 make exceptions for this.  Our decision will be guided by the two goals
302 of preserving the free status of all derivatives of our free software and
303 of promoting the sharing and reuse of software generally.
304 ";
305 static char *warranty=
306 "                            NO WARRANTY
307
308   11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
309 FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
310 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
311 PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
312 OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
313 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
314 TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
315 PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
316 REPAIR OR CORRECTION.
317
318   12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
319 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
320 REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
321 INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
322 OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
323 TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
324 YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
325 PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
326 POSSIBILITY OF SUCH DAMAGES.
327 ";
328 #endif
329
330 static void printTypeInfo(link *);
331 static void printValAggregates (symbol *,link *,char,unsigned int);
332
333 int srcMode = SRC_CMODE ;
334
335 /*-----------------------------------------------------------------*/
336 /* funcWithName - returns function with name                       */
337 /*-----------------------------------------------------------------*/
338 DEFSETFUNC(funcWithName)
339 {
340     function *func = item;
341     V_ARG(char *,name);
342     V_ARG(function **,funcp);
343
344     if (*funcp)
345         return 0;
346
347     if (strcmp(func->sym->name,name) == 0) {
348         *funcp = func;
349         return 1;
350     }
351     
352     return 0;
353 }
354
355 /*-----------------------------------------------------------------*/
356 /* setBPatModLine - set break point at the line specified for the  */
357 /*-----------------------------------------------------------------*/
358 static void setBPatModLine (module *mod, int line)
359 {
360   int next_line;
361
362     /* look for the first executable line after the line
363        specified & get the break point there */    
364     if (srcMode == SRC_CMODE && line > mod->ncLines) {
365         fprintf(stderr,"No line %d in file \"%s\".\n",
366                 line,mod->c_name);
367         return ;
368     }
369     
370     if (srcMode == SRC_AMODE && line > mod->nasmLines) {
371         fprintf(stderr,"No line %d in file \"%s\".\n",
372                 line,mod->asm_name);
373         return ;
374     }
375
376     next_line = line;
377     for ( ; next_line < (srcMode == SRC_CMODE ? mod->ncLines : mod->nasmLines ) ; 
378           next_line++ ) {
379         if (srcMode == SRC_CMODE) {
380             if (mod->cLines[next_line]->addr) {
381                 setBreakPoint (mod->cLines[next_line]->addr, CODE, USER, 
382                                userBpCB, mod->c_name, next_line);
383                 return;
384 //              break;
385             }
386         }
387         else {
388            if (mod->asmLines[next_line]->addr) {
389                setBreakPoint (mod->asmLines[next_line]->addr, CODE, USER, 
390                               userBpCB, mod->asm_name, next_line);
391                 return;
392 //             break;
393            } 
394         }
395     }
396
397         fprintf(stderr,"No line %d or after in file \"%s\"..\n",
398                         line,mod->c_name);
399
400     return;
401 }
402
403 /*-----------------------------------------------------------------*/
404 /* clearBPatModLine - clr break point at the line specified        */
405 /*-----------------------------------------------------------------*/
406 static void clearBPatModLine (module *mod, int line)
407 {
408     /* look for the first executable line after the line
409        specified & get the break point there */
410     if (srcMode == SRC_CMODE && line > mod->ncLines) {
411         fprintf(stderr,"No line %d in file \"%s\".\n",
412                 line,mod->c_name);
413         return ;
414     }
415     
416     if (srcMode == SRC_AMODE && line > mod->ncLines) {
417         fprintf(stderr,"No line %d in file \"%s\".\n",
418                 line,mod->c_name);
419         return ;
420     }    
421     
422     for ( ; line < (srcMode == SRC_CMODE ? mod->ncLines : mod->nasmLines ) ; 
423           line++ ) {
424         if (srcMode == SRC_CMODE) 
425             if (mod->cLines[line]->addr) {
426                 clearUSERbp (mod->cLines[line]->addr);                    
427                 break;
428             }
429         else
430             if (mod->asmLines[line]->addr) {
431                 clearUSERbp (mod->asmLines[line]->addr);                          
432                 break;
433             }
434     }
435
436     return;
437 }
438
439 /*-----------------------------------------------------------------*/
440 /* funcWithNameModule - returns functions with a name module combo */
441 /*-----------------------------------------------------------------*/
442 DEFSETFUNC(funcWithNameModule) 
443 {
444     function *func = item;
445     V_ARG(char *,fname);
446     V_ARG(char *,mname);
447     V_ARG(function **,funcp);
448
449     if (*funcp)
450         return 0;
451
452     if (strcmp(func->sym->name,fname) == 0 &&
453         strcmp(func->mod->c_name,mname) == 0) {
454         *funcp = func;
455         return 1;
456     }
457
458     return 0;
459 }
460
461 /*-----------------------------------------------------------------*/
462 /* funcInAddr - given an address returns the function              */
463 /*-----------------------------------------------------------------*/
464 DEFSETFUNC(funcInAddr)
465 {
466     function *func = item;
467     V_ARG(unsigned int,addr);
468     V_ARG(function **,funcp);
469
470     if (*funcp)
471         return 0;
472
473     /* in the address range */
474     if (func->sym->addr <= addr &&
475         func->sym->eaddr >= addr) {
476         
477         *funcp = func;
478         return 1;
479     }
480
481     return 0;       
482 }
483
484 /*-----------------------------------------------------------------*/
485 /* setStepBp - will set STEP Bp @ function entry points            */
486 /*-----------------------------------------------------------------*/
487 DEFSETFUNC(setStepBp)
488 {
489     function *func = item;
490     
491     if (func->sym && func->sym->addr ) {
492         
493         /* set the entry break point */
494         setBreakPoint (func->sym->addr , CODE , STEP , 
495                        stepBpCB ,func->mod->c_name , func->entryline);
496
497         return 1;
498     }
499
500     return 0;
501 }
502
503 /*-----------------------------------------------------------------*/
504 /* setStepEPBp - sets a given type of bp @ the execution point     */
505 /*-----------------------------------------------------------------*/
506 DEFSETFUNC(setStepEPBp)
507 {
508     exePoint *ep = item;
509     V_ARG(int,bptype);
510     V_ARG(char *,mname);
511    
512     setBreakPoint (ep->addr, CODE, bptype, 
513                    stepBpCB, mname, ep->line);
514     return 1;
515 }
516
517 /*-----------------------------------------------------------------*/
518 /* setNextEPBp - sets a given type of bp @ the execution point     */
519 /*-----------------------------------------------------------------*/
520 DEFSETFUNC(setNextEPBp)
521 {
522     exePoint *ep = item;
523     V_ARG(int,bptype);
524     V_ARG(char *,mname);
525    
526     setBreakPoint (ep->addr, CODE, bptype, 
527                    nextBpCB, mname, ep->line);
528     return 1;
529 }
530
531 /*-----------------------------------------------------------------*/
532 /* lineAtAddr - for execution points returns the one with addr     */
533 /*-----------------------------------------------------------------*/
534 DEFSETFUNC(lineAtAddr)
535 {
536     exePoint *ep = item;
537     V_ARG(unsigned int,addr);
538     V_ARG(int *,line);
539     V_ARG(int *,block);
540     V_ARG(int *,level);
541
542     /* address must be an exact match */
543     if (ep->addr == addr) {
544         *line = ep->line;
545         if (block)
546             *block = ep->block ;
547         if (level)
548             *level = ep->level ;
549         return 1;
550     }
551
552     return 0;
553     
554 }
555
556 /*-----------------------------------------------------------------*/
557 /* discoverContext - find out the current context of the bp        */
558 /*-----------------------------------------------------------------*/
559 context *discoverContext (unsigned addr)
560 {
561     function *func = NULL;
562     int line = 0;
563
564     /* find the function we are in */
565     if (!applyToSet(functions,funcInAddr,addr,&func)) {
566       fprintf(stderr, "Error?:discoverContext: cannot apply to set!\n");
567         return NULL;
568     }
569
570     currCtxt->func = func;
571     currCtxt->addr = func->laddr = addr;
572     currCtxt->modName = func->modName;
573     
574     /* find the c line number */
575     if(applyToSet(func->cfpoints,lineAtAddr,addr,
576                   &line,&currCtxt->block,&currCtxt->level)) 
577         currCtxt->cline = func->lline = line;
578     else
579         currCtxt->cline = func->exitline;
580     
581     /* find the asm line number */
582     line = 0;
583     if (applyToSet(func->afpoints,lineAtAddr,addr,
584                    &line,NULL,NULL))
585         currCtxt->asmline = line;       
586     else
587         currCtxt->asmline = -1;
588         
589     return currCtxt ;
590 }
591
592
593 /*-----------------------------------------------------------------*/
594 /* simGo - send 'go' cmd to simulator and wait till a break occurs */
595 /*-----------------------------------------------------------------*/
596 void simGo (unsigned int gaddr)
597 {   
598     unsigned int addr ;
599     context *ctxt;
600     int rv;
601     static int initial_break_flag = 0;
602
603  top:    
604     addr = simGoTillBp (gaddr);
605
606     /* got the pc for the break point now first
607        discover the program context i.e. module, function 
608        linenumber of the source etc, etc etc */
609     ctxt = discoverContext (addr);
610     
611     /* dispatch all the break point call back functions */
612     rv = dispatchCB (addr,ctxt);    
613
614  ret:    
615
616     /* the dispatch call back function will return
617        non-zero if an user break point has been hit
618        if not then we continue with the execution 
619        of the program */
620     if (!rv) {
621       if (!initial_break_flag) {
622         initial_break_flag = 1;  // kludge to stop only at first run
623         fprintf(stdout, "Stopping at entry.  You can now list and set breakpoints\n");
624       }
625       else {
626         gaddr = -1;
627         goto top ;
628       }
629
630 // notes: kpb
631 // I took this out, after running "run" it would just keep re-running
632 // even after a lot of break points hit.  For some reason above code
633 // not triggering(dispatchCB).  This seems to be by design, startup adds
634 // a bunch of breakpoints-but they are not USER breakpoints.  Perhaps the
635 // debugger changed with its implementation of "go"("run").  It seems we
636 // need to add a "next" or "step" followed by a "run"...
637 // I added a "step" in simi.c when we want a resume function, this seems
638 // to work.
639
640 // still there is question of how do we stop it initially, since
641 // it must be started before it can get a context.  If so, we would
642 // want it to just run up to an initial entry point you'd think...
643 // I don't see why we can't set breakpoints before an initial run,
644 // this does not seem right to me.
645
646 // line #'s are a bit off too.
647
648 #if 0
649         gaddr = -1;
650         goto top ;
651 #endif
652     }
653     
654 }
655
656 /*-----------------------------------------------------------------*/
657 /* cmdSetUserBp - set break point at the user specified location   */
658 /*-----------------------------------------------------------------*/
659 int cmdSetUserBp (char *s, context *cctxt)
660 {
661     char *bp ;
662     function *func = NULL;
663         
664     /* user break point location specification can be of the following
665        forms
666        a) <nothing>        - break point at current location
667        b) lineno           - number of the current module
668        c) filename:lineno  - line number of the given file
669        e) filename:function- function X in file Y (useful for static functions)
670        f) function         - function entry point
671     */
672
673     if (!cctxt) {
674         fprintf(stdout,"No symbol table is loaded.  Use the \"file\" command.\n");
675         return 0;
676     }
677     /* white space skip */
678     while (*s && isspace(*s)) s++;
679     
680     /* null terminate it after stripping trailing blanks*/
681     bp = s + strlen(s);
682     while (bp != s && isspace(*bp)) bp--;
683     *bp = '\0';
684
685     /* case a) nothing */
686     /* if nothing given then current location : we know
687        the current execution location from the currentContext */
688     if (! *s ) {
689
690         /* if current context is known */
691         if (cctxt->func) {
692             if (srcMode == SRC_CMODE)
693                 /* set the break point */
694                 setBreakPoint ( cctxt->addr , CODE , USER , userBpCB ,
695                                 cctxt->func->mod->c_name, cctxt->cline);
696             else
697                 setBreakPoint ( cctxt->addr , CODE , USER , userBpCB ,
698                                 cctxt->func->mod->asm_name, cctxt->asmline);
699                 
700         }
701         else
702             fprintf(stderr,"No default breakpoint address now.\n");
703                         
704         goto ret ;
705     }
706
707     /* case b) lineno */
708     /* check if line number */
709     if (isdigit(*s)) {
710         /* get the lineno */
711         int line = atoi(s);
712
713         /* if current context not present then we must get the module
714            which has main & set the break point @ line number provided
715            of that module : if current context known then set the bp 
716            at the line number given for the current module 
717         */
718         if (cctxt->func) {
719             if (!cctxt->func->mod) {
720                 if (!applyToSet(functions,funcWithName,"main"))
721                     fprintf(stderr,"Function \"main\" not defined.\n");
722                 else 
723                     setBPatModLine(func->mod,line);
724             } else 
725                 setBPatModLine(cctxt->func->mod,line);
726         } else {
727                 if (list_mod) {
728                         setBPatModLine(list_mod,line);
729                 } else {
730                   fprintf(stdout,"Sdcdb fails to have module symbol context at %d\n", __LINE__);
731                 }
732         }
733         
734         goto ret;
735     }
736
737     if ((bp = strchr(s,':'))) {
738         
739         module *mod = NULL;
740         *bp = '\0';
741         
742         if (srcMode == SRC_CMODE) {
743             if (!applyToSet(modules,moduleWithCName,s,&mod)) {
744                 fprintf (stderr,"No source file named %s.\n",s);
745                 goto ret;
746             }
747         } else {
748             if (!applyToSet(modules,moduleWithAsmName,s,&mod)) {
749                 fprintf (stderr,"No source file named %s.\n",s);
750                 goto ret;
751             }
752         }
753                 
754         /* case c) filename:lineno */
755         if (isdigit(*(bp +1))) {                    
756          
757             setBPatModLine (mod,atoi(bp+1));        
758             goto ret;
759             
760         }
761         /* case d) filename:function */
762         if (!applyToSet(functions,funcWithNameModule,bp+1,s,&func)) 
763             fprintf(stderr,"Function \"%s\" not defined.\n",bp+1); 
764         else        
765             setBPatModLine (mod,
766                             (srcMode == SRC_CMODE ? 
767                              func->entryline :
768                              func->aentryline));
769         
770         goto ret;
771     }
772             
773     /* case e) function */
774     if (!applyToSet(functions,funcWithName,s,&func))
775         fprintf(stderr,"Function \"%s\" not defined.\n",s); 
776     else
777         setBPatModLine(func->mod,
778                        (srcMode == SRC_CMODE ?
779                         func->entryline :
780                         func->aentryline));
781
782  ret:    
783     return 0;
784 }
785
786 /*-----------------------------------------------------------------*/
787 /* cmdListAsm - list assembler source code                         */
788 /*-----------------------------------------------------------------*/
789 int cmdListAsm (char *s, context *cctxt)
790 {
791     fprintf(stderr,"'listasm' command not yet implemented\n");
792     return 0;
793 }
794
795 /*-----------------------------------------------------------------*/
796 /* cmdSetOption - set debugger options                             */
797 /*-----------------------------------------------------------------*/
798 int cmdSetOption (char *s, context *cctxt)
799 {
800     while (*s && isspace(*s)) s++;
801     if (strncmp(s,"srcmode",7) == 0 ) {
802         if (srcMode == SRC_CMODE)
803             srcMode = SRC_AMODE;
804         else
805             srcMode = SRC_CMODE;
806         fprintf(stderr,"source mode set to '%s'\n", 
807                 (srcMode == SRC_CMODE ? "C" : "asm"));
808         return 0;
809     }
810     
811     fprintf(stderr,"'set %s' command not yet implemented\n",s);
812     return 0;
813 }
814
815 /*-----------------------------------------------------------------*/
816 /* cmdContinue - continue till next break point                    */
817 /*-----------------------------------------------------------------*/
818 int cmdContinue (char *s, context *cctxt)
819 {
820     if (!cctxt || !cctxt->func) {
821         fprintf(stdout,"The program is not being run.\n");
822         return 0;
823     }
824
825     fprintf(stdout,"Continuing.\n");
826     simGo(-1);
827     return 0;
828 }
829
830 /*-----------------------------------------------------------------*/
831 /* cmdDelUserBp - delete user break point                          */
832 /*-----------------------------------------------------------------*/
833 int cmdDelUserBp (char *s, context *cctxt)
834 {
835     int bpnum ;
836     while (isspace(*s)) s++;
837     
838     if (!*s ) {
839         if (userBpPresent) {
840             char buffer[10];
841             fprintf (stdout,"Delete all breakpoints? (y or n) ");
842             fflush(stdout);
843             fgets(buffer,sizeof(buffer),stdin);
844             if (toupper(buffer[0]) == 'Y')
845                 deleteUSERbp(-1);          
846         }
847         return 0;
848     }
849     
850     /* determine the break point number */
851     if (sscanf(s,"%d",&bpnum) == 1)
852         deleteUSERbp(bpnum);
853
854     return 0;
855 }
856
857 /*-----------------------------------------------------------------*/
858 /* cmdStep - single step thru C source file                        */
859 /*-----------------------------------------------------------------*/
860 int cmdStep (char *s, context *cctxt)
861 {
862     function *func = NULL;
863
864     if (!cctxt || !cctxt->func || !cctxt->func->mod) 
865         fprintf(stdout,"The program is not being run.\n");
866     else {
867         /* if we are @ the end of a function then set
868            break points at execution points of the
869            function in the call stack... */
870         if (cctxt->addr == cctxt->func->sym->eaddr) {
871             if ((func = STACK_PEEK(callStack))) {
872                 if (srcMode == SRC_CMODE)
873                     applyToSet (func->cfpoints,setStepEPBp,STEP,
874                                 func->mod->c_name);     
875                 else
876                     applyToSet (func->afpoints,setStepEPBp,STEP,
877                                 func->mod->asm_name);
878             }
879         } else {
880             /* set breakpoints at all function entry points
881                and all exepoints of this functions & for
882                all functions one up in the call stack */
883             
884             /* all function entry points */
885             applyToSet(functions,setStepBp); 
886             
887             if (srcMode == SRC_CMODE) {
888                 /* for all execution points in this function */
889                 applyToSet(cctxt->func->cfpoints,setStepEPBp,STEP,
890                            cctxt->func->mod->c_name);
891                 
892                 /* set a break point @ the current function's
893                    exit */
894                 setBreakPoint (cctxt->func->sym->eaddr, CODE, STEP , 
895                                stepBpCB, cctxt->func->mod->c_name, 
896                                cctxt->func->exitline);
897                 
898                 /* now break point @ callers execution points */
899                 if ((func = STACK_PPEEK(callStack))) {
900                     applyToSet (func->cfpoints,setStepEPBp,STEP,
901                                 func->mod->c_name);     
902                     /* set bp @ callers exit point */
903                     setBreakPoint (func->sym->eaddr, CODE, STEP , 
904                                    stepBpCB, func->mod->c_name, 
905                                    func->exitline);
906                 }
907             } else {
908                 /* for all execution points in this function */
909                 applyToSet(cctxt->func->afpoints,setStepEPBp,STEP,
910                            cctxt->func->mod->asm_name);
911                 
912                 /* set a break point @ the current function's
913                    exit */
914                 setBreakPoint (cctxt->func->sym->eaddr, CODE, STEP , 
915                                stepBpCB, cctxt->func->mod->asm_name, 
916                                cctxt->func->aexitline);
917                 
918                 /* now break point @ callers execution points */
919                 if ((func = STACK_PPEEK(callStack))) {
920                     
921                     applyToSet (func->afpoints,setStepEPBp,STEP,
922                                 func->mod->asm_name);   
923                     
924                     /* set bp @ callers exit point */
925                     setBreakPoint (func->sym->eaddr, CODE, STEP , 
926                                    stepBpCB, func->mod->asm_name, 
927                                    func->aexitline);
928                 }
929             }
930         }
931
932         simGo(-1);
933     }
934     return 0;
935 }
936
937 /*-----------------------------------------------------------------*/
938 /* cmdNext - next executable C statement file                      */
939 /*-----------------------------------------------------------------*/
940 int cmdNext (char *s, context *cctxt)
941 {
942     function *func = NULL;
943     /* next is almost the same as step except we don't
944        we don't set break point for all function entry
945        points */
946     if (!cctxt || !cctxt->func || !cctxt->func->mod) 
947         fprintf(stdout,"The program is not being run.\n");
948     else {
949         
950         /* if we are @ the end of a function then set
951            break points at execution points of the
952            function in the call stack... */
953         if (cctxt->addr == cctxt->func->sym->eaddr) {
954             if ((func = STACK_PEEK(callStack))) {
955                 if (srcMode == SRC_CMODE)
956                     applyToSet (func->cfpoints,setStepEPBp,STEP,
957                                 func->mod->c_name);     
958                 else
959                     applyToSet (func->afpoints,setStepEPBp,STEP,
960                                func->mod->asm_name);
961             }
962         } else {
963             if (srcMode == SRC_CMODE) {
964                 /* for all execution points in this function */
965                 applyToSet(cctxt->func->cfpoints,setNextEPBp,NEXT,
966                            cctxt->func->mod->c_name);
967                 /* set a break point @ the current function's
968                    exit */
969                 setBreakPoint (cctxt->func->sym->eaddr, CODE, NEXT , 
970                                nextBpCB, cctxt->func->mod->c_name, 
971                                cctxt->func->exitline);
972                 
973                 /* now break point @ callers execution points */        
974                 if ((func = STACK_PPEEK(callStack))) {
975                     applyToSet (func->cfpoints,setNextEPBp,NEXT ,
976                                 func->mod->c_name);     
977                     /* set bp @ callers exit point */
978                     setBreakPoint (func->sym->eaddr, CODE, NEXT , 
979                                    stepBpCB, func->mod->c_name, 
980                                    func->exitline);
981                 }
982             } else {
983                 /* for all execution points in this function */
984                 applyToSet(cctxt->func->afpoints,setNextEPBp,NEXT,
985                            cctxt->func->mod->asm_name);
986                 /* set a break point @ the current function's
987                    exit */
988                 setBreakPoint (cctxt->func->sym->eaddr, CODE, NEXT , 
989                                nextBpCB, cctxt->func->mod->asm_name, 
990                                cctxt->func->aexitline);
991                 
992                 /* now break point @ callers execution points */        
993                 if ((func = STACK_PPEEK(callStack))) {
994                     applyToSet (func->cfpoints,setNextEPBp,NEXT ,
995                                 func->mod->asm_name);   
996                     /* set bp @ callers exit point */
997                     setBreakPoint (func->sym->eaddr, CODE, NEXT , 
998                                    stepBpCB, func->mod->asm_name, 
999                                    func->aexitline);
1000                 }
1001             }
1002             simGo(-1);  
1003         }
1004     }    
1005     return 0;
1006 }
1007
1008 /*-----------------------------------------------------------------*/
1009 /* cmdRun  - run till next break point                             */
1010 /*-----------------------------------------------------------------*/
1011 int cmdRun (char *s, context *cctxt)
1012 {
1013     char buff[10];
1014     if (!cctxt || !cctxt->func || !cctxt->func->mod) {
1015         fprintf(stdout,"Starting program\n");
1016         simGo(0);
1017     } else {
1018         
1019         fprintf(stdout,
1020                 "The program being debugged has been started already.\n");
1021         fprintf(stdout,"Start it from the beginning? (y or n) ");
1022         fflush(stdout);
1023
1024         fgets(buff,sizeof(buff),stdin);
1025         if (toupper(buff[0]) == 'Y') {
1026             simReset();
1027             simGo(0);
1028         }
1029     }
1030
1031     return 0;
1032 }
1033
1034 /*-----------------------------------------------------------------
1035  cmdListSymbols - list symbols
1036 |-----------------------------------------------------------------*/
1037 int cmdListSymbols (char *s, context *cctxt)
1038 {
1039     int our_verbose = 0;
1040     symbol *sy;
1041     int i;
1042
1043     if (strstr(s, "v1")) {
1044       our_verbose = 1;
1045     } else if (strstr(s, "v2")) {
1046       our_verbose = 2;
1047     }
1048
1049     printf("[symbols]\n");
1050     sy = setFirstItem(symbols);
1051     i = 0;
1052     for (;;) {
1053       if (sy == NULL)
1054         break;
1055       if (our_verbose <= 1)
1056         printf("<%s>", sy->name);
1057
1058       if (our_verbose > 1) {
1059         printf("  %d) name:%s, size:%d, level:%d block:%d\n", i,
1060           sy->name, sy->size, sy->level, sy->block);
1061         printf("    isonstack:%d, isfunc:%d, offset:%d addr:%d\n",
1062           sy->isonstack, sy->isfunc, sy->offset, sy->addr);
1063         printf("    eaddr:%d, addr_type:%c, type:%x etype:%x\n",
1064           sy->eaddr, sy->addr_type, sy->type, sy->etype);
1065         printf("    scopetype:%c, sname:%s, rname:%s addrspace:%c\n",
1066           sy->scopetype, sy->sname, sy->rname, sy->addrspace);
1067         printf("    next:%x\n", sy->next);
1068       }
1069       ++i;
1070       sy = setNextItem(symbols);
1071     }
1072     printf("   %d symbols\n", i);
1073     return 0;
1074 }
1075
1076 /*-----------------------------------------------------------------
1077  cmdListFunctions - list functions.
1078 |-----------------------------------------------------------------*/
1079 int cmdListFunctions (char *s, context *cctxt)
1080 {
1081     function *f;
1082     int i;
1083     int our_verbose = 0;
1084
1085     if (strstr(s, "v1")) {
1086       our_verbose = 1;
1087     } else if (strstr(s, "v2")) {
1088       our_verbose = 2;
1089     }
1090
1091     printf("[functions]\n");
1092     f = setFirstItem(functions);
1093     i = 0;
1094     for (;;) {
1095       if (f == NULL)
1096         break;
1097       if (our_verbose) {
1098         printf("  %d) sym:%x, modName:%s, mod:%x\n", i,
1099           f->sym, f->modName, f->mod);
1100         printf("    entryline:%d, aentryline:%d, exitline:%d, aexitline:%d\n",
1101                 f->entryline, f->aentryline, f->exitline, f->aexitline);
1102         printf("    cfpoints:%x, afpoints:%x, laddr:%x, lline:%d\n",
1103                 f->cfpoints, f->afpoints, f->laddr, f->lline);
1104       }
1105       else {
1106         printf("<%s>", f->modName);
1107       }
1108       ++i;
1109       f = setNextItem(functions);
1110     }
1111     printf("   %d functions\n", i);
1112     return 0;
1113 }
1114
1115 /*-----------------------------------------------------------------
1116  cmdListModules - list functions.
1117 |-----------------------------------------------------------------*/
1118 int cmdListModules (char *s, context *cctxt)
1119 {
1120     module *m;
1121     srcLine *cs, *as;
1122     int i, mi;
1123     int our_verbose = 0;
1124
1125     if (strstr(s, "v1")) {
1126       our_verbose = 1;
1127     } else if (strstr(s, "v2")) {
1128       our_verbose = 2;
1129     }
1130
1131     printf("[modules]\n");
1132     m = setFirstItem(modules);
1133     mi = 0;
1134     for (;;) {
1135       if (m == NULL)
1136         break;
1137
1138       if (our_verbose >= 0) {
1139       printf("  %d) cfullname:%s, afullname:%s, name:%s\n", ++mi,
1140         m->cfullname, m->afullname, m->name);
1141       printf("    c_name:%s, asm_name:%s, ncLines:%d, nasmLines:%d\n",
1142               m->c_name, m->asm_name, m->ncLines, m->nasmLines);
1143       printf("    cLines:%x, asmLines:%x\n",
1144               m->cLines, m->asmLines);
1145       }
1146       if (our_verbose > 2) {
1147         i = 0;
1148         if (m->cLines) {
1149           cs = m->cLines[i++];
1150           printf("    [cLines] ");
1151           while (cs) {
1152             if (our_verbose)
1153               printf("   (%d) addr:%x, block:%d, level:%d, src:%s\n",
1154                  i, cs->addr, cs->block, cs->level, cs->src);
1155             cs = m->cLines[i++];
1156           }
1157           if (!our_verbose)
1158               printf("%d records", i);
1159         }
1160         i = 0;
1161         if (m->asmLines) {
1162           as = m->asmLines[i++];
1163           printf("    [asmLines] ");
1164           while (as) {
1165             if (our_verbose)
1166               printf("   (%d) addr:%x, block:%d, level:%d, src:%s\n",
1167                  i, as->addr, as->block, as->level, as->src);
1168             as = m->asmLines[i++];
1169           }
1170           if (!our_verbose)
1171               printf("%d records", i);
1172         }
1173         printf("\n");
1174       }
1175
1176       m = setNextItem(modules);
1177     }
1178     return 0;
1179 }
1180
1181 /*-----------------------------------------------------------------
1182  infoSymbols - This is really just a tool to dump all these
1183    huge program structures out into human readable form.
1184 |-----------------------------------------------------------------*/
1185 static void infoSymbols(context *ctxt)
1186 {
1187   int our_verbose = 0;
1188
1189   printf("[context:%x] func:%x modName:%s addr:%x\n",
1190     ctxt, ctxt->func, ctxt->modName, ctxt->addr);
1191
1192   printf("  cline:%d asmline:%d block:%d level:%d\n",
1193     ctxt->cline, ctxt->asmline, ctxt->level);
1194
1195   printf("[globals] currCtxt:%x, modules:%x, functions:%x symbols:%x\n",
1196     currCtxt, modules, functions, symbols);
1197   printf("  nStructs:%d, structs:%x, ssdirl:%s\n",
1198     nStructs, structs, ssdirl);
1199
1200   /**************** modules *******************/
1201   {
1202     module *m;
1203     srcLine *cs, *as;
1204     int i, mi;
1205     printf("[modules]\n");
1206     m = setFirstItem(modules);
1207     mi = 0;
1208     for (;;) {
1209       if (m == NULL)
1210         break;
1211       printf("  %d) cfullname:%s, afullname:%s, name:%s\n", ++mi,
1212         m->cfullname, m->afullname, m->name);
1213       printf("    c_name:%s, asm_name:%s, ncLines:%d, nasmLines:%d\n",
1214               m->c_name, m->asm_name, m->ncLines, m->nasmLines);
1215       printf("    cLines:%x, asmLines:%x\n",
1216               m->cLines, m->asmLines);
1217       i = 0;
1218       if (m->cLines) {
1219         cs = m->cLines[i++];
1220         printf("    [cLines] ");
1221         while (cs) {
1222           if (our_verbose)
1223             printf("   (%d) addr:%x, block:%d, level:%d, src:%s\n",
1224                i, cs->addr, cs->block, cs->level, cs->src);
1225           cs = m->cLines[i++];
1226         }
1227         if (!our_verbose)
1228             printf("%d records", i);
1229       }
1230       i = 0;
1231       if (m->asmLines) {
1232         as = m->asmLines[i++];
1233         printf("    [asmLines] ");
1234         while (as) {
1235           if (our_verbose)
1236             printf("   (%d) addr:%x, block:%d, level:%d, src:%s\n",
1237                i, as->addr, as->block, as->level, as->src);
1238           as = m->asmLines[i++];
1239         }
1240         if (!our_verbose)
1241             printf("%d records", i);
1242       }
1243       printf("\n");
1244
1245       m = setNextItem(modules);
1246     }
1247   }
1248
1249   /**************** functions *******************/
1250   {
1251     function *f;
1252     int i;
1253     printf("[functions]\n");
1254     f = setFirstItem(functions);
1255     i = 0;
1256     for (;;) {
1257       if (f == NULL)
1258         break;
1259       if (our_verbose) {
1260         printf("  %d) sym:%x, modName:%s, mod:%x\n", i,
1261           f->sym, f->modName, f->mod);
1262         printf("    entryline:%d, aentryline:%d, exitline:%d, aexitline:%d\n",
1263                 f->entryline, f->aentryline, f->exitline, f->aexitline);
1264         printf("    cfpoints:%x, afpoints:%x, laddr:%x, lline:%d\n",
1265                 f->cfpoints, f->afpoints, f->laddr, f->lline);
1266       }
1267       ++i;
1268       f = setNextItem(functions);
1269     }
1270     if (!our_verbose)
1271       printf("   %d functions\n", i);
1272   }
1273
1274   /**************** symbols *******************/
1275   {
1276     symbol *s;
1277     int i;
1278     printf("[symbols]\n");
1279     s = setFirstItem(symbols);
1280     i = 0;
1281     for (;;) {
1282       if (s == NULL)
1283         break;
1284       if (our_verbose) {
1285         printf("  %d) name:%s, size:%d, level:%d block:%d\n", i,
1286           s->name, s->size, s->level, s->block);
1287         printf("    isonstack:%d, isfunc:%d, offset:%d addr:%d\n",
1288           s->isonstack, s->isfunc, s->offset, s->addr);
1289         printf("    eaddr:%d, addr_type:%c, type:%x etype:%x\n",
1290           s->eaddr, s->addr_type, s->type, s->etype);
1291         printf("    scopetype:%c, sname:%s, rname:%s addrspace:%c\n",
1292           s->scopetype, s->sname, s->rname, s->addrspace);
1293         printf("    next:%x\n", s->next);
1294       }
1295       ++i;
1296       s = setNextItem(symbols);
1297     }
1298     if (!our_verbose)
1299       printf("   %d symbols\n", i);
1300   }
1301
1302 }
1303
1304 /*-----------------------------------------------------------------*/
1305 /* infoStack - print call stack information                        */
1306 /*-----------------------------------------------------------------*/
1307 static void infoStack(context *ctxt)
1308 {
1309     function *func ;
1310     int i = 0 ;
1311
1312     STACK_STARTWALK(callStack) ;
1313     while ((func = STACK_WALK(callStack))) {
1314
1315         fprintf(stdout,"#%d 0x%04x %s () at %s:%d\n",i++,
1316                 func->laddr,func->sym->name,
1317                 func->mod->c_name,func->lline);
1318     }
1319
1320 }
1321
1322 /*-----------------------------------------------------------------*/
1323 /* cmdInfo - info command                                          */
1324 /*-----------------------------------------------------------------*/
1325 int cmdInfo (char *s, context *cctxt)
1326 {
1327     while (isspace(*s)) s++;
1328
1329     /* list all break points */
1330     if (strcmp(s,"break") == 0) {
1331         listUSERbp();
1332         return 0;
1333     }
1334
1335     /* info frame same as frame */
1336     if (strcmp(s,"frame") == 0) {
1337         cmdFrame (s,cctxt);
1338         return 0;
1339     }
1340
1341     /* info stack display call stack */
1342     if (strcmp(s,"stack") == 0) {
1343         infoStack(cctxt);
1344         return 0;
1345     }
1346
1347     /* info stack display call stack */
1348     if (strcmp(s,"registers") == 0) {
1349         fprintf(stdout,"%s",simRegs());
1350         return 0;
1351     }
1352
1353     /* info stack display call stack */
1354     if (strcmp(s,"symbols") == 0) {
1355       /* dump out symbols we have read in */
1356       fprintf(stdout,"Dumping symbols...\n");
1357       infoSymbols(cctxt);
1358       return 0;
1359     }
1360
1361     fprintf(stdout,"Undefined info command: \"%s\".  Try \"help\n",s);
1362     return 0;
1363
1364 }
1365
1366 /*-----------------------------------------------------------------*/
1367 /* cmdQuit  - quit debugging                                       */
1368 /*-----------------------------------------------------------------*/
1369 int cmdQuit (char *s, context *cctxt)
1370 {   
1371     if (simactive)
1372         closeSimulator();
1373     return 1;
1374 }
1375
1376 /*-----------------------------------------------------------------*/
1377 /* cmdListSrc  - list src                                          */
1378 /*-----------------------------------------------------------------*/
1379 int cmdListSrc (char *s, context *cctxt)
1380 {   
1381     static int currline = 0;
1382     int i =0 ;
1383     int pline = 0;
1384     int llines = listLines;
1385
1386     while (*s && isspace(*s)) s++;
1387     
1388     /* if the user has spcified line numer then the line number
1389        can be of the following formats
1390        LINE          - just line number
1391        FILE:LINE     - filename line number
1392        FUNCTION      - list a function
1393        FILE:FUNCTION - function in file */
1394
1395     if (*s) {
1396         /* case a) LINE */
1397         if (isdigit(*s)) {
1398             sscanf(s,"%d",&pline);
1399             if (!cctxt || !cctxt->func || !cctxt->func->mod) {
1400               if (!list_mod) {
1401                 fprintf(stdout,"Sdcdb fails to have a proper context at %d.\n", __LINE__);
1402                 return 0;
1403               }
1404             }
1405             else
1406               list_mod = cctxt->func->mod;
1407         }
1408         else {
1409             char *bp;
1410             function *func = NULL;
1411             
1412             /* if ':' present then FILE:LINE || FILE:FUNCTION */
1413             if ((bp = strchr(s,':'))) {
1414                 *bp = '\0';
1415                 bp ++;
1416                 if (isdigit(*bp)) {
1417                     /* FILE:LINE */
1418                     list_mod=NULL;  /* bug fix 2-09-02, moduleWithCName expects mod to be null */
1419                     if (srcMode == SRC_CMODE) {
1420                         if (!applyToSet(modules,moduleWithCName,s,&list_mod)) {
1421                             fprintf (stderr,"No c source file named %s.\n",s);
1422                             return 0;
1423                         }
1424                     } else {
1425                         if (!applyToSet(modules,moduleWithAsmName,s,&list_mod)) {
1426                             fprintf (stderr,"No source file named %s.\n",s);
1427                             return 0;
1428                         }
1429                     }
1430                     sscanf(bp,"%d",&pline);
1431                 } else {
1432                     /* FILE:FUCTION */
1433                     if (!applyToSet(functions,funcWithNameModule,bp,s,&func)) {
1434                         fprintf(stdout,"Function \"%s\" not defined.\n",bp);
1435                         return 0;
1436                     }
1437                     list_mod = func->mod;
1438                     if (srcMode == SRC_CMODE) {
1439                         pline = func->entryline;
1440                         llines = func->exitline - func->entryline + 1;
1441                     } else {
1442                         pline = func->aentryline;
1443                         llines = func->aexitline - func->aentryline + 1;
1444                     }
1445                 }
1446             }
1447             else {
1448                 /* FUNCTION */
1449                 if (!applyToSet(functions,funcWithName,s,&func)) {
1450                     fprintf(stderr,"Function \"%s\" not defined.\n",s); 
1451                     return 0;
1452                 }
1453                 else {
1454                     list_mod = func->mod;
1455                     if (srcMode == SRC_CMODE) {
1456                         pline = func->entryline;
1457                         llines = func->exitline - func->entryline + 1; 
1458                     } else {
1459                         pline = func->aentryline;
1460                         llines = func->aexitline - func->aentryline + 1; 
1461                     }
1462                 }
1463             }
1464         }
1465     } else {
1466         /* if no line specified & we had listed
1467            before then continue from that listing */
1468         if (currline)
1469             pline = currline ;
1470         else {
1471             if (!cctxt || !cctxt->func || !cctxt->func->mod) {
1472               fprintf(stdout,"Missing context at %d. Try list filename:lineno\n", __LINE__);
1473               return 0;
1474             }
1475             list_mod = cctxt->func->mod;
1476             if (srcMode == SRC_CMODE)
1477                 pline = cctxt->cline;
1478             else
1479                 pline = cctxt->asmline;
1480         }
1481     }
1482
1483     if (!list_mod) {
1484       fprintf(stdout,"Sdcdb fails to have a valid module context at %d.\n", __LINE__);
1485       return 0;
1486     }
1487     
1488     if (llines > listLines) llines = listLines;
1489
1490     for ( i = 0 ; i < llines ; i++ ) {
1491         if (srcMode == SRC_CMODE) {
1492             if ( (pline + i) >= list_mod->ncLines )
1493                 break;
1494             fprintf(stdout,"%d\t%s",pline + i,
1495                     list_mod->cLines[pline +i]->src);
1496         } else {
1497             if ( (pline + i) >= list_mod->nasmLines )
1498                 break;
1499             fprintf(stdout,"%d\t%s",pline + i,
1500                     list_mod->asmLines[pline +i]->src);
1501         }
1502     }
1503     currline = pline + i ;
1504     return 0;
1505 }
1506
1507 /*-----------------------------------------------------------------*/
1508 /* printValBasic - print value of basic types                      */
1509 /*-----------------------------------------------------------------*/
1510 static void printValBasic(symbol *sym,unsigned addr,char mem, int size)
1511 {
1512     union {     
1513         float f;     
1514         unsigned long val;
1515         long         sval;
1516         struct {
1517             short    lo;
1518             short    hi;
1519         } i;
1520         unsigned char b[4];
1521     }v;
1522     union {
1523         unsigned char b[4];
1524     }v1;
1525     
1526     v.val = simGetValue(addr,mem,size);
1527     /* if this a floating point number then */
1528     if (IS_FLOAT(sym->type))    
1529         fprintf(stdout,"%f",v.f);    
1530     else
1531         if (IS_PTR(sym->type))
1532             fprintf(stdout,"0x%x",v.val);
1533         else
1534             if (IS_SPEC(sym->type) && IS_INTEGRAL(sym->type)) {
1535                 if (IS_CHAR(sym->etype)) 
1536                     fprintf(stdout,"'%c' %d 0x%x",v.val,v.val,v.val);
1537                 else
1538                     if (IS_INT(sym->etype)) 
1539                         if (IS_LONG(sym->etype))
1540                             if (SPEC_USIGN(sym->etype))
1541                                 fprintf(stdout,"%d 0x%x",v.val,v.val);
1542                             else
1543                                 fprintf(stdout,"%d 0x%x",v.sval,v.sval);
1544                         else
1545                             fprintf(stdout,"%d 0x%x",v.i.lo,v.i.lo);
1546                     else
1547                         fprintf(stdout,"0x%x",v.val);
1548             } else
1549                 fprintf(stdout,"0x%x",v.val);
1550                 
1551     
1552 }
1553
1554 /*-----------------------------------------------------------------*/
1555 /* printValFunc  - prints function values                          */
1556 /*-----------------------------------------------------------------*/
1557 static void printValFunc (symbol *sym)
1558 {
1559     fprintf(stdout,"print function not yet implemented\n");
1560 }
1561
1562 /*-----------------------------------------------------------------*/
1563 /* printArrayValue - will print the values of array elements       */
1564 /*-----------------------------------------------------------------*/
1565 static void printArrayValue (symbol *sym, char space, unsigned int addr)
1566 {
1567         link *elem_type = sym->type->next;
1568         int i;
1569         
1570         fprintf(stdout," { ");
1571         for (i = 0 ; i < DCL_ELEM(sym->type) ; i++) {           
1572                 if (IS_AGGREGATE(elem_type)) {
1573                         printValAggregates(sym,elem_type,space,addr);                  
1574                 } else {
1575                         printValBasic(sym,addr,space,getSize(elem_type));
1576                 }
1577                 addr += getSize(elem_type);
1578                 if (i != DCL_ELEM(sym->type) -1)
1579                         fprintf(stdout,",");
1580         }
1581
1582         fprintf(stdout,"}\n");          
1583 }
1584
1585 /*-----------------------------------------------------------------*/
1586 /* printStructValue - prints structures elements                   */
1587 /*-----------------------------------------------------------------*/
1588 static void printStructValue (symbol *sym,link *type, char space, unsigned int addr) 
1589 {
1590         symbol *fields = SPEC_STRUCT(type)->fields;
1591
1592         fprintf(stdout," { ");
1593         while (fields) {
1594                 fprintf(stdout,"%s = ",fields->name);
1595                 if (IS_AGGREGATE(fields->type)) {
1596                         printValAggregates(fields,fields->type,space, addr);
1597                 } else {
1598                         printValBasic(fields,addr,space,getSize(fields->type));
1599                 }
1600                 addr += getSize(fields->type);
1601                 fields = fields->next;
1602         }
1603         fprintf(stdout,"}\n");
1604 }
1605
1606 /*-----------------------------------------------------------------*/
1607 /* printValAggregates - print value of aggregates                  */
1608 /*-----------------------------------------------------------------*/
1609 static void printValAggregates (symbol *sym, link *type,char space,unsigned int addr)
1610 {
1611
1612         if (IS_ARRAY(type)) {
1613                 printArrayValue(sym, space, addr);
1614                 return ;
1615         }
1616
1617         if (IS_STRUCT(type)) { 
1618                 printStructValue(sym,sym->type,space, addr); 
1619                 return; 
1620         } 
1621 }
1622
1623 /*-----------------------------------------------------------------*/
1624 /* printSymValue - print value of a symbol                         */
1625 /*-----------------------------------------------------------------*/
1626 static void printSymValue (symbol *sym, context *cctxt)
1627 {
1628     static int stack = 1;
1629     unsigned long val;
1630     /* if it is on stack then compute address & fall thru */
1631     if (sym->isonstack) {
1632         symbol *bp = symLookup("bp",cctxt);
1633         if (!bp) {
1634             fprintf(stdout,"cannot determine stack frame\n");
1635             return ;
1636         }
1637
1638         sym->addr = simGetValue(bp->addr,bp->addrspace,bp->size)
1639           + sym->offset ;      
1640     }
1641     
1642     /* get the value from the simulator and
1643        print it */
1644     fprintf(stdout,"$%d = ",stack++);
1645     /* arrays & structures first */
1646     if (IS_AGGREGATE(sym->type))
1647         printValAggregates(sym,sym->type,sym->addrspace,sym->addr);
1648     else
1649         /* functions */
1650         if (IS_FUNC(sym->type))
1651             printValFunc(sym);
1652         else {
1653             printValBasic(sym,sym->addr,sym->addrspace,sym->size);
1654             fprintf(stdout,"\n");
1655             //fprintf(stdout,"(BASIC %x,%c,%d)\n",sym->addr,sym->addrspace,sym->size);
1656         }
1657 }
1658
1659 /*-----------------------------------------------------------------*/
1660 /* printStructInfo - print out structure information               */
1661 /*-----------------------------------------------------------------*/
1662 static void printStructInfo (structdef *sdef)
1663 {
1664     symbol *field = sdef->fields ;
1665     int i = 0 ;
1666     
1667     while (field) {
1668         i += field->offset;
1669         field = field->next;
1670     }
1671
1672     fprintf(stdout,"%s %s {\n",(i ? "struct" : "union" ), sdef->tag);
1673     field = sdef->fields;
1674     while (field) {
1675         printTypeInfo (field->type);
1676         fprintf(stdout," %s ;\n",field->name);
1677         field = field->next ;
1678     }
1679
1680     fprintf(stdout,"}\n");
1681
1682 }
1683
1684 /*-----------------------------------------------------------------*/
1685 /* printTypeInfo - print out the type information                  */
1686 /*-----------------------------------------------------------------*/
1687 static void printTypeInfo(link *p)
1688 {
1689     if (!p)
1690         return ;
1691
1692     if (IS_DECL(p)) {
1693         switch (DCL_TYPE(p))  {
1694         case FUNCTION:
1695             printTypeInfo (p->next);
1696             fprintf(stdout,"()");
1697             break;
1698         case ARRAY:
1699             printTypeInfo (p->next);
1700             fprintf(stdout,"[%d]",DCL_ELEM(p));
1701             break;
1702         
1703         case IPOINTER:
1704         case PPOINTER:
1705         case POINTER:
1706             printTypeInfo (p->next);
1707             fprintf(stdout,"(_near *)");
1708             break;
1709
1710         case FPOINTER:
1711             printTypeInfo (p->next);
1712             fprintf(stdout,"(_xdata *)");
1713             break;
1714
1715         case CPOINTER:
1716             printTypeInfo( p->next);
1717             fprintf(stdout,"(_code *)");
1718             break;
1719             
1720         case GPOINTER:
1721             printTypeInfo( p->next);
1722             fprintf(stdout,"(_generic *)");
1723             break;                   
1724         }
1725     } else {
1726         switch (SPEC_NOUN(p)) { /* depending on the specifier type */
1727         case V_INT:
1728             (IS_LONG(p) ? fputs("long ",stdout) : 
1729              ( IS_SHORT(p) ? fputs("short ",stdout) : 
1730                fputs("int ",stdout))) ;
1731             break;
1732         case V_FLOAT:
1733              fputs("float ",stdout);
1734              break;
1735
1736         case V_CHAR:
1737             fputs ("char ",stdout);
1738             break;
1739
1740         case V_VOID:
1741             fputs("void ",stdout);
1742             break;
1743
1744         case V_STRUCT:
1745             printStructInfo (SPEC_STRUCT(p));
1746             break;
1747
1748         case V_SBIT:
1749             fputs("sbit ",stdout);
1750             break;
1751
1752         case V_BIT:
1753             fprintf(stdout,": %d" ,SPEC_BLEN(p));       
1754             break;
1755         }
1756     }
1757 }
1758
1759 /*-----------------------------------------------------------------*/
1760 /* cmdPrint - print value of variable                              */
1761 /*-----------------------------------------------------------------*/
1762 int cmdPrint (char *s, context *cctxt)
1763 {   
1764     symbol *sym ;
1765     char *bp = s+strlen(s) -1;
1766
1767     while (isspace(*s)) s++;
1768     if (!*s) return 0;
1769     while (isspace(*bp)) bp--;
1770     bp++ ;
1771     *bp = '\0';
1772
1773     if ((sym = symLookup(s,cctxt))) {
1774         printSymValue(sym,cctxt);
1775     } else {
1776         fprintf(stdout,
1777                 "No symbol \"%s\" in current context.\n",              
1778                 s);
1779     }
1780     return 0;
1781 }
1782
1783 /*-----------------------------------------------------------------*/
1784 /* cmdPrintType - print type of a variable                         */
1785 /*-----------------------------------------------------------------*/
1786 int cmdPrintType (char *s, context *cctxt)
1787 {   
1788         symbol *sym ;
1789     char *bp = s+strlen(s) -1;
1790
1791     while (isspace(*s)) s++;
1792     if (!*s) return 0;
1793     while (isspace(*bp)) bp--;
1794     bp++ ;
1795     *bp = '\0';
1796
1797     if ((sym = symLookup(s,cctxt))) {
1798         printTypeInfo(sym->type);
1799         fprintf(stdout,"\n");
1800     } else {
1801         fprintf(stdout,
1802                 "No symbol \"%s\" in current context.\n",
1803                 s);
1804     }
1805     return 0;   
1806 }
1807
1808 /*-----------------------------------------------------------------*/
1809 /* cmdClrUserBp - clear user break point                           */
1810 /*-----------------------------------------------------------------*/
1811 int cmdClrUserBp (char *s, context *cctxt)
1812 {   
1813     char *bp ;    
1814     function *func = NULL;
1815         
1816     /* clear break point location specification can be of the following
1817        forms
1818        a) <nothing>        - break point at current location
1819        b) lineno           - number of the current module
1820        c) filename:lineno  - line number of the given file
1821        e) filename:function- function X in file Y (useful for static functions)
1822        f) function         - function entry point
1823     */
1824
1825     if (!cctxt) {
1826         fprintf(stdout,"No symbol table is loaded.  Use the \"file\" command.\n");
1827         return 0;
1828     }
1829
1830     /* white space skip */
1831     while (*s && isspace(*s)) s++;
1832     
1833     /* null terminate it after stripping trailing blanks*/
1834     bp = s + strlen(s);
1835     while (bp != s && isspace(*bp)) bp--;
1836     *bp = '\0';
1837
1838     /* case a) nothing */
1839     /* if nothing given then current location : we know
1840        the current execution location from the currentContext */
1841     if (! *s ) {
1842
1843         /* if current context is known */
1844         if (cctxt->func) 
1845             /* clear the break point @ current location */
1846             clearUSERbp (cctxt->addr);
1847         else
1848             fprintf(stderr,"No default breakpoint address now.\n");
1849                         
1850         goto ret ;
1851     }
1852
1853     /* case b) lineno */
1854     /* check if line number */
1855     if (isdigit(*s)) {
1856         /* get the lineno */
1857         int line = atoi(s);
1858
1859         /* if current context not present then we must get the module
1860            which has main & set the break point @ line number provided
1861            of that module : if current context known then set the bp 
1862            at the line number given for the current module 
1863         */
1864         if (cctxt->func) {
1865             if (!cctxt->func->mod) {
1866                 if (!applyToSet(functions,funcWithName,"main"))
1867                     fprintf(stderr,"Function \"main\" not defined.\n");
1868                 else 
1869                     clearBPatModLine(func->mod,line);
1870             } else 
1871                 clearBPatModLine(cctxt->func->mod,line);                        
1872         }
1873         
1874         goto ret;
1875     }
1876
1877     if ((bp = strchr(s,':'))) {
1878         
1879         module *mod = NULL;
1880         *bp = '\0';
1881         
1882         if (!applyToSet(modules,moduleWithCName,s,&mod)) {
1883             fprintf (stderr,"No source file named %s.\n",s);
1884             goto ret;
1885         }
1886
1887         /* case c) filename:lineno */
1888         if (isdigit(*(bp +1))) {                    
1889          
1890             clearBPatModLine (mod,atoi(bp+1));      
1891             goto ret;
1892             
1893         }
1894         /* case d) filename:function */
1895         if (!applyToSet(functions,funcWithNameModule,bp+1,s,&func)) 
1896             fprintf(stderr,"Function \"%s\" not defined.\n",bp+1); 
1897         else
1898             clearBPatModLine (mod,func->entryline);
1899         
1900         goto ret;
1901     }
1902             
1903     /* case e) function */
1904     if (!applyToSet(functions,funcWithName,s,&func))
1905         fprintf(stderr,"Function \"%s\" not defined.\n",s); 
1906     else
1907         clearBPatModLine(func->mod,func->entryline);
1908
1909  ret:    
1910     return 0;        
1911 }
1912
1913
1914 /*-----------------------------------------------------------------*/
1915 /* cmdSimulator - send command to simulator                        */
1916 /*-----------------------------------------------------------------*/
1917 int cmdSimulator (char *s, context *cctxt)
1918 {   
1919   char tmpstr[82];
1920
1921     if (strlen(s) > 80) {
1922       printf("error 3A\n");
1923       exit(1);
1924     }
1925     strcpy(tmpstr, s);
1926     strcat(tmpstr, "\n");
1927     sendSim(tmpstr);
1928     waitForSim();
1929     fprintf(stdout,"%s",simResponse());
1930     return 0;
1931 }
1932
1933 /*-----------------------------------------------------------------*/
1934 /* cmdFrame - Frame command                                        */
1935 /*-----------------------------------------------------------------*/
1936 int cmdFrame (char *s, context *cctxt)
1937 {   
1938     function *func ;
1939
1940     if ((func = STACK_PEEK(callStack))) {
1941         fprintf(stdout,"#0  %s () at %s:%d\n",
1942                 func->sym->name,func->mod->c_name,cctxt->cline);
1943
1944         if (cctxt->cline < func->mod->ncLines)      
1945             fprintf(stdout,"%d\t%s",
1946                     cctxt->cline,
1947                     func->mod->cLines[cctxt->cline]->src);
1948     } else
1949         fprintf(stdout,"No stack.\n");
1950     return 0;
1951 }
1952
1953 /*-----------------------------------------------------------------*/
1954 /* cmdFinish - exec till end of current function                   */
1955 /*-----------------------------------------------------------------*/
1956 int cmdFinish (char *s, context *ctxt)
1957 {
1958     if (!ctxt || ! ctxt->func) {
1959         fprintf(stdout,"The program is not running.\n");
1960         return 0;
1961     }
1962
1963     if (srcMode == SRC_CMODE) {
1964         setBreakPoint (ctxt->func->sym->eaddr, CODE, STEP, 
1965                        stepBpCB, ctxt->func->mod->c_name, 
1966                        ctxt->func->exitline);
1967     } else {
1968         setBreakPoint (ctxt->func->sym->eaddr, CODE, STEP, 
1969                        stepBpCB, ctxt->func->mod->asm_name, 
1970                        ctxt->func->aexitline);
1971     }
1972
1973     simGo(-1);
1974     return 0;
1975     
1976 }
1977
1978
1979 /*-----------------------------------------------------------------*/
1980 /* cmdShow - show command                                          */
1981 /*-----------------------------------------------------------------*/
1982 int cmdShow (char *s, context *cctxt)
1983 {
1984     /* skip white space */
1985     while (*s && isspace(*s)) s++ ;
1986
1987     if (strcmp(s,"copying") == 0) {
1988         fputs(copying,stdout);
1989         return 0;
1990     }
1991     
1992     if (strcmp(s,"warranty") == 0) {
1993         fputs(warranty,stdout);
1994         return 0;
1995     }
1996
1997     return 0;
1998 }
1999