Skip to content

Commit

Permalink
Merge pull request iovisor#445 from goldshtn/tp-vars
Browse files Browse the repository at this point in the history
Allowing more natural syntax for tracepoints with no "tp" struct prefix
  • Loading branch information
4ast committed Mar 24, 2016
2 parents 3abb8d6 + 11cb3bc commit b61519c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
7 changes: 4 additions & 3 deletions man/man8/argdist.8
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ You may use the parameters directly, or valid C expressions that involve the
parameters, such as "size % 10".
Tracepoints may access a special structure called "tp" that is formatted according
to the tracepoint format (which you can obtain using tplist). For example, the
block:block_rq_complete tracepoint can access tp.nr_sector.
block:block_rq_complete tracepoint can access tp.nr_sector. You may also use the
members of the "tp" struct directly, e.g. "nr_sector" instead of "tp.nr_sector".
Return probes can use the argument values received by the
function when it was entered, through the $entry(paramname) special variable.
Return probes can also access the function's return value in $retval, and the
Expand Down Expand Up @@ -147,11 +148,11 @@ Count fork() calls in libc across all processes, grouped by pid:
.TP
Print histogram of number of sectors in completing block I/O requests:
#
.B argdist -H 't:block:block_rq_complete():u32:tp.nr_sector'
.B argdist -H 't:block:block_rq_complete():u32:nr_sector'
.TP
Aggregate interrupts by interrupt request (IRQ):
#
.B argdist -C 't:irq:irq_handler_entry():int:tp.irq'
.B argdist -C 't:irq:irq_handler_entry():int:irq'
.TP
Print histograms of sleep() and nanosleep() parameter values:
#
Expand Down
6 changes: 4 additions & 2 deletions man/man8/trace.8
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ same special keywords as in the predicate (arg1, arg2, etc.).
In tracepoints, both the predicate and the arguments may refer to the tracepoint
format structure, which is stored in the special "tp" variable. For example, the
block:block_rq_complete tracepoint can print or filter by tp.nr_sector. To
discover the format of your tracepoint, use the tplist tool.
discover the format of your tracepoint, use the tplist tool. Note that you can
also use the members of the "tp" struct directly, e.g "nr_sector" instead of
"tp.nr_sector".

The predicate expression and the format specifier replacements for printing
may also use the following special keywords: $pid, $tgid to refer to the
Expand All @@ -118,7 +120,7 @@ Trace returns from the readline function in bash and print the return value as a
.TP
Trace the block:block_rq_complete tracepoint and print the number of sectors completed:
#
.B trace 't:block:block_rq_complete """%d sectors"", tp.nr_sector'
.B trace 't:block:block_rq_complete """%d sectors"", nr_sector'
.SH SOURCE
This is from bcc.
.IP
Expand Down
22 changes: 18 additions & 4 deletions src/python/bcc/tracepoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ def __init__(self, category, event, tp_id):
self.category = category
self.event = event
self.tp_id = tp_id
self._retrieve_struct_fields()

def _generate_struct_fields(self):
def _retrieve_struct_fields(self):
self.struct_fields = []
format_lines = Tracepoint.get_tpoint_format(self.category,
self.event)
text = ""
for line in format_lines:
match = re.search(r'field:([^;]*);.*size:(\d+);', line)
if match is None:
Expand All @@ -126,6 +127,11 @@ def _generate_struct_fields(self):
continue
if field_name.startswith("common_"):
continue
self.struct_fields.append((field_type, field_name))

def _generate_struct_fields(self):
text = ""
for field_type, field_name in self.struct_fields:
text += " %s %s;\n" % (field_type, field_name)
return text

Expand All @@ -134,18 +140,26 @@ def generate_struct(self):
return """
struct %s {
u64 __do_not_use__;
%s
%s
};
""" % (self.struct_name, self._generate_struct_fields())

def _generate_struct_locals(self):
text = ""
for field_type, field_name in self.struct_fields:
text += " %s %s = tp.%s;\n" % (
field_type, field_name, field_name)
return text

def generate_get_struct(self):
return """
u64 tid = bpf_get_current_pid_tgid();
u64 *di = __trace_di.lookup(&tid);
if (di == 0) { return 0; }
struct %s tp = {};
bpf_probe_read(&tp, sizeof(tp), (void *)*di);
""" % self.struct_name
%s
""" % (self.struct_name, self._generate_struct_locals())

@classmethod
def enable_tracepoint(cls, category, event):
Expand Down

0 comments on commit b61519c

Please sign in to comment.