Skip to content

Instantly share code, notes, and snippets.

@robotadam
Created April 13, 2011 03:12
Show Gist options
  • Save robotadam/916896 to your computer and use it in GitHub Desktop.
Save robotadam/916896 to your computer and use it in GitHub Desktop.
import os
import socket
import sys
import threading
import traceback
class SocketDumper(threading.Thread):
daemon = True
def stacktraces(self):
code = []
threads = dict(
(t.ident, t) for t in threading.enumerate())
for thread_id, stack in sys._current_frames().items():
code.append("\n# Thread Name: %s, ThreadID: %s\n" %
(threads[thread_id].name, thread_id))
code.extend(traceback.format_list(traceback.extract_stack(stack)))
return code
def run(self):
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
filename = '/tmp/socketdumper-%s' % os.getpid()
try:
os.remove(filename)
except OSError:
pass
try:
s.bind(filename)
s.listen(1)
print "Serving on %s" % filename
while 1:
conn, addr = s.accept()
conn.sendall(''.join(self.stacktraces()))
conn.close()
finally:
try:
os.remove(filename)
except OSError:
pass
sockthread = None
def launch():
global sockthread
sockthread = SocketDumper()
sockthread.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment