gdb-3.3
[platform/upstream/binutils.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 GDB is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
10
11 GDB 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 GDB; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include "param.h"
22 #include "symtab.h"
23 #include "frame.h"
24
25 #include <stdio.h>
26
27 /* This is the sequence of bytes we insert for a breakpoint.  */
28
29 static char break_insn[] = BREAKPOINT;
30
31 /* States of enablement of breakpoint.
32    `temporary' means disable when hit.
33    `delete' means delete when hit.  */
34
35 enum enable { disabled, enabled, temporary, delete};
36
37 /* Not that the ->silent field is not currently used by any commands
38    (though the code is in there if it was to be and set_raw_breakpoint
39    does set it to 0).  I implemented it because I thought it would be
40    useful for a hack I had to put in; I'm going to leave it in because
41    I can see how there might be times when it would indeed be useful */
42
43 struct breakpoint
44 {
45   struct breakpoint *next;
46   /* Number assigned to distinguish breakpoints.  */
47   int number;
48   /* Address to break at.  */
49   CORE_ADDR address;
50   /* Line number of this address.  Redundant.  */
51   int line_number;
52   /* Symtab of file of this address.  Redundant.  */
53   struct symtab *symtab;
54   /* Zero means disabled; remember the info but don't break here.  */
55   enum enable enable;
56   /* Non-zero means a silent breakpoint (don't print frame info
57      if we stop here). */
58   unsigned char silent;
59   /* Number of stops at this breakpoint that should
60      be continued automatically before really stopping.  */
61   int ignore_count;
62   /* "Real" contents of byte where breakpoint has been inserted.
63      Valid only when breakpoints are in the program.  */
64   char shadow_contents[sizeof break_insn];
65   /* Nonzero if this breakpoint is now inserted.  */
66   char inserted;
67   /* Nonzero if this is not the first breakpoint in the list
68      for the given address.  */
69   char duplicate;
70   /* Chain of command lines to execute when this breakpoint is hit.  */
71   struct command_line *commands;
72   /* Stack depth (address of frame).  If nonzero, break only if fp
73      equals this.  */
74   FRAME_ADDR frame;
75   /* Conditional.  Break only if this expression's value is nonzero.  */
76   struct expression *cond;
77 };
78
79 #define ALL_BREAKPOINTS(b)  for (b = breakpoint_chain; b; b = b->next)
80
81 /* Chain of all breakpoints defined.  */
82
83 struct breakpoint *breakpoint_chain;
84
85 /* Number of last breakpoint made.  */
86
87 static int breakpoint_count;
88
89 /* Default address, symtab and line to put a breakpoint at
90    for "break" command with no arg.
91    if default_breakpoint_valid is zero, the other three are
92    not valid, and "break" with no arg is an error.
93
94    This set by print_stack_frame, which calls set_default_breakpoint.  */
95
96 int default_breakpoint_valid;
97 CORE_ADDR default_breakpoint_address;
98 struct symtab *default_breakpoint_symtab;
99 int default_breakpoint_line;
100
101 /* Remaining commands (not yet executed)
102    of last breakpoint hit.  */
103
104 struct command_line *breakpoint_commands;
105
106 static void delete_breakpoint ();
107 void clear_momentary_breakpoints ();
108 void breakpoint_auto_delete ();
109
110 /* Flag indicating extra verbosity for xgdb.  */
111 extern int xgdb_verbose;
112 \f
113 /* condition N EXP -- set break condition of breakpoint N to EXP.  */
114
115 static void
116 condition_command (arg, from_tty)
117      char *arg;
118      int from_tty;
119 {
120   register struct breakpoint *b;
121   register char *p;
122   register int bnum;
123   register struct expression *expr;
124
125   if (arg == 0)
126     error_no_arg ("breakpoint number");
127
128   p = arg;
129   while (*p >= '0' && *p <= '9') p++;
130   if (p == arg)
131     /* There is no number here.  (e.g. "cond a == b").  */
132     error_no_arg ("breakpoint number");
133   bnum = atoi (arg);
134
135   ALL_BREAKPOINTS (b)
136     if (b->number == bnum)
137       {
138         if (b->cond)
139           free (b->cond);
140         if (*p == 0)
141           {
142             b->cond = 0;
143             if (from_tty)
144               printf ("Breakpoint %d now unconditional.\n", bnum);
145           }
146         else
147           {
148             if (*p != ' ' && *p != '\t')
149               error ("Arguments must be an integer (breakpoint number) and an expression.");
150
151             /* Find start of expression */
152             while (*p == ' ' || *p == '\t') p++;
153
154             arg = p;
155             b->cond = (struct expression *) parse_c_1 (&arg, block_for_pc (b->address), 0);
156             if (*arg)
157               error ("Junk at end of expression");
158           }
159         return;
160       }
161
162   error ("No breakpoint number %d.", bnum);
163 }
164
165 static void
166 commands_command (arg)
167      char *arg;
168 {
169   register struct breakpoint *b;
170   register char *p, *p1;
171   register int bnum;
172   struct command_line *l;
173
174   /* If we allowed this, we would have problems with when to
175      free the storage, if we change the commands currently
176      being read from.  */
177
178   if (breakpoint_commands)
179     error ("Can't use the \"commands\" command among a breakpoint's commands.");
180
181   /* Allow commands by itself to refer to the last breakpoint.  */
182   if (arg == 0)
183     bnum = breakpoint_count;
184   else
185     {
186       p = arg;
187       if (! (*p >= '0' && *p <= '9'))
188         error ("Argument must be integer (a breakpoint number).");
189       
190       while (*p >= '0' && *p <= '9') p++;
191       if (*p)
192         error ("Unexpected extra arguments following breakpoint number.");
193       
194       bnum = atoi (arg);
195     }
196
197   ALL_BREAKPOINTS (b)
198     if (b->number == bnum)
199       {
200         if (input_from_terminal_p ())
201           {
202             printf ("Type commands for when breakpoint %d is hit, one per line.\n\
203 End with a line saying just \"end\".\n", bnum);
204             fflush (stdout);
205           }
206         l = read_command_lines ();
207         free_command_lines (&b->commands);
208         b->commands = l;
209         return;
210       }
211   error ("No breakpoint number %d.", bnum);
212 }
213
214 /* Called from command loop to execute the commands
215    associated with the breakpoint we just stopped at.  */
216
217 void
218 do_breakpoint_commands ()
219 {
220   while (breakpoint_commands)
221     {
222       char *line = breakpoint_commands->line;
223       breakpoint_commands = breakpoint_commands->next;
224       execute_command (line, 0);
225       /* If command was "cont", breakpoint_commands is now 0,
226          of if we stopped at yet another breakpoint which has commands,
227          it is now the commands for the new breakpoint.  */
228     }
229   clear_momentary_breakpoints ();
230 }
231
232 /* Used when the program is proceeded, to eliminate any remaining
233    commands attached to the previous breakpoint we stopped at.  */
234
235 void
236 clear_breakpoint_commands ()
237 {
238   breakpoint_commands = 0;
239   breakpoint_auto_delete (0);
240 }
241
242 /* Functions to get and set the current list of pending
243    breakpoint commands.  These are used by run_stack_dummy
244    to preserve the commands around a function call.  */
245
246 struct command_line *
247 get_breakpoint_commands ()
248 {
249   return breakpoint_commands;
250 }
251
252 void
253 set_breakpoint_commands (cmds)
254      struct command_line *cmds;
255 {
256   breakpoint_commands = cmds;
257 }
258 \f
259 /* insert_breakpoints is used when starting or continuing the program.
260    remove_breakpoints is used when the program stops.
261    Both return zero if successful,
262    or an `errno' value if could not write the inferior.  */
263
264 int
265 insert_breakpoints ()
266 {
267   register struct breakpoint *b;
268   int val;
269
270 #ifdef BREAKPOINT_DEBUG
271   printf ("Inserting breakpoints.\n");
272 #endif /* BREAKPOINT_DEBUG */
273
274   ALL_BREAKPOINTS (b)
275     if (b->enable != disabled && ! b->inserted && ! b->duplicate)
276       {
277         read_memory (b->address, b->shadow_contents, sizeof break_insn);
278         val = write_memory (b->address, break_insn, sizeof break_insn);
279         if (val)
280           return val;
281 #ifdef BREAKPOINT_DEBUG
282         printf ("Inserted breakpoint at 0x%x, shadow 0x%x, 0x%x.\n",
283                 b->address, b->shadow_contents[0], b->shadow_contents[1]);
284 #endif /* BREAKPOINT_DEBUG */
285         b->inserted = 1;
286       }
287   return 0;
288 }
289
290 int
291 remove_breakpoints ()
292 {
293   register struct breakpoint *b;
294   int val;
295
296 #ifdef BREAKPOINT_DEBUG
297   printf ("Removing breakpoints.\n");
298 #endif /* BREAKPOINT_DEBUG */
299
300   ALL_BREAKPOINTS (b)
301     if (b->inserted)
302       {
303         val = write_memory (b->address, b->shadow_contents, sizeof break_insn);
304         if (val)
305           return val;
306         b->inserted = 0;
307 #ifdef BREAKPOINT_DEBUG
308         printf ("Removed breakpoint at 0x%x, shadow 0x%x, 0x%x.\n",
309                 b->address, b->shadow_contents[0], b->shadow_contents[1]);
310 #endif /* BREAKPOINT_DEBUG */
311       }
312
313   return 0;
314 }
315
316 /* Clear the "inserted" flag in all breakpoints.
317    This is done when the inferior is loaded.  */
318
319 void
320 mark_breakpoints_out ()
321 {
322   register struct breakpoint *b;
323
324   ALL_BREAKPOINTS (b)
325     b->inserted = 0;
326 }
327
328 /* breakpoint_here_p (PC) returns 1 if an enabled breakpoint exists at PC.
329    When continuing from a location with a breakpoint,
330    we actually single step once before calling insert_breakpoints.  */
331
332 int
333 breakpoint_here_p (pc)
334      CORE_ADDR pc;
335 {
336   register struct breakpoint *b;
337
338   ALL_BREAKPOINTS (b)
339     if (b->enable != disabled && b->address == pc)
340       return 1;
341
342   return 0;
343 }
344
345 /* Evaluate the expression EXP and return 1 if value is zero.
346    This is used inside a catch_errors to evaluate the breakpoint condition.  */
347
348 int
349 breakpoint_cond_eval (exp)
350      struct expression *exp;
351 {
352   return value_zerop (evaluate_expression (exp));
353 }
354
355 /* Return 0 if PC is not the address just after a breakpoint,
356    or -1 if breakpoint says do not stop now,
357    or -2 if breakpoint says it has deleted itself and don't stop,
358    or -3 if hit a breakpoint number -3 (delete when program stops),
359    or else the number of the breakpoint,
360    with 0x1000000 added (or subtracted, for a negative return value) for
361    a silent breakpoint.  */
362
363 int
364 breakpoint_stop_status (pc, frame_address)
365      CORE_ADDR pc;
366      FRAME_ADDR frame_address;
367 {
368   register struct breakpoint *b;
369   register int cont = 0;
370
371   /* Get the address where the breakpoint would have been.  */
372   pc -= DECR_PC_AFTER_BREAK;
373
374   ALL_BREAKPOINTS (b)
375     if (b->enable != disabled && b->address == pc)
376       {
377         if (b->frame && b->frame != frame_address)
378           cont = -1;
379         else
380           {
381             int value_zero;
382             if (b->cond)
383               {
384                 /* Need to select the frame, with all that implies
385                    so that the conditions will have the right context.  */
386                 select_frame (get_current_frame (), 0);
387                 value_zero
388                   = catch_errors (breakpoint_cond_eval, b->cond,
389                                   "Error occurred in testing breakpoint condition.");
390                 free_all_values ();
391               }
392             if (b->cond && value_zero)
393               {
394                 cont = -1;
395               }
396             else if (b->ignore_count > 0)
397               {
398                 b->ignore_count--;
399                 cont = -1;
400               }
401             else
402               {
403                 if (b->enable == temporary)
404                   b->enable = disabled;
405                 breakpoint_commands = b->commands;
406                 if (b->silent
407                     || (breakpoint_commands
408                         && !strcmp ("silent", breakpoint_commands->line)))
409                   {
410                     if (breakpoint_commands)
411                       breakpoint_commands = breakpoint_commands->next;
412                     return (b->number > 0 ?
413                             0x1000000 + b->number :
414                             b->number - 0x1000000);
415                   }
416                 return b->number;
417               }
418           }
419       }
420
421   return cont;
422 }
423 \f
424 static void
425 breakpoint_1 (bnum)
426      int bnum;
427 {
428   register struct breakpoint *b;
429   register struct command_line *l;
430   register struct symbol *sym;
431   CORE_ADDR last_addr = (CORE_ADDR)-1;
432
433   ALL_BREAKPOINTS (b)
434     if (bnum == -1 || bnum == b->number)
435       {
436         printf_filtered ("#%-3d %c  0x%08x ", b->number,
437                 "nyod"[(int) b->enable],
438                 b->address);
439         last_addr = b->address;
440         if (b->symtab)
441           {
442             sym = find_pc_function (b->address);
443             if (sym)
444               printf_filtered (" in %s (%s line %d)", SYMBOL_NAME (sym),
445                                b->symtab->filename, b->line_number);
446             else
447               printf_filtered ("%s line %d", b->symtab->filename, b->line_number);
448           }
449         else
450           {
451             char *name;
452             int addr;
453
454             if (find_pc_partial_function (b->address, &name, &addr))
455               {
456                 if (b->address - addr)
457                   printf_filtered ("<%s+%d>", name, b->address - addr);
458                 else
459                   printf_filtered ("<%s>", name);
460               }
461           }
462               
463         printf_filtered ("\n");
464
465         if (b->ignore_count)
466           printf_filtered ("\tignore next %d hits\n", b->ignore_count);
467         if (b->frame)
468           printf_filtered ("\tstop only in stack frame at 0x%x\n", b->frame);
469         if (b->cond)
470           {
471             printf_filtered ("\tbreak only if ");
472             print_expression (b->cond, stdout);
473             printf_filtered ("\n");
474           }
475         if (l = b->commands)
476           while (l)
477             {
478               printf_filtered ("\t%s\n", l->line);
479               l = l->next;
480             }
481       }
482
483   /* Compare against (CORE_ADDR)-1 in case some compiler decides
484      that a comparison of an unsigned with -1 is always false.  */
485   if (last_addr != (CORE_ADDR)-1)
486     set_next_address (last_addr);
487 }
488
489 static void
490 breakpoints_info (bnum_exp)
491      char *bnum_exp;
492 {
493   int bnum = -1;
494
495   if (bnum_exp)
496     bnum = parse_and_eval_address (bnum_exp);
497   else if (breakpoint_chain == 0)
498     printf_filtered ("No breakpoints.\n");
499   else
500     printf_filtered ("Breakpoints:\n\
501 Num Enb   Address    Where\n");
502
503   breakpoint_1 (bnum);
504 }
505
506 /* Print a message describing any breakpoints set at PC.  */
507
508 static void
509 describe_other_breakpoints (pc)
510      register CORE_ADDR pc;
511 {
512   register int others = 0;
513   register struct breakpoint *b;
514
515   ALL_BREAKPOINTS (b)
516     if (b->address == pc)
517       others++;
518   if (others > 0)
519     {
520       printf ("Note: breakpoint%s ", (others > 1) ? "s" : "");
521       ALL_BREAKPOINTS (b)
522         if (b->address == pc)
523           {
524             others--;
525             printf ("%d%s%s ",
526                     b->number,
527                     (b->enable == disabled) ? " (disabled)" : "",
528                     (others > 1) ? "," : ((others == 1) ? " and" : ""));
529           }
530       printf (" also set at pc 0x%x\n", pc);
531     }
532 }
533 \f
534 /* Set the default place to put a breakpoint
535    for the `break' command with no arguments.  */
536
537 void
538 set_default_breakpoint (valid, addr, symtab, line)
539      int valid;
540      CORE_ADDR addr;
541      struct symtab *symtab;
542      int line;
543 {
544   default_breakpoint_valid = valid;
545   default_breakpoint_address = addr;
546   default_breakpoint_symtab = symtab;
547   default_breakpoint_line = line;
548 }
549
550 /* Rescan breakpoints at address ADDRESS,
551    marking the first one as "first" and any others as "duplicates".
552    This is so that the bpt instruction is only inserted once.  */
553
554 static void
555 check_duplicates (address)
556      CORE_ADDR address;
557 {
558   register struct breakpoint *b;
559   register int count = 0;
560
561   ALL_BREAKPOINTS (b)
562     if (b->enable != disabled && b->address == address)
563       {
564         count++;
565         b->duplicate = count > 1;
566       }
567 }
568
569 /* Low level routine to set a breakpoint.
570    Takes as args the three things that every breakpoint must have.
571    Returns the breakpoint object so caller can set other things.
572    Does not set the breakpoint number!
573    Does not print anything.  */
574
575 static struct breakpoint *
576 set_raw_breakpoint (sal)
577      struct symtab_and_line sal;
578 {
579   register struct breakpoint *b, *b1;
580
581   b = (struct breakpoint *) xmalloc (sizeof (struct breakpoint));
582   bzero (b, sizeof *b);
583   b->address = sal.pc;
584   b->symtab = sal.symtab;
585   b->line_number = sal.line;
586   b->enable = enabled;
587   b->next = 0;
588   b->silent = 0;
589
590   /* Add this breakpoint to the end of the chain
591      so that a list of breakpoints will come out in order
592      of increasing numbers.  */
593
594   b1 = breakpoint_chain;
595   if (b1 == 0)
596     breakpoint_chain = b;
597   else
598     {
599       while (b1->next)
600         b1 = b1->next;
601       b1->next = b;
602     }
603
604   check_duplicates (sal.pc);
605
606   return b;
607 }
608
609 /* Set a breakpoint that will evaporate an end of command
610    at address specified by SAL.
611    Restrict it to frame FRAME if FRAME is nonzero.  */
612
613 void
614 set_momentary_breakpoint (sal, frame)
615      struct symtab_and_line sal;
616      FRAME frame;
617 {
618   register struct breakpoint *b;
619   b = set_raw_breakpoint (sal);
620   b->number = -3;
621   b->enable = delete;
622   b->frame = (frame ? FRAME_FP (frame) : 0);
623 }
624
625 void
626 clear_momentary_breakpoints ()
627 {
628   register struct breakpoint *b;
629   ALL_BREAKPOINTS (b)
630     if (b->number == -3)
631       {
632         delete_breakpoint (b);
633         break;
634       }
635 }
636 \f
637 /* Set a breakpoint from a symtab and line.
638    If TEMPFLAG is nonzero, it is a temporary breakpoint.
639    Print the same confirmation messages that the breakpoint command prints.  */
640
641 void
642 set_breakpoint (s, line, tempflag)
643      struct symtab *s;
644      int line;
645      int tempflag;
646 {
647   register struct breakpoint *b;
648   struct symtab_and_line sal;
649   
650   sal.symtab = s;
651   sal.line = line;
652   sal.pc = find_line_pc (sal.symtab, sal.line);
653   if (sal.pc == 0)
654     error ("No line %d in file \"%s\".\n", sal.line, sal.symtab->filename);
655   else
656     {
657       describe_other_breakpoints (sal.pc);
658
659       b = set_raw_breakpoint (sal);
660       b->number = ++breakpoint_count;
661       b->cond = 0;
662       if (tempflag)
663         b->enable = temporary;
664
665       printf ("Breakpoint %d at 0x%x", b->number, b->address);
666       if (b->symtab)
667         printf (": file %s, line %d.", b->symtab->filename, b->line_number);
668       printf ("\n");
669     }
670 }
671 \f
672 /* Set a breakpoint according to ARG (function, linenum or *address)
673    and make it temporary if TEMPFLAG is nonzero. */
674
675 static void
676 break_command_1 (arg, tempflag, from_tty)
677      char *arg;
678      int tempflag, from_tty;
679 {
680   struct symtabs_and_lines sals;
681   struct symtab_and_line sal;
682   register struct expression *cond = 0;
683   register struct breakpoint *b;
684   char *save_arg;
685   int i;
686   CORE_ADDR pc;
687
688   sals.sals = NULL;
689   sals.nelts = 0;
690
691   sal.line = sal.pc = sal.end = 0;
692   sal.symtab = 0;
693
694   /* If no arg given, or if first arg is 'if ', use the default breakpoint. */
695
696   if (!arg || (arg[0] == 'i' && arg[1] == 'f' 
697                && (arg[2] == ' ' || arg[2] == '\t')))
698     {
699       if (default_breakpoint_valid)
700         {
701           sals.sals = (struct symtab_and_line *) 
702             malloc (sizeof (struct symtab_and_line));
703           sal.pc = default_breakpoint_address;
704           sal.line = default_breakpoint_line;
705           sal.symtab = default_breakpoint_symtab;
706           sals.sals[0] = sal;
707           sals.nelts = 1;
708         }
709       else
710         error ("No default breakpoint address now.");
711     }
712   else
713     /* Force almost all breakpoints to be in terms of the
714        current_source_symtab (which is decode_line_1's default).  This
715        should produce the results we want almost all of the time while
716        leaving default_breakpoint_* alone.  */
717     if (default_breakpoint_valid
718         && (!current_source_symtab
719             || (arg && (*arg == '+' || *arg == '-'))))
720       sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
721                             default_breakpoint_line);
722     else
723       sals = decode_line_1 (&arg, 1, 0, 0);
724   
725   if (! sals.nelts) 
726     return;
727
728   save_arg = arg;
729   for (i = 0; i < sals.nelts; i++)
730     {
731       sal = sals.sals[i];
732       if (sal.pc == 0 && sal.symtab != 0)
733         {
734           pc = find_line_pc (sal.symtab, sal.line);
735           if (pc == 0)
736             error ("No line %d in file \"%s\".",
737                    sal.line, sal.symtab->filename);
738         }
739       else 
740         pc = sal.pc;
741       
742       while (arg && *arg)
743         {
744           if (arg[0] == 'i' && arg[1] == 'f'
745               && (arg[2] == ' ' || arg[2] == '\t'))
746             cond = (struct expression *) parse_c_1 ((arg += 2, &arg),
747                                                     block_for_pc (pc), 0);
748           else
749             error ("Junk at end of arguments.");
750         }
751       arg = save_arg;
752       sals.sals[i].pc = pc;
753     }
754
755   for (i = 0; i < sals.nelts; i++)
756     {
757       sal = sals.sals[i];
758
759       if (from_tty)
760         describe_other_breakpoints (sal.pc);
761
762       b = set_raw_breakpoint (sal);
763       b->number = ++breakpoint_count;
764       b->cond = cond;
765       if (tempflag)
766         b->enable = temporary;
767
768       printf ("Breakpoint %d at 0x%x", b->number, b->address);
769       if (b->symtab)
770         printf (": file %s, line %d.", b->symtab->filename, b->line_number);
771       printf ("\n");
772     }
773
774   if (sals.nelts > 1)
775     {
776       printf ("Multiple breakpoints were set.\n");
777       printf ("Use the \"delete\" command to delete unwanted breakpoints.\n");
778     }
779   free (sals.sals);
780 }
781
782 static void
783 break_command (arg, from_tty)
784      char *arg;
785      int from_tty;
786 {
787   break_command_1 (arg, 0, from_tty);
788 }
789
790 static void
791 tbreak_command (arg, from_tty)
792      char *arg;
793      int from_tty;
794 {
795   break_command_1 (arg, 1, from_tty);
796 }
797 \f
798 /*
799  * Helper routine for the until_command routine in infcmd.c.  Here
800  * because it uses the mechanisms of breakpoints.
801  */
802 void
803 until_break_command (arg, from_tty)
804      char *arg;
805      int from_tty;
806 {
807   struct symtabs_and_lines sals;
808   struct symtab_and_line sal;
809   FRAME prev_frame = get_prev_frame (selected_frame);
810
811   clear_proceed_status ();
812
813   /* Set a breakpoint where the user wants it and at return from
814      this function */
815   
816   if (default_breakpoint_valid)
817     sals = decode_line_1 (&arg, 1, default_breakpoint_symtab,
818                           default_breakpoint_line);
819   else
820     sals = decode_line_1 (&arg, 1, 0, 0);
821   
822   if (sals.nelts != 1)
823     error ("Couldn't get information on specified line.");
824   
825   sal = sals.sals[0];
826   free (sals.sals);             /* malloc'd, so freed */
827   
828   if (*arg)
829     error ("Junk at end of arguments.");
830   
831   if (sal.pc == 0 && sal.symtab != 0)
832     sal.pc = find_line_pc (sal.symtab, sal.line);
833   
834   if (sal.pc == 0)
835     error ("No line %d in file \"%s\".", sal.line, sal.symtab->filename);
836   
837   set_momentary_breakpoint (sal, selected_frame);
838   
839   /* Keep within the current frame */
840   
841   if (prev_frame)
842     {
843       struct frame_info *fi;
844       
845       fi = get_frame_info (prev_frame);
846       sal = find_pc_line (fi->pc, 0);
847       sal.pc = fi->pc;
848       set_momentary_breakpoint (sal, prev_frame);
849     }
850   
851   proceed (-1, -1, 0);
852 }
853 \f
854 static void
855 clear_command (arg, from_tty)
856      char *arg;
857      int from_tty;
858 {
859   register struct breakpoint *b, *b1;
860   struct symtabs_and_lines sals;
861   struct symtab_and_line sal;
862   register struct breakpoint *found;
863   int i;
864
865   if (arg)
866     {
867       sals = decode_line_spec (arg, 1);
868     }
869   else
870     {
871       sals.sals = (struct symtab_and_line *) malloc (sizeof (struct symtab_and_line));
872       sal.line = default_breakpoint_line;
873       sal.symtab = default_breakpoint_symtab;
874       sal.pc = 0;
875       if (sal.symtab == 0)
876         error ("No source file specified.");
877
878       sals.sals[0] = sal;
879       sals.nelts = 1;
880     }
881
882   for (i = 0; i < sals.nelts; i++)
883     {
884       /* If exact pc given, clear bpts at that pc.
885          But if sal.pc is zero, clear all bpts on specified line.  */
886       sal = sals.sals[i];
887       found = (struct breakpoint *) 0;
888       while (breakpoint_chain
889              && (sal.pc ? breakpoint_chain->address == sal.pc
890                  : (breakpoint_chain->symtab == sal.symtab
891                     && breakpoint_chain->line_number == sal.line)))
892         {
893           b1 = breakpoint_chain;
894           breakpoint_chain = b1->next;
895           b1->next = found;
896           found = b1;
897         }
898
899       ALL_BREAKPOINTS (b)
900         while (b->next
901                && (sal.pc ? b->next->address == sal.pc
902                    : (b->next->symtab == sal.symtab
903                       && b->next->line_number == sal.line)))
904           {
905             b1 = b->next;
906             b->next = b1->next;
907             b1->next = found;
908             found = b1;
909           }
910
911       if (found == 0)
912         error ("No breakpoint at %s.", arg);
913
914       if (found->next) from_tty = 1; /* Always report if deleted more than one */
915       if (from_tty) printf ("Deleted breakpoint%s ", found->next ? "s" : "");
916       while (found)
917         {
918           if (from_tty) printf ("%d ", found->number);
919           b1 = found->next;
920           delete_breakpoint (found);
921           found = b1;
922         }
923       if (from_tty) putchar ('\n');
924     }
925   free (sals.sals);
926 }
927 \f
928 /* Delete breakpoint number BNUM if it is a `delete' breakpoint.
929    This is called after breakpoint BNUM has been hit.
930    Also delete any breakpoint numbered -3 unless there are breakpoint
931    commands to be executed.  */
932
933 void
934 breakpoint_auto_delete (bnum)
935      int bnum;
936 {
937   register struct breakpoint *b;
938   if (bnum != 0)
939     ALL_BREAKPOINTS (b)
940       if (b->number == bnum)
941         {
942           if (b->enable == delete)
943             delete_breakpoint (b);
944           break;
945         }
946   if (breakpoint_commands == 0)
947     clear_momentary_breakpoints ();
948 }
949
950 static void
951 delete_breakpoint (bpt)
952      struct breakpoint *bpt;
953 {
954   register struct breakpoint *b;
955
956   if (bpt->inserted)
957     write_memory (bpt->address, bpt->shadow_contents, sizeof break_insn);
958
959   if (breakpoint_chain == bpt)
960     breakpoint_chain = bpt->next;
961
962   ALL_BREAKPOINTS (b)
963     if (b->next == bpt)
964       {
965         b->next = bpt->next;
966         break;
967       }
968
969   check_duplicates (bpt->address);
970
971   free_command_lines (&bpt->commands);
972   if (bpt->cond)
973     free (bpt->cond);
974
975   if (xgdb_verbose && bpt->number >=0)
976     printf ("breakpoint #%d deleted\n", bpt->number);
977
978   free (bpt);
979 }
980
981 static void map_breakpoint_numbers ();
982
983 static void
984 delete_command (arg, from_tty)
985      char *arg;
986      int from_tty;
987 {
988   register struct breakpoint *b, *b1;
989
990   if (arg == 0)
991     {
992       /* Ask user only if there are some breakpoints to delete.  */
993       if (!from_tty
994           || breakpoint_chain && query ("Delete all breakpoints? "))
995         {
996           /* No arg; clear all breakpoints.  */
997           while (breakpoint_chain)
998             delete_breakpoint (breakpoint_chain);
999         }
1000     }
1001   else
1002     map_breakpoint_numbers (arg, delete_breakpoint);
1003 }
1004
1005 /* Delete all breakpoints.
1006    Done when new symtabs are loaded, since the break condition expressions
1007    may become invalid, and the breakpoints are probably wrong anyway.  */
1008
1009 void
1010 clear_breakpoints ()
1011 {
1012   delete_command (0, 0);
1013 }
1014 \f
1015 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
1016    If from_tty is nonzero, it prints a message to that effect,
1017    which ends with a period (no newline).  */
1018
1019 void
1020 set_ignore_count (bptnum, count, from_tty)
1021      int bptnum, count, from_tty;
1022 {
1023   register struct breakpoint *b;
1024
1025   if (count < 0)
1026     count = 0;
1027
1028   ALL_BREAKPOINTS (b)
1029     if (b->number == bptnum)
1030       {
1031         b->ignore_count = count;
1032         if (!from_tty)
1033           return;
1034         else if (count == 0)
1035           printf ("Will stop next time breakpoint %d is reached.", bptnum);
1036         else if (count == 1)
1037           printf ("Will ignore next crossing of breakpoint %d.", bptnum);
1038         else
1039           printf ("Will ignore next %d crossings of breakpoint %d.",
1040                   count, bptnum);
1041         return;
1042       }
1043
1044   error ("No breakpoint number %d.", bptnum);
1045 }
1046
1047 /* Command to set ignore-count of breakpoint N to COUNT.  */
1048
1049 static void
1050 ignore_command (args, from_tty)
1051      char *args;
1052      int from_tty;
1053 {
1054   register char *p = args;
1055   register int num;
1056
1057   if (p == 0)
1058     error_no_arg ("a breakpoint number");
1059   
1060   while (*p >= '0' && *p <= '9') p++;
1061   if (*p && *p != ' ' && *p != '\t')
1062     error ("First argument must be a breakpoint number.");
1063
1064   num = atoi (args);
1065
1066   if (*p == 0)
1067     error ("Second argument (specified ignore-count) is missing.");
1068
1069   set_ignore_count (num, parse_and_eval_address (p), from_tty);
1070   printf ("\n");
1071 }
1072 \f
1073 /* Call FUNCTION on each of the breakpoints
1074    whose numbers are given in ARGS.  */
1075
1076 static void
1077 map_breakpoint_numbers (args, function)
1078      char *args;
1079      void (*function) ();
1080 {
1081   register char *p = args;
1082   register char *p1;
1083   register int num;
1084   register struct breakpoint *b;
1085
1086   if (p == 0)
1087     error_no_arg ("one or more breakpoint numbers");
1088
1089   while (*p)
1090     {
1091       p1 = p;
1092       while (*p1 >= '0' && *p1 <= '9') p1++;
1093       if (*p1 && *p1 != ' ' && *p1 != '\t')
1094         error ("Arguments must be breakpoint numbers.");
1095
1096       num = atoi (p);
1097
1098       ALL_BREAKPOINTS (b)
1099         if (b->number == num)
1100           {
1101             function (b);
1102             goto win;
1103           }
1104       printf ("No breakpoint number %d.\n", num);
1105     win:
1106       p = p1;
1107       while (*p == ' ' || *p == '\t') p++;
1108     }
1109 }
1110
1111 static void
1112 enable_breakpoint (bpt)
1113      struct breakpoint *bpt;
1114 {
1115   bpt->enable = enabled;
1116
1117   if (xgdb_verbose && bpt->number >= 0)
1118     printf ("breakpoint #%d enabled\n", bpt->number);
1119
1120   check_duplicates (bpt->address);
1121 }
1122
1123 static void
1124 enable_command (args)
1125      char *args;
1126 {
1127   struct breakpoint *bpt;
1128   if (args == 0)
1129     ALL_BREAKPOINTS (bpt)
1130       enable_breakpoint (bpt);
1131   else
1132     map_breakpoint_numbers (args, enable_breakpoint);
1133 }
1134
1135 static void
1136 disable_breakpoint (bpt)
1137      struct breakpoint *bpt;
1138 {
1139   bpt->enable = disabled;
1140
1141   if (xgdb_verbose && bpt->number >= 0)
1142     printf ("breakpoint #%d disabled\n", bpt->number);
1143
1144   check_duplicates (bpt->address);
1145 }
1146
1147 static void
1148 disable_command (args)
1149      char *args;
1150 {
1151   register struct breakpoint *bpt;
1152   if (args == 0)
1153     ALL_BREAKPOINTS (bpt)
1154       disable_breakpoint (bpt);
1155   else
1156     map_breakpoint_numbers (args, disable_breakpoint);
1157 }
1158
1159 static void
1160 enable_once_breakpoint (bpt)
1161      struct breakpoint *bpt;
1162 {
1163   bpt->enable = temporary;
1164
1165   check_duplicates (bpt->address);
1166 }
1167
1168 static void
1169 enable_once_command (args)
1170      char *args;
1171 {
1172   map_breakpoint_numbers (args, enable_once_breakpoint);
1173 }
1174
1175 static void
1176 enable_delete_breakpoint (bpt)
1177      struct breakpoint *bpt;
1178 {
1179   bpt->enable = delete;
1180
1181   check_duplicates (bpt->address);
1182 }
1183
1184 static void
1185 enable_delete_command (args)
1186      char *args;
1187 {
1188   map_breakpoint_numbers (args, enable_delete_breakpoint);
1189 }
1190 \f
1191 /*
1192  * Use default_breakpoint_'s, or nothing if they aren't valid.
1193  */
1194 struct symtabs_and_lines
1195 decode_line_spec_1 (string, funfirstline)
1196      char *string;
1197      int funfirstline;
1198 {
1199   struct symtabs_and_lines sals;
1200   if (string == 0)
1201     error ("Empty line specification.");
1202   if (default_breakpoint_valid)
1203     sals = decode_line_1 (&string, funfirstline,
1204                           default_breakpoint_symtab, default_breakpoint_line);
1205   else
1206     sals = decode_line_1 (&string, funfirstline, 0, 0);
1207   if (*string)
1208     error ("Junk at end of line specification: %s", string);
1209   return sals;
1210 }
1211 \f
1212
1213 /* Chain containing all defined enable commands.  */
1214
1215 extern struct cmd_list_element 
1216   *enablelist, *disablelist,
1217   *deletelist, *enablebreaklist;
1218
1219 extern struct cmd_list_element *cmdlist;
1220
1221 void
1222 _initialize_breakpoint ()
1223 {
1224   breakpoint_chain = 0;
1225   breakpoint_count = 0;
1226
1227   add_com ("ignore", class_breakpoint, ignore_command,
1228            "Set ignore-count of breakpoint number N to COUNT.");
1229
1230   add_com ("commands", class_breakpoint, commands_command,
1231            "Set commands to be executed when a breakpoint is hit.\n\
1232 Give breakpoint number as argument after \"commands\".\n\
1233 With no argument, the targeted breakpoint is the last one set.\n\
1234 The commands themselves follow starting on the next line.\n\
1235 Type a line containing \"end\" to indicate the end of them.\n\
1236 Give \"silent\" as the first line to make the breakpoint silent;\n\
1237 then no output is printed when it is hit, except what the commands print.");
1238
1239   add_com ("condition", class_breakpoint, condition_command,
1240            "Specify breakpoint number N to break only if COND is true.\n\
1241 N is an integer; COND is a C expression to be evaluated whenever\n\
1242 breakpoint N is reached.  Actually break only when COND is nonzero.");
1243
1244   add_com ("tbreak", class_breakpoint, tbreak_command,
1245            "Set a temporary breakpoint.  Args like \"break\" command.\n\
1246 Like \"break\" except the breakpoint is only enabled temporarily,\n\
1247 so it will be disabled when hit.  Equivalent to \"break\" followed\n\
1248 by using \"enable once\" on the breakpoint number.");
1249
1250   add_prefix_cmd ("enable", class_breakpoint, enable_command,
1251                   "Enable some breakpoints or auto-display expressions.\n\
1252 Give breakpoint numbers (separated by spaces) as arguments.\n\
1253 With no subcommand, breakpoints are enabled until you command otherwise.\n\
1254 This is used to cancel the effect of the \"disable\" command.\n\
1255 With a subcommand you can enable temporarily.\n\
1256 \n\
1257 The \"display\" subcommand applies to auto-displays instead of breakpoints.",
1258                   &enablelist, "enable ", 1, &cmdlist);
1259
1260   add_abbrev_prefix_cmd ("breakpoints", class_breakpoint, enable_command,
1261                   "Enable some breakpoints or auto-display expressions.\n\
1262 Give breakpoint numbers (separated by spaces) as arguments.\n\
1263 With no subcommand, breakpoints are enabled until you command otherwise.\n\
1264 This is used to cancel the effect of the \"disable\" command.\n\
1265 May be abbreviates to simply \"enable\".\n\
1266 With a subcommand you can enable temporarily.",
1267                   &enablebreaklist, "enable breakpoints ", 1, &enablelist);
1268
1269   add_cmd ("once", no_class, enable_once_command,
1270            "Enable breakpoints for one hit.  Give breakpoint numbers.\n\
1271 If a breakpoint is hit while enabled in this fashion, it becomes disabled.\n\
1272 See the \"tbreak\" command which sets a breakpoint and enables it once.",
1273            &enablebreaklist);
1274
1275   add_cmd ("delete", no_class, enable_delete_command,
1276            "Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
1277 If a breakpoint is hit while enabled in this fashion, it is deleted.",
1278            &enablebreaklist);
1279
1280   add_cmd ("delete", no_class, enable_delete_command,
1281            "Enable breakpoints and delete when hit.  Give breakpoint numbers.\n\
1282 If a breakpoint is hit while enabled in this fashion, it is deleted.",
1283            &enablelist);
1284
1285   add_cmd ("once", no_class, enable_once_command,
1286            "Enable breakpoints for one hit.  Give breakpoint numbers.\n\
1287 If a breakpoint is hit while enabled in this fashion, it becomes disabled.\n\
1288 See the \"tbreak\" command which sets a breakpoint and enables it once.",
1289            &enablelist);
1290
1291   add_prefix_cmd ("disable", class_breakpoint, disable_command,
1292            "Disable some breakpoints or auto-display expressions.\n\
1293 Arguments are breakpoint numbers with spaces in between.\n\
1294 To disable all breakpoints, give no argument.\n\
1295 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
1296 \n\
1297 The \"display\" subcommand applies to auto-displays instead of breakpoints.",
1298                   &disablelist, "disable ", 1, &cmdlist);
1299   add_com_alias ("dis", "disable", class_breakpoint, 1);
1300
1301   add_abbrev_cmd ("breakpoints", class_breakpoint, disable_command,
1302            "Disable some breakpoints or auto-display expressions.\n\
1303 Arguments are breakpoint numbers with spaces in between.\n\
1304 To disable all breakpoints, give no argument.\n\
1305 A disabled breakpoint is not forgotten, but has no effect until reenabled.\n\
1306 This command may be abbreviated \"disable\".",
1307            &disablelist);
1308
1309   add_prefix_cmd ("delete", class_breakpoint, delete_command,
1310            "Delete some breakpoints or auto-display expressions.\n\
1311 Arguments are breakpoint numbers with spaces in between.\n\
1312 To delete all breakpoints, give no argument.\n\
1313 \n\
1314 Also a prefix command for deletion of other GDB objects.\n\
1315 The \"unset\" command is also an alias for \"delete\".",
1316                   &deletelist, "delete ", 1, &cmdlist);
1317   add_com_alias ("d", "delete", class_breakpoint, 1);
1318   add_com_alias ("unset", "delete", class_alias, 1);
1319
1320   add_cmd ("breakpoints", class_alias, delete_command,
1321            "Delete some breakpoints or auto-display expressions.\n\
1322 Arguments are breakpoint numbers with spaces in between.\n\
1323 To delete all breakpoints, give no argument.\n\
1324 This command may be abbreviated \"delete\".",
1325            &deletelist);
1326
1327   add_com ("clear", class_breakpoint, clear_command,
1328            "Clear breakpoint at specified line or function.\n\
1329 Argument may be line number, function name, or \"*\" and an address.\n\
1330 If line number is specified, all breakpoints in that line are cleared.\n\
1331 If function is specified, breakpoints at beginning of function are cleared.\n\
1332 If an address is specified, breakpoints at that address are cleared.\n\n\
1333 With no argument, clears all breakpoints in the line that the selected frame\n\
1334 is executing in.\n\
1335 \n\
1336 See also the \"delete\" command which clears breakpoints by number.");
1337
1338   add_com ("break", class_breakpoint, break_command,
1339            "Set breakpoint at specified line or function.\n\
1340 Argument may be line number, function name, or \"*\" and an address.\n\
1341 If line number is specified, break at start of code for that line.\n\
1342 If function is specified, break at start of code for that function.\n\
1343 If an address is specified, break at that exact address.\n\
1344 With no arg, uses current execution address of selected stack frame.\n\
1345 This is useful for breaking on return to a stack frame.\n\
1346 \n\
1347 Multiple breakpoints at one place are permitted, and useful if conditional.\n\
1348 \n\
1349 Do \"help breakpoints\" for info on other commands dealing with breakpoints.");
1350   add_com_alias ("b", "break", class_run, 1);
1351   add_com_alias ("br", "break", class_run, 1);
1352   add_com_alias ("bre", "break", class_run, 1);
1353   add_com_alias ("brea", "break", class_run, 1);
1354
1355   add_info ("breakpoints", breakpoints_info,
1356             "Status of all breakpoints, or breakpoint number NUMBER.\n\
1357 Second column is \"y\" for enabled breakpoint, \"n\" for disabled,\n\
1358 \"o\" for enabled once (disable when hit), \"d\" for enable but delete when hit.\n\
1359 Then come the address and the file/line number.\n\n\
1360 Convenience variable \"$_\" and default examine address for \"x\"\n\
1361 are set to the address of the last breakpoint listed.");
1362 }
1363