]>
Commit | Line | Data |
---|---|---|
afedecd3 MM |
1 | /* Branch trace support for GDB, the GNU debugger. |
2 | ||
ecd75fc8 | 3 | Copyright (C) 2013-2014 Free Software Foundation, Inc. |
afedecd3 MM |
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 | ||
23a7fe75 | 77 | if (btinfo->begin == NULL) |
afedecd3 MM |
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 | ||
8213266a | 145 | record_preopen (); |
afedecd3 MM |
146 | |
147 | if (!target_has_execution) | |
148 | error (_("The program is not being run.")); | |
149 | ||
150 | if (!target_supports_btrace ()) | |
151 | error (_("Target does not support branch tracing.")); | |
152 | ||
153 | gdb_assert (record_btrace_thread_observer == NULL); | |
154 | ||
155 | disable_chain = make_cleanup (null_cleanup, NULL); | |
156 | ALL_THREADS (tp) | |
157 | if (args == NULL || *args == 0 || number_is_in_list (args, tp->num)) | |
158 | { | |
159 | btrace_enable (tp); | |
160 | ||
161 | make_cleanup (record_btrace_disable_callback, tp); | |
162 | } | |
163 | ||
164 | record_btrace_auto_enable (); | |
165 | ||
166 | push_target (&record_btrace_ops); | |
167 | ||
168 | observer_notify_record_changed (current_inferior (), 1); | |
169 | ||
170 | discard_cleanups (disable_chain); | |
171 | } | |
172 | ||
173 | /* The to_stop_recording method of target record-btrace. */ | |
174 | ||
175 | static void | |
176 | record_btrace_stop_recording (void) | |
177 | { | |
178 | struct thread_info *tp; | |
179 | ||
180 | DEBUG ("stop recording"); | |
181 | ||
182 | record_btrace_auto_disable (); | |
183 | ||
184 | ALL_THREADS (tp) | |
185 | if (tp->btrace.target != NULL) | |
186 | btrace_disable (tp); | |
187 | } | |
188 | ||
189 | /* The to_close method of target record-btrace. */ | |
190 | ||
191 | static void | |
460014f5 | 192 | record_btrace_close (void) |
afedecd3 | 193 | { |
99c819ee MM |
194 | /* Make sure automatic recording gets disabled even if we did not stop |
195 | recording before closing the record-btrace target. */ | |
196 | record_btrace_auto_disable (); | |
197 | ||
afedecd3 MM |
198 | /* We already stopped recording. */ |
199 | } | |
200 | ||
201 | /* The to_info_record method of target record-btrace. */ | |
202 | ||
203 | static void | |
204 | record_btrace_info (void) | |
205 | { | |
206 | struct btrace_thread_info *btinfo; | |
207 | struct thread_info *tp; | |
23a7fe75 | 208 | unsigned int insns, calls; |
afedecd3 MM |
209 | |
210 | DEBUG ("info"); | |
211 | ||
212 | tp = find_thread_ptid (inferior_ptid); | |
213 | if (tp == NULL) | |
214 | error (_("No thread.")); | |
215 | ||
216 | btrace_fetch (tp); | |
217 | ||
23a7fe75 MM |
218 | insns = 0; |
219 | calls = 0; | |
220 | ||
afedecd3 | 221 | btinfo = &tp->btrace; |
23a7fe75 MM |
222 | if (btinfo->begin != NULL) |
223 | { | |
224 | struct btrace_call_iterator call; | |
225 | struct btrace_insn_iterator insn; | |
226 | ||
227 | btrace_call_end (&call, btinfo); | |
228 | btrace_call_prev (&call, 1); | |
5de9129b | 229 | calls = btrace_call_number (&call); |
23a7fe75 MM |
230 | |
231 | btrace_insn_end (&insn, btinfo); | |
232 | btrace_insn_prev (&insn, 1); | |
5de9129b | 233 | insns = btrace_insn_number (&insn); |
23a7fe75 | 234 | } |
afedecd3 MM |
235 | |
236 | printf_unfiltered (_("Recorded %u instructions in %u functions for thread " | |
23a7fe75 | 237 | "%d (%s).\n"), insns, calls, tp->num, |
afedecd3 MM |
238 | target_pid_to_str (tp->ptid)); |
239 | } | |
240 | ||
241 | /* Print an unsigned int. */ | |
242 | ||
243 | static void | |
244 | ui_out_field_uint (struct ui_out *uiout, const char *fld, unsigned int val) | |
245 | { | |
246 | ui_out_field_fmt (uiout, fld, "%u", val); | |
247 | } | |
248 | ||
249 | /* Disassemble a section of the recorded instruction trace. */ | |
250 | ||
251 | static void | |
23a7fe75 MM |
252 | btrace_insn_history (struct ui_out *uiout, |
253 | const struct btrace_insn_iterator *begin, | |
254 | const struct btrace_insn_iterator *end, int flags) | |
afedecd3 MM |
255 | { |
256 | struct gdbarch *gdbarch; | |
23a7fe75 | 257 | struct btrace_insn_iterator it; |
afedecd3 | 258 | |
23a7fe75 MM |
259 | DEBUG ("itrace (0x%x): [%u; %u)", flags, btrace_insn_number (begin), |
260 | btrace_insn_number (end)); | |
afedecd3 MM |
261 | |
262 | gdbarch = target_gdbarch (); | |
263 | ||
23a7fe75 | 264 | for (it = *begin; btrace_insn_cmp (&it, end) != 0; btrace_insn_next (&it, 1)) |
afedecd3 | 265 | { |
23a7fe75 MM |
266 | const struct btrace_insn *insn; |
267 | ||
268 | insn = btrace_insn_get (&it); | |
269 | ||
afedecd3 | 270 | /* Print the instruction index. */ |
23a7fe75 | 271 | ui_out_field_uint (uiout, "index", btrace_insn_number (&it)); |
afedecd3 MM |
272 | ui_out_text (uiout, "\t"); |
273 | ||
274 | /* Disassembly with '/m' flag may not produce the expected result. | |
275 | See PR gdb/11833. */ | |
23a7fe75 | 276 | gdb_disassembly (gdbarch, uiout, NULL, flags, 1, insn->pc, insn->pc + 1); |
afedecd3 MM |
277 | } |
278 | } | |
279 | ||
280 | /* The to_insn_history method of target record-btrace. */ | |
281 | ||
282 | static void | |
283 | record_btrace_insn_history (int size, int flags) | |
284 | { | |
285 | struct btrace_thread_info *btinfo; | |
23a7fe75 MM |
286 | struct btrace_insn_history *history; |
287 | struct btrace_insn_iterator begin, end; | |
afedecd3 MM |
288 | struct cleanup *uiout_cleanup; |
289 | struct ui_out *uiout; | |
23a7fe75 | 290 | unsigned int context, covered; |
afedecd3 MM |
291 | |
292 | uiout = current_uiout; | |
293 | uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, | |
294 | "insn history"); | |
afedecd3 | 295 | context = abs (size); |
afedecd3 MM |
296 | if (context == 0) |
297 | error (_("Bad record instruction-history-size.")); | |
298 | ||
23a7fe75 MM |
299 | btinfo = require_btrace (); |
300 | history = btinfo->insn_history; | |
301 | if (history == NULL) | |
afedecd3 | 302 | { |
23a7fe75 MM |
303 | /* No matter the direction, we start with the tail of the trace. */ |
304 | btrace_insn_end (&begin, btinfo); | |
305 | end = begin; | |
afedecd3 | 306 | |
23a7fe75 | 307 | DEBUG ("insn-history (0x%x): %d", flags, size); |
afedecd3 | 308 | |
23a7fe75 | 309 | covered = btrace_insn_prev (&begin, context); |
afedecd3 MM |
310 | } |
311 | else | |
312 | { | |
23a7fe75 MM |
313 | begin = history->begin; |
314 | end = history->end; | |
afedecd3 | 315 | |
23a7fe75 MM |
316 | DEBUG ("insn-history (0x%x): %d, prev: [%u; %u)", flags, size, |
317 | btrace_insn_number (&begin), btrace_insn_number (&end)); | |
afedecd3 | 318 | |
23a7fe75 MM |
319 | if (size < 0) |
320 | { | |
321 | end = begin; | |
322 | covered = btrace_insn_prev (&begin, context); | |
323 | } | |
324 | else | |
325 | { | |
326 | begin = end; | |
327 | covered = btrace_insn_next (&end, context); | |
328 | } | |
afedecd3 MM |
329 | } |
330 | ||
23a7fe75 MM |
331 | if (covered > 0) |
332 | btrace_insn_history (uiout, &begin, &end, flags); | |
333 | else | |
334 | { | |
335 | if (size < 0) | |
336 | printf_unfiltered (_("At the start of the branch trace record.\n")); | |
337 | else | |
338 | printf_unfiltered (_("At the end of the branch trace record.\n")); | |
339 | } | |
afedecd3 | 340 | |
23a7fe75 | 341 | btrace_set_insn_history (btinfo, &begin, &end); |
afedecd3 MM |
342 | do_cleanups (uiout_cleanup); |
343 | } | |
344 | ||
345 | /* The to_insn_history_range method of target record-btrace. */ | |
346 | ||
347 | static void | |
348 | record_btrace_insn_history_range (ULONGEST from, ULONGEST to, int flags) | |
349 | { | |
350 | struct btrace_thread_info *btinfo; | |
23a7fe75 MM |
351 | struct btrace_insn_history *history; |
352 | struct btrace_insn_iterator begin, end; | |
afedecd3 MM |
353 | struct cleanup *uiout_cleanup; |
354 | struct ui_out *uiout; | |
23a7fe75 MM |
355 | unsigned int low, high; |
356 | int found; | |
afedecd3 MM |
357 | |
358 | uiout = current_uiout; | |
359 | uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, | |
360 | "insn history"); | |
23a7fe75 MM |
361 | low = from; |
362 | high = to; | |
afedecd3 | 363 | |
23a7fe75 | 364 | DEBUG ("insn-history (0x%x): [%u; %u)", flags, low, high); |
afedecd3 MM |
365 | |
366 | /* Check for wrap-arounds. */ | |
23a7fe75 | 367 | if (low != from || high != to) |
afedecd3 MM |
368 | error (_("Bad range.")); |
369 | ||
0688d04e | 370 | if (high < low) |
afedecd3 MM |
371 | error (_("Bad range.")); |
372 | ||
23a7fe75 | 373 | btinfo = require_btrace (); |
afedecd3 | 374 | |
23a7fe75 MM |
375 | found = btrace_find_insn_by_number (&begin, btinfo, low); |
376 | if (found == 0) | |
377 | error (_("Range out of bounds.")); | |
afedecd3 | 378 | |
23a7fe75 MM |
379 | found = btrace_find_insn_by_number (&end, btinfo, high); |
380 | if (found == 0) | |
0688d04e MM |
381 | { |
382 | /* Silently truncate the range. */ | |
383 | btrace_insn_end (&end, btinfo); | |
384 | } | |
385 | else | |
386 | { | |
387 | /* We want both begin and end to be inclusive. */ | |
388 | btrace_insn_next (&end, 1); | |
389 | } | |
afedecd3 | 390 | |
23a7fe75 MM |
391 | btrace_insn_history (uiout, &begin, &end, flags); |
392 | btrace_set_insn_history (btinfo, &begin, &end); | |
afedecd3 MM |
393 | |
394 | do_cleanups (uiout_cleanup); | |
395 | } | |
396 | ||
397 | /* The to_insn_history_from method of target record-btrace. */ | |
398 | ||
399 | static void | |
400 | record_btrace_insn_history_from (ULONGEST from, int size, int flags) | |
401 | { | |
402 | ULONGEST begin, end, context; | |
403 | ||
404 | context = abs (size); | |
0688d04e MM |
405 | if (context == 0) |
406 | error (_("Bad record instruction-history-size.")); | |
afedecd3 MM |
407 | |
408 | if (size < 0) | |
409 | { | |
410 | end = from; | |
411 | ||
412 | if (from < context) | |
413 | begin = 0; | |
414 | else | |
0688d04e | 415 | begin = from - context + 1; |
afedecd3 MM |
416 | } |
417 | else | |
418 | { | |
419 | begin = from; | |
0688d04e | 420 | end = from + context - 1; |
afedecd3 MM |
421 | |
422 | /* Check for wrap-around. */ | |
423 | if (end < begin) | |
424 | end = ULONGEST_MAX; | |
425 | } | |
426 | ||
427 | record_btrace_insn_history_range (begin, end, flags); | |
428 | } | |
429 | ||
430 | /* Print the instruction number range for a function call history line. */ | |
431 | ||
432 | static void | |
23a7fe75 MM |
433 | btrace_call_history_insn_range (struct ui_out *uiout, |
434 | const struct btrace_function *bfun) | |
afedecd3 | 435 | { |
7acbe133 MM |
436 | unsigned int begin, end, size; |
437 | ||
438 | size = VEC_length (btrace_insn_s, bfun->insn); | |
439 | gdb_assert (size > 0); | |
afedecd3 | 440 | |
23a7fe75 | 441 | begin = bfun->insn_offset; |
7acbe133 | 442 | end = begin + size - 1; |
afedecd3 | 443 | |
23a7fe75 | 444 | ui_out_field_uint (uiout, "insn begin", begin); |
8710b709 | 445 | ui_out_text (uiout, ","); |
23a7fe75 | 446 | ui_out_field_uint (uiout, "insn end", end); |
afedecd3 MM |
447 | } |
448 | ||
449 | /* Print the source line information for a function call history line. */ | |
450 | ||
451 | static void | |
23a7fe75 MM |
452 | btrace_call_history_src_line (struct ui_out *uiout, |
453 | const struct btrace_function *bfun) | |
afedecd3 MM |
454 | { |
455 | struct symbol *sym; | |
23a7fe75 | 456 | int begin, end; |
afedecd3 MM |
457 | |
458 | sym = bfun->sym; | |
459 | if (sym == NULL) | |
460 | return; | |
461 | ||
462 | ui_out_field_string (uiout, "file", | |
463 | symtab_to_filename_for_display (sym->symtab)); | |
464 | ||
23a7fe75 MM |
465 | begin = bfun->lbegin; |
466 | end = bfun->lend; | |
467 | ||
468 | if (end < begin) | |
afedecd3 MM |
469 | return; |
470 | ||
471 | ui_out_text (uiout, ":"); | |
23a7fe75 | 472 | ui_out_field_int (uiout, "min line", begin); |
afedecd3 | 473 | |
23a7fe75 | 474 | if (end == begin) |
afedecd3 MM |
475 | return; |
476 | ||
8710b709 | 477 | ui_out_text (uiout, ","); |
23a7fe75 | 478 | ui_out_field_int (uiout, "max line", end); |
afedecd3 MM |
479 | } |
480 | ||
481 | /* Disassemble a section of the recorded function trace. */ | |
482 | ||
483 | static void | |
23a7fe75 | 484 | btrace_call_history (struct ui_out *uiout, |
8710b709 | 485 | const struct btrace_thread_info *btinfo, |
23a7fe75 MM |
486 | const struct btrace_call_iterator *begin, |
487 | const struct btrace_call_iterator *end, | |
afedecd3 MM |
488 | enum record_print_flag flags) |
489 | { | |
23a7fe75 | 490 | struct btrace_call_iterator it; |
afedecd3 | 491 | |
23a7fe75 MM |
492 | DEBUG ("ftrace (0x%x): [%u; %u)", flags, btrace_call_number (begin), |
493 | btrace_call_number (end)); | |
afedecd3 | 494 | |
23a7fe75 | 495 | for (it = *begin; btrace_call_cmp (&it, end) < 0; btrace_call_next (&it, 1)) |
afedecd3 | 496 | { |
23a7fe75 MM |
497 | const struct btrace_function *bfun; |
498 | struct minimal_symbol *msym; | |
499 | struct symbol *sym; | |
500 | ||
501 | bfun = btrace_call_get (&it); | |
502 | msym = bfun->msym; | |
503 | sym = bfun->sym; | |
504 | ||
afedecd3 | 505 | /* Print the function index. */ |
23a7fe75 | 506 | ui_out_field_uint (uiout, "index", bfun->number); |
afedecd3 MM |
507 | ui_out_text (uiout, "\t"); |
508 | ||
8710b709 MM |
509 | if ((flags & RECORD_PRINT_INDENT_CALLS) != 0) |
510 | { | |
511 | int level = bfun->level + btinfo->level, i; | |
512 | ||
513 | for (i = 0; i < level; ++i) | |
514 | ui_out_text (uiout, " "); | |
515 | } | |
516 | ||
517 | if (sym != NULL) | |
518 | ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (sym)); | |
519 | else if (msym != NULL) | |
520 | ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (msym)); | |
521 | else if (!ui_out_is_mi_like_p (uiout)) | |
522 | ui_out_field_string (uiout, "function", "??"); | |
523 | ||
1e038f67 | 524 | if ((flags & RECORD_PRINT_INSN_RANGE) != 0) |
afedecd3 | 525 | { |
8710b709 | 526 | ui_out_text (uiout, _("\tinst ")); |
23a7fe75 | 527 | btrace_call_history_insn_range (uiout, bfun); |
afedecd3 MM |
528 | } |
529 | ||
1e038f67 | 530 | if ((flags & RECORD_PRINT_SRC_LINE) != 0) |
afedecd3 | 531 | { |
8710b709 | 532 | ui_out_text (uiout, _("\tat ")); |
23a7fe75 | 533 | btrace_call_history_src_line (uiout, bfun); |
afedecd3 MM |
534 | } |
535 | ||
afedecd3 MM |
536 | ui_out_text (uiout, "\n"); |
537 | } | |
538 | } | |
539 | ||
540 | /* The to_call_history method of target record-btrace. */ | |
541 | ||
542 | static void | |
543 | record_btrace_call_history (int size, int flags) | |
544 | { | |
545 | struct btrace_thread_info *btinfo; | |
23a7fe75 MM |
546 | struct btrace_call_history *history; |
547 | struct btrace_call_iterator begin, end; | |
afedecd3 MM |
548 | struct cleanup *uiout_cleanup; |
549 | struct ui_out *uiout; | |
23a7fe75 | 550 | unsigned int context, covered; |
afedecd3 MM |
551 | |
552 | uiout = current_uiout; | |
553 | uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, | |
554 | "insn history"); | |
afedecd3 | 555 | context = abs (size); |
afedecd3 MM |
556 | if (context == 0) |
557 | error (_("Bad record function-call-history-size.")); | |
558 | ||
23a7fe75 MM |
559 | btinfo = require_btrace (); |
560 | history = btinfo->call_history; | |
561 | if (history == NULL) | |
afedecd3 | 562 | { |
23a7fe75 MM |
563 | /* No matter the direction, we start with the tail of the trace. */ |
564 | btrace_call_end (&begin, btinfo); | |
565 | end = begin; | |
afedecd3 | 566 | |
23a7fe75 | 567 | DEBUG ("call-history (0x%x): %d", flags, size); |
afedecd3 | 568 | |
23a7fe75 | 569 | covered = btrace_call_prev (&begin, context); |
afedecd3 MM |
570 | } |
571 | else | |
572 | { | |
23a7fe75 MM |
573 | begin = history->begin; |
574 | end = history->end; | |
afedecd3 | 575 | |
23a7fe75 MM |
576 | DEBUG ("call-history (0x%x): %d, prev: [%u; %u)", flags, size, |
577 | btrace_call_number (&begin), btrace_call_number (&end)); | |
afedecd3 | 578 | |
23a7fe75 MM |
579 | if (size < 0) |
580 | { | |
581 | end = begin; | |
582 | covered = btrace_call_prev (&begin, context); | |
583 | } | |
584 | else | |
585 | { | |
586 | begin = end; | |
587 | covered = btrace_call_next (&end, context); | |
588 | } | |
afedecd3 MM |
589 | } |
590 | ||
23a7fe75 | 591 | if (covered > 0) |
8710b709 | 592 | btrace_call_history (uiout, btinfo, &begin, &end, flags); |
23a7fe75 MM |
593 | else |
594 | { | |
595 | if (size < 0) | |
596 | printf_unfiltered (_("At the start of the branch trace record.\n")); | |
597 | else | |
598 | printf_unfiltered (_("At the end of the branch trace record.\n")); | |
599 | } | |
afedecd3 | 600 | |
23a7fe75 | 601 | btrace_set_call_history (btinfo, &begin, &end); |
afedecd3 MM |
602 | do_cleanups (uiout_cleanup); |
603 | } | |
604 | ||
605 | /* The to_call_history_range method of target record-btrace. */ | |
606 | ||
607 | static void | |
608 | record_btrace_call_history_range (ULONGEST from, ULONGEST to, int flags) | |
609 | { | |
610 | struct btrace_thread_info *btinfo; | |
23a7fe75 MM |
611 | struct btrace_call_history *history; |
612 | struct btrace_call_iterator begin, end; | |
afedecd3 MM |
613 | struct cleanup *uiout_cleanup; |
614 | struct ui_out *uiout; | |
23a7fe75 MM |
615 | unsigned int low, high; |
616 | int found; | |
afedecd3 MM |
617 | |
618 | uiout = current_uiout; | |
619 | uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, | |
620 | "func history"); | |
23a7fe75 MM |
621 | low = from; |
622 | high = to; | |
afedecd3 | 623 | |
23a7fe75 | 624 | DEBUG ("call-history (0x%x): [%u; %u)", flags, low, high); |
afedecd3 MM |
625 | |
626 | /* Check for wrap-arounds. */ | |
23a7fe75 | 627 | if (low != from || high != to) |
afedecd3 MM |
628 | error (_("Bad range.")); |
629 | ||
0688d04e | 630 | if (high < low) |
afedecd3 MM |
631 | error (_("Bad range.")); |
632 | ||
23a7fe75 | 633 | btinfo = require_btrace (); |
afedecd3 | 634 | |
23a7fe75 MM |
635 | found = btrace_find_call_by_number (&begin, btinfo, low); |
636 | if (found == 0) | |
637 | error (_("Range out of bounds.")); | |
afedecd3 | 638 | |
23a7fe75 MM |
639 | found = btrace_find_call_by_number (&end, btinfo, high); |
640 | if (found == 0) | |
0688d04e MM |
641 | { |
642 | /* Silently truncate the range. */ | |
643 | btrace_call_end (&end, btinfo); | |
644 | } | |
645 | else | |
646 | { | |
647 | /* We want both begin and end to be inclusive. */ | |
648 | btrace_call_next (&end, 1); | |
649 | } | |
afedecd3 | 650 | |
8710b709 | 651 | btrace_call_history (uiout, btinfo, &begin, &end, flags); |
23a7fe75 | 652 | btrace_set_call_history (btinfo, &begin, &end); |
afedecd3 MM |
653 | |
654 | do_cleanups (uiout_cleanup); | |
655 | } | |
656 | ||
657 | /* The to_call_history_from method of target record-btrace. */ | |
658 | ||
659 | static void | |
660 | record_btrace_call_history_from (ULONGEST from, int size, int flags) | |
661 | { | |
662 | ULONGEST begin, end, context; | |
663 | ||
664 | context = abs (size); | |
0688d04e MM |
665 | if (context == 0) |
666 | error (_("Bad record function-call-history-size.")); | |
afedecd3 MM |
667 | |
668 | if (size < 0) | |
669 | { | |
670 | end = from; | |
671 | ||
672 | if (from < context) | |
673 | begin = 0; | |
674 | else | |
0688d04e | 675 | begin = from - context + 1; |
afedecd3 MM |
676 | } |
677 | else | |
678 | { | |
679 | begin = from; | |
0688d04e | 680 | end = from + context - 1; |
afedecd3 MM |
681 | |
682 | /* Check for wrap-around. */ | |
683 | if (end < begin) | |
684 | end = ULONGEST_MAX; | |
685 | } | |
686 | ||
687 | record_btrace_call_history_range (begin, end, flags); | |
688 | } | |
689 | ||
690 | /* Initialize the record-btrace target ops. */ | |
691 | ||
692 | static void | |
693 | init_record_btrace_ops (void) | |
694 | { | |
695 | struct target_ops *ops; | |
696 | ||
697 | ops = &record_btrace_ops; | |
698 | ops->to_shortname = "record-btrace"; | |
699 | ops->to_longname = "Branch tracing target"; | |
700 | ops->to_doc = "Collect control-flow trace and provide the execution history."; | |
701 | ops->to_open = record_btrace_open; | |
702 | ops->to_close = record_btrace_close; | |
703 | ops->to_detach = record_detach; | |
704 | ops->to_disconnect = record_disconnect; | |
705 | ops->to_mourn_inferior = record_mourn_inferior; | |
706 | ops->to_kill = record_kill; | |
707 | ops->to_create_inferior = find_default_create_inferior; | |
708 | ops->to_stop_recording = record_btrace_stop_recording; | |
709 | ops->to_info_record = record_btrace_info; | |
710 | ops->to_insn_history = record_btrace_insn_history; | |
711 | ops->to_insn_history_from = record_btrace_insn_history_from; | |
712 | ops->to_insn_history_range = record_btrace_insn_history_range; | |
713 | ops->to_call_history = record_btrace_call_history; | |
714 | ops->to_call_history_from = record_btrace_call_history_from; | |
715 | ops->to_call_history_range = record_btrace_call_history_range; | |
716 | ops->to_stratum = record_stratum; | |
717 | ops->to_magic = OPS_MAGIC; | |
718 | } | |
719 | ||
720 | /* Alias for "target record". */ | |
721 | ||
722 | static void | |
723 | cmd_record_btrace_start (char *args, int from_tty) | |
724 | { | |
725 | if (args != NULL && *args != 0) | |
726 | error (_("Invalid argument.")); | |
727 | ||
728 | execute_command ("target record-btrace", from_tty); | |
729 | } | |
730 | ||
731 | void _initialize_record_btrace (void); | |
732 | ||
733 | /* Initialize btrace commands. */ | |
734 | ||
735 | void | |
736 | _initialize_record_btrace (void) | |
737 | { | |
738 | add_cmd ("btrace", class_obscure, cmd_record_btrace_start, | |
739 | _("Start branch trace recording."), | |
740 | &record_cmdlist); | |
741 | add_alias_cmd ("b", "btrace", class_obscure, 1, &record_cmdlist); | |
742 | ||
743 | init_record_btrace_ops (); | |
744 | add_target (&record_btrace_ops); | |
745 | } |