]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/record-btrace.c
record: upcase record_print_flag enumeration constants
[thirdparty/binutils-gdb.git] / gdb / record-btrace.c
1 /* Branch trace support for GDB, the GNU debugger.
2
3 Copyright (C) 2013 Free Software Foundation, Inc.
4
5 Contributed by Intel Corp. <markus.t.metzger@intel.com>
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "record.h"
24 #include "gdbthread.h"
25 #include "target.h"
26 #include "gdbcmd.h"
27 #include "disasm.h"
28 #include "observer.h"
29 #include "exceptions.h"
30 #include "cli/cli-utils.h"
31 #include "source.h"
32 #include "ui-out.h"
33 #include "symtab.h"
34 #include "filenames.h"
35
36 /* The target_ops of record-btrace. */
37 static struct target_ops record_btrace_ops;
38
39 /* A new thread observer enabling branch tracing for the new thread. */
40 static struct observer *record_btrace_thread_observer;
41
42 /* Print a record-btrace debug message. Use do ... while (0) to avoid
43 ambiguities when used in if statements. */
44
45 #define DEBUG(msg, args...) \
46 do \
47 { \
48 if (record_debug != 0) \
49 fprintf_unfiltered (gdb_stdlog, \
50 "[record-btrace] " msg "\n", ##args); \
51 } \
52 while (0)
53
54
55 /* Update the branch trace for the current thread and return a pointer to its
56 branch trace information struct.
57
58 Throws an error if there is no thread or no trace. This function never
59 returns NULL. */
60
61 static struct btrace_thread_info *
62 require_btrace (void)
63 {
64 struct thread_info *tp;
65 struct btrace_thread_info *btinfo;
66
67 DEBUG ("require");
68
69 tp = find_thread_ptid (inferior_ptid);
70 if (tp == NULL)
71 error (_("No thread."));
72
73 btrace_fetch (tp);
74
75 btinfo = &tp->btrace;
76
77 if (VEC_empty (btrace_inst_s, btinfo->itrace))
78 error (_("No trace."));
79
80 return btinfo;
81 }
82
83 /* Enable branch tracing for one thread. Warn on errors. */
84
85 static void
86 record_btrace_enable_warn (struct thread_info *tp)
87 {
88 volatile struct gdb_exception error;
89
90 TRY_CATCH (error, RETURN_MASK_ERROR)
91 btrace_enable (tp);
92
93 if (error.message != NULL)
94 warning ("%s", error.message);
95 }
96
97 /* Callback function to disable branch tracing for one thread. */
98
99 static void
100 record_btrace_disable_callback (void *arg)
101 {
102 struct thread_info *tp;
103
104 tp = arg;
105
106 btrace_disable (tp);
107 }
108
109 /* Enable automatic tracing of new threads. */
110
111 static void
112 record_btrace_auto_enable (void)
113 {
114 DEBUG ("attach thread observer");
115
116 record_btrace_thread_observer
117 = observer_attach_new_thread (record_btrace_enable_warn);
118 }
119
120 /* Disable automatic tracing of new threads. */
121
122 static void
123 record_btrace_auto_disable (void)
124 {
125 /* The observer may have been detached, already. */
126 if (record_btrace_thread_observer == NULL)
127 return;
128
129 DEBUG ("detach thread observer");
130
131 observer_detach_new_thread (record_btrace_thread_observer);
132 record_btrace_thread_observer = NULL;
133 }
134
135 /* The to_open method of target record-btrace. */
136
137 static void
138 record_btrace_open (char *args, int from_tty)
139 {
140 struct cleanup *disable_chain;
141 struct thread_info *tp;
142
143 DEBUG ("open");
144
145 if (RECORD_IS_USED)
146 error (_("The process is already being recorded."));
147
148 if (!target_has_execution)
149 error (_("The program is not being run."));
150
151 if (!target_supports_btrace ())
152 error (_("Target does not support branch tracing."));
153
154 gdb_assert (record_btrace_thread_observer == NULL);
155
156 disable_chain = make_cleanup (null_cleanup, NULL);
157 ALL_THREADS (tp)
158 if (args == NULL || *args == 0 || number_is_in_list (args, tp->num))
159 {
160 btrace_enable (tp);
161
162 make_cleanup (record_btrace_disable_callback, tp);
163 }
164
165 record_btrace_auto_enable ();
166
167 push_target (&record_btrace_ops);
168
169 observer_notify_record_changed (current_inferior (), 1);
170
171 discard_cleanups (disable_chain);
172 }
173
174 /* The to_stop_recording method of target record-btrace. */
175
176 static void
177 record_btrace_stop_recording (void)
178 {
179 struct thread_info *tp;
180
181 DEBUG ("stop recording");
182
183 record_btrace_auto_disable ();
184
185 ALL_THREADS (tp)
186 if (tp->btrace.target != NULL)
187 btrace_disable (tp);
188 }
189
190 /* The to_close method of target record-btrace. */
191
192 static void
193 record_btrace_close (void)
194 {
195 /* Make sure automatic recording gets disabled even if we did not stop
196 recording before closing the record-btrace target. */
197 record_btrace_auto_disable ();
198
199 /* We already stopped recording. */
200 }
201
202 /* The to_info_record method of target record-btrace. */
203
204 static void
205 record_btrace_info (void)
206 {
207 struct btrace_thread_info *btinfo;
208 struct thread_info *tp;
209 unsigned int insts, funcs;
210
211 DEBUG ("info");
212
213 tp = find_thread_ptid (inferior_ptid);
214 if (tp == NULL)
215 error (_("No thread."));
216
217 btrace_fetch (tp);
218
219 btinfo = &tp->btrace;
220 insts = VEC_length (btrace_inst_s, btinfo->itrace);
221 funcs = VEC_length (btrace_func_s, btinfo->ftrace);
222
223 printf_unfiltered (_("Recorded %u instructions in %u functions for thread "
224 "%d (%s).\n"), insts, funcs, tp->num,
225 target_pid_to_str (tp->ptid));
226 }
227
228 /* Print an unsigned int. */
229
230 static void
231 ui_out_field_uint (struct ui_out *uiout, const char *fld, unsigned int val)
232 {
233 ui_out_field_fmt (uiout, fld, "%u", val);
234 }
235
236 /* Disassemble a section of the recorded instruction trace. */
237
238 static void
239 btrace_insn_history (struct btrace_thread_info *btinfo, struct ui_out *uiout,
240 unsigned int begin, unsigned int end, int flags)
241 {
242 struct gdbarch *gdbarch;
243 struct btrace_inst *inst;
244 unsigned int idx;
245
246 DEBUG ("itrace (0x%x): [%u; %u[", flags, begin, end);
247
248 gdbarch = target_gdbarch ();
249
250 for (idx = begin; VEC_iterate (btrace_inst_s, btinfo->itrace, idx, inst)
251 && idx < end; ++idx)
252 {
253 /* Print the instruction index. */
254 ui_out_field_uint (uiout, "index", idx);
255 ui_out_text (uiout, "\t");
256
257 /* Disassembly with '/m' flag may not produce the expected result.
258 See PR gdb/11833. */
259 gdb_disassembly (gdbarch, uiout, NULL, flags, 1, inst->pc, inst->pc + 1);
260 }
261 }
262
263 /* The to_insn_history method of target record-btrace. */
264
265 static void
266 record_btrace_insn_history (int size, int flags)
267 {
268 struct btrace_thread_info *btinfo;
269 struct cleanup *uiout_cleanup;
270 struct ui_out *uiout;
271 unsigned int context, last, begin, end;
272
273 uiout = current_uiout;
274 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
275 "insn history");
276 btinfo = require_btrace ();
277 last = VEC_length (btrace_inst_s, btinfo->itrace);
278
279 context = abs (size);
280 begin = btinfo->insn_iterator.begin;
281 end = btinfo->insn_iterator.end;
282
283 DEBUG ("insn-history (0x%x): %d, prev: [%u; %u[", flags, size, begin, end);
284
285 if (context == 0)
286 error (_("Bad record instruction-history-size."));
287
288 /* We start at the end. */
289 if (end < begin)
290 {
291 /* Truncate the context, if necessary. */
292 context = min (context, last);
293
294 end = last;
295 begin = end - context;
296 }
297 else if (size < 0)
298 {
299 if (begin == 0)
300 {
301 printf_unfiltered (_("At the start of the branch trace record.\n"));
302
303 btinfo->insn_iterator.end = 0;
304 return;
305 }
306
307 /* Truncate the context, if necessary. */
308 context = min (context, begin);
309
310 end = begin;
311 begin -= context;
312 }
313 else
314 {
315 if (end == last)
316 {
317 printf_unfiltered (_("At the end of the branch trace record.\n"));
318
319 btinfo->insn_iterator.begin = last;
320 return;
321 }
322
323 /* Truncate the context, if necessary. */
324 context = min (context, last - end);
325
326 begin = end;
327 end += context;
328 }
329
330 btrace_insn_history (btinfo, uiout, begin, end, flags);
331
332 btinfo->insn_iterator.begin = begin;
333 btinfo->insn_iterator.end = end;
334
335 do_cleanups (uiout_cleanup);
336 }
337
338 /* The to_insn_history_range method of target record-btrace. */
339
340 static void
341 record_btrace_insn_history_range (ULONGEST from, ULONGEST to, int flags)
342 {
343 struct btrace_thread_info *btinfo;
344 struct cleanup *uiout_cleanup;
345 struct ui_out *uiout;
346 unsigned int last, begin, end;
347
348 uiout = current_uiout;
349 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
350 "insn history");
351 btinfo = require_btrace ();
352 last = VEC_length (btrace_inst_s, btinfo->itrace);
353
354 begin = (unsigned int) from;
355 end = (unsigned int) to;
356
357 DEBUG ("insn-history (0x%x): [%u; %u[", flags, begin, end);
358
359 /* Check for wrap-arounds. */
360 if (begin != from || end != to)
361 error (_("Bad range."));
362
363 if (end <= begin)
364 error (_("Bad range."));
365
366 if (last <= begin)
367 error (_("Range out of bounds."));
368
369 /* Truncate the range, if necessary. */
370 if (last < end)
371 end = last;
372
373 btrace_insn_history (btinfo, uiout, begin, end, flags);
374
375 btinfo->insn_iterator.begin = begin;
376 btinfo->insn_iterator.end = end;
377
378 do_cleanups (uiout_cleanup);
379 }
380
381 /* The to_insn_history_from method of target record-btrace. */
382
383 static void
384 record_btrace_insn_history_from (ULONGEST from, int size, int flags)
385 {
386 ULONGEST begin, end, context;
387
388 context = abs (size);
389
390 if (size < 0)
391 {
392 end = from;
393
394 if (from < context)
395 begin = 0;
396 else
397 begin = from - context;
398 }
399 else
400 {
401 begin = from;
402 end = from + context;
403
404 /* Check for wrap-around. */
405 if (end < begin)
406 end = ULONGEST_MAX;
407 }
408
409 record_btrace_insn_history_range (begin, end, flags);
410 }
411
412 /* Print the instruction number range for a function call history line. */
413
414 static void
415 btrace_func_history_insn_range (struct ui_out *uiout, struct btrace_func *bfun)
416 {
417 ui_out_field_uint (uiout, "insn begin", bfun->ibegin);
418
419 if (bfun->ibegin == bfun->iend)
420 return;
421
422 ui_out_text (uiout, "-");
423 ui_out_field_uint (uiout, "insn end", bfun->iend);
424 }
425
426 /* Print the source line information for a function call history line. */
427
428 static void
429 btrace_func_history_src_line (struct ui_out *uiout, struct btrace_func *bfun)
430 {
431 struct symbol *sym;
432
433 sym = bfun->sym;
434 if (sym == NULL)
435 return;
436
437 ui_out_field_string (uiout, "file",
438 symtab_to_filename_for_display (sym->symtab));
439
440 if (bfun->lend == 0)
441 return;
442
443 ui_out_text (uiout, ":");
444 ui_out_field_int (uiout, "min line", bfun->lbegin);
445
446 if (bfun->lend == bfun->lbegin)
447 return;
448
449 ui_out_text (uiout, "-");
450 ui_out_field_int (uiout, "max line", bfun->lend);
451 }
452
453 /* Disassemble a section of the recorded function trace. */
454
455 static void
456 btrace_func_history (struct btrace_thread_info *btinfo, struct ui_out *uiout,
457 unsigned int begin, unsigned int end,
458 enum record_print_flag flags)
459 {
460 struct btrace_func *bfun;
461 unsigned int idx;
462
463 DEBUG ("ftrace (0x%x): [%u; %u[", flags, begin, end);
464
465 for (idx = begin; VEC_iterate (btrace_func_s, btinfo->ftrace, idx, bfun)
466 && idx < end; ++idx)
467 {
468 /* Print the function index. */
469 ui_out_field_uint (uiout, "index", idx);
470 ui_out_text (uiout, "\t");
471
472 if ((flags & RECORD_PRINT_INSN_RANGE) != 0)
473 {
474 btrace_func_history_insn_range (uiout, bfun);
475 ui_out_text (uiout, "\t");
476 }
477
478 if ((flags & RECORD_PRINT_SRC_LINE) != 0)
479 {
480 btrace_func_history_src_line (uiout, bfun);
481 ui_out_text (uiout, "\t");
482 }
483
484 if (bfun->sym != NULL)
485 ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (bfun->sym));
486 else if (bfun->msym != NULL)
487 ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (bfun->msym));
488 ui_out_text (uiout, "\n");
489 }
490 }
491
492 /* The to_call_history method of target record-btrace. */
493
494 static void
495 record_btrace_call_history (int size, int flags)
496 {
497 struct btrace_thread_info *btinfo;
498 struct cleanup *uiout_cleanup;
499 struct ui_out *uiout;
500 unsigned int context, last, begin, end;
501
502 uiout = current_uiout;
503 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
504 "insn history");
505 btinfo = require_btrace ();
506 last = VEC_length (btrace_func_s, btinfo->ftrace);
507
508 context = abs (size);
509 begin = btinfo->func_iterator.begin;
510 end = btinfo->func_iterator.end;
511
512 DEBUG ("func-history (0x%x): %d, prev: [%u; %u[", flags, size, begin, end);
513
514 if (context == 0)
515 error (_("Bad record function-call-history-size."));
516
517 /* We start at the end. */
518 if (end < begin)
519 {
520 /* Truncate the context, if necessary. */
521 context = min (context, last);
522
523 end = last;
524 begin = end - context;
525 }
526 else if (size < 0)
527 {
528 if (begin == 0)
529 {
530 printf_unfiltered (_("At the start of the branch trace record.\n"));
531
532 btinfo->func_iterator.end = 0;
533 return;
534 }
535
536 /* Truncate the context, if necessary. */
537 context = min (context, begin);
538
539 end = begin;
540 begin -= context;
541 }
542 else
543 {
544 if (end == last)
545 {
546 printf_unfiltered (_("At the end of the branch trace record.\n"));
547
548 btinfo->func_iterator.begin = last;
549 return;
550 }
551
552 /* Truncate the context, if necessary. */
553 context = min (context, last - end);
554
555 begin = end;
556 end += context;
557 }
558
559 btrace_func_history (btinfo, uiout, begin, end, flags);
560
561 btinfo->func_iterator.begin = begin;
562 btinfo->func_iterator.end = end;
563
564 do_cleanups (uiout_cleanup);
565 }
566
567 /* The to_call_history_range method of target record-btrace. */
568
569 static void
570 record_btrace_call_history_range (ULONGEST from, ULONGEST to, int flags)
571 {
572 struct btrace_thread_info *btinfo;
573 struct cleanup *uiout_cleanup;
574 struct ui_out *uiout;
575 unsigned int last, begin, end;
576
577 uiout = current_uiout;
578 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
579 "func history");
580 btinfo = require_btrace ();
581 last = VEC_length (btrace_func_s, btinfo->ftrace);
582
583 begin = (unsigned int) from;
584 end = (unsigned int) to;
585
586 DEBUG ("func-history (0x%x): [%u; %u[", flags, begin, end);
587
588 /* Check for wrap-arounds. */
589 if (begin != from || end != to)
590 error (_("Bad range."));
591
592 if (end <= begin)
593 error (_("Bad range."));
594
595 if (last <= begin)
596 error (_("Range out of bounds."));
597
598 /* Truncate the range, if necessary. */
599 if (last < end)
600 end = last;
601
602 btrace_func_history (btinfo, uiout, begin, end, flags);
603
604 btinfo->func_iterator.begin = begin;
605 btinfo->func_iterator.end = end;
606
607 do_cleanups (uiout_cleanup);
608 }
609
610 /* The to_call_history_from method of target record-btrace. */
611
612 static void
613 record_btrace_call_history_from (ULONGEST from, int size, int flags)
614 {
615 ULONGEST begin, end, context;
616
617 context = abs (size);
618
619 if (size < 0)
620 {
621 end = from;
622
623 if (from < context)
624 begin = 0;
625 else
626 begin = from - context;
627 }
628 else
629 {
630 begin = from;
631 end = from + context;
632
633 /* Check for wrap-around. */
634 if (end < begin)
635 end = ULONGEST_MAX;
636 }
637
638 record_btrace_call_history_range (begin, end, flags);
639 }
640
641 /* Initialize the record-btrace target ops. */
642
643 static void
644 init_record_btrace_ops (void)
645 {
646 struct target_ops *ops;
647
648 ops = &record_btrace_ops;
649 ops->to_shortname = "record-btrace";
650 ops->to_longname = "Branch tracing target";
651 ops->to_doc = "Collect control-flow trace and provide the execution history.";
652 ops->to_open = record_btrace_open;
653 ops->to_close = record_btrace_close;
654 ops->to_detach = record_detach;
655 ops->to_disconnect = record_disconnect;
656 ops->to_mourn_inferior = record_mourn_inferior;
657 ops->to_kill = record_kill;
658 ops->to_create_inferior = find_default_create_inferior;
659 ops->to_stop_recording = record_btrace_stop_recording;
660 ops->to_info_record = record_btrace_info;
661 ops->to_insn_history = record_btrace_insn_history;
662 ops->to_insn_history_from = record_btrace_insn_history_from;
663 ops->to_insn_history_range = record_btrace_insn_history_range;
664 ops->to_call_history = record_btrace_call_history;
665 ops->to_call_history_from = record_btrace_call_history_from;
666 ops->to_call_history_range = record_btrace_call_history_range;
667 ops->to_stratum = record_stratum;
668 ops->to_magic = OPS_MAGIC;
669 }
670
671 /* Alias for "target record". */
672
673 static void
674 cmd_record_btrace_start (char *args, int from_tty)
675 {
676 if (args != NULL && *args != 0)
677 error (_("Invalid argument."));
678
679 execute_command ("target record-btrace", from_tty);
680 }
681
682 void _initialize_record_btrace (void);
683
684 /* Initialize btrace commands. */
685
686 void
687 _initialize_record_btrace (void)
688 {
689 add_cmd ("btrace", class_obscure, cmd_record_btrace_start,
690 _("Start branch trace recording."),
691 &record_cmdlist);
692 add_alias_cmd ("b", "btrace", class_obscure, 1, &record_cmdlist);
693
694 init_record_btrace_ops ();
695 add_target (&record_btrace_ops);
696 }