[PATCH v4 05/12] btrace: Use function segment index in insn iterator.

Tim Wiederhake
2017-05-10T11:48:09Z

Remove FUNCTION pointer in struct btrace_insn_iterator and use an index into
the list of function segments instead.

2017-05-10 Tim Wiederhake <tim.wiederhake@intel.com>

gdb/ChangeLog:

* btrace.c: (btrace_insn_get, btrace_insn_get_error, btrace_insn_number,
btrace_insn_begin, btrace_insn_end, btrace_insn_next, btrace_insn_prev,
btrace_find_insn_by_number): Replace function segment pointer with
index.
(btrace_insn_cmp): Simplify.
* btrace.h: (struct btrace_insn_iterator) Rename index to
insn_index. Replace function segment pointer with index into function
segment vector.
* record-btrace.c (record_btrace_call_history): Replace function
segment pointer use with index.
(record_btrace_frame_sniffer): Retrieve function call segment through
vector.
(record_btrace_set_replay): Remove defunc't safety check.

---
gdb/btrace.c | 50 ++++++++++++++++++++++++++++----------------------
gdb/btrace.h | 7 +++----
gdb/record-btrace.c | 6 +++---
3 files changed, 34 insertions(+), 29 deletions(-)

diff --git a/gdb/btrace.c b/gdb/btrace.c
index 42ab33d..541d873 100644
--- a/gdb/btrace.c
+++ b/gdb/btrace.c
@@ -2248,8 +2248,8 @@ btrace_insn_get (const struct btrace_insn_iterator *it)
const struct btrace_function *bfun;
unsigned int index, end;

- index = it->index;
- bfun = it->function;
+ index = it->insn_index;
+ bfun = it->btinfo->functions[it->call_index];

/* Check if the iterator points to a gap in the trace. */
if (bfun->errcode != 0)
@@ -2268,7 +2268,10 @@ btrace_insn_get (const struct btrace_insn_iterator *it)
int
btrace_insn_get_error (const struct btrace_insn_iterator *it)
{
- return it->function->errcode;
+ const struct btrace_function *bfun;
+
+ bfun = it->btinfo->functions[it->call_index];
+ return bfun->errcode;
}

/* See btrace.h. */
@@ -2276,7 +2279,10 @@ btrace_insn_get_error (const struct btrace_insn_iterator *it)
unsigned int
btrace_insn_number (const struct btrace_insn_iterator *it)
{
- return it->function->insn_offset + it->index;
+ const struct btrace_function *bfun;
+
+ bfun = it->btinfo->functions[it->call_index];
+ return bfun->insn_offset + it->insn_index;
}

/* See btrace.h. */
@@ -2292,8 +2298,8 @@ btrace_insn_begin (struct btrace_insn_iterator *it,
error (_("No trace."));

it->btinfo = btinfo;
- it->function = bfun;
- it->index = 0;
+ it->call_index = 0;
+ it->insn_index = 0;
}

/* See btrace.h. */
@@ -2318,8 +2324,8 @@ btrace_insn_end (struct btrace_insn_iterator *it,
length -= 1;

it->btinfo = btinfo;
- it->function = bfun;
- it->index = length;
+ it->call_index = bfun->number - 1;
+ it->insn_index = length;
}

/* See btrace.h. */
@@ -2330,9 +2336,9 @@ btrace_insn_next (struct btrace_insn_iterator *it, unsigned int stride)
const struct btrace_function *bfun;
unsigned int index, steps;

- bfun = it->function;
+ bfun = it->btinfo->functions[it->call_index];
steps = 0;
- index = it->index;
+ index = it->insn_index;

while (stride != 0)
{
@@ -2398,8 +2404,8 @@ btrace_insn_next (struct btrace_insn_iterator *it, unsigned int stride)
}

/* Update the iterator. */
- it->function = bfun;
- it->index = index;
+ it->call_index = bfun->number - 1;
+ it->insn_index = index;

return steps;
}
@@ -2412,9 +2418,9 @@ btrace_insn_prev (struct btrace_insn_iterator *it, unsigned int stride)
const struct btrace_function *bfun;
unsigned int index, steps;

- bfun = it->function;
+ bfun = it->btinfo->functions[it->call_index];
steps = 0;
- index = it->index;
+ index = it->insn_index;

while (stride != 0)
{
@@ -2456,8 +2462,8 @@ btrace_insn_prev (struct btrace_insn_iterator *it, unsigned int stride)
}

/* Update the iterator. */
- it->function = bfun;
- it->index = index;
+ it->call_index = bfun->number - 1;
+ it->insn_index = index;

return steps;
}
@@ -2468,12 +2474,12 @@ int
btrace_insn_cmp (const struct btrace_insn_iterator *lhs,
const struct btrace_insn_iterator *rhs)
{
- unsigned int lnum, rnum;
+ gdb_assert (lhs->btinfo == rhs->btinfo);

- lnum = btrace_insn_number (lhs);
- rnum = btrace_insn_number (rhs);
+ if (lhs->call_index != rhs->call_index)
+ return lhs->call_index - rhs->call_index;

- return (int) (lnum - rnum);
+ return lhs->insn_index - rhs->insn_index;
}

/* See btrace.h. */
@@ -2522,8 +2528,8 @@ btrace_find_insn_by_number (struct btrace_insn_iterator *it,
}

it->btinfo = btinfo;
- it->function = bfun;
- it->index = number - bfun->insn_offset;
+ it->call_index = bfun->number - 1;
+ it->insn_index = number - bfun->insn_offset;
return 1;
}

diff --git a/gdb/btrace.h b/gdb/btrace.h
index 8fefc84..9dc92b7 100644
--- a/gdb/btrace.h
+++ b/gdb/btrace.h
@@ -195,12 +195,11 @@ struct btrace_insn_iterator
/* The branch trace information for this thread. Will never be NULL. */
const struct btrace_thread_info *btinfo;

- /* The branch trace function segment containing the instruction.
- Will never be NULL. */
- const struct btrace_function *function;
+ /* The index of the function segment in BTINFO->FUNCTIONS. */
+ unsigned int call_index;

/* The index into the function segment's instruction vector. */
- unsigned int index;
+ unsigned int insn_index;
};

/* A branch trace function call iterator. */
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index 86a4b1e..ec940f6 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -1102,7 +1102,7 @@ record_btrace_call_history (struct target_ops *self, int size, int int_flags)
if (replay != NULL)
{
begin.btinfo = btinfo;
- begin.index = replay->function->number - 1;
+ begin.index = replay->call_index;
}
else
btrace_call_end (&begin, btinfo);
@@ -1678,7 +1678,7 @@ record_btrace_frame_sniffer (const struct frame_unwind *self,

replay = tp->btrace.replay;
if (replay != NULL)
- bfun = replay->function;
+ bfun = replay->btinfo->functions[replay->call_index];
}
else
{
@@ -2691,7 +2691,7 @@ record_btrace_set_replay (struct thread_info *tp,

btinfo = &tp->btrace;

- if (it == NULL || it->function == NULL)
+ if (it == NULL)
record_btrace_stop_replaying (tp);
else
{
--
2.7.4