btrace: kernel address filtering

For the BTS recording format, we sometimes get a FROM->TO record where the
FROM address lies in the kernel and the TO address lies in user space at
whatever address the user process was resumed.

GDB has a heuristic to filter out such records based on looking at the most
significant bit in the PC.  This works fine for 64-bit systems but it doesn't
always work for 32-bit systems.  Libraries that are loaded at fairly high
addresses might be mistaken for kernel code and branches inside the library
are filtered out.

Change the heuristic to (again heuristically) try to determine the lowest
address in kernel space.  Any PC that is smaller than that should be in
user space.

On today's systems, there should be a symbol "_text" at that address.
Read /proc/kallsyms and search for that symbol.

It is not guaranteed that /proc/kallsyms is readable on all systems.  On
64-bit systems, we fall back to check the most significant bit.  On 32-bit
systems, we refrain from filtering out addresses.

The filtering should really be done by the kernel.  And it soon will be:
https://lkml.org/lkml/2015/8/31/212.

gdb/
	* nat/linux-btrace.h (struct btrace_target_info) <ptr_bits>: Remove.
	* nat/linux-btrace.c: Include filestuff.h and inttypes.h.
	Remove include of sys/utsname.h.
	(linux_determine_kernel_ptr_bits): Remove.
	(linux_determine_kernel_start): New.
	(perf_event_is_kernel_addr): Remove tinfo argument.  Update users.
	Update check.
	(perf_event_skip_bts_record): Remove tinfo argument.  Update users.
	(linux_enable_bts, linux_enable_pt): Remove tinfo->ptr_bits
	initialization.
	* x86-linux-nat.c (x86_linux_enable_btrace): Remove ptr_bits
	assignment.

gdbserver/
	* linux-low.c (linux_low_enable_btrace): Remove.
	(linux_target_ops): Replace linux_low_enable_btrace with
	linux_enable_btrace.
This commit is contained in:
Markus Metzger 2015-08-11 11:05:58 +02:00
parent c0fa8fbd1c
commit 0568462bbf
6 changed files with 73 additions and 64 deletions

View file

@ -6663,26 +6663,6 @@ linux_qxfer_libraries_svr4 (const char *annex, unsigned char *readbuf,
#ifdef HAVE_LINUX_BTRACE
/* See to_enable_btrace target method. */
static struct btrace_target_info *
linux_low_enable_btrace (ptid_t ptid, const struct btrace_config *conf)
{
struct btrace_target_info *tinfo;
tinfo = linux_enable_btrace (ptid, conf);
if (tinfo != NULL && tinfo->ptr_bits == 0)
{
struct thread_info *thread = find_thread_ptid (ptid);
struct regcache *regcache = get_thread_regcache (thread, 0);
tinfo->ptr_bits = register_size (regcache->tdesc, 0) * 8;
}
return tinfo;
}
/* See to_disable_btrace target method. */
static int
@ -6936,7 +6916,7 @@ static struct target_ops linux_target_ops = {
linux_supports_agent,
#ifdef HAVE_LINUX_BTRACE
linux_supports_btrace,
linux_low_enable_btrace,
linux_enable_btrace,
linux_low_disable_btrace,
linux_low_read_btrace,
linux_low_btrace_conf,