Advanced python tracing

strace python -m trace --trace myprog.py | grep -v 'write(1,'
Trace python statement execution and syscalls invoked during that simultaneously
Sample Output
echo 'print("Hello world!")' > helloworld.py; strace python -m trace --trace helloworld.py | grep -v 'write(1,'
execve("/usr/bin/python", ["python", "-m", "trace", "--trace", "helloworld.py"], [/* 22 vars */]) = 0
brk(0)                                  = 0x222e000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f364a418000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=81069, ...}) = 0
mmap(NULL, 81069, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f364a404000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
..snip...
write(1, " --- modulename: helloworld, fun"..., 188 --- modulename: helloworld, funcname: <module>
helloworld.py(1): print("Hello world!")
Hello world!
 --- modulename: trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)
) = 188
exit_group(0)                           = ?
+++ exited with 0 +++

0
By: roolebo
2016-05-27 21:01:01

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: