Re: [patch v9 02/23] linux, btrace: perf_event based branch tracing

Jan Kratochvil
2013-03-05T20:06:34Z

On Mon, 04 Mar 2013 18:05:49 +0100, Markus Metzger wrote:
> Implement branch tracing on Linux based on perf_event such taht it can be shared
> between gdb and gdbserver.
>
> The actual btrace target ops will be implemented on top.
>
> 2013-03-04 Markus Metzger <markus.t.metzger@intel.com>
>
> * common/linux_btrace.h: New file.
> * common/linux_btrace.c: New file.
> * Makefile.in (SFILES): Add btrace.c.
> (HFILES_NO_SRCDIR): Add common/linux-btrace.h.
> (COMMON_OBS): Add btrace.o.
> (linux-btrace.o): New rule.
>
> gdbserver/
> * Makefile.in (SFILES): Add $(srcdir)/common/linux-btrace.c.
> (linux_btrace_h): New variable.
> (linux-btrace.o): New rule.
[...]
> --- /dev/null
> +++ b/gdb/common/linux-btrace.c
[...]
> +/* See linux-btrace.h. */
> +
> +struct btrace_target_info *
> +linux_enable_btrace (ptid_t ptid)
> +{
> + struct btrace_target_info *tinfo;
> + int pid;
> +
> + tinfo = xzalloc (sizeof (*tinfo));
> + tinfo->ptid = ptid;
> +
> + tinfo->attr.size = sizeof (tinfo->attr);
> + tinfo->attr.type = PERF_TYPE_HARDWARE;
> + tinfo->attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
> + tinfo->attr.sample_period = 1;
> +
> + /* We sample from and to address. */
> + tinfo->attr.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_ADDR;
> +
> + tinfo->attr.exclude_kernel = 1;
> + tinfo->attr.exclude_hv = 1;
> + tinfo->attr.exclude_idle = 1;
> +
> + tinfo->ptr_bits = 0;
> +
> + pid = ptid_get_lwp (ptid);
> + if (pid == 0)
> + pid = ptid_get_pid (ptid);
> +
> + errno = 0;
> + tinfo->file = syscall (SYS_perf_event_open, &tinfo->attr, pid, -1, -1, 0);
> + if (tinfo->file < 0)
> + goto err;
> +
> + /* We hard-code the trace buffer size.
> + At some later time, we should make this configurable. */
> + tinfo->size = 1;
> + tinfo->buffer = mmap (NULL, perf_event_mmap_size (tinfo),
> + PROT_READ, MAP_SHARED, tinfo->file, 0);
> + if (tinfo->buffer == MAP_FAILED)
> + goto err_file;
> +
> + return tinfo;
> +
> +err_file:

Labels should be indented by one space so they do not clash with function
names, like:
err_file:

> + close (tinfo->file);
> +
> +err:
> + xfree (tinfo);
> + return NULL;
> +}

OK for check-in.

Thanks,
Jan