merge from gcc
[gdb/gnu.git] / gdb / record-btrace.c
blob68f40c859ae4f971cee181cef0fc16050d007d20
1 /* Branch trace support for GDB, the GNU debugger.
3 Copyright (C) 2013 Free Software Foundation, Inc.
5 Contributed by Intel Corp. <markus.t.metzger@intel.com>
7 This file is part of GDB.
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.
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.
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/>. */
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"
36 /* The target_ops of record-btrace. */
37 static struct target_ops record_btrace_ops;
39 /* A new thread observer enabling branch tracing for the new thread. */
40 static struct observer *record_btrace_thread_observer;
42 /* Print a record-btrace debug message. Use do ... while (0) to avoid
43 ambiguities when used in if statements. */
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)
55 /* Update the branch trace for the current thread and return a pointer to its
56 branch trace information struct.
58 Throws an error if there is no thread or no trace. This function never
59 returns NULL. */
61 static struct btrace_thread_info *
62 require_btrace (void)
64 struct thread_info *tp;
65 struct btrace_thread_info *btinfo;
67 DEBUG ("require");
69 tp = find_thread_ptid (inferior_ptid);
70 if (tp == NULL)
71 error (_("No thread."));
73 btrace_fetch (tp);
75 btinfo = &tp->btrace;
77 if (VEC_empty (btrace_inst_s, btinfo->itrace))
78 error (_("No trace."));
80 return btinfo;
83 /* Enable branch tracing for one thread. Warn on errors. */
85 static void
86 record_btrace_enable_warn (struct thread_info *tp)
88 volatile struct gdb_exception error;
90 TRY_CATCH (error, RETURN_MASK_ERROR)
91 btrace_enable (tp);
93 if (error.message != NULL)
94 warning ("%s", error.message);
97 /* Callback function to disable branch tracing for one thread. */
99 static void
100 record_btrace_disable_callback (void *arg)
102 struct thread_info *tp;
104 tp = arg;
106 btrace_disable (tp);
109 /* Enable automatic tracing of new threads. */
111 static void
112 record_btrace_auto_enable (void)
114 DEBUG ("attach thread observer");
116 record_btrace_thread_observer
117 = observer_attach_new_thread (record_btrace_enable_warn);
120 /* Disable automatic tracing of new threads. */
122 static void
123 record_btrace_auto_disable (void)
125 /* The observer may have been detached, already. */
126 if (record_btrace_thread_observer == NULL)
127 return;
129 DEBUG ("detach thread observer");
131 observer_detach_new_thread (record_btrace_thread_observer);
132 record_btrace_thread_observer = NULL;
135 /* The to_open method of target record-btrace. */
137 static void
138 record_btrace_open (char *args, int from_tty)
140 struct cleanup *disable_chain;
141 struct thread_info *tp;
143 DEBUG ("open");
145 if (RECORD_IS_USED)
146 error (_("The process is already being recorded."));
148 if (!target_has_execution)
149 error (_("The program is not being run."));
151 if (!target_supports_btrace ())
152 error (_("Target does not support branch tracing."));
154 gdb_assert (record_btrace_thread_observer == NULL);
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))
160 btrace_enable (tp);
162 make_cleanup (record_btrace_disable_callback, tp);
165 record_btrace_auto_enable ();
167 push_target (&record_btrace_ops);
169 observer_notify_record_changed (current_inferior (), 1);
171 discard_cleanups (disable_chain);
174 /* The to_stop_recording method of target record-btrace. */
176 static void
177 record_btrace_stop_recording (void)
179 struct thread_info *tp;
181 DEBUG ("stop recording");
183 record_btrace_auto_disable ();
185 ALL_THREADS (tp)
186 if (tp->btrace.target != NULL)
187 btrace_disable (tp);
190 /* The to_close method of target record-btrace. */
192 static void
193 record_btrace_close (void)
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 ();
199 /* We already stopped recording. */
202 /* The to_info_record method of target record-btrace. */
204 static void
205 record_btrace_info (void)
207 struct btrace_thread_info *btinfo;
208 struct thread_info *tp;
209 unsigned int insts, funcs;
211 DEBUG ("info");
213 tp = find_thread_ptid (inferior_ptid);
214 if (tp == NULL)
215 error (_("No thread."));
217 btrace_fetch (tp);
219 btinfo = &tp->btrace;
220 insts = VEC_length (btrace_inst_s, btinfo->itrace);
221 funcs = VEC_length (btrace_func_s, btinfo->ftrace);
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));
228 /* Print an unsigned int. */
230 static void
231 ui_out_field_uint (struct ui_out *uiout, const char *fld, unsigned int val)
233 ui_out_field_fmt (uiout, fld, "%u", val);
236 /* Disassemble a section of the recorded instruction trace. */
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)
242 struct gdbarch *gdbarch;
243 struct btrace_inst *inst;
244 unsigned int idx;
246 DEBUG ("itrace (0x%x): [%u; %u[", flags, begin, end);
248 gdbarch = target_gdbarch ();
250 for (idx = begin; VEC_iterate (btrace_inst_s, btinfo->itrace, idx, inst)
251 && idx < end; ++idx)
253 /* Print the instruction index. */
254 ui_out_field_uint (uiout, "index", idx);
255 ui_out_text (uiout, "\t");
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);
263 /* The to_insn_history method of target record-btrace. */
265 static void
266 record_btrace_insn_history (int size, int flags)
268 struct btrace_thread_info *btinfo;
269 struct cleanup *uiout_cleanup;
270 struct ui_out *uiout;
271 unsigned int context, last, begin, end;
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);
279 context = abs (size);
280 begin = btinfo->insn_iterator.begin;
281 end = btinfo->insn_iterator.end;
283 DEBUG ("insn-history (0x%x): %d, prev: [%u; %u[", flags, size, begin, end);
285 if (context == 0)
286 error (_("Bad record instruction-history-size."));
288 /* We start at the end. */
289 if (end < begin)
291 /* Truncate the context, if necessary. */
292 context = min (context, last);
294 end = last;
295 begin = end - context;
297 else if (size < 0)
299 if (begin == 0)
301 printf_unfiltered (_("At the start of the branch trace record.\n"));
303 btinfo->insn_iterator.end = 0;
304 return;
307 /* Truncate the context, if necessary. */
308 context = min (context, begin);
310 end = begin;
311 begin -= context;
313 else
315 if (end == last)
317 printf_unfiltered (_("At the end of the branch trace record.\n"));
319 btinfo->insn_iterator.begin = last;
320 return;
323 /* Truncate the context, if necessary. */
324 context = min (context, last - end);
326 begin = end;
327 end += context;
330 btrace_insn_history (btinfo, uiout, begin, end, flags);
332 btinfo->insn_iterator.begin = begin;
333 btinfo->insn_iterator.end = end;
335 do_cleanups (uiout_cleanup);
338 /* The to_insn_history_range method of target record-btrace. */
340 static void
341 record_btrace_insn_history_range (ULONGEST from, ULONGEST to, int flags)
343 struct btrace_thread_info *btinfo;
344 struct cleanup *uiout_cleanup;
345 struct ui_out *uiout;
346 unsigned int last, begin, end;
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);
354 begin = (unsigned int) from;
355 end = (unsigned int) to;
357 DEBUG ("insn-history (0x%x): [%u; %u[", flags, begin, end);
359 /* Check for wrap-arounds. */
360 if (begin != from || end != to)
361 error (_("Bad range."));
363 if (end <= begin)
364 error (_("Bad range."));
366 if (last <= begin)
367 error (_("Range out of bounds."));
369 /* Truncate the range, if necessary. */
370 if (last < end)
371 end = last;
373 btrace_insn_history (btinfo, uiout, begin, end, flags);
375 btinfo->insn_iterator.begin = begin;
376 btinfo->insn_iterator.end = end;
378 do_cleanups (uiout_cleanup);
381 /* The to_insn_history_from method of target record-btrace. */
383 static void
384 record_btrace_insn_history_from (ULONGEST from, int size, int flags)
386 ULONGEST begin, end, context;
388 context = abs (size);
390 if (size < 0)
392 end = from;
394 if (from < context)
395 begin = 0;
396 else
397 begin = from - context;
399 else
401 begin = from;
402 end = from + context;
404 /* Check for wrap-around. */
405 if (end < begin)
406 end = ULONGEST_MAX;
409 record_btrace_insn_history_range (begin, end, flags);
412 /* Print the instruction number range for a function call history line. */
414 static void
415 btrace_func_history_insn_range (struct ui_out *uiout, struct btrace_func *bfun)
417 ui_out_field_uint (uiout, "insn begin", bfun->ibegin);
419 if (bfun->ibegin == bfun->iend)
420 return;
422 ui_out_text (uiout, "-");
423 ui_out_field_uint (uiout, "insn end", bfun->iend);
426 /* Print the source line information for a function call history line. */
428 static void
429 btrace_func_history_src_line (struct ui_out *uiout, struct btrace_func *bfun)
431 struct symbol *sym;
433 sym = bfun->sym;
434 if (sym == NULL)
435 return;
437 ui_out_field_string (uiout, "file",
438 symtab_to_filename_for_display (sym->symtab));
440 if (bfun->lend == 0)
441 return;
443 ui_out_text (uiout, ":");
444 ui_out_field_int (uiout, "min line", bfun->lbegin);
446 if (bfun->lend == bfun->lbegin)
447 return;
449 ui_out_text (uiout, "-");
450 ui_out_field_int (uiout, "max line", bfun->lend);
453 /* Disassemble a section of the recorded function trace. */
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)
460 struct btrace_func *bfun;
461 unsigned int idx;
463 DEBUG ("ftrace (0x%x): [%u; %u[", flags, begin, end);
465 for (idx = begin; VEC_iterate (btrace_func_s, btinfo->ftrace, idx, bfun)
466 && idx < end; ++idx)
468 /* Print the function index. */
469 ui_out_field_uint (uiout, "index", idx);
470 ui_out_text (uiout, "\t");
472 if ((flags & RECORD_PRINT_INSN_RANGE) != 0)
474 btrace_func_history_insn_range (uiout, bfun);
475 ui_out_text (uiout, "\t");
478 if ((flags & RECORD_PRINT_SRC_LINE) != 0)
480 btrace_func_history_src_line (uiout, bfun);
481 ui_out_text (uiout, "\t");
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");
492 /* The to_call_history method of target record-btrace. */
494 static void
495 record_btrace_call_history (int size, int flags)
497 struct btrace_thread_info *btinfo;
498 struct cleanup *uiout_cleanup;
499 struct ui_out *uiout;
500 unsigned int context, last, begin, end;
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);
508 context = abs (size);
509 begin = btinfo->func_iterator.begin;
510 end = btinfo->func_iterator.end;
512 DEBUG ("func-history (0x%x): %d, prev: [%u; %u[", flags, size, begin, end);
514 if (context == 0)
515 error (_("Bad record function-call-history-size."));
517 /* We start at the end. */
518 if (end < begin)
520 /* Truncate the context, if necessary. */
521 context = min (context, last);
523 end = last;
524 begin = end - context;
526 else if (size < 0)
528 if (begin == 0)
530 printf_unfiltered (_("At the start of the branch trace record.\n"));
532 btinfo->func_iterator.end = 0;
533 return;
536 /* Truncate the context, if necessary. */
537 context = min (context, begin);
539 end = begin;
540 begin -= context;
542 else
544 if (end == last)
546 printf_unfiltered (_("At the end of the branch trace record.\n"));
548 btinfo->func_iterator.begin = last;
549 return;
552 /* Truncate the context, if necessary. */
553 context = min (context, last - end);
555 begin = end;
556 end += context;
559 btrace_func_history (btinfo, uiout, begin, end, flags);
561 btinfo->func_iterator.begin = begin;
562 btinfo->func_iterator.end = end;
564 do_cleanups (uiout_cleanup);
567 /* The to_call_history_range method of target record-btrace. */
569 static void
570 record_btrace_call_history_range (ULONGEST from, ULONGEST to, int flags)
572 struct btrace_thread_info *btinfo;
573 struct cleanup *uiout_cleanup;
574 struct ui_out *uiout;
575 unsigned int last, begin, end;
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);
583 begin = (unsigned int) from;
584 end = (unsigned int) to;
586 DEBUG ("func-history (0x%x): [%u; %u[", flags, begin, end);
588 /* Check for wrap-arounds. */
589 if (begin != from || end != to)
590 error (_("Bad range."));
592 if (end <= begin)
593 error (_("Bad range."));
595 if (last <= begin)
596 error (_("Range out of bounds."));
598 /* Truncate the range, if necessary. */
599 if (last < end)
600 end = last;
602 btrace_func_history (btinfo, uiout, begin, end, flags);
604 btinfo->func_iterator.begin = begin;
605 btinfo->func_iterator.end = end;
607 do_cleanups (uiout_cleanup);
610 /* The to_call_history_from method of target record-btrace. */
612 static void
613 record_btrace_call_history_from (ULONGEST from, int size, int flags)
615 ULONGEST begin, end, context;
617 context = abs (size);
619 if (size < 0)
621 end = from;
623 if (from < context)
624 begin = 0;
625 else
626 begin = from - context;
628 else
630 begin = from;
631 end = from + context;
633 /* Check for wrap-around. */
634 if (end < begin)
635 end = ULONGEST_MAX;
638 record_btrace_call_history_range (begin, end, flags);
641 /* Initialize the record-btrace target ops. */
643 static void
644 init_record_btrace_ops (void)
646 struct target_ops *ops;
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;
671 /* Alias for "target record". */
673 static void
674 cmd_record_btrace_start (char *args, int from_tty)
676 if (args != NULL && *args != 0)
677 error (_("Invalid argument."));
679 execute_command ("target record-btrace", from_tty);
682 void _initialize_record_btrace (void);
684 /* Initialize btrace commands. */
686 void
687 _initialize_record_btrace (void)
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);
694 init_record_btrace_ops ();
695 add_target (&record_btrace_ops);