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