gdb/btrace.h - gdb

Global variables defined

Data types defined

Macros defined

Source code

  1. /* Branch trace support for GDB, the GNU debugger.

  2.    Copyright (C) 2013-2015 Free Software Foundation, Inc.

  3.    Contributed by Intel Corp. <markus.t.metzger@intel.com>.

  4.    This file is part of GDB.

  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 3 of the License, or
  8.    (at your option) any later version.

  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.

  13.    You should have received a copy of the GNU General Public License
  14.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  15. #ifndef BTRACE_H
  16. #define BTRACE_H

  17. /* Branch tracing (btrace) is a per-thread control-flow execution trace of the
  18.    inferior.  For presentation purposes, the branch trace is represented as a
  19.    list of sequential control-flow blocks, one such list per thread.  */

  20. #include "btrace-common.h"

  21. struct thread_info;
  22. struct btrace_function;

  23. /* A branch trace instruction.

  24.    This represents a single instruction in a branch trace.  */
  25. struct btrace_insn
  26. {
  27.   /* The address of this instruction.  */
  28.   CORE_ADDR pc;
  29. };

  30. /* A vector of branch trace instructions.  */
  31. typedef struct btrace_insn btrace_insn_s;
  32. DEF_VEC_O (btrace_insn_s);

  33. /* A doubly-linked list of branch trace function segments.  */
  34. struct btrace_func_link
  35. {
  36.   struct btrace_function *prev;
  37.   struct btrace_function *next;
  38. };

  39. /* Flags for btrace function segments.  */
  40. enum btrace_function_flag
  41. {
  42.   /* The 'up' link interpretation.
  43.      If set, it points to the function segment we returned to.
  44.      If clear, it points to the function segment we called from.  */
  45.   BFUN_UP_LINKS_TO_RET = (1 << 0),

  46.   /* The 'up' link points to a tail call.  This obviously only makes sense
  47.      if bfun_up_links_to_ret is clear.  */
  48.   BFUN_UP_LINKS_TO_TAILCALL = (1 << 1)
  49. };

  50. /* A branch trace function segment.

  51.    This represents a function segment in a branch trace, i.e. a consecutive
  52.    number of instructions belonging to the same function.

  53.    We do not allow function segments without any instructions.  */
  54. struct btrace_function
  55. {
  56.   /* The full and minimal symbol for the function.  Both may be NULL.  */
  57.   struct minimal_symbol *msym;
  58.   struct symbol *sym;

  59.   /* The previous and next segment belonging to the same function.
  60.      If a function calls another function, the former will have at least
  61.      two segments: one before the call and another after the return.  */
  62.   struct btrace_func_link segment;

  63.   /* The previous and next function in control flow order.  */
  64.   struct btrace_func_link flow;

  65.   /* The directly preceding function segment in a (fake) call stack.  */
  66.   struct btrace_function *up;

  67.   /* The instructions in this function segment.
  68.      The instruction vector will never be empty.  */
  69.   VEC (btrace_insn_s) *insn;

  70.   /* The instruction number offset for the first instruction in this
  71.      function segment.  */
  72.   unsigned int insn_offset;

  73.   /* The function number in control-flow order.  */
  74.   unsigned int number;

  75.   /* The function level in a back trace across the entire branch trace.
  76.      A caller's level is one lower than the level of its callee.

  77.      Levels can be negative if we see returns for which we have not seen
  78.      the corresponding calls.  The branch trace thread information provides
  79.      a fixup to normalize function levels so the smallest level is zero.  */
  80.   int level;

  81.   /* The source line range of this function segment (both inclusive).  */
  82.   int lbegin, lend;

  83.   /* A bit-vector of btrace_function_flag.  */
  84.   enum btrace_function_flag flags;
  85. };

  86. /* A branch trace instruction iterator.  */
  87. struct btrace_insn_iterator
  88. {
  89.   /* The branch trace function segment containing the instruction.
  90.      Will never be NULL.  */
  91.   const struct btrace_function *function;

  92.   /* The index into the function segment's instruction vector.  */
  93.   unsigned int index;
  94. };

  95. /* A branch trace function call iterator.  */
  96. struct btrace_call_iterator
  97. {
  98.   /* The branch trace information for this thread.  Will never be NULL.  */
  99.   const struct btrace_thread_info *btinfo;

  100.   /* The branch trace function segment.
  101.      This will be NULL for the iterator pointing to the end of the trace.  */
  102.   const struct btrace_function *function;
  103. };

  104. /* Branch trace iteration state for "record instruction-history".  */
  105. struct btrace_insn_history
  106. {
  107.   /* The branch trace instruction range from BEGIN (inclusive) to
  108.      END (exclusive) that has been covered last time.  */
  109.   struct btrace_insn_iterator begin;
  110.   struct btrace_insn_iterator end;
  111. };

  112. /* Branch trace iteration state for "record function-call-history".  */
  113. struct btrace_call_history
  114. {
  115.   /* The branch trace function range from BEGIN (inclusive) to END (exclusive)
  116.      that has been covered last time.  */
  117.   struct btrace_call_iterator begin;
  118.   struct btrace_call_iterator end;
  119. };

  120. /* Branch trace thread flags.  */
  121. enum btrace_thread_flag
  122. {
  123.   /* The thread is to be stepped forwards.  */
  124.   BTHR_STEP = (1 << 0),

  125.   /* The thread is to be stepped backwards.  */
  126.   BTHR_RSTEP = (1 << 1),

  127.   /* The thread is to be continued forwards.  */
  128.   BTHR_CONT = (1 << 2),

  129.   /* The thread is to be continued backwards.  */
  130.   BTHR_RCONT = (1 << 3),

  131.   /* The thread is to be moved.  */
  132.   BTHR_MOVE = (BTHR_STEP | BTHR_RSTEP | BTHR_CONT | BTHR_RCONT)
  133. };

  134. /* Branch trace information per thread.

  135.    This represents the branch trace configuration as well as the entry point
  136.    into the branch trace data.  For the latter, it also contains the index into
  137.    an array of branch trace blocks used for iterating though the branch trace
  138.    blocks of a thread.  */
  139. struct btrace_thread_info
  140. {
  141.   /* The target branch trace information for this thread.

  142.      This contains the branch trace configuration as well as any
  143.      target-specific information necessary for implementing branch tracing on
  144.      the underlying architecture.  */
  145.   struct btrace_target_info *target;

  146.   /* The current branch trace for this thread (both inclusive).

  147.      The last instruction of END is the current instruction, which is not
  148.      part of the execution history.
  149.      Both will be NULL if there is no branch trace available.  If there is
  150.      branch trace available, both will be non-NULL.  */
  151.   struct btrace_function *begin;
  152.   struct btrace_function *end;

  153.   /* The function level offset.  When added to each function's LEVEL,
  154.      this normalizes the function levels such that the smallest level
  155.      becomes zero.  */
  156.   int level;

  157.   /* A bit-vector of btrace_thread_flag.  */
  158.   enum btrace_thread_flag flags;

  159.   /* The instruction history iterator.  */
  160.   struct btrace_insn_history *insn_history;

  161.   /* The function call history iterator.  */
  162.   struct btrace_call_history *call_history;

  163.   /* The current replay position.  NULL if not replaying.  */
  164.   struct btrace_insn_iterator *replay;
  165. };

  166. /* Enable branch tracing for a thread.  */
  167. extern void btrace_enable (struct thread_info *tp);

  168. /* Disable branch tracing for a thread.
  169.    This will also delete the current branch trace data.  */
  170. extern void btrace_disable (struct thread_info *);

  171. /* Disable branch tracing for a thread during teardown.
  172.    This is similar to btrace_disable, except that it will use
  173.    target_teardown_btrace instead of target_disable_btrace.  */
  174. extern void btrace_teardown (struct thread_info *);

  175. /* Fetch the branch trace for a single thread.  */
  176. extern void btrace_fetch (struct thread_info *);

  177. /* Clear the branch trace for a single thread.  */
  178. extern void btrace_clear (struct thread_info *);

  179. /* Clear the branch trace for all threads when an object file goes away.  */
  180. extern void btrace_free_objfile (struct objfile *);

  181. /* Parse a branch trace xml document into a block vector.  */
  182. extern VEC (btrace_block_s) *parse_xml_btrace (const char*);

  183. /* Dereference a branch trace instruction iterator.  Return a pointer to the
  184.    instruction the iterator points to.  */
  185. extern const struct btrace_insn *
  186.   btrace_insn_get (const struct btrace_insn_iterator *);

  187. /* Return the instruction number for a branch trace iterator.
  188.    Returns one past the maximum instruction number for the end iterator.
  189.    Returns zero if the iterator does not point to a valid instruction.  */
  190. extern unsigned int btrace_insn_number (const struct btrace_insn_iterator *);

  191. /* Initialize a branch trace instruction iterator to point to the begin/end of
  192.    the branch trace.  Throws an error if there is no branch trace.  */
  193. extern void btrace_insn_begin (struct btrace_insn_iterator *,
  194.                                const struct btrace_thread_info *);
  195. extern void btrace_insn_end (struct btrace_insn_iterator *,
  196.                              const struct btrace_thread_info *);

  197. /* Increment/decrement a branch trace instruction iterator by at most STRIDE
  198.    instructions.  Return the number of instructions by which the instruction
  199.    iterator has been advanced.
  200.    Returns zero, if the operation failed or STRIDE had been zero.  */
  201. extern unsigned int btrace_insn_next (struct btrace_insn_iterator *,
  202.                                       unsigned int stride);
  203. extern unsigned int btrace_insn_prev (struct btrace_insn_iterator *,
  204.                                       unsigned int stride);

  205. /* Compare two branch trace instruction iterators.
  206.    Return a negative number if LHS < RHS.
  207.    Return zero if LHS == RHS.
  208.    Return a positive number if LHS > RHS.  */
  209. extern int btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
  210.                             const struct btrace_insn_iterator *rhs);

  211. /* Find an instruction in the function branch trace by its number.
  212.    If the instruction is found, initialize the branch trace instruction
  213.    iterator to point to this instruction and return non-zero.
  214.    Return zero otherwise.  */
  215. extern int btrace_find_insn_by_number (struct btrace_insn_iterator *,
  216.                                        const struct btrace_thread_info *,
  217.                                        unsigned int number);

  218. /* Dereference a branch trace call iterator.  Return a pointer to the
  219.    function the iterator points to or NULL if the interator points past
  220.    the end of the branch trace.  */
  221. extern const struct btrace_function *
  222.   btrace_call_get (const struct btrace_call_iterator *);

  223. /* Return the function number for a branch trace call iterator.
  224.    Returns one past the maximum function number for the end iterator.
  225.    Returns zero if the iterator does not point to a valid function.  */
  226. extern unsigned int btrace_call_number (const struct btrace_call_iterator *);

  227. /* Initialize a branch trace call iterator to point to the begin/end of
  228.    the branch trace.  Throws an error if there is no branch trace.  */
  229. extern void btrace_call_begin (struct btrace_call_iterator *,
  230.                                const struct btrace_thread_info *);
  231. extern void btrace_call_end (struct btrace_call_iterator *,
  232.                              const struct btrace_thread_info *);

  233. /* Increment/decrement a branch trace call iterator by at most STRIDE function
  234.    segments.  Return the number of function segments by which the call
  235.    iterator has been advanced.
  236.    Returns zero, if the operation failed or STRIDE had been zero.  */
  237. extern unsigned int btrace_call_next (struct btrace_call_iterator *,
  238.                                       unsigned int stride);
  239. extern unsigned int btrace_call_prev (struct btrace_call_iterator *,
  240.                                       unsigned int stride);

  241. /* Compare two branch trace call iterators.
  242.    Return a negative number if LHS < RHS.
  243.    Return zero if LHS == RHS.
  244.    Return a positive number if LHS > RHS.  */
  245. extern int btrace_call_cmp (const struct btrace_call_iterator *lhs,
  246.                             const struct btrace_call_iterator *rhs);

  247. /* Find a function in the function branch trace by its NUMBER.
  248.    If the function is found, initialize the branch trace call
  249.    iterator to point to this function and return non-zero.
  250.    Return zero otherwise.  */
  251. extern int btrace_find_call_by_number (struct btrace_call_iterator *,
  252.                                        const struct btrace_thread_info *,
  253.                                        unsigned int number);

  254. /* Set the branch trace instruction history from BEGIN (inclusive) to
  255.    END (exclusive).  */
  256. extern void btrace_set_insn_history (struct btrace_thread_info *,
  257.                                      const struct btrace_insn_iterator *begin,
  258.                                      const struct btrace_insn_iterator *end);

  259. /* Set the branch trace function call history from BEGIN (inclusive) to
  260.    END (exclusive).  */
  261. extern void btrace_set_call_history (struct btrace_thread_info *,
  262.                                      const struct btrace_call_iterator *begin,
  263.                                      const struct btrace_call_iterator *end);

  264. /* Determine if branch tracing is currently replaying TP.  */
  265. extern int btrace_is_replaying (struct thread_info *tp);

  266. /* Return non-zero if the branch trace for TP is empty; zero otherwise.  */
  267. extern int btrace_is_empty (struct thread_info *tp);


  268. #endif /* BTRACE_H */