2013-03-12 Sebastian Huber <sebastian.huber@embedded-brains.de>
[binutils-gdb.git] / gdb / record-btrace.c
blobbbb0bd57df36dff5fc0691460b5561090b08025d
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 (int quitting)
195 /* We already stopped recording. */
198 /* The to_info_record method of target record-btrace. */
200 static void
201 record_btrace_info (void)
203 struct btrace_thread_info *btinfo;
204 struct thread_info *tp;
205 unsigned int insts, funcs;
207 DEBUG ("info");
209 tp = find_thread_ptid (inferior_ptid);
210 if (tp == NULL)
211 error (_("No thread."));
213 btrace_fetch (tp);
215 btinfo = &tp->btrace;
216 insts = VEC_length (btrace_inst_s, btinfo->itrace);
217 funcs = VEC_length (btrace_func_s, btinfo->ftrace);
219 printf_unfiltered (_("Recorded %u instructions in %u functions for thread "
220 "%d (%s).\n"), insts, funcs, tp->num,
221 target_pid_to_str (tp->ptid));
224 /* Print an unsigned int. */
226 static void
227 ui_out_field_uint (struct ui_out *uiout, const char *fld, unsigned int val)
229 ui_out_field_fmt (uiout, fld, "%u", val);
232 /* Disassemble a section of the recorded instruction trace. */
234 static void
235 btrace_insn_history (struct btrace_thread_info *btinfo, struct ui_out *uiout,
236 unsigned int begin, unsigned int end, int flags)
238 struct gdbarch *gdbarch;
239 struct btrace_inst *inst;
240 unsigned int idx;
242 DEBUG ("itrace (0x%x): [%u; %u[", flags, begin, end);
244 gdbarch = target_gdbarch ();
246 for (idx = begin; VEC_iterate (btrace_inst_s, btinfo->itrace, idx, inst)
247 && idx < end; ++idx)
249 /* Print the instruction index. */
250 ui_out_field_uint (uiout, "index", idx);
251 ui_out_text (uiout, "\t");
253 /* Disassembly with '/m' flag may not produce the expected result.
254 See PR gdb/11833. */
255 gdb_disassembly (gdbarch, uiout, NULL, flags, 1, inst->pc, inst->pc + 1);
259 /* The to_insn_history method of target record-btrace. */
261 static void
262 record_btrace_insn_history (int size, int flags)
264 struct btrace_thread_info *btinfo;
265 struct cleanup *uiout_cleanup;
266 struct ui_out *uiout;
267 unsigned int context, last, begin, end;
269 uiout = current_uiout;
270 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
271 "insn history");
272 btinfo = require_btrace ();
273 last = VEC_length (btrace_inst_s, btinfo->itrace);
275 context = abs (size);
276 begin = btinfo->insn_iterator.begin;
277 end = btinfo->insn_iterator.end;
279 DEBUG ("insn-history (0x%x): %d, prev: [%u; %u[", flags, size, begin, end);
281 if (context == 0)
282 error (_("Bad record instruction-history-size."));
284 /* We start at the end. */
285 if (end < begin)
287 /* Truncate the context, if necessary. */
288 context = min (context, last);
290 end = last;
291 begin = end - context;
293 else if (size < 0)
295 if (begin == 0)
297 printf_unfiltered (_("At the start of the branch trace record.\n"));
299 btinfo->insn_iterator.end = 0;
300 return;
303 /* Truncate the context, if necessary. */
304 context = min (context, begin);
306 end = begin;
307 begin -= context;
309 else
311 if (end == last)
313 printf_unfiltered (_("At the end of the branch trace record.\n"));
315 btinfo->insn_iterator.begin = last;
316 return;
319 /* Truncate the context, if necessary. */
320 context = min (context, last - end);
322 begin = end;
323 end += context;
326 btrace_insn_history (btinfo, uiout, begin, end, flags);
328 btinfo->insn_iterator.begin = begin;
329 btinfo->insn_iterator.end = end;
331 do_cleanups (uiout_cleanup);
334 /* The to_insn_history_range method of target record-btrace. */
336 static void
337 record_btrace_insn_history_range (ULONGEST from, ULONGEST to, int flags)
339 struct btrace_thread_info *btinfo;
340 struct cleanup *uiout_cleanup;
341 struct ui_out *uiout;
342 unsigned int last, begin, end;
344 uiout = current_uiout;
345 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
346 "insn history");
347 btinfo = require_btrace ();
348 last = VEC_length (btrace_inst_s, btinfo->itrace);
350 begin = (unsigned int) from;
351 end = (unsigned int) to;
353 DEBUG ("insn-history (0x%x): [%u; %u[", flags, begin, end);
355 /* Check for wrap-arounds. */
356 if (begin != from || end != to)
357 error (_("Bad range."));
359 if (end <= begin)
360 error (_("Bad range."));
362 if (last <= begin)
363 error (_("Range out of bounds."));
365 /* Truncate the range, if necessary. */
366 if (last < end)
367 end = last;
369 btrace_insn_history (btinfo, uiout, begin, end, flags);
371 btinfo->insn_iterator.begin = begin;
372 btinfo->insn_iterator.end = end;
374 do_cleanups (uiout_cleanup);
377 /* The to_insn_history_from method of target record-btrace. */
379 static void
380 record_btrace_insn_history_from (ULONGEST from, int size, int flags)
382 ULONGEST begin, end, context;
384 context = abs (size);
386 if (size < 0)
388 end = from;
390 if (from < context)
391 begin = 0;
392 else
393 begin = from - context;
395 else
397 begin = from;
398 end = from + context;
400 /* Check for wrap-around. */
401 if (end < begin)
402 end = ULONGEST_MAX;
405 record_btrace_insn_history_range (begin, end, flags);
408 /* Print the instruction number range for a function call history line. */
410 static void
411 btrace_func_history_insn_range (struct ui_out *uiout, struct btrace_func *bfun)
413 ui_out_field_uint (uiout, "insn begin", bfun->ibegin);
415 if (bfun->ibegin == bfun->iend)
416 return;
418 ui_out_text (uiout, "-");
419 ui_out_field_uint (uiout, "insn end", bfun->iend);
422 /* Print the source line information for a function call history line. */
424 static void
425 btrace_func_history_src_line (struct ui_out *uiout, struct btrace_func *bfun)
427 struct symbol *sym;
429 sym = bfun->sym;
430 if (sym == NULL)
431 return;
433 ui_out_field_string (uiout, "file",
434 symtab_to_filename_for_display (sym->symtab));
436 if (bfun->lend == 0)
437 return;
439 ui_out_text (uiout, ":");
440 ui_out_field_int (uiout, "min line", bfun->lbegin);
442 if (bfun->lend == bfun->lbegin)
443 return;
445 ui_out_text (uiout, "-");
446 ui_out_field_int (uiout, "max line", bfun->lend);
449 /* Disassemble a section of the recorded function trace. */
451 static void
452 btrace_func_history (struct btrace_thread_info *btinfo, struct ui_out *uiout,
453 unsigned int begin, unsigned int end,
454 enum record_print_flag flags)
456 struct btrace_func *bfun;
457 unsigned int idx;
459 DEBUG ("ftrace (0x%x): [%u; %u[", flags, begin, end);
461 for (idx = begin; VEC_iterate (btrace_func_s, btinfo->ftrace, idx, bfun)
462 && idx < end; ++idx)
464 /* Print the function index. */
465 ui_out_field_uint (uiout, "index", idx);
466 ui_out_text (uiout, "\t");
468 if ((flags & record_print_insn_range) != 0)
470 btrace_func_history_insn_range (uiout, bfun);
471 ui_out_text (uiout, "\t");
474 if ((flags & record_print_src_line) != 0)
476 btrace_func_history_src_line (uiout, bfun);
477 ui_out_text (uiout, "\t");
480 if (bfun->sym != NULL)
481 ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (bfun->sym));
482 else if (bfun->msym != NULL)
483 ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (bfun->msym));
484 ui_out_text (uiout, "\n");
488 /* The to_call_history method of target record-btrace. */
490 static void
491 record_btrace_call_history (int size, int flags)
493 struct btrace_thread_info *btinfo;
494 struct cleanup *uiout_cleanup;
495 struct ui_out *uiout;
496 unsigned int context, last, begin, end;
498 uiout = current_uiout;
499 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
500 "insn history");
501 btinfo = require_btrace ();
502 last = VEC_length (btrace_func_s, btinfo->ftrace);
504 context = abs (size);
505 begin = btinfo->func_iterator.begin;
506 end = btinfo->func_iterator.end;
508 DEBUG ("func-history (0x%x): %d, prev: [%u; %u[", flags, size, begin, end);
510 if (context == 0)
511 error (_("Bad record function-call-history-size."));
513 /* We start at the end. */
514 if (end < begin)
516 /* Truncate the context, if necessary. */
517 context = min (context, last);
519 end = last;
520 begin = end - context;
522 else if (size < 0)
524 if (begin == 0)
526 printf_unfiltered (_("At the start of the branch trace record.\n"));
528 btinfo->func_iterator.end = 0;
529 return;
532 /* Truncate the context, if necessary. */
533 context = min (context, begin);
535 end = begin;
536 begin -= context;
538 else
540 if (end == last)
542 printf_unfiltered (_("At the end of the branch trace record.\n"));
544 btinfo->func_iterator.begin = last;
545 return;
548 /* Truncate the context, if necessary. */
549 context = min (context, last - end);
551 begin = end;
552 end += context;
555 btrace_func_history (btinfo, uiout, begin, end, flags);
557 btinfo->func_iterator.begin = begin;
558 btinfo->func_iterator.end = end;
560 do_cleanups (uiout_cleanup);
563 /* The to_call_history_range method of target record-btrace. */
565 static void
566 record_btrace_call_history_range (ULONGEST from, ULONGEST to, int flags)
568 struct btrace_thread_info *btinfo;
569 struct cleanup *uiout_cleanup;
570 struct ui_out *uiout;
571 unsigned int last, begin, end;
573 uiout = current_uiout;
574 uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
575 "func history");
576 btinfo = require_btrace ();
577 last = VEC_length (btrace_func_s, btinfo->ftrace);
579 begin = (unsigned int) from;
580 end = (unsigned int) to;
582 DEBUG ("func-history (0x%x): [%u; %u[", flags, begin, end);
584 /* Check for wrap-arounds. */
585 if (begin != from || end != to)
586 error (_("Bad range."));
588 if (end <= begin)
589 error (_("Bad range."));
591 if (last <= begin)
592 error (_("Range out of bounds."));
594 /* Truncate the range, if necessary. */
595 if (last < end)
596 end = last;
598 btrace_func_history (btinfo, uiout, begin, end, flags);
600 btinfo->func_iterator.begin = begin;
601 btinfo->func_iterator.end = end;
603 do_cleanups (uiout_cleanup);
606 /* The to_call_history_from method of target record-btrace. */
608 static void
609 record_btrace_call_history_from (ULONGEST from, int size, int flags)
611 ULONGEST begin, end, context;
613 context = abs (size);
615 if (size < 0)
617 end = from;
619 if (from < context)
620 begin = 0;
621 else
622 begin = from - context;
624 else
626 begin = from;
627 end = from + context;
629 /* Check for wrap-around. */
630 if (end < begin)
631 end = ULONGEST_MAX;
634 record_btrace_call_history_range (begin, end, flags);
637 /* Initialize the record-btrace target ops. */
639 static void
640 init_record_btrace_ops (void)
642 struct target_ops *ops;
644 ops = &record_btrace_ops;
645 ops->to_shortname = "record-btrace";
646 ops->to_longname = "Branch tracing target";
647 ops->to_doc = "Collect control-flow trace and provide the execution history.";
648 ops->to_open = record_btrace_open;
649 ops->to_close = record_btrace_close;
650 ops->to_detach = record_detach;
651 ops->to_disconnect = record_disconnect;
652 ops->to_mourn_inferior = record_mourn_inferior;
653 ops->to_kill = record_kill;
654 ops->to_create_inferior = find_default_create_inferior;
655 ops->to_stop_recording = record_btrace_stop_recording;
656 ops->to_info_record = record_btrace_info;
657 ops->to_insn_history = record_btrace_insn_history;
658 ops->to_insn_history_from = record_btrace_insn_history_from;
659 ops->to_insn_history_range = record_btrace_insn_history_range;
660 ops->to_call_history = record_btrace_call_history;
661 ops->to_call_history_from = record_btrace_call_history_from;
662 ops->to_call_history_range = record_btrace_call_history_range;
663 ops->to_stratum = record_stratum;
664 ops->to_magic = OPS_MAGIC;
667 /* Alias for "target record". */
669 static void
670 cmd_record_btrace_start (char *args, int from_tty)
672 if (args != NULL && *args != 0)
673 error (_("Invalid argument."));
675 execute_command ("target record-btrace", from_tty);
678 void _initialize_record_btrace (void);
680 /* Initialize btrace commands. */
682 void
683 _initialize_record_btrace (void)
685 add_cmd ("btrace", class_obscure, cmd_record_btrace_start,
686 _("Start branch trace recording."),
687 &record_cmdlist);
688 add_alias_cmd ("b", "btrace", class_obscure, 1, &record_cmdlist);
690 init_record_btrace_ops ();
691 add_target (&record_btrace_ops);