annotate python/attach.py @ 694:ebca6d85213a

File "/usr/lib/python3/dist-packages/IPython/config/__init__.py", line 16, in <module> from .application import * File "/usr/lib/python3/dist-packages/IPython/config/application.py", line 31, in <module> from IPython.config.configurable import SingletonConfigurable File "/usr/lib/python3/dist-packages/IPython/config/configurable.py", line 33, in <module> from IPython.utils.text import indent, wrap_paragraphs File "/usr/lib/python3/dist-packages/IPython/utils/text.py", line 28, in <module> from IPython.external.path import path File "/usr/lib/python3/dist-packages/IPython/external/path/__init__.py", line 2, in <module> from path import * File "/home/jhammel/python/path.py", line 25 print root(path) ^
author Jeff Hammel <k0scist@gmail.com>
date Wed, 09 Jul 2014 16:26:49 -0700
parents 72f33eb3aa23
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
431
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 attach strace to a process:
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 624 sudo strace -p $(pidof firefox)
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 625 kill 6976
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 626 isrunning firefox
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 Optionally kill the process when detached
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 """
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 import optparse
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 import os
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 import subprocess
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 import sys
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 from subprocess import check_output
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 def main(args=sys.argv[1:]):
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 usage = '%prog [options] ProgramName_or_PID'
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 parser = optparse.OptionParser(usage=usage, description=__doc__)
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 parser.add_option('-k', '--kill',
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 action='store_true', default=False,
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 help="kill process after strace is done")
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 options, args = parser.parse_args(args)
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 if len(args) != 1:
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 parser.print_usage()
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 parser.error("Please specify program or PID to attach to")
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 # get the PID
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 try:
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 pid = int(args[0])
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 except ValueError:
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 pid = check_output(["pidof", args[0]]).strip().split()
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 if len(pid) > 1:
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 parser.error("Multiple PIDs found for %s:\n%s" % (args[0],
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 '\n'.join(pid)))
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 # TODO: handle not matching case
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 pid = int(pid[0])
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 # invoke strace
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 subprocess.call(['sudo', 'strace', '-p', str(pid)])
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 # kill if specified
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 os.kill(pid, 9) # TODO: better than SIGKILL
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 if __name__ == '__main__':
72f33eb3aa23 add an attach.py script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 main()