Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a read_trace_fields() Python function #149

Closed
brendangregg opened this issue Aug 18, 2015 · 3 comments
Closed

Add a read_trace_fields() Python function #149

brendangregg opened this issue Aug 18, 2015 · 3 comments

Comments

@brendangregg
Copy link
Member

As a follow on from #136, a function could be added that returned the fields printed by bpf_trace_printk(). Eg, so one can use:

b = BPF(...)
[...]
task, pid, cpu, flags, timestamp, function, arguments = b.read_trace_fields()

This is slightly harder than it sounds: I believe the task name can contain spaces.

drzaeus77 pushed a commit that referenced this issue Aug 24, 2015
Add trace_readline_fields helper to parse the output of trace_pipe
Add field parsing support to trace_print. Addresses #149.
Fix typo in trace_open s/trace/tracefile/
Make nonblocking=False the default in trace_readline
Use IOError vs BlockingIOError for greater compatibility (untested)

Fixes: #149
Signed-off-by: Brenden Blanco <bblanco@plumgrid.com>
@drzaeus77
Copy link
Collaborator

I added the suggested change, as well as an option to trace_print:
example simplistic json printer:

b.trace_print(fmt='{{"pid":{1}, "msg":"{5}"}}')
sudo ./hello_world.py 
{"pid":1118, "msg":"Hello, World!"}
{"pid":1118, "msg":"Hello, World!"}
{"pid":8696, "msg":"Hello, World!"}

@brendangregg
Copy link
Member Author

Thanks, trace_readline_fields() works for me.

I'm not sure about the fmt= option's stability. Eg, the number of fields have changed in the past, which if it happened again would break indexes. But that's probably very unlikely...

Here's an example of using the new function (disksnoop.py):

Before:

while 1:
        try:
                line = trace.readline().rstrip()
        except KeyboardInterrupt:
                pass; exit()
        prolog, time_s, colon, bytes_s, flags_s, us_s = \
                line.rsplit(" ", 5)

        time_s = time_s[:-1]    # strip trailing ":"

After:

while 1:
        (task, pid, cpu, flags, ts, msg) = b.trace_readline_fields()
        (bytes_s, bflags_s, us_s) = msg.split()

So that's getting much better. And this should be commonly used.

Maybe as a follow on issue, we could have trace_readline_fields() split the msg on whitespace, so the above could be one line. I also think this may be better called just trace_fields().

@drzaeus77
Copy link
Collaborator

Shortening to trace_fields is fine to me.

Splitting msg doesn't seem right to me, since the formatting of msg is completely up to the user...trace_fields() can't know to split or not to split.

drzaeus77 pushed a commit that referenced this issue Sep 5, 2015
* Per suggestion in #149

Signed-off-by: Brenden Blanco <bblanco@plumgrid.com>
brendangregg pushed a commit to brendangregg/bcc that referenced this issue Sep 7, 2015
* Per suggestion in iovisor#149

Signed-off-by: Brenden Blanco <bblanco@plumgrid.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants