Skip to content

Commit be4c063

Browse files
fabiozint19h
authored andcommitted
Revert "Sync from pydevd: improve attach to pid (patches by Søren L. Hansen)"
This reverts commit b8e66cb.
1 parent 4331cf5 commit be4c063

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

src/debugpy/_vendored/pydevd/pydevd_attach_to_process/add_code_to_python_process.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -266,17 +266,9 @@ def _win_write_to_shared_named_memory(python_code, pid):
266266
finally:
267267
CloseHandle(filemap)
268268

269-
def escape_for_gdb_and_lldb(s):
270-
"""Returns a version of given string suitable for use as gdb
271-
and lldb function argument"""
272-
if not s:
273-
return '""'
274-
275-
# wrap string in double qoutes, and escape any inner double
276-
# quotes
277-
return '"' + s.replace('"', '\\"') + '"'
278269

279270
def run_python_code_linux(pid, python_code, connect_debugger_tracing=False, show_debug_info=0):
271+
assert '\'' not in python_code, 'Having a single quote messes with our command.'
280272
filedir = os.path.dirname(__file__)
281273

282274
# Valid arguments for arch are i386, i386:x86-64, i386:x64-32, i8086,
@@ -312,14 +304,14 @@ def run_python_code_linux(pid, python_code, connect_debugger_tracing=False, show
312304
# '--batch-silent',
313305
]
314306

315-
cmd.extend(["--eval-command=set scheduler-locking off"]) # If on we'll deadlock.
307+
cmd.extend(["--eval-command='set scheduler-locking off'"]) # If on we'll deadlock.
316308

317-
cmd.extend(["--eval-command=set architecture %s" % arch])
309+
cmd.extend(["--eval-command='set architecture %s'" % arch])
318310

319311
cmd.extend([
320-
"--eval-command=call (void*)dlopen(\"%s\", 2)" % target_dll,
321-
'--eval-command=call (int)DoAttach(%s, %s, %s)' % (
322-
is_debug, escape_for_gdb_and_lldb(python_code), show_debug_info)
312+
"--eval-command='call (void*)dlopen(\"%s\", 2)'" % target_dll,
313+
"--eval-command='call (int)DoAttach(%s, \"%s\", %s)'" % (
314+
is_debug, python_code, show_debug_info)
323315
])
324316

325317
# print ' '.join(cmd)
@@ -331,7 +323,8 @@ def run_python_code_linux(pid, python_code, connect_debugger_tracing=False, show
331323
env.pop('PYTHONPATH', None)
332324
print('Running: %s' % (' '.join(cmd)))
333325
p = subprocess.Popen(
334-
cmd,
326+
' '.join(cmd),
327+
shell=True,
335328
env=env,
336329
stdout=subprocess.PIPE,
337330
stderr=subprocess.PIPE,
@@ -353,6 +346,7 @@ def find_helper_script(filedir, script_name):
353346

354347

355348
def run_python_code_mac(pid, python_code, connect_debugger_tracing=False, show_debug_info=0):
349+
assert '\'' not in python_code, 'Having a single quote messes with our command.'
356350
filedir = os.path.dirname(__file__)
357351

358352
# Valid arguments for arch are i386, i386:x86-64, i386:x64-32, i8086,
@@ -369,7 +363,7 @@ def run_python_code_mac(pid, python_code, connect_debugger_tracing=False, show_d
369363
print('Attaching with arch: %s' % (arch,))
370364

371365
target_dll = os.path.join(filedir, 'attach_%s' % suffix)
372-
target_dll = os.path.abspath(target_dll)
366+
target_dll = os.path.normpath(target_dll)
373367
if not os.path.exists(target_dll):
374368
raise RuntimeError('Could not find dll file to inject: %s' % target_dll)
375369

@@ -391,15 +385,15 @@ def run_python_code_mac(pid, python_code, connect_debugger_tracing=False, show_d
391385
]
392386

393387
cmd.extend([
394-
"-o", 'process attach --pid %d' % pid,
395-
"-o", 'command script import "%s"' % (lldb_prepare_file,),
396-
"-o", 'load_lib_and_attach "%s" %s %s %s' % (target_dll,
397-
is_debug, escape_for_gdb_and_lldb(python_code), show_debug_info),
388+
"-o 'process attach --pid %d'" % pid,
389+
"-o 'command script import \"%s\"'" % (lldb_prepare_file,),
390+
"-o 'load_lib_and_attach \"%s\" %s \"%s\" %s'" % (target_dll,
391+
is_debug, python_code, show_debug_info),
398392
])
399393

400394
cmd.extend([
401-
"-o", 'process detach',
402-
"-o", 'script import os; os._exit(1)',
395+
"-o 'process detach'",
396+
"-o 'script import os; os._exit(1)'",
403397
])
404398

405399
# print ' '.join(cmd)
@@ -411,7 +405,8 @@ def run_python_code_mac(pid, python_code, connect_debugger_tracing=False, show_d
411405
env.pop('PYTHONPATH', None)
412406
print('Running: %s' % (' '.join(cmd)))
413407
p = subprocess.Popen(
414-
cmd,
408+
' '.join(cmd),
409+
shell=True,
415410
env=env,
416411
stdout=subprocess.PIPE,
417412
stderr=subprocess.PIPE,

0 commit comments

Comments
 (0)