record-btrace: add bts buffer size configuration option
[external/binutils.git] / gdb / record-btrace.c
1 /* Branch trace support for GDB, the GNU debugger.
2
3    Copyright (C) 2013-2015 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 "cli/cli-utils.h"
30 #include "source.h"
31 #include "ui-out.h"
32 #include "symtab.h"
33 #include "filenames.h"
34 #include "regcache.h"
35 #include "frame-unwind.h"
36 #include "hashtab.h"
37 #include "infrun.h"
38 #include "event-loop.h"
39 #include "inf-loop.h"
40
41 /* The target_ops of record-btrace.  */
42 static struct target_ops record_btrace_ops;
43
44 /* A new thread observer enabling branch tracing for the new thread.  */
45 static struct observer *record_btrace_thread_observer;
46
47 /* Memory access types used in set/show record btrace replay-memory-access.  */
48 static const char replay_memory_access_read_only[] = "read-only";
49 static const char replay_memory_access_read_write[] = "read-write";
50 static const char *const replay_memory_access_types[] =
51 {
52   replay_memory_access_read_only,
53   replay_memory_access_read_write,
54   NULL
55 };
56
57 /* The currently allowed replay memory access type.  */
58 static const char *replay_memory_access = replay_memory_access_read_only;
59
60 /* Command lists for "set/show record btrace".  */
61 static struct cmd_list_element *set_record_btrace_cmdlist;
62 static struct cmd_list_element *show_record_btrace_cmdlist;
63
64 /* The execution direction of the last resume we got.  See record-full.c.  */
65 static enum exec_direction_kind record_btrace_resume_exec_dir = EXEC_FORWARD;
66
67 /* The async event handler for reverse/replay execution.  */
68 static struct async_event_handler *record_btrace_async_inferior_event_handler;
69
70 /* A flag indicating that we are currently generating a core file.  */
71 static int record_btrace_generating_corefile;
72
73 /* The current branch trace configuration.  */
74 static struct btrace_config record_btrace_conf;
75
76 /* Command list for "record btrace".  */
77 static struct cmd_list_element *record_btrace_cmdlist;
78
79 /* Command lists for "set/show record btrace".  */
80 static struct cmd_list_element *set_record_btrace_cmdlist;
81 static struct cmd_list_element *show_record_btrace_cmdlist;
82
83 /* Command lists for "set/show record btrace bts".  */
84 static struct cmd_list_element *set_record_btrace_bts_cmdlist;
85 static struct cmd_list_element *show_record_btrace_bts_cmdlist;
86
87 /* Print a record-btrace debug message.  Use do ... while (0) to avoid
88    ambiguities when used in if statements.  */
89
90 #define DEBUG(msg, args...)                                             \
91   do                                                                    \
92     {                                                                   \
93       if (record_debug != 0)                                            \
94         fprintf_unfiltered (gdb_stdlog,                                 \
95                             "[record-btrace] " msg "\n", ##args);       \
96     }                                                                   \
97   while (0)
98
99
100 /* Update the branch trace for the current thread and return a pointer to its
101    thread_info.
102
103    Throws an error if there is no thread or no trace.  This function never
104    returns NULL.  */
105
106 static struct thread_info *
107 require_btrace_thread (void)
108 {
109   struct thread_info *tp;
110
111   DEBUG ("require");
112
113   tp = find_thread_ptid (inferior_ptid);
114   if (tp == NULL)
115     error (_("No thread."));
116
117   btrace_fetch (tp);
118
119   if (btrace_is_empty (tp))
120     error (_("No trace."));
121
122   return tp;
123 }
124
125 /* Update the branch trace for the current thread and return a pointer to its
126    branch trace information struct.
127
128    Throws an error if there is no thread or no trace.  This function never
129    returns NULL.  */
130
131 static struct btrace_thread_info *
132 require_btrace (void)
133 {
134   struct thread_info *tp;
135
136   tp = require_btrace_thread ();
137
138   return &tp->btrace;
139 }
140
141 /* Enable branch tracing for one thread.  Warn on errors.  */
142
143 static void
144 record_btrace_enable_warn (struct thread_info *tp)
145 {
146   volatile struct gdb_exception error;
147
148   TRY_CATCH (error, RETURN_MASK_ERROR)
149     btrace_enable (tp, &record_btrace_conf);
150
151   if (error.message != NULL)
152     warning ("%s", error.message);
153 }
154
155 /* Callback function to disable branch tracing for one thread.  */
156
157 static void
158 record_btrace_disable_callback (void *arg)
159 {
160   struct thread_info *tp;
161
162   tp = arg;
163
164   btrace_disable (tp);
165 }
166
167 /* Enable automatic tracing of new threads.  */
168
169 static void
170 record_btrace_auto_enable (void)
171 {
172   DEBUG ("attach thread observer");
173
174   record_btrace_thread_observer
175     = observer_attach_new_thread (record_btrace_enable_warn);
176 }
177
178 /* Disable automatic tracing of new threads.  */
179
180 static void
181 record_btrace_auto_disable (void)
182 {
183   /* The observer may have been detached, already.  */
184   if (record_btrace_thread_observer == NULL)
185     return;
186
187   DEBUG ("detach thread observer");
188
189   observer_detach_new_thread (record_btrace_thread_observer);
190   record_btrace_thread_observer = NULL;
191 }
192
193 /* The record-btrace async event handler function.  */
194
195 static void
196 record_btrace_handle_async_inferior_event (gdb_client_data data)
197 {
198   inferior_event_handler (INF_REG_EVENT, NULL);
199 }
200
201 /* The to_open method of target record-btrace.  */
202
203 static void
204 record_btrace_open (const char *args, int from_tty)
205 {
206   struct cleanup *disable_chain;
207   struct thread_info *tp;
208
209   DEBUG ("open");
210
211   record_preopen ();
212
213   if (!target_has_execution)
214     error (_("The program is not being run."));
215
216   if (non_stop)
217     error (_("Record btrace can't debug inferior in non-stop mode."));
218
219   gdb_assert (record_btrace_thread_observer == NULL);
220
221   disable_chain = make_cleanup (null_cleanup, NULL);
222   ALL_NON_EXITED_THREADS (tp)
223     if (args == NULL || *args == 0 || number_is_in_list (args, tp->num))
224       {
225         btrace_enable (tp, &record_btrace_conf);
226
227         make_cleanup (record_btrace_disable_callback, tp);
228       }
229
230   record_btrace_auto_enable ();
231
232   push_target (&record_btrace_ops);
233
234   record_btrace_async_inferior_event_handler
235     = create_async_event_handler (record_btrace_handle_async_inferior_event,
236                                   NULL);
237   record_btrace_generating_corefile = 0;
238
239   observer_notify_record_changed (current_inferior (),  1);
240
241   discard_cleanups (disable_chain);
242 }
243
244 /* The to_stop_recording method of target record-btrace.  */
245
246 static void
247 record_btrace_stop_recording (struct target_ops *self)
248 {
249   struct thread_info *tp;
250
251   DEBUG ("stop recording");
252
253   record_btrace_auto_disable ();
254
255   ALL_NON_EXITED_THREADS (tp)
256     if (tp->btrace.target != NULL)
257       btrace_disable (tp);
258 }
259
260 /* The to_close method of target record-btrace.  */
261
262 static void
263 record_btrace_close (struct target_ops *self)
264 {
265   struct thread_info *tp;
266
267   if (record_btrace_async_inferior_event_handler != NULL)
268     delete_async_event_handler (&record_btrace_async_inferior_event_handler);
269
270   /* Make sure automatic recording gets disabled even if we did not stop
271      recording before closing the record-btrace target.  */
272   record_btrace_auto_disable ();
273
274   /* We should have already stopped recording.
275      Tear down btrace in case we have not.  */
276   ALL_NON_EXITED_THREADS (tp)
277     btrace_teardown (tp);
278 }
279
280 /* The to_async method of target record-btrace.  */
281
282 static void
283 record_btrace_async (struct target_ops *ops,
284                      void (*callback) (enum inferior_event_type event_type,
285                                        void *context),
286                      void *context)
287 {
288   if (callback != NULL)
289     mark_async_event_handler (record_btrace_async_inferior_event_handler);
290   else
291     clear_async_event_handler (record_btrace_async_inferior_event_handler);
292
293   ops->beneath->to_async (ops->beneath, callback, context);
294 }
295
296 /* Adjusts the size and returns a human readable size suffix.  */
297
298 static const char *
299 record_btrace_adjust_size (unsigned int *size)
300 {
301   unsigned int sz;
302
303   sz = *size;
304
305   if ((sz & ((1u << 30) - 1)) == 0)
306     {
307       *size = sz >> 30;
308       return "GB";
309     }
310   else if ((sz & ((1u << 20) - 1)) == 0)
311     {
312       *size = sz >> 20;
313       return "MB";
314     }
315   else if ((sz & ((1u << 10) - 1)) == 0)
316     {
317       *size = sz >> 10;
318       return "kB";
319     }
320   else
321     return "";
322 }
323
324 /* Print a BTS configuration.  */
325
326 static void
327 record_btrace_print_bts_conf (const struct btrace_config_bts *conf)
328 {
329   const char *suffix;
330   unsigned int size;
331
332   size = conf->size;
333   if (size > 0)
334     {
335       suffix = record_btrace_adjust_size (&size);
336       printf_unfiltered (_("Buffer size: %u%s.\n"), size, suffix);
337     }
338 }
339
340 /* Print a branch tracing configuration.  */
341
342 static void
343 record_btrace_print_conf (const struct btrace_config *conf)
344 {
345   printf_unfiltered (_("Recording format: %s.\n"),
346                      btrace_format_string (conf->format));
347
348   switch (conf->format)
349     {
350     case BTRACE_FORMAT_NONE:
351       return;
352
353     case BTRACE_FORMAT_BTS:
354       record_btrace_print_bts_conf (&conf->bts);
355       return;
356     }
357
358   internal_error (__FILE__, __LINE__, _("Unkown branch trace format."));
359 }
360
361 /* The to_info_record method of target record-btrace.  */
362
363 static void
364 record_btrace_info (struct target_ops *self)
365 {
366   struct btrace_thread_info *btinfo;
367   const struct btrace_config *conf;
368   struct thread_info *tp;
369   unsigned int insns, calls;
370
371   DEBUG ("info");
372
373   tp = find_thread_ptid (inferior_ptid);
374   if (tp == NULL)
375     error (_("No thread."));
376
377   btinfo = &tp->btrace;
378
379   conf = btrace_conf (btinfo);
380   if (conf != NULL)
381     record_btrace_print_conf (conf);
382
383   btrace_fetch (tp);
384
385   insns = 0;
386   calls = 0;
387
388   if (!btrace_is_empty (tp))
389     {
390       struct btrace_call_iterator call;
391       struct btrace_insn_iterator insn;
392
393       btrace_call_end (&call, btinfo);
394       btrace_call_prev (&call, 1);
395       calls = btrace_call_number (&call);
396
397       btrace_insn_end (&insn, btinfo);
398       btrace_insn_prev (&insn, 1);
399       insns = btrace_insn_number (&insn);
400     }
401
402   printf_unfiltered (_("Recorded %u instructions in %u functions for thread "
403                        "%d (%s).\n"), insns, calls, tp->num,
404                      target_pid_to_str (tp->ptid));
405
406   if (btrace_is_replaying (tp))
407     printf_unfiltered (_("Replay in progress.  At instruction %u.\n"),
408                        btrace_insn_number (btinfo->replay));
409 }
410
411 /* Print an unsigned int.  */
412
413 static void
414 ui_out_field_uint (struct ui_out *uiout, const char *fld, unsigned int val)
415 {
416   ui_out_field_fmt (uiout, fld, "%u", val);
417 }
418
419 /* Disassemble a section of the recorded instruction trace.  */
420
421 static void
422 btrace_insn_history (struct ui_out *uiout,
423                      const struct btrace_insn_iterator *begin,
424                      const struct btrace_insn_iterator *end, int flags)
425 {
426   struct gdbarch *gdbarch;
427   struct btrace_insn_iterator it;
428
429   DEBUG ("itrace (0x%x): [%u; %u)", flags, btrace_insn_number (begin),
430          btrace_insn_number (end));
431
432   gdbarch = target_gdbarch ();
433
434   for (it = *begin; btrace_insn_cmp (&it, end) != 0; btrace_insn_next (&it, 1))
435     {
436       const struct btrace_insn *insn;
437
438       insn = btrace_insn_get (&it);
439
440       /* Print the instruction index.  */
441       ui_out_field_uint (uiout, "index", btrace_insn_number (&it));
442       ui_out_text (uiout, "\t");
443
444       /* Disassembly with '/m' flag may not produce the expected result.
445          See PR gdb/11833.  */
446       gdb_disassembly (gdbarch, uiout, NULL, flags, 1, insn->pc, insn->pc + 1);
447     }
448 }
449
450 /* The to_insn_history method of target record-btrace.  */
451
452 static void
453 record_btrace_insn_history (struct target_ops *self, int size, int flags)
454 {
455   struct btrace_thread_info *btinfo;
456   struct btrace_insn_history *history;
457   struct btrace_insn_iterator begin, end;
458   struct cleanup *uiout_cleanup;
459   struct ui_out *uiout;
460   unsigned int context, covered;
461
462   uiout = current_uiout;
463   uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
464                                                        "insn history");
465   context = abs (size);
466   if (context == 0)
467     error (_("Bad record instruction-history-size."));
468
469   btinfo = require_btrace ();
470   history = btinfo->insn_history;
471   if (history == NULL)
472     {
473       struct btrace_insn_iterator *replay;
474
475       DEBUG ("insn-history (0x%x): %d", flags, size);
476
477       /* If we're replaying, we start at the replay position.  Otherwise, we
478          start at the tail of the trace.  */
479       replay = btinfo->replay;
480       if (replay != NULL)
481         begin = *replay;
482       else
483         btrace_insn_end (&begin, btinfo);
484
485       /* We start from here and expand in the requested direction.  Then we
486          expand in the other direction, as well, to fill up any remaining
487          context.  */
488       end = begin;
489       if (size < 0)
490         {
491           /* We want the current position covered, as well.  */
492           covered = btrace_insn_next (&end, 1);
493           covered += btrace_insn_prev (&begin, context - covered);
494           covered += btrace_insn_next (&end, context - covered);
495         }
496       else
497         {
498           covered = btrace_insn_next (&end, context);
499           covered += btrace_insn_prev (&begin, context - covered);
500         }
501     }
502   else
503     {
504       begin = history->begin;
505       end = history->end;
506
507       DEBUG ("insn-history (0x%x): %d, prev: [%u; %u)", flags, size,
508              btrace_insn_number (&begin), btrace_insn_number (&end));
509
510       if (size < 0)
511         {
512           end = begin;
513           covered = btrace_insn_prev (&begin, context);
514         }
515       else
516         {
517           begin = end;
518           covered = btrace_insn_next (&end, context);
519         }
520     }
521
522   if (covered > 0)
523     btrace_insn_history (uiout, &begin, &end, flags);
524   else
525     {
526       if (size < 0)
527         printf_unfiltered (_("At the start of the branch trace record.\n"));
528       else
529         printf_unfiltered (_("At the end of the branch trace record.\n"));
530     }
531
532   btrace_set_insn_history (btinfo, &begin, &end);
533   do_cleanups (uiout_cleanup);
534 }
535
536 /* The to_insn_history_range method of target record-btrace.  */
537
538 static void
539 record_btrace_insn_history_range (struct target_ops *self,
540                                   ULONGEST from, ULONGEST to, int flags)
541 {
542   struct btrace_thread_info *btinfo;
543   struct btrace_insn_history *history;
544   struct btrace_insn_iterator begin, end;
545   struct cleanup *uiout_cleanup;
546   struct ui_out *uiout;
547   unsigned int low, high;
548   int found;
549
550   uiout = current_uiout;
551   uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
552                                                        "insn history");
553   low = from;
554   high = to;
555
556   DEBUG ("insn-history (0x%x): [%u; %u)", flags, low, high);
557
558   /* Check for wrap-arounds.  */
559   if (low != from || high != to)
560     error (_("Bad range."));
561
562   if (high < low)
563     error (_("Bad range."));
564
565   btinfo = require_btrace ();
566
567   found = btrace_find_insn_by_number (&begin, btinfo, low);
568   if (found == 0)
569     error (_("Range out of bounds."));
570
571   found = btrace_find_insn_by_number (&end, btinfo, high);
572   if (found == 0)
573     {
574       /* Silently truncate the range.  */
575       btrace_insn_end (&end, btinfo);
576     }
577   else
578     {
579       /* We want both begin and end to be inclusive.  */
580       btrace_insn_next (&end, 1);
581     }
582
583   btrace_insn_history (uiout, &begin, &end, flags);
584   btrace_set_insn_history (btinfo, &begin, &end);
585
586   do_cleanups (uiout_cleanup);
587 }
588
589 /* The to_insn_history_from method of target record-btrace.  */
590
591 static void
592 record_btrace_insn_history_from (struct target_ops *self,
593                                  ULONGEST from, int size, int flags)
594 {
595   ULONGEST begin, end, context;
596
597   context = abs (size);
598   if (context == 0)
599     error (_("Bad record instruction-history-size."));
600
601   if (size < 0)
602     {
603       end = from;
604
605       if (from < context)
606         begin = 0;
607       else
608         begin = from - context + 1;
609     }
610   else
611     {
612       begin = from;
613       end = from + context - 1;
614
615       /* Check for wrap-around.  */
616       if (end < begin)
617         end = ULONGEST_MAX;
618     }
619
620   record_btrace_insn_history_range (self, begin, end, flags);
621 }
622
623 /* Print the instruction number range for a function call history line.  */
624
625 static void
626 btrace_call_history_insn_range (struct ui_out *uiout,
627                                 const struct btrace_function *bfun)
628 {
629   unsigned int begin, end, size;
630
631   size = VEC_length (btrace_insn_s, bfun->insn);
632   gdb_assert (size > 0);
633
634   begin = bfun->insn_offset;
635   end = begin + size - 1;
636
637   ui_out_field_uint (uiout, "insn begin", begin);
638   ui_out_text (uiout, ",");
639   ui_out_field_uint (uiout, "insn end", end);
640 }
641
642 /* Print the source line information for a function call history line.  */
643
644 static void
645 btrace_call_history_src_line (struct ui_out *uiout,
646                               const struct btrace_function *bfun)
647 {
648   struct symbol *sym;
649   int begin, end;
650
651   sym = bfun->sym;
652   if (sym == NULL)
653     return;
654
655   ui_out_field_string (uiout, "file",
656                        symtab_to_filename_for_display (symbol_symtab (sym)));
657
658   begin = bfun->lbegin;
659   end = bfun->lend;
660
661   if (end < begin)
662     return;
663
664   ui_out_text (uiout, ":");
665   ui_out_field_int (uiout, "min line", begin);
666
667   if (end == begin)
668     return;
669
670   ui_out_text (uiout, ",");
671   ui_out_field_int (uiout, "max line", end);
672 }
673
674 /* Get the name of a branch trace function.  */
675
676 static const char *
677 btrace_get_bfun_name (const struct btrace_function *bfun)
678 {
679   struct minimal_symbol *msym;
680   struct symbol *sym;
681
682   if (bfun == NULL)
683     return "??";
684
685   msym = bfun->msym;
686   sym = bfun->sym;
687
688   if (sym != NULL)
689     return SYMBOL_PRINT_NAME (sym);
690   else if (msym != NULL)
691     return MSYMBOL_PRINT_NAME (msym);
692   else
693     return "??";
694 }
695
696 /* Disassemble a section of the recorded function trace.  */
697
698 static void
699 btrace_call_history (struct ui_out *uiout,
700                      const struct btrace_thread_info *btinfo,
701                      const struct btrace_call_iterator *begin,
702                      const struct btrace_call_iterator *end,
703                      enum record_print_flag flags)
704 {
705   struct btrace_call_iterator it;
706
707   DEBUG ("ftrace (0x%x): [%u; %u)", flags, btrace_call_number (begin),
708          btrace_call_number (end));
709
710   for (it = *begin; btrace_call_cmp (&it, end) < 0; btrace_call_next (&it, 1))
711     {
712       const struct btrace_function *bfun;
713       struct minimal_symbol *msym;
714       struct symbol *sym;
715
716       bfun = btrace_call_get (&it);
717       sym = bfun->sym;
718       msym = bfun->msym;
719
720       /* Print the function index.  */
721       ui_out_field_uint (uiout, "index", bfun->number);
722       ui_out_text (uiout, "\t");
723
724       if ((flags & RECORD_PRINT_INDENT_CALLS) != 0)
725         {
726           int level = bfun->level + btinfo->level, i;
727
728           for (i = 0; i < level; ++i)
729             ui_out_text (uiout, "  ");
730         }
731
732       if (sym != NULL)
733         ui_out_field_string (uiout, "function", SYMBOL_PRINT_NAME (sym));
734       else if (msym != NULL)
735         ui_out_field_string (uiout, "function", MSYMBOL_PRINT_NAME (msym));
736       else if (!ui_out_is_mi_like_p (uiout))
737         ui_out_field_string (uiout, "function", "??");
738
739       if ((flags & RECORD_PRINT_INSN_RANGE) != 0)
740         {
741           ui_out_text (uiout, _("\tinst "));
742           btrace_call_history_insn_range (uiout, bfun);
743         }
744
745       if ((flags & RECORD_PRINT_SRC_LINE) != 0)
746         {
747           ui_out_text (uiout, _("\tat "));
748           btrace_call_history_src_line (uiout, bfun);
749         }
750
751       ui_out_text (uiout, "\n");
752     }
753 }
754
755 /* The to_call_history method of target record-btrace.  */
756
757 static void
758 record_btrace_call_history (struct target_ops *self, int size, int flags)
759 {
760   struct btrace_thread_info *btinfo;
761   struct btrace_call_history *history;
762   struct btrace_call_iterator begin, end;
763   struct cleanup *uiout_cleanup;
764   struct ui_out *uiout;
765   unsigned int context, covered;
766
767   uiout = current_uiout;
768   uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
769                                                        "insn history");
770   context = abs (size);
771   if (context == 0)
772     error (_("Bad record function-call-history-size."));
773
774   btinfo = require_btrace ();
775   history = btinfo->call_history;
776   if (history == NULL)
777     {
778       struct btrace_insn_iterator *replay;
779
780       DEBUG ("call-history (0x%x): %d", flags, size);
781
782       /* If we're replaying, we start at the replay position.  Otherwise, we
783          start at the tail of the trace.  */
784       replay = btinfo->replay;
785       if (replay != NULL)
786         {
787           begin.function = replay->function;
788           begin.btinfo = btinfo;
789         }
790       else
791         btrace_call_end (&begin, btinfo);
792
793       /* We start from here and expand in the requested direction.  Then we
794          expand in the other direction, as well, to fill up any remaining
795          context.  */
796       end = begin;
797       if (size < 0)
798         {
799           /* We want the current position covered, as well.  */
800           covered = btrace_call_next (&end, 1);
801           covered += btrace_call_prev (&begin, context - covered);
802           covered += btrace_call_next (&end, context - covered);
803         }
804       else
805         {
806           covered = btrace_call_next (&end, context);
807           covered += btrace_call_prev (&begin, context- covered);
808         }
809     }
810   else
811     {
812       begin = history->begin;
813       end = history->end;
814
815       DEBUG ("call-history (0x%x): %d, prev: [%u; %u)", flags, size,
816              btrace_call_number (&begin), btrace_call_number (&end));
817
818       if (size < 0)
819         {
820           end = begin;
821           covered = btrace_call_prev (&begin, context);
822         }
823       else
824         {
825           begin = end;
826           covered = btrace_call_next (&end, context);
827         }
828     }
829
830   if (covered > 0)
831     btrace_call_history (uiout, btinfo, &begin, &end, flags);
832   else
833     {
834       if (size < 0)
835         printf_unfiltered (_("At the start of the branch trace record.\n"));
836       else
837         printf_unfiltered (_("At the end of the branch trace record.\n"));
838     }
839
840   btrace_set_call_history (btinfo, &begin, &end);
841   do_cleanups (uiout_cleanup);
842 }
843
844 /* The to_call_history_range method of target record-btrace.  */
845
846 static void
847 record_btrace_call_history_range (struct target_ops *self,
848                                   ULONGEST from, ULONGEST to, int flags)
849 {
850   struct btrace_thread_info *btinfo;
851   struct btrace_call_history *history;
852   struct btrace_call_iterator begin, end;
853   struct cleanup *uiout_cleanup;
854   struct ui_out *uiout;
855   unsigned int low, high;
856   int found;
857
858   uiout = current_uiout;
859   uiout_cleanup = make_cleanup_ui_out_tuple_begin_end (uiout,
860                                                        "func history");
861   low = from;
862   high = to;
863
864   DEBUG ("call-history (0x%x): [%u; %u)", flags, low, high);
865
866   /* Check for wrap-arounds.  */
867   if (low != from || high != to)
868     error (_("Bad range."));
869
870   if (high < low)
871     error (_("Bad range."));
872
873   btinfo = require_btrace ();
874
875   found = btrace_find_call_by_number (&begin, btinfo, low);
876   if (found == 0)
877     error (_("Range out of bounds."));
878
879   found = btrace_find_call_by_number (&end, btinfo, high);
880   if (found == 0)
881     {
882       /* Silently truncate the range.  */
883       btrace_call_end (&end, btinfo);
884     }
885   else
886     {
887       /* We want both begin and end to be inclusive.  */
888       btrace_call_next (&end, 1);
889     }
890
891   btrace_call_history (uiout, btinfo, &begin, &end, flags);
892   btrace_set_call_history (btinfo, &begin, &end);
893
894   do_cleanups (uiout_cleanup);
895 }
896
897 /* The to_call_history_from method of target record-btrace.  */
898
899 static void
900 record_btrace_call_history_from (struct target_ops *self,
901                                  ULONGEST from, int size, int flags)
902 {
903   ULONGEST begin, end, context;
904
905   context = abs (size);
906   if (context == 0)
907     error (_("Bad record function-call-history-size."));
908
909   if (size < 0)
910     {
911       end = from;
912
913       if (from < context)
914         begin = 0;
915       else
916         begin = from - context + 1;
917     }
918   else
919     {
920       begin = from;
921       end = from + context - 1;
922
923       /* Check for wrap-around.  */
924       if (end < begin)
925         end = ULONGEST_MAX;
926     }
927
928   record_btrace_call_history_range (self, begin, end, flags);
929 }
930
931 /* The to_record_is_replaying method of target record-btrace.  */
932
933 static int
934 record_btrace_is_replaying (struct target_ops *self)
935 {
936   struct thread_info *tp;
937
938   ALL_NON_EXITED_THREADS (tp)
939     if (btrace_is_replaying (tp))
940       return 1;
941
942   return 0;
943 }
944
945 /* The to_xfer_partial method of target record-btrace.  */
946
947 static enum target_xfer_status
948 record_btrace_xfer_partial (struct target_ops *ops, enum target_object object,
949                             const char *annex, gdb_byte *readbuf,
950                             const gdb_byte *writebuf, ULONGEST offset,
951                             ULONGEST len, ULONGEST *xfered_len)
952 {
953   struct target_ops *t;
954
955   /* Filter out requests that don't make sense during replay.  */
956   if (replay_memory_access == replay_memory_access_read_only
957       && !record_btrace_generating_corefile
958       && record_btrace_is_replaying (ops))
959     {
960       switch (object)
961         {
962         case TARGET_OBJECT_MEMORY:
963           {
964             struct target_section *section;
965
966             /* We do not allow writing memory in general.  */
967             if (writebuf != NULL)
968               {
969                 *xfered_len = len;
970                 return TARGET_XFER_UNAVAILABLE;
971               }
972
973             /* We allow reading readonly memory.  */
974             section = target_section_by_addr (ops, offset);
975             if (section != NULL)
976               {
977                 /* Check if the section we found is readonly.  */
978                 if ((bfd_get_section_flags (section->the_bfd_section->owner,
979                                             section->the_bfd_section)
980                      & SEC_READONLY) != 0)
981                   {
982                     /* Truncate the request to fit into this section.  */
983                     len = min (len, section->endaddr - offset);
984                     break;
985                   }
986               }
987
988             *xfered_len = len;
989             return TARGET_XFER_UNAVAILABLE;
990           }
991         }
992     }
993
994   /* Forward the request.  */
995   ops = ops->beneath;
996   return ops->to_xfer_partial (ops, object, annex, readbuf, writebuf,
997                                offset, len, xfered_len);
998 }
999
1000 /* The to_insert_breakpoint method of target record-btrace.  */
1001
1002 static int
1003 record_btrace_insert_breakpoint (struct target_ops *ops,
1004                                  struct gdbarch *gdbarch,
1005                                  struct bp_target_info *bp_tgt)
1006 {
1007   volatile struct gdb_exception except;
1008   const char *old;
1009   int ret;
1010
1011   /* Inserting breakpoints requires accessing memory.  Allow it for the
1012      duration of this function.  */
1013   old = replay_memory_access;
1014   replay_memory_access = replay_memory_access_read_write;
1015
1016   ret = 0;
1017   TRY_CATCH (except, RETURN_MASK_ALL)
1018     ret = ops->beneath->to_insert_breakpoint (ops->beneath, gdbarch, bp_tgt);
1019
1020   replay_memory_access = old;
1021
1022   if (except.reason < 0)
1023     throw_exception (except);
1024
1025   return ret;
1026 }
1027
1028 /* The to_remove_breakpoint method of target record-btrace.  */
1029
1030 static int
1031 record_btrace_remove_breakpoint (struct target_ops *ops,
1032                                  struct gdbarch *gdbarch,
1033                                  struct bp_target_info *bp_tgt)
1034 {
1035   volatile struct gdb_exception except;
1036   const char *old;
1037   int ret;
1038
1039   /* Removing breakpoints requires accessing memory.  Allow it for the
1040      duration of this function.  */
1041   old = replay_memory_access;
1042   replay_memory_access = replay_memory_access_read_write;
1043
1044   ret = 0;
1045   TRY_CATCH (except, RETURN_MASK_ALL)
1046     ret = ops->beneath->to_remove_breakpoint (ops->beneath, gdbarch, bp_tgt);
1047
1048   replay_memory_access = old;
1049
1050   if (except.reason < 0)
1051     throw_exception (except);
1052
1053   return ret;
1054 }
1055
1056 /* The to_fetch_registers method of target record-btrace.  */
1057
1058 static void
1059 record_btrace_fetch_registers (struct target_ops *ops,
1060                                struct regcache *regcache, int regno)
1061 {
1062   struct btrace_insn_iterator *replay;
1063   struct thread_info *tp;
1064
1065   tp = find_thread_ptid (inferior_ptid);
1066   gdb_assert (tp != NULL);
1067
1068   replay = tp->btrace.replay;
1069   if (replay != NULL && !record_btrace_generating_corefile)
1070     {
1071       const struct btrace_insn *insn;
1072       struct gdbarch *gdbarch;
1073       int pcreg;
1074
1075       gdbarch = get_regcache_arch (regcache);
1076       pcreg = gdbarch_pc_regnum (gdbarch);
1077       if (pcreg < 0)
1078         return;
1079
1080       /* We can only provide the PC register.  */
1081       if (regno >= 0 && regno != pcreg)
1082         return;
1083
1084       insn = btrace_insn_get (replay);
1085       gdb_assert (insn != NULL);
1086
1087       regcache_raw_supply (regcache, regno, &insn->pc);
1088     }
1089   else
1090     {
1091       struct target_ops *t = ops->beneath;
1092
1093       t->to_fetch_registers (t, regcache, regno);
1094     }
1095 }
1096
1097 /* The to_store_registers method of target record-btrace.  */
1098
1099 static void
1100 record_btrace_store_registers (struct target_ops *ops,
1101                                struct regcache *regcache, int regno)
1102 {
1103   struct target_ops *t;
1104
1105   if (!record_btrace_generating_corefile && record_btrace_is_replaying (ops))
1106     error (_("This record target does not allow writing registers."));
1107
1108   gdb_assert (may_write_registers != 0);
1109
1110   t = ops->beneath;
1111   t->to_store_registers (t, regcache, regno);
1112 }
1113
1114 /* The to_prepare_to_store method of target record-btrace.  */
1115
1116 static void
1117 record_btrace_prepare_to_store (struct target_ops *ops,
1118                                 struct regcache *regcache)
1119 {
1120   struct target_ops *t;
1121
1122   if (!record_btrace_generating_corefile && record_btrace_is_replaying (ops))
1123     return;
1124
1125   t = ops->beneath;
1126   t->to_prepare_to_store (t, regcache);
1127 }
1128
1129 /* The branch trace frame cache.  */
1130
1131 struct btrace_frame_cache
1132 {
1133   /* The thread.  */
1134   struct thread_info *tp;
1135
1136   /* The frame info.  */
1137   struct frame_info *frame;
1138
1139   /* The branch trace function segment.  */
1140   const struct btrace_function *bfun;
1141 };
1142
1143 /* A struct btrace_frame_cache hash table indexed by NEXT.  */
1144
1145 static htab_t bfcache;
1146
1147 /* hash_f for htab_create_alloc of bfcache.  */
1148
1149 static hashval_t
1150 bfcache_hash (const void *arg)
1151 {
1152   const struct btrace_frame_cache *cache = arg;
1153
1154   return htab_hash_pointer (cache->frame);
1155 }
1156
1157 /* eq_f for htab_create_alloc of bfcache.  */
1158
1159 static int
1160 bfcache_eq (const void *arg1, const void *arg2)
1161 {
1162   const struct btrace_frame_cache *cache1 = arg1;
1163   const struct btrace_frame_cache *cache2 = arg2;
1164
1165   return cache1->frame == cache2->frame;
1166 }
1167
1168 /* Create a new btrace frame cache.  */
1169
1170 static struct btrace_frame_cache *
1171 bfcache_new (struct frame_info *frame)
1172 {
1173   struct btrace_frame_cache *cache;
1174   void **slot;
1175
1176   cache = FRAME_OBSTACK_ZALLOC (struct btrace_frame_cache);
1177   cache->frame = frame;
1178
1179   slot = htab_find_slot (bfcache, cache, INSERT);
1180   gdb_assert (*slot == NULL);
1181   *slot = cache;
1182
1183   return cache;
1184 }
1185
1186 /* Extract the branch trace function from a branch trace frame.  */
1187
1188 static const struct btrace_function *
1189 btrace_get_frame_function (struct frame_info *frame)
1190 {
1191   const struct btrace_frame_cache *cache;
1192   const struct btrace_function *bfun;
1193   struct btrace_frame_cache pattern;
1194   void **slot;
1195
1196   pattern.frame = frame;
1197
1198   slot = htab_find_slot (bfcache, &pattern, NO_INSERT);
1199   if (slot == NULL)
1200     return NULL;
1201
1202   cache = *slot;
1203   return cache->bfun;
1204 }
1205
1206 /* Implement stop_reason method for record_btrace_frame_unwind.  */
1207
1208 static enum unwind_stop_reason
1209 record_btrace_frame_unwind_stop_reason (struct frame_info *this_frame,
1210                                         void **this_cache)
1211 {
1212   const struct btrace_frame_cache *cache;
1213   const struct btrace_function *bfun;
1214
1215   cache = *this_cache;
1216   bfun = cache->bfun;
1217   gdb_assert (bfun != NULL);
1218
1219   if (bfun->up == NULL)
1220     return UNWIND_UNAVAILABLE;
1221
1222   return UNWIND_NO_REASON;
1223 }
1224
1225 /* Implement this_id method for record_btrace_frame_unwind.  */
1226
1227 static void
1228 record_btrace_frame_this_id (struct frame_info *this_frame, void **this_cache,
1229                              struct frame_id *this_id)
1230 {
1231   const struct btrace_frame_cache *cache;
1232   const struct btrace_function *bfun;
1233   CORE_ADDR code, special;
1234
1235   cache = *this_cache;
1236
1237   bfun = cache->bfun;
1238   gdb_assert (bfun != NULL);
1239
1240   while (bfun->segment.prev != NULL)
1241     bfun = bfun->segment.prev;
1242
1243   code = get_frame_func (this_frame);
1244   special = bfun->number;
1245
1246   *this_id = frame_id_build_unavailable_stack_special (code, special);
1247
1248   DEBUG ("[frame] %s id: (!stack, pc=%s, special=%s)",
1249          btrace_get_bfun_name (cache->bfun),
1250          core_addr_to_string_nz (this_id->code_addr),
1251          core_addr_to_string_nz (this_id->special_addr));
1252 }
1253
1254 /* Implement prev_register method for record_btrace_frame_unwind.  */
1255
1256 static struct value *
1257 record_btrace_frame_prev_register (struct frame_info *this_frame,
1258                                    void **this_cache,
1259                                    int regnum)
1260 {
1261   const struct btrace_frame_cache *cache;
1262   const struct btrace_function *bfun, *caller;
1263   const struct btrace_insn *insn;
1264   struct gdbarch *gdbarch;
1265   CORE_ADDR pc;
1266   int pcreg;
1267
1268   gdbarch = get_frame_arch (this_frame);
1269   pcreg = gdbarch_pc_regnum (gdbarch);
1270   if (pcreg < 0 || regnum != pcreg)
1271     throw_error (NOT_AVAILABLE_ERROR,
1272                  _("Registers are not available in btrace record history"));
1273
1274   cache = *this_cache;
1275   bfun = cache->bfun;
1276   gdb_assert (bfun != NULL);
1277
1278   caller = bfun->up;
1279   if (caller == NULL)
1280     throw_error (NOT_AVAILABLE_ERROR,
1281                  _("No caller in btrace record history"));
1282
1283   if ((bfun->flags & BFUN_UP_LINKS_TO_RET) != 0)
1284     {
1285       insn = VEC_index (btrace_insn_s, caller->insn, 0);
1286       pc = insn->pc;
1287     }
1288   else
1289     {
1290       insn = VEC_last (btrace_insn_s, caller->insn);
1291       pc = insn->pc;
1292
1293       pc += gdb_insn_length (gdbarch, pc);
1294     }
1295
1296   DEBUG ("[frame] unwound PC in %s on level %d: %s",
1297          btrace_get_bfun_name (bfun), bfun->level,
1298          core_addr_to_string_nz (pc));
1299
1300   return frame_unwind_got_address (this_frame, regnum, pc);
1301 }
1302
1303 /* Implement sniffer method for record_btrace_frame_unwind.  */
1304
1305 static int
1306 record_btrace_frame_sniffer (const struct frame_unwind *self,
1307                              struct frame_info *this_frame,
1308                              void **this_cache)
1309 {
1310   const struct btrace_function *bfun;
1311   struct btrace_frame_cache *cache;
1312   struct thread_info *tp;
1313   struct frame_info *next;
1314
1315   /* THIS_FRAME does not contain a reference to its thread.  */
1316   tp = find_thread_ptid (inferior_ptid);
1317   gdb_assert (tp != NULL);
1318
1319   bfun = NULL;
1320   next = get_next_frame (this_frame);
1321   if (next == NULL)
1322     {
1323       const struct btrace_insn_iterator *replay;
1324
1325       replay = tp->btrace.replay;
1326       if (replay != NULL)
1327         bfun = replay->function;
1328     }
1329   else
1330     {
1331       const struct btrace_function *callee;
1332
1333       callee = btrace_get_frame_function (next);
1334       if (callee != NULL && (callee->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
1335         bfun = callee->up;
1336     }
1337
1338   if (bfun == NULL)
1339     return 0;
1340
1341   DEBUG ("[frame] sniffed frame for %s on level %d",
1342          btrace_get_bfun_name (bfun), bfun->level);
1343
1344   /* This is our frame.  Initialize the frame cache.  */
1345   cache = bfcache_new (this_frame);
1346   cache->tp = tp;
1347   cache->bfun = bfun;
1348
1349   *this_cache = cache;
1350   return 1;
1351 }
1352
1353 /* Implement sniffer method for record_btrace_tailcall_frame_unwind.  */
1354
1355 static int
1356 record_btrace_tailcall_frame_sniffer (const struct frame_unwind *self,
1357                                       struct frame_info *this_frame,
1358                                       void **this_cache)
1359 {
1360   const struct btrace_function *bfun, *callee;
1361   struct btrace_frame_cache *cache;
1362   struct frame_info *next;
1363
1364   next = get_next_frame (this_frame);
1365   if (next == NULL)
1366     return 0;
1367
1368   callee = btrace_get_frame_function (next);
1369   if (callee == NULL)
1370     return 0;
1371
1372   if ((callee->flags & BFUN_UP_LINKS_TO_TAILCALL) == 0)
1373     return 0;
1374
1375   bfun = callee->up;
1376   if (bfun == NULL)
1377     return 0;
1378
1379   DEBUG ("[frame] sniffed tailcall frame for %s on level %d",
1380          btrace_get_bfun_name (bfun), bfun->level);
1381
1382   /* This is our frame.  Initialize the frame cache.  */
1383   cache = bfcache_new (this_frame);
1384   cache->tp = find_thread_ptid (inferior_ptid);
1385   cache->bfun = bfun;
1386
1387   *this_cache = cache;
1388   return 1;
1389 }
1390
1391 static void
1392 record_btrace_frame_dealloc_cache (struct frame_info *self, void *this_cache)
1393 {
1394   struct btrace_frame_cache *cache;
1395   void **slot;
1396
1397   cache = this_cache;
1398
1399   slot = htab_find_slot (bfcache, cache, NO_INSERT);
1400   gdb_assert (slot != NULL);
1401
1402   htab_remove_elt (bfcache, cache);
1403 }
1404
1405 /* btrace recording does not store previous memory content, neither the stack
1406    frames content.  Any unwinding would return errorneous results as the stack
1407    contents no longer matches the changed PC value restored from history.
1408    Therefore this unwinder reports any possibly unwound registers as
1409    <unavailable>.  */
1410
1411 const struct frame_unwind record_btrace_frame_unwind =
1412 {
1413   NORMAL_FRAME,
1414   record_btrace_frame_unwind_stop_reason,
1415   record_btrace_frame_this_id,
1416   record_btrace_frame_prev_register,
1417   NULL,
1418   record_btrace_frame_sniffer,
1419   record_btrace_frame_dealloc_cache
1420 };
1421
1422 const struct frame_unwind record_btrace_tailcall_frame_unwind =
1423 {
1424   TAILCALL_FRAME,
1425   record_btrace_frame_unwind_stop_reason,
1426   record_btrace_frame_this_id,
1427   record_btrace_frame_prev_register,
1428   NULL,
1429   record_btrace_tailcall_frame_sniffer,
1430   record_btrace_frame_dealloc_cache
1431 };
1432
1433 /* Implement the to_get_unwinder method.  */
1434
1435 static const struct frame_unwind *
1436 record_btrace_to_get_unwinder (struct target_ops *self)
1437 {
1438   return &record_btrace_frame_unwind;
1439 }
1440
1441 /* Implement the to_get_tailcall_unwinder method.  */
1442
1443 static const struct frame_unwind *
1444 record_btrace_to_get_tailcall_unwinder (struct target_ops *self)
1445 {
1446   return &record_btrace_tailcall_frame_unwind;
1447 }
1448
1449 /* Indicate that TP should be resumed according to FLAG.  */
1450
1451 static void
1452 record_btrace_resume_thread (struct thread_info *tp,
1453                              enum btrace_thread_flag flag)
1454 {
1455   struct btrace_thread_info *btinfo;
1456
1457   DEBUG ("resuming %d (%s): %u", tp->num, target_pid_to_str (tp->ptid), flag);
1458
1459   btinfo = &tp->btrace;
1460
1461   if ((btinfo->flags & BTHR_MOVE) != 0)
1462     error (_("Thread already moving."));
1463
1464   /* Fetch the latest branch trace.  */
1465   btrace_fetch (tp);
1466
1467   btinfo->flags |= flag;
1468 }
1469
1470 /* Find the thread to resume given a PTID.  */
1471
1472 static struct thread_info *
1473 record_btrace_find_resume_thread (ptid_t ptid)
1474 {
1475   struct thread_info *tp;
1476
1477   /* When asked to resume everything, we pick the current thread.  */
1478   if (ptid_equal (minus_one_ptid, ptid) || ptid_is_pid (ptid))
1479     ptid = inferior_ptid;
1480
1481   return find_thread_ptid (ptid);
1482 }
1483
1484 /* Start replaying a thread.  */
1485
1486 static struct btrace_insn_iterator *
1487 record_btrace_start_replaying (struct thread_info *tp)
1488 {
1489   volatile struct gdb_exception except;
1490   struct btrace_insn_iterator *replay;
1491   struct btrace_thread_info *btinfo;
1492   int executing;
1493
1494   btinfo = &tp->btrace;
1495   replay = NULL;
1496
1497   /* We can't start replaying without trace.  */
1498   if (btinfo->begin == NULL)
1499     return NULL;
1500
1501   /* Clear the executing flag to allow changes to the current frame.
1502      We are not actually running, yet.  We just started a reverse execution
1503      command or a record goto command.
1504      For the latter, EXECUTING is false and this has no effect.
1505      For the former, EXECUTING is true and we're in to_wait, about to
1506      move the thread.  Since we need to recompute the stack, we temporarily
1507      set EXECUTING to flase.  */
1508   executing = is_executing (tp->ptid);
1509   set_executing (tp->ptid, 0);
1510
1511   /* GDB stores the current frame_id when stepping in order to detects steps
1512      into subroutines.
1513      Since frames are computed differently when we're replaying, we need to
1514      recompute those stored frames and fix them up so we can still detect
1515      subroutines after we started replaying.  */
1516   TRY_CATCH (except, RETURN_MASK_ALL)
1517     {
1518       struct frame_info *frame;
1519       struct frame_id frame_id;
1520       int upd_step_frame_id, upd_step_stack_frame_id;
1521
1522       /* The current frame without replaying - computed via normal unwind.  */
1523       frame = get_current_frame ();
1524       frame_id = get_frame_id (frame);
1525
1526       /* Check if we need to update any stepping-related frame id's.  */
1527       upd_step_frame_id = frame_id_eq (frame_id,
1528                                        tp->control.step_frame_id);
1529       upd_step_stack_frame_id = frame_id_eq (frame_id,
1530                                              tp->control.step_stack_frame_id);
1531
1532       /* We start replaying at the end of the branch trace.  This corresponds
1533          to the current instruction.  */
1534       replay = xmalloc (sizeof (*replay));
1535       btrace_insn_end (replay, btinfo);
1536
1537       /* We're not replaying, yet.  */
1538       gdb_assert (btinfo->replay == NULL);
1539       btinfo->replay = replay;
1540
1541       /* Make sure we're not using any stale registers.  */
1542       registers_changed_ptid (tp->ptid);
1543
1544       /* The current frame with replaying - computed via btrace unwind.  */
1545       frame = get_current_frame ();
1546       frame_id = get_frame_id (frame);
1547
1548       /* Replace stepping related frames where necessary.  */
1549       if (upd_step_frame_id)
1550         tp->control.step_frame_id = frame_id;
1551       if (upd_step_stack_frame_id)
1552         tp->control.step_stack_frame_id = frame_id;
1553     }
1554
1555   /* Restore the previous execution state.  */
1556   set_executing (tp->ptid, executing);
1557
1558   if (except.reason < 0)
1559     {
1560       xfree (btinfo->replay);
1561       btinfo->replay = NULL;
1562
1563       registers_changed_ptid (tp->ptid);
1564
1565       throw_exception (except);
1566     }
1567
1568   return replay;
1569 }
1570
1571 /* Stop replaying a thread.  */
1572
1573 static void
1574 record_btrace_stop_replaying (struct thread_info *tp)
1575 {
1576   struct btrace_thread_info *btinfo;
1577
1578   btinfo = &tp->btrace;
1579
1580   xfree (btinfo->replay);
1581   btinfo->replay = NULL;
1582
1583   /* Make sure we're not leaving any stale registers.  */
1584   registers_changed_ptid (tp->ptid);
1585 }
1586
1587 /* The to_resume method of target record-btrace.  */
1588
1589 static void
1590 record_btrace_resume (struct target_ops *ops, ptid_t ptid, int step,
1591                       enum gdb_signal signal)
1592 {
1593   struct thread_info *tp, *other;
1594   enum btrace_thread_flag flag;
1595
1596   DEBUG ("resume %s: %s", target_pid_to_str (ptid), step ? "step" : "cont");
1597
1598   /* Store the execution direction of the last resume.  */
1599   record_btrace_resume_exec_dir = execution_direction;
1600
1601   tp = record_btrace_find_resume_thread (ptid);
1602   if (tp == NULL)
1603     error (_("Cannot find thread to resume."));
1604
1605   /* Stop replaying other threads if the thread to resume is not replaying.  */
1606   if (!btrace_is_replaying (tp) && execution_direction != EXEC_REVERSE)
1607     ALL_NON_EXITED_THREADS (other)
1608       record_btrace_stop_replaying (other);
1609
1610   /* As long as we're not replaying, just forward the request.  */
1611   if (!record_btrace_is_replaying (ops) && execution_direction != EXEC_REVERSE)
1612     {
1613       ops = ops->beneath;
1614       return ops->to_resume (ops, ptid, step, signal);
1615     }
1616
1617   /* Compute the btrace thread flag for the requested move.  */
1618   if (step == 0)
1619     flag = execution_direction == EXEC_REVERSE ? BTHR_RCONT : BTHR_CONT;
1620   else
1621     flag = execution_direction == EXEC_REVERSE ? BTHR_RSTEP : BTHR_STEP;
1622
1623   /* At the moment, we only move a single thread.  We could also move
1624      all threads in parallel by single-stepping each resumed thread
1625      until the first runs into an event.
1626      When we do that, we would want to continue all other threads.
1627      For now, just resume one thread to not confuse to_wait.  */
1628   record_btrace_resume_thread (tp, flag);
1629
1630   /* We just indicate the resume intent here.  The actual stepping happens in
1631      record_btrace_wait below.  */
1632
1633   /* Async support.  */
1634   if (target_can_async_p ())
1635     {
1636       target_async (inferior_event_handler, 0);
1637       mark_async_event_handler (record_btrace_async_inferior_event_handler);
1638     }
1639 }
1640
1641 /* Find a thread to move.  */
1642
1643 static struct thread_info *
1644 record_btrace_find_thread_to_move (ptid_t ptid)
1645 {
1646   struct thread_info *tp;
1647
1648   /* First check the parameter thread.  */
1649   tp = find_thread_ptid (ptid);
1650   if (tp != NULL && (tp->btrace.flags & BTHR_MOVE) != 0)
1651     return tp;
1652
1653   /* Otherwise, find one other thread that has been resumed.  */
1654   ALL_NON_EXITED_THREADS (tp)
1655     if ((tp->btrace.flags & BTHR_MOVE) != 0)
1656       return tp;
1657
1658   return NULL;
1659 }
1660
1661 /* Return a target_waitstatus indicating that we ran out of history.  */
1662
1663 static struct target_waitstatus
1664 btrace_step_no_history (void)
1665 {
1666   struct target_waitstatus status;
1667
1668   status.kind = TARGET_WAITKIND_NO_HISTORY;
1669
1670   return status;
1671 }
1672
1673 /* Return a target_waitstatus indicating that a step finished.  */
1674
1675 static struct target_waitstatus
1676 btrace_step_stopped (void)
1677 {
1678   struct target_waitstatus status;
1679
1680   status.kind = TARGET_WAITKIND_STOPPED;
1681   status.value.sig = GDB_SIGNAL_TRAP;
1682
1683   return status;
1684 }
1685
1686 /* Clear the record histories.  */
1687
1688 static void
1689 record_btrace_clear_histories (struct btrace_thread_info *btinfo)
1690 {
1691   xfree (btinfo->insn_history);
1692   xfree (btinfo->call_history);
1693
1694   btinfo->insn_history = NULL;
1695   btinfo->call_history = NULL;
1696 }
1697
1698 /* Step a single thread.  */
1699
1700 static struct target_waitstatus
1701 record_btrace_step_thread (struct thread_info *tp)
1702 {
1703   struct btrace_insn_iterator *replay, end;
1704   struct btrace_thread_info *btinfo;
1705   struct address_space *aspace;
1706   struct inferior *inf;
1707   enum btrace_thread_flag flags;
1708   unsigned int steps;
1709
1710   /* We can't step without an execution history.  */
1711   if (btrace_is_empty (tp))
1712     return btrace_step_no_history ();
1713
1714   btinfo = &tp->btrace;
1715   replay = btinfo->replay;
1716
1717   flags = btinfo->flags & BTHR_MOVE;
1718   btinfo->flags &= ~BTHR_MOVE;
1719
1720   DEBUG ("stepping %d (%s): %u", tp->num, target_pid_to_str (tp->ptid), flags);
1721
1722   switch (flags)
1723     {
1724     default:
1725       internal_error (__FILE__, __LINE__, _("invalid stepping type."));
1726
1727     case BTHR_STEP:
1728       /* We're done if we're not replaying.  */
1729       if (replay == NULL)
1730         return btrace_step_no_history ();
1731
1732       /* We are always able to step at least once.  */
1733       steps = btrace_insn_next (replay, 1);
1734       gdb_assert (steps == 1);
1735
1736       /* Determine the end of the instruction trace.  */
1737       btrace_insn_end (&end, btinfo);
1738
1739       /* We stop replaying if we reached the end of the trace.  */
1740       if (btrace_insn_cmp (replay, &end) == 0)
1741         record_btrace_stop_replaying (tp);
1742
1743       return btrace_step_stopped ();
1744
1745     case BTHR_RSTEP:
1746       /* Start replaying if we're not already doing so.  */
1747       if (replay == NULL)
1748         replay = record_btrace_start_replaying (tp);
1749
1750       /* If we can't step any further, we reached the end of the history.  */
1751       steps = btrace_insn_prev (replay, 1);
1752       if (steps == 0)
1753         return btrace_step_no_history ();
1754
1755       return btrace_step_stopped ();
1756
1757     case BTHR_CONT:
1758       /* We're done if we're not replaying.  */
1759       if (replay == NULL)
1760         return btrace_step_no_history ();
1761
1762       inf = find_inferior_ptid (tp->ptid);
1763       aspace = inf->aspace;
1764
1765       /* Determine the end of the instruction trace.  */
1766       btrace_insn_end (&end, btinfo);
1767
1768       for (;;)
1769         {
1770           const struct btrace_insn *insn;
1771
1772           /* We are always able to step at least once.  */
1773           steps = btrace_insn_next (replay, 1);
1774           gdb_assert (steps == 1);
1775
1776           /* We stop replaying if we reached the end of the trace.  */
1777           if (btrace_insn_cmp (replay, &end) == 0)
1778             {
1779               record_btrace_stop_replaying (tp);
1780               return btrace_step_no_history ();
1781             }
1782
1783           insn = btrace_insn_get (replay);
1784           gdb_assert (insn);
1785
1786           DEBUG ("stepping %d (%s) ... %s", tp->num,
1787                  target_pid_to_str (tp->ptid),
1788                  core_addr_to_string_nz (insn->pc));
1789
1790           if (breakpoint_here_p (aspace, insn->pc))
1791             return btrace_step_stopped ();
1792         }
1793
1794     case BTHR_RCONT:
1795       /* Start replaying if we're not already doing so.  */
1796       if (replay == NULL)
1797         replay = record_btrace_start_replaying (tp);
1798
1799       inf = find_inferior_ptid (tp->ptid);
1800       aspace = inf->aspace;
1801
1802       for (;;)
1803         {
1804           const struct btrace_insn *insn;
1805
1806           /* If we can't step any further, we're done.  */
1807           steps = btrace_insn_prev (replay, 1);
1808           if (steps == 0)
1809             return btrace_step_no_history ();
1810
1811           insn = btrace_insn_get (replay);
1812           gdb_assert (insn);
1813
1814           DEBUG ("reverse-stepping %d (%s) ... %s", tp->num,
1815                  target_pid_to_str (tp->ptid),
1816                  core_addr_to_string_nz (insn->pc));
1817
1818           if (breakpoint_here_p (aspace, insn->pc))
1819             return btrace_step_stopped ();
1820         }
1821     }
1822 }
1823
1824 /* The to_wait method of target record-btrace.  */
1825
1826 static ptid_t
1827 record_btrace_wait (struct target_ops *ops, ptid_t ptid,
1828                     struct target_waitstatus *status, int options)
1829 {
1830   struct thread_info *tp, *other;
1831
1832   DEBUG ("wait %s (0x%x)", target_pid_to_str (ptid), options);
1833
1834   /* As long as we're not replaying, just forward the request.  */
1835   if (!record_btrace_is_replaying (ops) && execution_direction != EXEC_REVERSE)
1836     {
1837       ops = ops->beneath;
1838       return ops->to_wait (ops, ptid, status, options);
1839     }
1840
1841   /* Let's find a thread to move.  */
1842   tp = record_btrace_find_thread_to_move (ptid);
1843   if (tp == NULL)
1844     {
1845       DEBUG ("wait %s: no thread", target_pid_to_str (ptid));
1846
1847       status->kind = TARGET_WAITKIND_IGNORE;
1848       return minus_one_ptid;
1849     }
1850
1851   /* We only move a single thread.  We're not able to correlate threads.  */
1852   *status = record_btrace_step_thread (tp);
1853
1854   /* Stop all other threads. */
1855   if (!non_stop)
1856     ALL_NON_EXITED_THREADS (other)
1857       other->btrace.flags &= ~BTHR_MOVE;
1858
1859   /* Start record histories anew from the current position.  */
1860   record_btrace_clear_histories (&tp->btrace);
1861
1862   /* We moved the replay position but did not update registers.  */
1863   registers_changed_ptid (tp->ptid);
1864
1865   return tp->ptid;
1866 }
1867
1868 /* The to_can_execute_reverse method of target record-btrace.  */
1869
1870 static int
1871 record_btrace_can_execute_reverse (struct target_ops *self)
1872 {
1873   return 1;
1874 }
1875
1876 /* The to_decr_pc_after_break method of target record-btrace.  */
1877
1878 static CORE_ADDR
1879 record_btrace_decr_pc_after_break (struct target_ops *ops,
1880                                    struct gdbarch *gdbarch)
1881 {
1882   /* When replaying, we do not actually execute the breakpoint instruction
1883      so there is no need to adjust the PC after hitting a breakpoint.  */
1884   if (record_btrace_is_replaying (ops))
1885     return 0;
1886
1887   return ops->beneath->to_decr_pc_after_break (ops->beneath, gdbarch);
1888 }
1889
1890 /* The to_update_thread_list method of target record-btrace.  */
1891
1892 static void
1893 record_btrace_update_thread_list (struct target_ops *ops)
1894 {
1895   /* We don't add or remove threads during replay.  */
1896   if (record_btrace_is_replaying (ops))
1897     return;
1898
1899   /* Forward the request.  */
1900   ops = ops->beneath;
1901   ops->to_update_thread_list (ops);
1902 }
1903
1904 /* The to_thread_alive method of target record-btrace.  */
1905
1906 static int
1907 record_btrace_thread_alive (struct target_ops *ops, ptid_t ptid)
1908 {
1909   /* We don't add or remove threads during replay.  */
1910   if (record_btrace_is_replaying (ops))
1911     return find_thread_ptid (ptid) != NULL;
1912
1913   /* Forward the request.  */
1914   ops = ops->beneath;
1915   return ops->to_thread_alive (ops, ptid);
1916 }
1917
1918 /* Set the replay branch trace instruction iterator.  If IT is NULL, replay
1919    is stopped.  */
1920
1921 static void
1922 record_btrace_set_replay (struct thread_info *tp,
1923                           const struct btrace_insn_iterator *it)
1924 {
1925   struct btrace_thread_info *btinfo;
1926
1927   btinfo = &tp->btrace;
1928
1929   if (it == NULL || it->function == NULL)
1930     record_btrace_stop_replaying (tp);
1931   else
1932     {
1933       if (btinfo->replay == NULL)
1934         record_btrace_start_replaying (tp);
1935       else if (btrace_insn_cmp (btinfo->replay, it) == 0)
1936         return;
1937
1938       *btinfo->replay = *it;
1939       registers_changed_ptid (tp->ptid);
1940     }
1941
1942   /* Start anew from the new replay position.  */
1943   record_btrace_clear_histories (btinfo);
1944 }
1945
1946 /* The to_goto_record_begin method of target record-btrace.  */
1947
1948 static void
1949 record_btrace_goto_begin (struct target_ops *self)
1950 {
1951   struct thread_info *tp;
1952   struct btrace_insn_iterator begin;
1953
1954   tp = require_btrace_thread ();
1955
1956   btrace_insn_begin (&begin, &tp->btrace);
1957   record_btrace_set_replay (tp, &begin);
1958
1959   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1960 }
1961
1962 /* The to_goto_record_end method of target record-btrace.  */
1963
1964 static void
1965 record_btrace_goto_end (struct target_ops *ops)
1966 {
1967   struct thread_info *tp;
1968
1969   tp = require_btrace_thread ();
1970
1971   record_btrace_set_replay (tp, NULL);
1972
1973   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
1974 }
1975
1976 /* The to_goto_record method of target record-btrace.  */
1977
1978 static void
1979 record_btrace_goto (struct target_ops *self, ULONGEST insn)
1980 {
1981   struct thread_info *tp;
1982   struct btrace_insn_iterator it;
1983   unsigned int number;
1984   int found;
1985
1986   number = insn;
1987
1988   /* Check for wrap-arounds.  */
1989   if (number != insn)
1990     error (_("Instruction number out of range."));
1991
1992   tp = require_btrace_thread ();
1993
1994   found = btrace_find_insn_by_number (&it, &tp->btrace, number);
1995   if (found == 0)
1996     error (_("No such instruction."));
1997
1998   record_btrace_set_replay (tp, &it);
1999
2000   print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2001 }
2002
2003 /* The to_execution_direction target method.  */
2004
2005 static enum exec_direction_kind
2006 record_btrace_execution_direction (struct target_ops *self)
2007 {
2008   return record_btrace_resume_exec_dir;
2009 }
2010
2011 /* The to_prepare_to_generate_core target method.  */
2012
2013 static void
2014 record_btrace_prepare_to_generate_core (struct target_ops *self)
2015 {
2016   record_btrace_generating_corefile = 1;
2017 }
2018
2019 /* The to_done_generating_core target method.  */
2020
2021 static void
2022 record_btrace_done_generating_core (struct target_ops *self)
2023 {
2024   record_btrace_generating_corefile = 0;
2025 }
2026
2027 /* Initialize the record-btrace target ops.  */
2028
2029 static void
2030 init_record_btrace_ops (void)
2031 {
2032   struct target_ops *ops;
2033
2034   ops = &record_btrace_ops;
2035   ops->to_shortname = "record-btrace";
2036   ops->to_longname = "Branch tracing target";
2037   ops->to_doc = "Collect control-flow trace and provide the execution history.";
2038   ops->to_open = record_btrace_open;
2039   ops->to_close = record_btrace_close;
2040   ops->to_async = record_btrace_async;
2041   ops->to_detach = record_detach;
2042   ops->to_disconnect = record_disconnect;
2043   ops->to_mourn_inferior = record_mourn_inferior;
2044   ops->to_kill = record_kill;
2045   ops->to_stop_recording = record_btrace_stop_recording;
2046   ops->to_info_record = record_btrace_info;
2047   ops->to_insn_history = record_btrace_insn_history;
2048   ops->to_insn_history_from = record_btrace_insn_history_from;
2049   ops->to_insn_history_range = record_btrace_insn_history_range;
2050   ops->to_call_history = record_btrace_call_history;
2051   ops->to_call_history_from = record_btrace_call_history_from;
2052   ops->to_call_history_range = record_btrace_call_history_range;
2053   ops->to_record_is_replaying = record_btrace_is_replaying;
2054   ops->to_xfer_partial = record_btrace_xfer_partial;
2055   ops->to_remove_breakpoint = record_btrace_remove_breakpoint;
2056   ops->to_insert_breakpoint = record_btrace_insert_breakpoint;
2057   ops->to_fetch_registers = record_btrace_fetch_registers;
2058   ops->to_store_registers = record_btrace_store_registers;
2059   ops->to_prepare_to_store = record_btrace_prepare_to_store;
2060   ops->to_get_unwinder = &record_btrace_to_get_unwinder;
2061   ops->to_get_tailcall_unwinder = &record_btrace_to_get_tailcall_unwinder;
2062   ops->to_resume = record_btrace_resume;
2063   ops->to_wait = record_btrace_wait;
2064   ops->to_update_thread_list = record_btrace_update_thread_list;
2065   ops->to_thread_alive = record_btrace_thread_alive;
2066   ops->to_goto_record_begin = record_btrace_goto_begin;
2067   ops->to_goto_record_end = record_btrace_goto_end;
2068   ops->to_goto_record = record_btrace_goto;
2069   ops->to_can_execute_reverse = record_btrace_can_execute_reverse;
2070   ops->to_decr_pc_after_break = record_btrace_decr_pc_after_break;
2071   ops->to_execution_direction = record_btrace_execution_direction;
2072   ops->to_prepare_to_generate_core = record_btrace_prepare_to_generate_core;
2073   ops->to_done_generating_core = record_btrace_done_generating_core;
2074   ops->to_stratum = record_stratum;
2075   ops->to_magic = OPS_MAGIC;
2076 }
2077
2078 /* Start recording in BTS format.  */
2079
2080 static void
2081 cmd_record_btrace_bts_start (char *args, int from_tty)
2082 {
2083   volatile struct gdb_exception exception;
2084
2085   if (args != NULL && *args != 0)
2086     error (_("Invalid argument."));
2087
2088   record_btrace_conf.format = BTRACE_FORMAT_BTS;
2089
2090   TRY_CATCH (exception, RETURN_MASK_ALL)
2091     execute_command ("target record-btrace", from_tty);
2092
2093   if (exception.error != 0)
2094     {
2095       record_btrace_conf.format = BTRACE_FORMAT_NONE;
2096       throw_exception (exception);
2097     }
2098 }
2099
2100 /* Alias for "target record".  */
2101
2102 static void
2103 cmd_record_btrace_start (char *args, int from_tty)
2104 {
2105   volatile struct gdb_exception exception;
2106
2107   if (args != NULL && *args != 0)
2108     error (_("Invalid argument."));
2109
2110   record_btrace_conf.format = BTRACE_FORMAT_BTS;
2111
2112   TRY_CATCH (exception, RETURN_MASK_ALL)
2113     execute_command ("target record-btrace", from_tty);
2114
2115   if (exception.error == 0)
2116     return;
2117
2118   record_btrace_conf.format = BTRACE_FORMAT_NONE;
2119   throw_exception (exception);
2120 }
2121
2122 /* The "set record btrace" command.  */
2123
2124 static void
2125 cmd_set_record_btrace (char *args, int from_tty)
2126 {
2127   cmd_show_list (set_record_btrace_cmdlist, from_tty, "");
2128 }
2129
2130 /* The "show record btrace" command.  */
2131
2132 static void
2133 cmd_show_record_btrace (char *args, int from_tty)
2134 {
2135   cmd_show_list (show_record_btrace_cmdlist, from_tty, "");
2136 }
2137
2138 /* The "show record btrace replay-memory-access" command.  */
2139
2140 static void
2141 cmd_show_replay_memory_access (struct ui_file *file, int from_tty,
2142                                struct cmd_list_element *c, const char *value)
2143 {
2144   fprintf_filtered (gdb_stdout, _("Replay memory access is %s.\n"),
2145                     replay_memory_access);
2146 }
2147
2148 /* The "set record btrace bts" command.  */
2149
2150 static void
2151 cmd_set_record_btrace_bts (char *args, int from_tty)
2152 {
2153   printf_unfiltered (_("\"set record btrace bts\" must be followed "
2154                        "by an apporpriate subcommand.\n"));
2155   help_list (set_record_btrace_bts_cmdlist, "set record btrace bts ",
2156              all_commands, gdb_stdout);
2157 }
2158
2159 /* The "show record btrace bts" command.  */
2160
2161 static void
2162 cmd_show_record_btrace_bts (char *args, int from_tty)
2163 {
2164   cmd_show_list (show_record_btrace_bts_cmdlist, from_tty, "");
2165 }
2166
2167 void _initialize_record_btrace (void);
2168
2169 /* Initialize btrace commands.  */
2170
2171 void
2172 _initialize_record_btrace (void)
2173 {
2174   add_prefix_cmd ("btrace", class_obscure, cmd_record_btrace_start,
2175                   _("Start branch trace recording."), &record_btrace_cmdlist,
2176                   "record btrace ", 0, &record_cmdlist);
2177   add_alias_cmd ("b", "btrace", class_obscure, 1, &record_cmdlist);
2178
2179   add_cmd ("bts", class_obscure, cmd_record_btrace_bts_start,
2180            _("\
2181 Start branch trace recording in Branch Trace Store (BTS) format.\n\n\
2182 The processor stores a from/to record for each branch into a cyclic buffer.\n\
2183 This format may not be available on all processors."),
2184            &record_btrace_cmdlist);
2185   add_alias_cmd ("bts", "btrace bts", class_obscure, 1, &record_cmdlist);
2186
2187   add_prefix_cmd ("btrace", class_support, cmd_set_record_btrace,
2188                   _("Set record options"), &set_record_btrace_cmdlist,
2189                   "set record btrace ", 0, &set_record_cmdlist);
2190
2191   add_prefix_cmd ("btrace", class_support, cmd_show_record_btrace,
2192                   _("Show record options"), &show_record_btrace_cmdlist,
2193                   "show record btrace ", 0, &show_record_cmdlist);
2194
2195   add_setshow_enum_cmd ("replay-memory-access", no_class,
2196                         replay_memory_access_types, &replay_memory_access, _("\
2197 Set what memory accesses are allowed during replay."), _("\
2198 Show what memory accesses are allowed during replay."),
2199                            _("Default is READ-ONLY.\n\n\
2200 The btrace record target does not trace data.\n\
2201 The memory therefore corresponds to the live target and not \
2202 to the current replay position.\n\n\
2203 When READ-ONLY, allow accesses to read-only memory during replay.\n\
2204 When READ-WRITE, allow accesses to read-only and read-write memory during \
2205 replay."),
2206                            NULL, cmd_show_replay_memory_access,
2207                            &set_record_btrace_cmdlist,
2208                            &show_record_btrace_cmdlist);
2209
2210   add_prefix_cmd ("bts", class_support, cmd_set_record_btrace_bts,
2211                   _("Set record btrace bts options"),
2212                   &set_record_btrace_bts_cmdlist,
2213                   "set record btrace bts ", 0, &set_record_btrace_cmdlist);
2214
2215   add_prefix_cmd ("bts", class_support, cmd_show_record_btrace_bts,
2216                   _("Show record btrace bts options"),
2217                   &show_record_btrace_bts_cmdlist,
2218                   "show record btrace bts ", 0, &show_record_btrace_cmdlist);
2219
2220   add_setshow_uinteger_cmd ("buffer-size", no_class,
2221                             &record_btrace_conf.bts.size,
2222                             _("Set the record/replay bts buffer size."),
2223                             _("Show the record/replay bts buffer size."), _("\
2224 When starting recording request a trace buffer of this size.  \
2225 The actual buffer size may differ from the requested size.  \
2226 Use \"info record\" to see the actual buffer size.\n\n\
2227 Bigger buffers allow longer recording but also take more time to process \
2228 the recorded execution trace.\n\n\
2229 The trace buffer size may not be changed while recording."), NULL, NULL,
2230                             &set_record_btrace_bts_cmdlist,
2231                             &show_record_btrace_bts_cmdlist);
2232
2233   init_record_btrace_ops ();
2234   add_target (&record_btrace_ops);
2235
2236   bfcache = htab_create_alloc (50, bfcache_hash, bfcache_eq, NULL,
2237                                xcalloc, xfree);
2238
2239   record_btrace_conf.bts.size = 64 * 1024;
2240 }