[orca] Add some debugging methods



commit 269aa47cfe66f155edc9ce8314ca39f6ab50c066
Author: Joanmarie Diggs <jdiggs igalia com>
Date:   Thu Feb 21 09:48:22 2013 -0500

    Add some debugging methods

 src/orca/debug.py          |   59 ++++++++++++++++++++++++++++++++++++++++++++
 src/orca/orca.py           |   30 +---------------------
 src/orca/script_manager.py |    2 +
 3 files changed, 62 insertions(+), 29 deletions(-)
---
diff --git a/src/orca/debug.py b/src/orca/debug.py
index b66949e..bfeb4de 100644
--- a/src/orca/debug.py
+++ b/src/orca/debug.py
@@ -30,7 +30,9 @@ __license__   = "LGPL"
 
 import inspect
 import traceback
+import os
 import pyatspi
+import subprocess
 import sys
 
 from . import orca_state
@@ -444,3 +446,60 @@ def traceit(frame, event, arg):
     println(LEVEL_ALL, output)
 
     return traceit
+
+def getOpenFDCount(pid):
+    procs = subprocess.check_output([ 'lsof', '-w', '-Ff', '-p', str(pid)])
+    procs = procs.decode('UTF-8').split('\n')
+    files = list(filter(lambda s: s and s[0] == 'f' and s[1:].isdigit(), procs))
+
+    return len(files)
+
+def getCmdline(pid):
+    try:
+        openFile = os.popen('cat /proc/%s/cmdline' % pid)
+        cmdline = openFile.read()
+        openFile.close()
+    except:
+        cmdline = '(Could not obtain cmdline)'
+    cmdline = cmdline.replace('\x00', ' ')
+
+    return cmdline
+
+def pidOf(procName):
+    openFile = subprocess.Popen('pgrep %s' % procName,
+                                shell=True,
+                                stdout=subprocess.PIPE).stdout
+    pids = openFile.read()
+    openFile.close()
+    return [int(p) for p in pids.split()]
+
+def examineProcesses():
+    desktop = pyatspi.Registry.getDesktop(0)
+    println(LEVEL_ALL, 'INFO: Desktop has %i apps:' % desktop.childCount)
+    for i, app in enumerate(desktop):
+        pid = app.get_process_id()
+        cmd = getCmdline(pid)
+        fds = getOpenFDCount(pid)
+        try:
+            name = app.name
+        except:
+            name = 'ERROR: Could not get name'
+        else:
+            if name == '':
+                name = 'WARNING: Possible hang'
+        println(LEVEL_ALL, '%3i. %s (pid: %s) %s file descriptors: %i' \
+                    % (i+1, name, pid, cmd, fds))
+
+    # Other 'suspect' processes which might not show up as accessible apps.
+    otherApps = ['apport']
+    for app in otherApps:
+        pids = pidOf(app)
+        if not pids:
+            println(LEVEL_ALL, 'INFO: no pid for %s' % app)
+            continue
+
+        for pid in pids:
+            cmd = getCmdline(pid)
+            fds = getOpenFDCount(pid)
+            println(LEVEL_ALL, 'INFO: %s (pid: %s) %s file descriptors: %i' \
+                        % (app, pid, cmd, fds))
diff --git a/src/orca/orca.py b/src/orca/orca.py
index 834f40f..d492d9e 100644
--- a/src/orca/orca.py
+++ b/src/orca/orca.py
@@ -626,7 +626,7 @@ def timeout(signum=None, frame=None):
     debug.println(debug.LEVEL_SEVERE,
                   "TIMEOUT: something has hung.  Aborting.")
     debug.printStack(debug.LEVEL_ALL)
-    examineProcesses()
+    debug.examineProcesses()
     die(EXIT_CODE_HANG)
 
 def shutdown(script=None, inputEvent=None):
@@ -724,34 +724,6 @@ def abortOnSignal(signum, frame):
                   % signum)
     die(signum)
 
-def getCmdline(pid):
-    try:
-        openFile = os.popen('cat /proc/%s/cmdline' % pid)
-        cmdline = openFile.read()
-        openFile.close()
-    except:
-        cmdline = '(Could not obtain cmdline)'
-    cmdline = cmdline.replace('\x00', ' ')
-
-    return cmdline
-
-def examineProcesses():
-    desktop = pyatspi.Registry.getDesktop(0)
-    debug.println(
-        debug.LEVEL_ALL, 'INFO: Desktop has %i apps:' % desktop.childCount)
-    for i, app in enumerate(desktop):
-        pid = app.get_process_id()
-        cmd = getCmdline(pid)
-        try:
-            name = app.name
-        except:
-            name = 'ERROR: Could not get name'
-        else:
-            if name == '':
-                name = 'WARNING: Possible hang'
-        debug.println(
-            debug.LEVEL_ALL, '%3i. %s (pid: %s) %s' % (i+1, name, pid, cmd))
-
 def main():
     """The main entry point for Orca.  The exit codes for Orca will
     loosely be based on signals, where the exit code will be the
diff --git a/src/orca/script_manager.py b/src/orca/script_manager.py
index 6cd38a6..698a7e3 100644
--- a/src/orca/script_manager.py
+++ b/src/orca/script_manager.py
@@ -160,6 +160,8 @@ class ScriptManager:
                 debug.println(
                     debug.LEVEL_FINE, "Could not import %s.py" % moduleName)
                 continue
+            except OSError:
+                debug.examineProcesses()
 
             debug.println(debug.LEVEL_FINE, "Found %s.py" % moduleName)
             try:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]