1 /*-------------------------------------------------------------------------
2 cmd.c - source file for debugger command execution
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
32 EXTERN_STACK_DCL(callStack,function *,1024);
34 #if defined(__APPLE__) && defined(__MACH__)
36 {" GNU GENERAL PUBLIC LICENSE Version 2"};
39 " GNU GENERAL PUBLIC LICENSE
42 Copyright (C) 1989, 1991 Free Software Foundation, Inc.
43 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
44 Everyone is permitted to copy and distribute verbatim copies
45 of this license document, but changing it is not allowed.
49 The licenses for most software are designed to take away your
50 freedom to share and change it. By contrast, the GNU General Public
51 License is intended to guarantee your freedom to share and change free
52 software--to make sure the software is free for all its users. This
53 General Public License applies to most of the Free Software
54 Foundation's software and to any other program whose authors commit to
55 using it. (Some other Free Software Foundation software is covered by
56 the GNU Library General Public License instead.) You can apply it to
59 When we speak of free software, we are referring to freedom, not
60 price. Our General Public Licenses are designed to make sure that you
61 have the freedom to distribute copies of free software (and charge for
62 this service if you wish), that you receive source code or can get it
63 if you want it, that you can change the software or use pieces of it
64 in new free programs; and that you know you can do these things.
66 To protect your rights, we need to make restrictions that forbid
67 anyone to deny you these rights or to ask you to surrender the rights.
68 These restrictions translate to certain responsibilities for you if you
69 distribute copies of the software, or if you modify it.
71 For example, if you distribute copies of such a program, whether
72 gratis or for a fee, you must give the recipients all the rights that
73 you have. You must make sure that they, too, receive or can get the
74 source code. And you must show them these terms so they know their
77 We protect your rights with two steps: (1) copyright the software, and
78 (2) offer you this license which gives you legal permission to copy,
79 distribute and/or modify the software.
81 Also, for each author's protection and ours, we want to make certain
82 that everyone understands that there is no warranty for this free
83 software. If the software is modified by someone else and passed on, we
84 want its recipients to know that what they have is not the original, so
85 that any problems introduced by others will not reflect on the original
88 Finally, any free program is threatened constantly by software
89 patents. We wish to avoid the danger that redistributors of a free
90 program will individually obtain patent licenses, in effect making the
91 program proprietary. To prevent this, we have made it clear that any
92 patent must be licensed for everyone's free use or not licensed at all.
94 The precise terms and conditions for copying, distribution and
97 GNU GENERAL PUBLIC LICENSE
98 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
100 0. This License applies to any program or other work which contains
101 a notice placed by the copyright holder saying it may be distributed
102 under the terms of this General Public License. The \"Program\", below,
103 refers to any such program or work, and a \"work based on the Program\"
104 means either the Program or any derivative work under copyright law:
105 that is to say, a work containing the Program or a portion of it,
106 either verbatim or with modifications and/or translated into another
107 language. (Hereinafter, translation is included without limitation in
108 the term \"modification\".) Each licensee is addressed as \"you\".
110 Activities other than copying, distribution and modification are not
111 covered by this License; they are outside its scope. The act of
112 running the Program is not restricted, and the output from the Program
113 is covered only if its contents constitute a work based on the
114 Program (independent of having been made by running the Program).
115 Whether that is true depends on what the Program does.
117 1. You may copy and distribute verbatim copies of the Program's
118 source code as you receive it, in any medium, provided that you
119 conspicuously and appropriately publish on each copy an appropriate
120 copyright notice and disclaimer of warranty; keep intact all the
121 notices that refer to this License and to the absence of any warranty;
122 and give any other recipients of the Program a copy of this License
123 along with the Program.
125 You may charge a fee for the physical act of transferring a copy, and
126 you may at your option offer warranty protection in exchange for a fee.
128 2. You may modify your copy or copies of the Program or any portion
129 of it, thus forming a work based on the Program, and copy and
130 distribute such modifications or work under the terms of Section 1
131 above, provided that you also meet all of these conditions:
133 a) You must cause the modified files to carry prominent notices
134 stating that you changed the files and the date of any change.
136 b) You must cause any work that you distribute or publish, that in
137 whole or in part contains or is derived from the Program or any
138 part thereof, to be licensed as a whole at no charge to all third
139 parties under the terms of this License.
141 c) If the modified program normally reads commands interactively
142 when run, you must cause it, when started running for such
143 interactive use in the most ordinary way, to print or display an
144 announcement including an appropriate copyright notice and a
145 notice that there is no warranty (or else, saying that you provide
146 a warranty) and that users may redistribute the program under
147 these conditions, and telling the user how to view a copy of this
148 License. (Exception: if the Program itself is interactive but
149 does not normally print such an announcement, your work based on
150 the Program is not required to print an announcement.)
152 These requirements apply to the modified work as a whole. If
153 identifiable sections of that work are not derived from the Program,
154 and can be reasonably considered independent and separate works in
155 themselves, then this License, and its terms, do not apply to those
156 sections when you distribute them as separate works. But when you
157 distribute the same sections as part of a whole which is a work based
158 on the Program, the distribution of the whole must be on the terms of
159 this License, whose permissions for other licensees extend to the
160 entire whole, and thus to each and every part regardless of who wrote it.
162 Thus, it is not the intent of this section to claim rights or contest
163 your rights to work written entirely by you; rather, the intent is to
164 exercise the right to control the distribution of derivative or
165 collective works based on the Program.
167 In addition, mere aggregation of another work not based on the Program
168 with the Program (or with a work based on the Program) on a volume of
169 a storage or distribution medium does not bring the other work under
170 the scope of this License.
172 3. You may copy and distribute the Program (or a work based on it,
173 under Section 2) in object code or executable form under the terms of
174 Sections 1 and 2 above provided that you also do one of the following:
176 a) Accompany it with the complete corresponding machine-readable
177 source code, which must be distributed under the terms of Sections
178 1 and 2 above on a medium customarily used for software interchange; or,
180 b) Accompany it with a written offer, valid for at least three
181 years, to give any third party, for a charge no more than your
182 cost of physically performing source distribution, a complete
183 machine-readable copy of the corresponding source code, to be
184 distributed under the terms of Sections 1 and 2 above on a medium
185 customarily used for software interchange; or,
187 c) Accompany it with the information you received as to the offer
188 to distribute corresponding source code. (This alternative is
189 allowed only for noncommercial distribution and only if you
190 received the program in object code or executable form with such
191 an offer, in accord with Subsection b above.)
193 The source code for a work means the preferred form of the work for
194 making modifications to it. For an executable work, complete source
195 code means all the source code for all modules it contains, plus any
196 associated interface definition files, plus the scripts used to
197 control compilation and installation of the executable. However, as a
198 special exception, the source code distributed need not include
199 anything that is normally distributed (in either source or binary
200 form) with the major components (compiler, kernel, and so on) of the
201 operating system on which the executable runs, unless that component
202 itself accompanies the executable.
204 If distribution of executable or object code is made by offering
205 access to copy from a designated place, then offering equivalent
206 access to copy the source code from the same place counts as
207 distribution of the source code, even though third parties are not
208 compelled to copy the source along with the object code.
210 4. You may not copy, modify, sublicense, or distribute the Program
211 except as expressly provided under this License. Any attempt
212 otherwise to copy, modify, sublicense or distribute the Program is
213 void, and will automatically terminate your rights under this License.
214 However, parties who have received copies, or rights, from you under
215 this License will not have their licenses terminated so long as such
216 parties remain in full compliance.
218 5. You are not required to accept this License, since you have not
219 signed it. However, nothing else grants you permission to modify or
220 distribute the Program or its derivative works. These actions are
221 prohibited by law if you do not accept this License. Therefore, by
222 modifying or distributing the Program (or any work based on the
223 Program), you indicate your acceptance of this License to do so, and
224 all its terms and conditions for copying, distributing or modifying
225 the Program or works based on it.
227 6. Each time you redistribute the Program (or any work based on the
228 Program), the recipient automatically receives a license from the
229 original licensor to copy, distribute or modify the Program subject to
230 these terms and conditions. You may not impose any further
231 restrictions on the recipients' exercise of the rights granted herein.
232 You are not responsible for enforcing compliance by third parties to
235 7. If, as a consequence of a court judgment or allegation of patent
236 infringement or for any other reason (not limited to patent issues),
237 conditions are imposed on you (whether by court order, agreement or
238 otherwise) that contradict the conditions of this License, they do not
239 excuse you from the conditions of this License. If you cannot
240 distribute so as to satisfy simultaneously your obligations under this
241 License and any other pertinent obligations, then as a consequence you
242 may not distribute the Program at all. For example, if a patent
243 license would not permit royalty-free redistribution of the Program by
244 all those who receive copies directly or indirectly through you, then
245 the only way you could satisfy both it and this License would be to
246 refrain entirely from distribution of the Program.
248 If any portion of this section is held invalid or unenforceable under
249 any particular circumstance, the balance of the section is intended to
250 apply and the section as a whole is intended to apply in other
253 It is not the purpose of this section to induce you to infringe any
254 patents or other property right claims or to contest validity of any
255 such claims; this section has the sole purpose of protecting the
256 integrity of the free software distribution system, which is
257 implemented by public license practices. Many people have made
258 generous contributions to the wide range of software distributed
259 through that system in reliance on consistent application of that
260 system; it is up to the author/donor to decide if he or she is willing
261 to distribute software through any other system and a licensee cannot
264 This section is intended to make thoroughly clear what is believed to
265 be a consequence of the rest of this License.
267 8. If the distribution and/or use of the Program is restricted in
268 certain countries either by patents or by copyrighted interfaces, the
269 original copyright holder who places the Program under this License
270 may add an explicit geographical distribution limitation excluding
271 those countries, so that distribution is permitted only in or among
272 countries not thus excluded. In such case, this License incorporates
273 the limitation as if written in the body of this License.
275 9. The Free Software Foundation may publish revised and/or new versions
276 of the General Public License from time to time. Such new versions will
277 be similar in spirit to the present version, but may differ in detail to
278 address new problems or concerns.
280 Each version is given a distinguishing version number. If the Program
281 specifies a version number of this License which applies to it and \"any
282 later version\", you have the option of following the terms and conditions
283 either of that version or of any later version published by the Free
284 Software Foundation. If the Program does not specify a version number of
285 this License, you may choose any version ever published by the Free Software
288 10. If you wish to incorporate parts of the Program into other free
289 programs whose distribution conditions are different, write to the author
290 to ask for permission. For software which is copyrighted by the Free
291 Software Foundation, write to the Free Software Foundation; we sometimes
292 make exceptions for this. Our decision will be guided by the two goals
293 of preserving the free status of all derivatives of our free software and
294 of promoting the sharing and reuse of software generally.
296 static char *warranty=
299 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
300 FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
301 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
302 PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
303 OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
304 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
305 TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
306 PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
307 REPAIR OR CORRECTION.
309 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
310 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
311 REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
312 INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
313 OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
314 TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
315 YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
316 PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
317 POSSIBILITY OF SUCH DAMAGES.
321 static void printTypeInfo(link *);
322 static void printValAggregates (symbol *,link *,char,unsigned int);
324 int srcMode = SRC_CMODE ;
326 /*-----------------------------------------------------------------*/
327 /* funcWithName - returns function with name */
328 /*-----------------------------------------------------------------*/
329 DEFSETFUNC(funcWithName)
331 function *func = item;
333 V_ARG(function **,funcp);
338 if (strcmp(func->sym->name,name) == 0) {
346 /*-----------------------------------------------------------------*/
347 /* setBPatModLine - set break point at the line specified for the */
348 /*-----------------------------------------------------------------*/
349 static void setBPatModLine (module *mod, int line)
351 /* look for the first executable line after the line
352 specified & get the break point there */
353 if (srcMode == SRC_CMODE && line > mod->ncLines) {
354 fprintf(stderr,"No line %d in file \"%s\".\n",
359 if (srcMode == SRC_AMODE && line > mod->nasmLines) {
360 fprintf(stderr,"No line %d in file \"%s\".\n",
365 for ( ; line < (srcMode == SRC_CMODE ? mod->ncLines : mod->nasmLines ) ;
367 if (srcMode == SRC_CMODE) {
368 if (mod->cLines[line]->addr) {
369 setBreakPoint (mod->cLines[line]->addr, CODE, USER,
370 userBpCB, mod->c_name, line);
375 if (mod->asmLines[line]->addr) {
376 setBreakPoint (mod->asmLines[line]->addr, CODE, USER,
377 userBpCB, mod->asm_name, line);
386 /*-----------------------------------------------------------------*/
387 /* clearBPatModLine - clr break point at the line specified */
388 /*-----------------------------------------------------------------*/
389 static void clearBPatModLine (module *mod, int line)
391 /* look for the first executable line after the line
392 specified & get the break point there */
393 if (srcMode == SRC_CMODE && line > mod->ncLines) {
394 fprintf(stderr,"No line %d in file \"%s\".\n",
399 if (srcMode == SRC_AMODE && line > mod->ncLines) {
400 fprintf(stderr,"No line %d in file \"%s\".\n",
405 for ( ; line < (srcMode == SRC_CMODE ? mod->ncLines : mod->nasmLines ) ;
407 if (srcMode == SRC_CMODE)
408 if (mod->cLines[line]->addr) {
409 clearUSERbp (mod->cLines[line]->addr);
413 if (mod->asmLines[line]->addr) {
414 clearUSERbp (mod->asmLines[line]->addr);
422 /*-----------------------------------------------------------------*/
423 /* funcWithNameModule - returns functions with a name module combo */
424 /*-----------------------------------------------------------------*/
425 DEFSETFUNC(funcWithNameModule)
427 function *func = item;
430 V_ARG(function **,funcp);
435 if (strcmp(func->sym->name,fname) == 0 &&
436 strcmp(func->mod->c_name,mname) == 0) {
444 /*-----------------------------------------------------------------*/
445 /* funcInAddr - given an address returns the function */
446 /*-----------------------------------------------------------------*/
447 DEFSETFUNC(funcInAddr)
449 function *func = item;
450 V_ARG(unsigned int,addr);
451 V_ARG(function **,funcp);
456 /* in the address range */
457 if (func->sym->addr <= addr &&
458 func->sym->eaddr >= addr) {
467 /*-----------------------------------------------------------------*/
468 /* setStepBp - will set STEP Bp @ function entry points */
469 /*-----------------------------------------------------------------*/
470 DEFSETFUNC(setStepBp)
472 function *func = item;
474 if (func->sym && func->sym->addr ) {
476 /* set the entry break point */
477 setBreakPoint (func->sym->addr , CODE , STEP ,
478 stepBpCB ,func->mod->c_name , func->entryline);
486 /*-----------------------------------------------------------------*/
487 /* setStepEPBp - sets a given type of bp @ the execution point */
488 /*-----------------------------------------------------------------*/
489 DEFSETFUNC(setStepEPBp)
495 setBreakPoint (ep->addr, CODE, bptype,
496 stepBpCB, mname, ep->line);
500 /*-----------------------------------------------------------------*/
501 /* setNextEPBp - sets a given type of bp @ the execution point */
502 /*-----------------------------------------------------------------*/
503 DEFSETFUNC(setNextEPBp)
509 setBreakPoint (ep->addr, CODE, bptype,
510 nextBpCB, mname, ep->line);
514 /*-----------------------------------------------------------------*/
515 /* lineAtAddr - for execution points returns the one with addr */
516 /*-----------------------------------------------------------------*/
517 DEFSETFUNC(lineAtAddr)
520 V_ARG(unsigned int,addr);
525 /* address must be an exact match */
526 if (ep->addr == addr) {
539 /*-----------------------------------------------------------------*/
540 /* discoverContext - find out the current context of the bp */
541 /*-----------------------------------------------------------------*/
542 context *discoverContext (unsigned addr)
544 function *func = NULL;
547 /* find the function we are in */
548 if (!applyToSet(functions,funcInAddr,addr,&func)) {
549 fprintf(stderr, "Error?:discoverContext: cannot apply to set!\n");
553 currCtxt->func = func;
554 currCtxt->addr = func->laddr = addr;
555 currCtxt->modName = func->modName;
557 /* find the c line number */
558 if(applyToSet(func->cfpoints,lineAtAddr,addr,
559 &line,&currCtxt->block,&currCtxt->level))
560 currCtxt->cline = func->lline = line;
562 currCtxt->cline = func->exitline;
564 /* find the asm line number */
566 if (applyToSet(func->afpoints,lineAtAddr,addr,
568 currCtxt->asmline = line;
570 currCtxt->asmline = -1;
576 /*-----------------------------------------------------------------*/
577 /* simGo - send 'go' cmd to simulator and wait till a break occurs */
578 /*-----------------------------------------------------------------*/
579 void simGo (unsigned int gaddr)
584 static int initial_break_flag = 0;
587 addr = simGoTillBp (gaddr);
589 /* got the pc for the break point now first
590 discover the program context i.e. module, function
591 linenumber of the source etc, etc etc */
592 ctxt = discoverContext (addr);
594 /* dispatch all the break point call back functions */
595 rv = dispatchCB (addr,ctxt);
599 /* the dispatch call back function will return
600 non-zero if an user break point has been hit
601 if not then we continue with the execution
604 if (!initial_break_flag) {
605 initial_break_flag = 1; // kludge to stop only at first run
606 fprintf(stdout, "Stopping at entry. You can now list and set breakpoints\n");
614 // I took this out, after running "run" it would just keep re-running
615 // even after a lot of break points hit. For some reason above code
616 // not triggering(dispatchCB). This seems to be by design, startup adds
617 // a bunch of breakpoints-but they are not USER breakpoints. Perhaps the
618 // debugger changed with its implementation of "go"("run"). It seems we
619 // need to add a "next" or "step" followed by a "run"...
620 // I added a "step" in simi.c when we want a resume function, this seems
623 // still there is question of how do we stop it initially, since
624 // it must be started before it can get a context. If so, we would
625 // want it to just run up to an initial entry point you'd think...
626 // I don't see why we can't set breakpoints before an initial run,
627 // this does not seem right to me.
629 // line #'s are a bit off too.
639 /*-----------------------------------------------------------------*/
640 /* cmdSetUserBp - set break point at the user specified location */
641 /*-----------------------------------------------------------------*/
642 int cmdSetUserBp (char *s, context *cctxt)
645 function *func = NULL;
647 /* user break point location specification can be of the following
649 a) <nothing> - break point at current location
650 b) lineno - number of the current module
651 c) filename:lineno - line number of the given file
652 e) filename:function- function X in file Y (useful for static functions)
653 f) function - function entry point
657 fprintf(stdout,"No symbol table is loaded. Use the \"file\" command.\n");
660 /* white space skip */
661 while (*s && isspace(*s)) s++;
663 /* null terminate it after stripping trailing blanks*/
665 while (bp != s && isspace(*bp)) bp--;
668 /* case a) nothing */
669 /* if nothing given then current location : we know
670 the current execution location from the currentContext */
673 /* if current context is known */
675 if (srcMode == SRC_CMODE)
676 /* set the break point */
677 setBreakPoint ( cctxt->addr , CODE , USER , userBpCB ,
678 cctxt->func->mod->c_name, cctxt->cline);
680 setBreakPoint ( cctxt->addr , CODE , USER , userBpCB ,
681 cctxt->func->mod->asm_name, cctxt->asmline);
685 fprintf(stderr,"No default breakpoint address now.\n");
691 /* check if line number */
696 /* if current context not present then we must get the module
697 which has main & set the break point @ line number provided
698 of that module : if current context known then set the bp
699 at the line number given for the current module
702 if (!cctxt->func->mod) {
703 if (!applyToSet(functions,funcWithName,"main"))
704 fprintf(stderr,"Function \"main\" not defined.\n");
706 setBPatModLine(func->mod,line);
708 setBPatModLine(cctxt->func->mod,line);
710 fprintf(stdout,"No symbol information currently\n");
716 if ((bp = strchr(s,':'))) {
721 if (srcMode == SRC_CMODE) {
722 if (!applyToSet(modules,moduleWithCName,s,&mod)) {
723 fprintf (stderr,"No source file named %s.\n",s);
727 if (!applyToSet(modules,moduleWithAsmName,s,&mod)) {
728 fprintf (stderr,"No source file named %s.\n",s);
733 /* case c) filename:lineno */
734 if (isdigit(*(bp +1))) {
736 setBPatModLine (mod,atoi(bp+1));
740 /* case d) filename:function */
741 if (!applyToSet(functions,funcWithNameModule,bp+1,s,&func))
742 fprintf(stderr,"Function \"%s\" not defined.\n",bp+1);
745 (srcMode == SRC_CMODE ?
752 /* case e) function */
753 if (!applyToSet(functions,funcWithName,s,&func))
754 fprintf(stderr,"Function \"%s\" not defined.\n",s);
756 setBPatModLine(func->mod,
757 (srcMode == SRC_CMODE ?
765 /*-----------------------------------------------------------------*/
766 /* cmdListAsm - list assembler source code */
767 /*-----------------------------------------------------------------*/
768 int cmdListAsm (char *s, context *cctxt)
770 fprintf(stderr,"'listasm' command not yet implemented\n");
774 /*-----------------------------------------------------------------*/
775 /* cmdSetOption - set debugger options */
776 /*-----------------------------------------------------------------*/
777 int cmdSetOption (char *s, context *cctxt)
779 while (*s && isspace(*s)) s++;
780 if (strncmp(s,"srcmode",7) == 0 ) {
781 if (srcMode == SRC_CMODE)
785 fprintf(stderr,"source mode set to '%s'\n",
786 (srcMode == SRC_CMODE ? "C" : "asm"));
790 fprintf(stderr,"'set %s' command not yet implemented\n",s);
794 /*-----------------------------------------------------------------*/
795 /* cmdContinue - continue till next break point */
796 /*-----------------------------------------------------------------*/
797 int cmdContinue (char *s, context *cctxt)
799 if (!cctxt || !cctxt->func) {
800 fprintf(stdout,"The program is not being run.\n");
804 fprintf(stdout,"Continuing.\n");
809 /*-----------------------------------------------------------------*/
810 /* cmdDelUserBp - delete user break point */
811 /*-----------------------------------------------------------------*/
812 int cmdDelUserBp (char *s, context *cctxt)
815 while (isspace(*s)) s++;
820 fprintf (stdout,"Delete all breakpoints? (y or n) ");
822 fgets(buffer,sizeof(buffer),stdin);
823 if (toupper(buffer[0]) == 'Y')
829 /* determine the break point number */
830 if (sscanf(s,"%d",&bpnum) == 1)
836 /*-----------------------------------------------------------------*/
837 /* cmdStep - single step thru C source file */
838 /*-----------------------------------------------------------------*/
839 int cmdStep (char *s, context *cctxt)
841 function *func = NULL;
843 if (!cctxt || !cctxt->func || !cctxt->func->mod)
844 fprintf(stdout,"The program is not being run.\n");
846 /* if we are @ the end of a function then set
847 break points at execution points of the
848 function in the call stack... */
849 if (cctxt->addr == cctxt->func->sym->eaddr) {
850 if ((func = STACK_PEEK(callStack))) {
851 if (srcMode == SRC_CMODE)
852 applyToSet (func->cfpoints,setStepEPBp,STEP,
855 applyToSet (func->afpoints,setStepEPBp,STEP,
856 func->mod->asm_name);
859 /* set breakpoints at all function entry points
860 and all exepoints of this functions & for
861 all functions one up in the call stack */
863 /* all function entry points */
864 applyToSet(functions,setStepBp);
866 if (srcMode == SRC_CMODE) {
867 /* for all execution points in this function */
868 applyToSet(cctxt->func->cfpoints,setStepEPBp,STEP,
869 cctxt->func->mod->c_name);
871 /* set a break point @ the current function's
873 setBreakPoint (cctxt->func->sym->eaddr, CODE, STEP ,
874 stepBpCB, cctxt->func->mod->c_name,
875 cctxt->func->exitline);
877 /* now break point @ callers execution points */
878 if ((func = STACK_PPEEK(callStack))) {
879 applyToSet (func->cfpoints,setStepEPBp,STEP,
881 /* set bp @ callers exit point */
882 setBreakPoint (func->sym->eaddr, CODE, STEP ,
883 stepBpCB, func->mod->c_name,
887 /* for all execution points in this function */
888 applyToSet(cctxt->func->afpoints,setStepEPBp,STEP,
889 cctxt->func->mod->asm_name);
891 /* set a break point @ the current function's
893 setBreakPoint (cctxt->func->sym->eaddr, CODE, STEP ,
894 stepBpCB, cctxt->func->mod->asm_name,
895 cctxt->func->aexitline);
897 /* now break point @ callers execution points */
898 if ((func = STACK_PPEEK(callStack))) {
900 applyToSet (func->afpoints,setStepEPBp,STEP,
901 func->mod->asm_name);
903 /* set bp @ callers exit point */
904 setBreakPoint (func->sym->eaddr, CODE, STEP ,
905 stepBpCB, func->mod->asm_name,
916 /*-----------------------------------------------------------------*/
917 /* cmdNext - next executable C statement file */
918 /*-----------------------------------------------------------------*/
919 int cmdNext (char *s, context *cctxt)
921 function *func = NULL;
922 /* next is almost the same as step except we don't
923 we don't set break point for all function entry
925 if (!cctxt || !cctxt->func || !cctxt->func->mod)
926 fprintf(stdout,"The program is not being run.\n");
929 /* if we are @ the end of a function then set
930 break points at execution points of the
931 function in the call stack... */
932 if (cctxt->addr == cctxt->func->sym->eaddr) {
933 if ((func = STACK_PEEK(callStack))) {
934 if (srcMode == SRC_CMODE)
935 applyToSet (func->cfpoints,setStepEPBp,STEP,
938 applyToSet (func->afpoints,setStepEPBp,STEP,
939 func->mod->asm_name);
942 if (srcMode == SRC_CMODE) {
943 /* for all execution points in this function */
944 applyToSet(cctxt->func->cfpoints,setNextEPBp,NEXT,
945 cctxt->func->mod->c_name);
946 /* set a break point @ the current function's
948 setBreakPoint (cctxt->func->sym->eaddr, CODE, NEXT ,
949 nextBpCB, cctxt->func->mod->c_name,
950 cctxt->func->exitline);
952 /* now break point @ callers execution points */
953 if ((func = STACK_PPEEK(callStack))) {
954 applyToSet (func->cfpoints,setNextEPBp,NEXT ,
956 /* set bp @ callers exit point */
957 setBreakPoint (func->sym->eaddr, CODE, NEXT ,
958 stepBpCB, func->mod->c_name,
962 /* for all execution points in this function */
963 applyToSet(cctxt->func->afpoints,setNextEPBp,NEXT,
964 cctxt->func->mod->asm_name);
965 /* set a break point @ the current function's
967 setBreakPoint (cctxt->func->sym->eaddr, CODE, NEXT ,
968 nextBpCB, cctxt->func->mod->asm_name,
969 cctxt->func->aexitline);
971 /* now break point @ callers execution points */
972 if ((func = STACK_PPEEK(callStack))) {
973 applyToSet (func->cfpoints,setNextEPBp,NEXT ,
974 func->mod->asm_name);
975 /* set bp @ callers exit point */
976 setBreakPoint (func->sym->eaddr, CODE, NEXT ,
977 stepBpCB, func->mod->asm_name,
987 /*-----------------------------------------------------------------*/
988 /* cmdRun - run till next break point */
989 /*-----------------------------------------------------------------*/
990 int cmdRun (char *s, context *cctxt)
993 if (!cctxt || !cctxt->func || !cctxt->func->mod) {
994 fprintf(stdout,"Starting program\n");
999 "The program being debugged has been started already.\n");
1000 fprintf(stdout,"Start it from the beginning? (y or n) ");
1003 fgets(buff,sizeof(buff),stdin);
1004 if (toupper(buff[0]) == 'Y') {
1013 /*-----------------------------------------------------------------*/
1014 /* infoStack - print call stack information */
1015 /*-----------------------------------------------------------------*/
1016 static void infoStack(context *ctxt)
1021 STACK_STARTWALK(callStack) ;
1022 while ((func = STACK_WALK(callStack))) {
1024 fprintf(stdout,"#%d 0x%04x %s () at %s:%d\n",i++,
1025 func->laddr,func->sym->name,
1026 func->mod->c_name,func->lline);
1031 /*-----------------------------------------------------------------*/
1032 /* cmdInfo - info command */
1033 /*-----------------------------------------------------------------*/
1034 int cmdInfo (char *s, context *cctxt)
1036 while (isspace(*s)) s++;
1038 /* list all break points */
1039 if (strcmp(s,"break") == 0) {
1044 /* info frame same as frame */
1045 if (strcmp(s,"frame") == 0) {
1050 /* info stack display call stack */
1051 if (strcmp(s,"stack") == 0) {
1056 /* info stack display call stack */
1057 if (strcmp(s,"registers") == 0) {
1058 fprintf(stdout,"%s",simRegs());
1062 fprintf(stdout,"Undefined info command: \"%s\". Try \"help\n",s);
1067 /*-----------------------------------------------------------------*/
1068 /* cmdQuit - quit debugging */
1069 /*-----------------------------------------------------------------*/
1070 int cmdQuit (char *s, context *cctxt)
1077 /*-----------------------------------------------------------------*/
1078 /* cmdListSrc - list src */
1079 /*-----------------------------------------------------------------*/
1080 int cmdListSrc (char *s, context *cctxt)
1082 static int currline = 0;
1085 static module *mod = NULL;
1086 int llines = listLines;
1088 while (*s && isspace(*s)) s++;
1090 /* if the user has spcified line numer then the line number
1091 can be of the following formats
1092 LINE - just line number
1093 FILE:LINE - filename line number
1094 FUNCTION - list a function
1095 FILE:FUNCTION - function in file */
1096 if (!cctxt || !cctxt->func || !cctxt->func->mod) {
1097 fprintf(stdout,"No symbol table is loaded. Use the \"file\" command.\n");
1103 sscanf(s,"%d",&pline);
1104 mod = cctxt->func->mod;
1108 function *func = NULL;
1110 /* if ':' present then FILE:LINE || FILE:FUNCTION */
1111 if ((bp = strchr(s,':'))) {
1116 if (srcMode == SRC_CMODE) {
1117 if (!applyToSet(modules,moduleWithCName,s,&mod)) {
1118 fprintf (stderr,"No source file named %s.\n",s);
1122 if (!applyToSet(modules,moduleWithAsmName,s,&mod)) {
1123 fprintf (stderr,"No source file named %s.\n",s);
1127 sscanf(bp,"%d",&pline);
1130 if (!applyToSet(functions,funcWithNameModule,bp,s,&func)) {
1131 fprintf(stdout,"Function \"%s\" not defined.\n",bp);
1135 if (srcMode == SRC_CMODE) {
1136 pline = func->entryline;
1137 llines = func->exitline - func->entryline + 1;
1139 pline = func->aentryline;
1140 llines = func->aexitline - func->aentryline + 1;
1146 if (!applyToSet(functions,funcWithName,s,&func)) {
1147 fprintf(stderr,"Function \"%s\" not defined.\n",s);
1152 if (srcMode == SRC_CMODE) {
1153 pline = func->entryline;
1154 llines = func->exitline - func->entryline + 1;
1156 pline = func->aentryline;
1157 llines = func->aexitline - func->aentryline + 1;
1163 /* if no line specified & we had listed
1164 before then continue from that listing */
1168 mod = cctxt->func->mod;
1169 if (srcMode == SRC_CMODE)
1170 pline = cctxt->cline;
1172 pline = cctxt->asmline;
1176 for ( i = 0 ; i < llines ; i++ ) {
1177 if (srcMode == SRC_CMODE) {
1178 if ( (pline + i) >= mod->ncLines )
1180 fprintf(stdout,"%d\t%s",pline + i,
1181 mod->cLines[pline +i]->src);
1183 if ( (pline + i) >= mod->nasmLines )
1185 fprintf(stdout,"%d\t%s",pline + i,
1186 mod->asmLines[pline +i]->src);
1189 currline = pline + i ;
1193 /*-----------------------------------------------------------------*/
1194 /* printValBasic - print value of basic types */
1195 /*-----------------------------------------------------------------*/
1196 static void printValBasic(symbol *sym,unsigned addr,char mem, int size)
1212 v.val = simGetValue(addr,mem,size);
1213 /* if this a floating point number then */
1214 if (IS_FLOAT(sym->type))
1215 fprintf(stdout,"%f",v.f);
1217 if (IS_PTR(sym->type))
1218 fprintf(stdout,"0x%x",v.val);
1220 if (IS_SPEC(sym->type) && IS_INTEGRAL(sym->type)) {
1221 if (IS_CHAR(sym->etype))
1222 fprintf(stdout,"'%c' %d 0x%x",v.val,v.val,v.val);
1224 if (IS_INT(sym->etype))
1225 if (IS_LONG(sym->etype))
1226 if (SPEC_USIGN(sym->etype))
1227 fprintf(stdout,"%d 0x%x",v.val,v.val);
1229 fprintf(stdout,"%d 0x%x",v.sval,v.sval);
1231 fprintf(stdout,"%d 0x%x",v.i.lo,v.i.lo);
1233 fprintf(stdout,"0x%x",v.val);
1235 fprintf(stdout,"0x%x",v.val);
1240 /*-----------------------------------------------------------------*/
1241 /* printValFunc - prints function values */
1242 /*-----------------------------------------------------------------*/
1243 static void printValFunc (symbol *sym)
1245 fprintf(stdout,"print function not yet implemented\n");
1248 /*-----------------------------------------------------------------*/
1249 /* printArrayValue - will print the values of array elements */
1250 /*-----------------------------------------------------------------*/
1251 static void printArrayValue (symbol *sym, char space, unsigned int addr)
1253 link *elem_type = sym->type->next;
1256 fprintf(stdout," { ");
1257 for (i = 0 ; i < DCL_ELEM(sym->type) ; i++) {
1258 if (IS_AGGREGATE(elem_type)) {
1259 printValAggregates(sym,elem_type,space,addr);
1261 printValBasic(sym,addr,space,getSize(elem_type));
1263 addr += getSize(elem_type);
1264 if (i != DCL_ELEM(sym->type) -1)
1265 fprintf(stdout,",");
1268 fprintf(stdout,"}\n");
1271 /*-----------------------------------------------------------------*/
1272 /* printStructValue - prints structures elements */
1273 /*-----------------------------------------------------------------*/
1274 static void printStructValue (symbol *sym,link *type, char space, unsigned int addr)
1276 symbol *fields = SPEC_STRUCT(type)->fields;
1278 fprintf(stdout," { ");
1280 fprintf(stdout,"%s = ",fields->name);
1281 if (IS_AGGREGATE(fields->type)) {
1282 printValAggregates(fields,fields->type,space, addr);
1284 printValBasic(fields,addr,space,getSize(fields->type));
1286 addr += getSize(fields->type);
1287 fields = fields->next;
1289 fprintf(stdout,"}\n");
1292 /*-----------------------------------------------------------------*/
1293 /* printValAggregates - print value of aggregates */
1294 /*-----------------------------------------------------------------*/
1295 static void printValAggregates (symbol *sym, link *type,char space,unsigned int addr)
1298 if (IS_ARRAY(type)) {
1299 printArrayValue(sym, space, addr);
1303 if (IS_STRUCT(type)) {
1304 printStructValue(sym,sym->type,space, addr);
1309 /*-----------------------------------------------------------------*/
1310 /* printSymValue - print value of a symbol */
1311 /*-----------------------------------------------------------------*/
1312 static void printSymValue (symbol *sym, context *cctxt)
1314 static int stack = 1;
1316 /* if it is on stack then compute address & fall thru */
1317 if (sym->isonstack) {
1318 symbol *bp = symLookup("bp",cctxt);
1320 fprintf(stdout,"cannot determine stack frame\n");
1324 sym->addr = simGetValue(bp->addr,bp->addrspace,bp->size)
1328 /* get the value from the simulator and
1330 fprintf(stdout,"$%d = ",stack++);
1331 /* arrays & structures first */
1332 if (IS_AGGREGATE(sym->type))
1333 printValAggregates(sym,sym->type,sym->addrspace,sym->addr);
1336 if (IS_FUNC(sym->type))
1339 printValBasic(sym,sym->addr,sym->addrspace,sym->size);
1340 fprintf(stdout,"\n");
1344 /*-----------------------------------------------------------------*/
1345 /* printStructInfo - print out structure information */
1346 /*-----------------------------------------------------------------*/
1347 static void printStructInfo (structdef *sdef)
1349 symbol *field = sdef->fields ;
1354 field = field->next;
1357 fprintf(stdout,"%s %s {\n",(i ? "struct" : "union" ), sdef->tag);
1358 field = sdef->fields;
1360 printTypeInfo (field->type);
1361 fprintf(stdout," %s ;\n",field->name);
1362 field = field->next ;
1365 fprintf(stdout,"}\n");
1369 /*-----------------------------------------------------------------*/
1370 /* printTypeInfo - print out the type information */
1371 /*-----------------------------------------------------------------*/
1372 static void printTypeInfo(link *p)
1378 switch (DCL_TYPE(p)) {
1380 printTypeInfo (p->next);
1381 fprintf(stdout,"()");
1384 printTypeInfo (p->next);
1385 fprintf(stdout,"[%d]",DCL_ELEM(p));
1391 printTypeInfo (p->next);
1392 fprintf(stdout,"(_near *)");
1396 printTypeInfo (p->next);
1397 fprintf(stdout,"(_xdata *)");
1401 printTypeInfo( p->next);
1402 fprintf(stdout,"(_code *)");
1406 printTypeInfo( p->next);
1407 fprintf(stdout,"(_generic *)");
1411 switch (SPEC_NOUN(p)) { /* depending on the specifier type */
1413 (IS_LONG(p) ? fputs("long ",stdout) :
1414 ( IS_SHORT(p) ? fputs("short ",stdout) :
1415 fputs("int ",stdout))) ;
1418 fputs("float ",stdout);
1422 fputs ("char ",stdout);
1426 fputs("void ",stdout);
1430 printStructInfo (SPEC_STRUCT(p));
1434 fputs("sbit ",stdout);
1438 fprintf(stdout,": %d" ,SPEC_BLEN(p));
1444 /*-----------------------------------------------------------------*/
1445 /* cmdPrint - print value of variable */
1446 /*-----------------------------------------------------------------*/
1447 int cmdPrint (char *s, context *cctxt)
1450 char *bp = s+strlen(s) -1;
1452 while (isspace(*s)) s++;
1454 while (isspace(*bp)) bp--;
1458 if (!cctxt || !cctxt->func) {
1459 fprintf(stdout,"No symbol \"%s\" in current context.\n",
1463 if ((sym = symLookup(s,cctxt))) {
1464 printSymValue(sym,cctxt);
1467 "No symbol \"%s\" in current context.\n",
1473 /*-----------------------------------------------------------------*/
1474 /* cmdPrintType - print type of a variable */
1475 /*-----------------------------------------------------------------*/
1476 int cmdPrintType (char *s, context *cctxt)
1479 char *bp = s+strlen(s) -1;
1481 while (isspace(*s)) s++;
1483 while (isspace(*bp)) bp--;
1487 if (!cctxt || !cctxt->func) {
1488 fprintf(stdout,"No symbol \"%s\" in current context.\n",
1493 if ((sym = symLookup(s,cctxt))) {
1494 printTypeInfo(sym->type);
1495 fprintf(stdout,"\n");
1498 "No symbol \"%s\" in current context.\n",
1504 /*-----------------------------------------------------------------*/
1505 /* cmdClrUserBp - clear user break point */
1506 /*-----------------------------------------------------------------*/
1507 int cmdClrUserBp (char *s, context *cctxt)
1510 function *func = NULL;
1512 /* clear break point location specification can be of the following
1514 a) <nothing> - break point at current location
1515 b) lineno - number of the current module
1516 c) filename:lineno - line number of the given file
1517 e) filename:function- function X in file Y (useful for static functions)
1518 f) function - function entry point
1522 fprintf(stdout,"No symbol table is loaded. Use the \"file\" command.\n");
1526 /* white space skip */
1527 while (*s && isspace(*s)) s++;
1529 /* null terminate it after stripping trailing blanks*/
1531 while (bp != s && isspace(*bp)) bp--;
1534 /* case a) nothing */
1535 /* if nothing given then current location : we know
1536 the current execution location from the currentContext */
1539 /* if current context is known */
1541 /* clear the break point @ current location */
1542 clearUSERbp (cctxt->addr);
1544 fprintf(stderr,"No default breakpoint address now.\n");
1549 /* case b) lineno */
1550 /* check if line number */
1552 /* get the lineno */
1555 /* if current context not present then we must get the module
1556 which has main & set the break point @ line number provided
1557 of that module : if current context known then set the bp
1558 at the line number given for the current module
1561 if (!cctxt->func->mod) {
1562 if (!applyToSet(functions,funcWithName,"main"))
1563 fprintf(stderr,"Function \"main\" not defined.\n");
1565 clearBPatModLine(func->mod,line);
1567 clearBPatModLine(cctxt->func->mod,line);
1573 if ((bp = strchr(s,':'))) {
1578 if (!applyToSet(modules,moduleWithCName,s,&mod)) {
1579 fprintf (stderr,"No source file named %s.\n",s);
1583 /* case c) filename:lineno */
1584 if (isdigit(*(bp +1))) {
1586 clearBPatModLine (mod,atoi(bp+1));
1590 /* case d) filename:function */
1591 if (!applyToSet(functions,funcWithNameModule,bp+1,s,&func))
1592 fprintf(stderr,"Function \"%s\" not defined.\n",bp+1);
1594 clearBPatModLine (mod,func->entryline);
1599 /* case e) function */
1600 if (!applyToSet(functions,funcWithName,s,&func))
1601 fprintf(stderr,"Function \"%s\" not defined.\n",s);
1603 clearBPatModLine(func->mod,func->entryline);
1610 /*-----------------------------------------------------------------*/
1611 /* cmdSimulator - send command to simulator */
1612 /*-----------------------------------------------------------------*/
1613 int cmdSimulator (char *s, context *cctxt)
1617 if (strlen(s) > 80) {
1618 printf("error 3A\n");
1622 strcat(tmpstr, "\n");
1625 fprintf(stdout,"%s",simResponse());
1629 /*-----------------------------------------------------------------*/
1630 /* cmdFrame - Frame command */
1631 /*-----------------------------------------------------------------*/
1632 int cmdFrame (char *s, context *cctxt)
1636 if ((func = STACK_PEEK(callStack))) {
1637 fprintf(stdout,"#0 %s () at %s:%d\n",
1638 func->sym->name,func->mod->c_name,cctxt->cline);
1640 if (cctxt->cline < func->mod->ncLines)
1641 fprintf(stdout,"%d\t%s",
1643 func->mod->cLines[cctxt->cline]->src);
1645 fprintf(stdout,"No stack.\n");
1649 /*-----------------------------------------------------------------*/
1650 /* cmdFinish - exec till end of current function */
1651 /*-----------------------------------------------------------------*/
1652 int cmdFinish (char *s, context *ctxt)
1654 if (!ctxt || ! ctxt->func) {
1655 fprintf(stdout,"The program is not running.\n");
1659 if (srcMode == SRC_CMODE) {
1660 setBreakPoint (ctxt->func->sym->eaddr, CODE, STEP,
1661 stepBpCB, ctxt->func->mod->c_name,
1662 ctxt->func->exitline);
1664 setBreakPoint (ctxt->func->sym->eaddr, CODE, STEP,
1665 stepBpCB, ctxt->func->mod->asm_name,
1666 ctxt->func->aexitline);
1675 /*-----------------------------------------------------------------*/
1676 /* cmdShow - show command */
1677 /*-----------------------------------------------------------------*/
1678 int cmdShow (char *s, context *cctxt)
1680 /* skip white space */
1681 while (*s && isspace(*s)) s++ ;
1683 if (strcmp(s,"copying") == 0) {
1684 fputs(copying,stdout);
1688 if (strcmp(s,"warranty") == 0) {
1689 fputs(warranty,stdout);