From 3c8313f95fa9eeba6831a1d71bf14082dcb10556 Mon Sep 17 00:00:00 2001 From: Dmitry Trofimov Date: Tue, 28 Oct 2014 23:58:48 +0100 Subject: [PATCH] Execute settrace only for process threads in lldb (PY-14247). --- .../add_code_to_python_process.py | 25 +++++++++++-------- .../linux/lldb_threads_prepare.py | 17 +++++++++++++ .../linux/lldb_threads_settrace.py | 9 +++---- 3 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 python/helpers/pydev/pydevd_attach_to_process/linux/lldb_threads_prepare.py diff --git a/python/helpers/pydev/pydevd_attach_to_process/add_code_to_python_process.py b/python/helpers/pydev/pydevd_attach_to_process/add_code_to_python_process.py index a1ec61e9ba4c..cf698e5230da 100644 --- a/python/helpers/pydev/pydevd_attach_to_process/add_code_to_python_process.py +++ b/python/helpers/pydev/pydevd_attach_to_process/add_code_to_python_process.py @@ -418,10 +418,7 @@ def run_python_code_linux(pid, python_code, connect_debugger_tracing=False, show if not os.path.exists(target_dll): raise RuntimeError('Could not find dll file to inject: %s' % target_dll) - gdb_threads_settrace_file = os.path.join(filedir, 'linux', 'gdb_threads_settrace.py') - gdb_threads_settrace_file = os.path.normpath(gdb_threads_settrace_file) - if not os.path.exists(gdb_threads_settrace_file): - raise RuntimeError('Could not find file to settrace: %s' % gdb_threads_settrace_file) + gdb_threads_settrace_file = find_helper_script(filedir, 'gdb_threads_settrace.py') # Note: we currently don't support debug builds is_debug = 0 @@ -476,6 +473,15 @@ def run_python_code_linux(pid, python_code, connect_debugger_tracing=False, show return out, err +def find_helper_script(filedir, script_name): + lldb_threads_settrace_file = os.path.join(filedir, 'linux', script_name) + lldb_threads_settrace_file = os.path.normpath(lldb_threads_settrace_file) + if not os.path.exists(lldb_threads_settrace_file): + raise RuntimeError('Could not find file to settrace: %s' % lldb_threads_settrace_file) + + return lldb_threads_settrace_file + + def run_python_code_mac(pid, python_code, connect_debugger_tracing=False, show_debug_info=0): assert '\'' not in python_code, 'Having a single quote messes with our command.' filedir = os.path.dirname(__file__) @@ -498,12 +504,10 @@ def run_python_code_mac(pid, python_code, connect_debugger_tracing=False, show_d if not os.path.exists(target_dll): raise RuntimeError('Could not find dll file to inject: %s' % target_dll) - lldb_threads_settrace_file = os.path.join(filedir, 'linux', 'lldb_threads_settrace.py') - lldb_threads_settrace_file = os.path.normpath(lldb_threads_settrace_file) - if not os.path.exists(lldb_threads_settrace_file): - raise RuntimeError('Could not find file to settrace: %s' % lldb_threads_settrace_file) - + lldb_threads_settrace_file = find_helper_script(filedir, 'lldb_threads_settrace.py') + lldb_threads_prepare_file = find_helper_script(filedir, 'lldb_threads_prepare.py') # Note: we currently don't support debug builds + is_debug = 0 # Note that the space in the beginning of each line in the multi-line is important! cmd = [ @@ -521,8 +525,9 @@ def run_python_code_mac(pid, python_code, connect_debugger_tracing=False, show_d cmd.extend([ "-o 'process attach --pid %d'"%pid, + "-o 'command script import \'%s\''" % (lldb_threads_prepare_file,), "-o 'expr (void*)dlopen(\"%s\", 2);'" % target_dll, - "-o 'expr (int)hello();'", + # "-o 'expr (int)hello();'", "-o 'expr (int)DoAttach(%s, \"%s\", %s);'" % ( is_debug, python_code, show_debug_info), ]) diff --git a/python/helpers/pydev/pydevd_attach_to_process/linux/lldb_threads_prepare.py b/python/helpers/pydev/pydevd_attach_to_process/linux/lldb_threads_prepare.py new file mode 100644 index 000000000000..3495cd5e25ec --- /dev/null +++ b/python/helpers/pydev/pydevd_attach_to_process/linux/lldb_threads_prepare.py @@ -0,0 +1,17 @@ +# This file is meant to be run inside lldb as a command before +# attaching the debugger to mark process threads as suspended +# to distinguish them from debugger threads later + +def __lldb_init_module(debugger, internal_dict): + import lldb + + try: + target = debugger.GetSelectedTarget() + if target: + process = target.GetProcess() + if process: + for thread in process: + internal_dict['_thread_%d' % thread.GetThreadID()] = True + # thread.Suspend() + except: + import traceback;traceback.print_exc() diff --git a/python/helpers/pydev/pydevd_attach_to_process/linux/lldb_threads_settrace.py b/python/helpers/pydev/pydevd_attach_to_process/linux/lldb_threads_settrace.py index b457e1547cd2..b645807367b5 100644 --- a/python/helpers/pydev/pydevd_attach_to_process/linux/lldb_threads_settrace.py +++ b/python/helpers/pydev/pydevd_attach_to_process/linux/lldb_threads_settrace.py @@ -15,14 +15,11 @@ def __lldb_init_module(debugger, internal_dict): for thread in process: # Get the first frame print('Thread %s, suspended %s\n'%(thread, thread.IsStopped())) + print(thread.GetName()) process.SetSelectedThread(thread) - if not thread.IsStopped(): - error = process.Stop() - print(error) - - if thread: + if getattr(internal_dict, '_thread_%d' % thread.GetThreadID(), False): frame = thread.GetSelectedFrame() if frame: print('Will settrace in: %s' % (frame,)) @@ -31,6 +28,6 @@ def __lldb_init_module(debugger, internal_dict): error = res.GetError() if error: print(error) - thread.Resume() + thread.Resume() except: import traceback;traceback.print_exc()