changeset 431:72f33eb3aa23

add an attach.py script
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 08 Aug 2013 00:24:41 -0700
parents 92ee74af9456
children a9890ec0eab6
files .bashrc python/attach.py
diffstat 2 files changed, 53 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.bashrc	Wed Aug 07 22:11:53 2013 -0700
+++ b/.bashrc	Thu Aug 08 00:24:41 2013 -0700
@@ -41,9 +41,10 @@
 alias distribute='python setup.py egg_info -RDb "" sdist register upload'
 alias grep='grep --colour=auto'
 alias ls='ls --color=auto'
-alias maze='python <(curl https://raw.github.com/joewing/maze/master/maze.py)'
+alias maze='python <(curl https://raw.github.com/joewing/maze/master/maze.py 2> /dev/null)'
 alias patch='patch --reject-file=-'
 alias random="python -c 'import sys, random; foo = sys.argv[1:]; random.shuffle(foo); print \" \".join(foo)'"
+alias straceff="attach.py firefox --kill"
 alias weekstamp="date --date=\"$((`date '+%u'`-1)) days ago\" '+%b %d'"
 alias wget='wget --no-check-certificate'
 alias xpcshell="LD_LIBRARY_PATH=${MOZOBJ}/dist/bin ${MOZOBJ}/dist/bin/xpcshell"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/attach.py	Thu Aug 08 00:24:41 2013 -0700
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+
+"""
+attach strace to a process:
+
+624  sudo strace -p $(pidof firefox)
+625  kill 6976
+626  isrunning firefox
+
+Optionally kill the process when detached
+"""
+
+
+import optparse
+import os
+import subprocess
+import sys
+
+from subprocess import check_output
+
+def main(args=sys.argv[1:]):
+
+    usage = '%prog [options] ProgramName_or_PID'
+    parser = optparse.OptionParser(usage=usage, description=__doc__)
+    parser.add_option('-k', '--kill',
+                      action='store_true', default=False,
+                      help="kill process after strace is done")
+    options, args = parser.parse_args(args)
+    if len(args) != 1:
+        parser.print_usage()
+        parser.error("Please specify program or PID to attach to")
+
+    # get the PID
+    try:
+        pid = int(args[0])
+    except ValueError:
+        pid = check_output(["pidof", args[0]]).strip().split()
+        if len(pid) > 1:
+            parser.error("Multiple PIDs found for %s:\n%s" % (args[0],
+                                                              '\n'.join(pid)))
+        # TODO: handle not matching case
+        pid = int(pid[0])
+
+    # invoke strace
+    subprocess.call(['sudo', 'strace', '-p', str(pid)])
+
+    # kill if specified
+    os.kill(pid, 9) # TODO: better than SIGKILL
+
+if __name__ == '__main__':
+    main()