1 /* Annotation routines for GDB.
2    Copyright (C) 1986-2024 Free Software Foundation, Inc.
3 
4    This file is part of GDB.
5 
6    This program 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 3 of the License, or
9    (at your option) any 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, see <http://www.gnu.org/licenses/>.  */
18 
19 #include "annotate.h"
20 #include "value.h"
21 #include "target.h"
22 #include "gdbtypes.h"
23 #include "breakpoint.h"
24 #include "observable.h"
25 #include "inferior.h"
26 #include "infrun.h"
27 #include "source.h"
28 #include "objfiles.h"
29 #include "source-cache.h"
30 #include "ui.h"
31 
32 /* See annotate.h.  */
33 
34 int annotation_level = 0;
35 
36 /* Prototypes for local functions.  */
37 
38 static void print_value_flags (struct type *);
39 
40 static void breakpoint_changed (struct breakpoint *b);
41 
42 
43 void (*deprecated_annotate_signalled_hook) (void);
44 void (*deprecated_annotate_signal_hook) (void);
45 
46 /* Booleans indicating whether we've emitted certain notifications.
47    Used to suppress useless repeated notifications until the next time
48    we're ready to accept more commands.  Reset whenever a prompt is
49    displayed.  */
50 static int frames_invalid_emitted;
51 static int breakpoints_invalid_emitted;
52 
53 static void
print_value_flags(struct type * t)54 print_value_flags (struct type *t)
55 {
56   if (can_dereference (t))
57     printf_unfiltered (("*"));
58   else
59     printf_unfiltered (("-"));
60 }
61 
62 static void
annotate_breakpoints_invalid(void)63 annotate_breakpoints_invalid (void)
64 {
65   if (annotation_level == 2
66       && (!breakpoints_invalid_emitted
67             || current_ui->prompt_state != PROMPT_BLOCKED))
68     {
69       target_terminal::scoped_restore_terminal_state term_state;
70       target_terminal::ours_for_output ();
71 
72       printf_unfiltered (("\n\032\032breakpoints-invalid\n"));
73       breakpoints_invalid_emitted = 1;
74     }
75 }
76 
77 void
annotate_breakpoint(int num)78 annotate_breakpoint (int num)
79 {
80   if (annotation_level > 1)
81     printf_unfiltered (("\n\032\032breakpoint %d\n"), num);
82 }
83 
84 void
annotate_catchpoint(int num)85 annotate_catchpoint (int num)
86 {
87   if (annotation_level > 1)
88     printf_unfiltered (("\n\032\032catchpoint %d\n"), num);
89 }
90 
91 void
annotate_watchpoint(int num)92 annotate_watchpoint (int num)
93 {
94   if (annotation_level > 1)
95     printf_unfiltered (("\n\032\032watchpoint %d\n"), num);
96 }
97 
98 void
annotate_starting(void)99 annotate_starting (void)
100 {
101   if (annotation_level > 1)
102     printf_unfiltered (("\n\032\032starting\n"));
103 }
104 
105 void
annotate_stopped(void)106 annotate_stopped (void)
107 {
108   if (annotation_level > 1)
109     printf_unfiltered (("\n\032\032stopped\n"));
110 }
111 
112 void
annotate_exited(int exitstatus)113 annotate_exited (int exitstatus)
114 {
115   if (annotation_level > 1)
116     printf_unfiltered (("\n\032\032exited %d\n"), exitstatus);
117 }
118 
119 void
annotate_signalled(void)120 annotate_signalled (void)
121 {
122   if (deprecated_annotate_signalled_hook)
123     deprecated_annotate_signalled_hook ();
124 
125   if (annotation_level > 1)
126     printf_unfiltered (("\n\032\032signalled\n"));
127 }
128 
129 void
annotate_signal_name(void)130 annotate_signal_name (void)
131 {
132   if (annotation_level == 2)
133     printf_unfiltered (("\n\032\032signal-name\n"));
134 }
135 
136 void
annotate_signal_name_end(void)137 annotate_signal_name_end (void)
138 {
139   if (annotation_level == 2)
140     printf_unfiltered (("\n\032\032signal-name-end\n"));
141 }
142 
143 void
annotate_signal_string(void)144 annotate_signal_string (void)
145 {
146   if (annotation_level == 2)
147     printf_unfiltered (("\n\032\032signal-string\n"));
148 }
149 
150 void
annotate_signal_string_end(void)151 annotate_signal_string_end (void)
152 {
153   if (annotation_level == 2)
154     printf_unfiltered (("\n\032\032signal-string-end\n"));
155 }
156 
157 void
annotate_signal(void)158 annotate_signal (void)
159 {
160   if (deprecated_annotate_signal_hook)
161     deprecated_annotate_signal_hook ();
162 
163   if (annotation_level > 1)
164     printf_unfiltered (("\n\032\032signal\n"));
165 }
166 
167 void
annotate_breakpoints_headers(void)168 annotate_breakpoints_headers (void)
169 {
170   if (annotation_level == 2)
171     printf_unfiltered (("\n\032\032breakpoints-headers\n"));
172 }
173 
174 void
annotate_field(int num)175 annotate_field (int num)
176 {
177   if (annotation_level == 2)
178     printf_unfiltered (("\n\032\032field %d\n"), num);
179 }
180 
181 void
annotate_breakpoints_table(void)182 annotate_breakpoints_table (void)
183 {
184   if (annotation_level == 2)
185     printf_unfiltered (("\n\032\032breakpoints-table\n"));
186 }
187 
188 void
annotate_record(void)189 annotate_record (void)
190 {
191   if (annotation_level == 2)
192     printf_unfiltered (("\n\032\032record\n"));
193 }
194 
195 void
annotate_breakpoints_table_end(void)196 annotate_breakpoints_table_end (void)
197 {
198   if (annotation_level == 2)
199     printf_unfiltered (("\n\032\032breakpoints-table-end\n"));
200 }
201 
202 void
annotate_frames_invalid(void)203 annotate_frames_invalid (void)
204 {
205   if (annotation_level == 2
206       && (!frames_invalid_emitted
207             || current_ui->prompt_state != PROMPT_BLOCKED))
208     {
209       target_terminal::scoped_restore_terminal_state term_state;
210       target_terminal::ours_for_output ();
211 
212       printf_unfiltered (("\n\032\032frames-invalid\n"));
213       frames_invalid_emitted = 1;
214     }
215 }
216 
217 void
annotate_new_thread(void)218 annotate_new_thread (void)
219 {
220   if (annotation_level > 1)
221     {
222       printf_unfiltered (("\n\032\032new-thread\n"));
223     }
224 }
225 
226 void
annotate_thread_changed(void)227 annotate_thread_changed (void)
228 {
229   if (annotation_level > 1)
230     {
231       printf_unfiltered (("\n\032\032thread-changed\n"));
232     }
233 }
234 
235 /* Emit notification on thread exit.  */
236 
237 static void
annotate_thread_exited(thread_info * t,std::optional<ULONGEST> exit_code,bool)238 annotate_thread_exited (thread_info *t,
239                               std::optional<ULONGEST> exit_code,
240                               bool /* silent */)
241 {
242   if (annotation_level > 1)
243     {
244       printf_unfiltered (("\n\032\032thread-exited,"
245                                 "id=\"%d\",group-id=\"i%d\"\n"),
246                                t->global_num, t->inf->num);
247     }
248 }
249 
250 void
annotate_field_begin(struct type * type)251 annotate_field_begin (struct type *type)
252 {
253   if (annotation_level == 2)
254     {
255       printf_unfiltered (("\n\032\032field-begin "));
256       print_value_flags (type);
257       printf_unfiltered (("\n"));
258     }
259 }
260 
261 void
annotate_field_name_end(void)262 annotate_field_name_end (void)
263 {
264   if (annotation_level == 2)
265     printf_unfiltered (("\n\032\032field-name-end\n"));
266 }
267 
268 void
annotate_field_value(void)269 annotate_field_value (void)
270 {
271   if (annotation_level == 2)
272     printf_unfiltered (("\n\032\032field-value\n"));
273 }
274 
275 void
annotate_field_end(void)276 annotate_field_end (void)
277 {
278   if (annotation_level == 2)
279     printf_unfiltered (("\n\032\032field-end\n"));
280 }
281 
282 void
annotate_quit(void)283 annotate_quit (void)
284 {
285   if (annotation_level > 1)
286     printf_unfiltered (("\n\032\032quit\n"));
287 }
288 
289 void
annotate_error(void)290 annotate_error (void)
291 {
292   if (annotation_level > 1)
293     printf_unfiltered (("\n\032\032error\n"));
294 }
295 
296 void
annotate_error_begin(void)297 annotate_error_begin (void)
298 {
299   if (annotation_level > 1)
300     gdb_printf (gdb_stderr, "\n\032\032error-begin\n");
301 }
302 
303 void
annotate_value_history_begin(int histindex,struct type * type)304 annotate_value_history_begin (int histindex, struct type *type)
305 {
306   if (annotation_level == 2)
307     {
308       printf_unfiltered (("\n\032\032value-history-begin %d "), histindex);
309       print_value_flags (type);
310       printf_unfiltered (("\n"));
311     }
312 }
313 
314 void
annotate_value_begin(struct type * type)315 annotate_value_begin (struct type *type)
316 {
317   if (annotation_level == 2)
318     {
319       printf_unfiltered (("\n\032\032value-begin "));
320       print_value_flags (type);
321       printf_unfiltered (("\n"));
322     }
323 }
324 
325 void
annotate_value_history_value(void)326 annotate_value_history_value (void)
327 {
328   if (annotation_level == 2)
329     printf_unfiltered (("\n\032\032value-history-value\n"));
330 }
331 
332 void
annotate_value_history_end(void)333 annotate_value_history_end (void)
334 {
335   if (annotation_level == 2)
336     printf_unfiltered (("\n\032\032value-history-end\n"));
337 }
338 
339 void
annotate_value_end(void)340 annotate_value_end (void)
341 {
342   if (annotation_level == 2)
343     printf_unfiltered (("\n\032\032value-end\n"));
344 }
345 
346 void
annotate_display_begin(void)347 annotate_display_begin (void)
348 {
349   if (annotation_level == 2)
350     printf_unfiltered (("\n\032\032display-begin\n"));
351 }
352 
353 void
annotate_display_number_end(void)354 annotate_display_number_end (void)
355 {
356   if (annotation_level == 2)
357     printf_unfiltered (("\n\032\032display-number-end\n"));
358 }
359 
360 void
annotate_display_format(void)361 annotate_display_format (void)
362 {
363   if (annotation_level == 2)
364     printf_unfiltered (("\n\032\032display-format\n"));
365 }
366 
367 void
annotate_display_expression(void)368 annotate_display_expression (void)
369 {
370   if (annotation_level == 2)
371     printf_unfiltered (("\n\032\032display-expression\n"));
372 }
373 
374 void
annotate_display_expression_end(void)375 annotate_display_expression_end (void)
376 {
377   if (annotation_level == 2)
378     printf_unfiltered (("\n\032\032display-expression-end\n"));
379 }
380 
381 void
annotate_display_value(void)382 annotate_display_value (void)
383 {
384   if (annotation_level == 2)
385     printf_unfiltered (("\n\032\032display-value\n"));
386 }
387 
388 void
annotate_display_end(void)389 annotate_display_end (void)
390 {
391   if (annotation_level == 2)
392     printf_unfiltered (("\n\032\032display-end\n"));
393 }
394 
395 void
annotate_arg_begin(void)396 annotate_arg_begin (void)
397 {
398   if (annotation_level == 2)
399     printf_unfiltered (("\n\032\032arg-begin\n"));
400 }
401 
402 void
annotate_arg_name_end(void)403 annotate_arg_name_end (void)
404 {
405   if (annotation_level == 2)
406     printf_unfiltered (("\n\032\032arg-name-end\n"));
407 }
408 
409 void
annotate_arg_value(struct type * type)410 annotate_arg_value (struct type *type)
411 {
412   if (annotation_level == 2)
413     {
414       printf_unfiltered (("\n\032\032arg-value "));
415       print_value_flags (type);
416       printf_unfiltered (("\n"));
417     }
418 }
419 
420 void
annotate_arg_end(void)421 annotate_arg_end (void)
422 {
423   if (annotation_level == 2)
424     printf_unfiltered (("\n\032\032arg-end\n"));
425 }
426 
427 static void
annotate_source(const char * filename,int line,int character,int mid,struct gdbarch * gdbarch,CORE_ADDR pc)428 annotate_source (const char *filename, int line, int character, int mid,
429                      struct gdbarch *gdbarch, CORE_ADDR pc)
430 {
431   if (annotation_level > 1)
432     printf_unfiltered (("\n\032\032source "));
433   else
434     printf_unfiltered (("\032\032"));
435 
436   printf_unfiltered (("%s:%d:%d:%s:%s\n"), filename, line, character,
437                          mid ? "middle" : "beg", paddress (gdbarch, pc));
438 }
439 
440 /* See annotate.h.  */
441 
442 bool
annotate_source_line(struct symtab * s,int line,int mid_statement,CORE_ADDR pc)443 annotate_source_line (struct symtab *s, int line, int mid_statement,
444                           CORE_ADDR pc)
445 {
446   if (annotation_level > 0)
447     {
448       const std::vector<off_t> *offsets;
449       if (!g_source_cache.get_line_charpos (s, &offsets))
450           return false;
451       if (line > offsets->size ())
452           return false;
453 
454       annotate_source (s->fullname, line, (int) (*offsets)[line - 1],
455                            mid_statement, s->compunit ()->objfile ()->arch (),
456                            pc);
457 
458       /* Update the current symtab and line.  */
459       symtab_and_line sal;
460       sal.pspace = s->compunit ()->objfile ()->pspace;
461       sal.symtab = s;
462       sal.line = line;
463       set_current_source_symtab_and_line (sal);
464 
465       return true;
466     }
467 
468   return false;
469 }
470 
471 
472 void
annotate_frame_begin(int level,struct gdbarch * gdbarch,CORE_ADDR pc)473 annotate_frame_begin (int level, struct gdbarch *gdbarch, CORE_ADDR pc)
474 {
475   if (annotation_level > 1)
476     printf_unfiltered (("\n\032\032frame-begin %d %s\n"),
477                            level, paddress (gdbarch, pc));
478 }
479 
480 void
annotate_function_call(void)481 annotate_function_call (void)
482 {
483   if (annotation_level == 2)
484     printf_unfiltered (("\n\032\032function-call\n"));
485 }
486 
487 void
annotate_signal_handler_caller(void)488 annotate_signal_handler_caller (void)
489 {
490   if (annotation_level == 2)
491     printf_unfiltered (("\n\032\032signal-handler-caller\n"));
492 }
493 
494 void
annotate_frame_address(void)495 annotate_frame_address (void)
496 {
497   if (annotation_level == 2)
498     printf_unfiltered (("\n\032\032frame-address\n"));
499 }
500 
501 void
annotate_frame_address_end(void)502 annotate_frame_address_end (void)
503 {
504   if (annotation_level == 2)
505     printf_unfiltered (("\n\032\032frame-address-end\n"));
506 }
507 
508 void
annotate_frame_function_name(void)509 annotate_frame_function_name (void)
510 {
511   if (annotation_level == 2)
512     printf_unfiltered (("\n\032\032frame-function-name\n"));
513 }
514 
515 void
annotate_frame_args(void)516 annotate_frame_args (void)
517 {
518   if (annotation_level == 2)
519     printf_unfiltered (("\n\032\032frame-args\n"));
520 }
521 
522 void
annotate_frame_source_begin(void)523 annotate_frame_source_begin (void)
524 {
525   if (annotation_level == 2)
526     printf_unfiltered (("\n\032\032frame-source-begin\n"));
527 }
528 
529 void
annotate_frame_source_file(void)530 annotate_frame_source_file (void)
531 {
532   if (annotation_level == 2)
533     printf_unfiltered (("\n\032\032frame-source-file\n"));
534 }
535 
536 void
annotate_frame_source_file_end(void)537 annotate_frame_source_file_end (void)
538 {
539   if (annotation_level == 2)
540     printf_unfiltered (("\n\032\032frame-source-file-end\n"));
541 }
542 
543 void
annotate_frame_source_line(void)544 annotate_frame_source_line (void)
545 {
546   if (annotation_level == 2)
547     printf_unfiltered (("\n\032\032frame-source-line\n"));
548 }
549 
550 void
annotate_frame_source_end(void)551 annotate_frame_source_end (void)
552 {
553   if (annotation_level == 2)
554     printf_unfiltered (("\n\032\032frame-source-end\n"));
555 }
556 
557 void
annotate_frame_where(void)558 annotate_frame_where (void)
559 {
560   if (annotation_level == 2)
561     printf_unfiltered (("\n\032\032frame-where\n"));
562 }
563 
564 void
annotate_frame_end(void)565 annotate_frame_end (void)
566 {
567   if (annotation_level == 2)
568     printf_unfiltered (("\n\032\032frame-end\n"));
569 }
570 
571 void
annotate_array_section_begin(int idx,struct type * elttype)572 annotate_array_section_begin (int idx, struct type *elttype)
573 {
574   if (annotation_level == 2)
575     {
576       printf_unfiltered (("\n\032\032array-section-begin %d "), idx);
577       print_value_flags (elttype);
578       printf_unfiltered (("\n"));
579     }
580 }
581 
582 void
annotate_elt_rep(unsigned int repcount)583 annotate_elt_rep (unsigned int repcount)
584 {
585   if (annotation_level == 2)
586     printf_unfiltered (("\n\032\032elt-rep %u\n"), repcount);
587 }
588 
589 void
annotate_elt_rep_end(void)590 annotate_elt_rep_end (void)
591 {
592   if (annotation_level == 2)
593     printf_unfiltered (("\n\032\032elt-rep-end\n"));
594 }
595 
596 void
annotate_elt(void)597 annotate_elt (void)
598 {
599   if (annotation_level == 2)
600     printf_unfiltered (("\n\032\032elt\n"));
601 }
602 
603 void
annotate_array_section_end(void)604 annotate_array_section_end (void)
605 {
606   if (annotation_level == 2)
607     printf_unfiltered (("\n\032\032array-section-end\n"));
608 }
609 
610 /* Called when GDB is about to display the prompt.  Used to reset
611    annotation suppression whenever we're ready to accept new
612    frontend/user commands.  */
613 
614 void
annotate_display_prompt(void)615 annotate_display_prompt (void)
616 {
617   frames_invalid_emitted = 0;
618   breakpoints_invalid_emitted = 0;
619 }
620 
621 static void
breakpoint_changed(struct breakpoint * b)622 breakpoint_changed (struct breakpoint *b)
623 {
624   if (b->number <= 0)
625     return;
626 
627   annotate_breakpoints_invalid ();
628 }
629 
630 void _initialize_annotate ();
631 void
_initialize_annotate()632 _initialize_annotate ()
633 {
634   gdb::observers::breakpoint_created.attach (breakpoint_changed, "annotate");
635   gdb::observers::breakpoint_deleted.attach (breakpoint_changed, "annotate");
636   gdb::observers::breakpoint_modified.attach (breakpoint_changed, "annotate");
637   gdb::observers::thread_exit.attach (annotate_thread_exited, "annotate");
638 }
639