MCPcopy Create free account
hub / github.com/microsoft/debugpy / attach_to_pid

Function attach_to_pid

src/debugpy/server/cli.py:413–510  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

411
412
413def attach_to_pid():
414 pid = options.target
415 log.info("Attaching to process with PID={0}", pid)
416
417 encode = lambda s: list(bytearray(s.encode("utf-8"))) if s is not None else None
418
419 script_dir = os.path.dirname(debugpy.server.__file__)
420 assert os.path.exists(script_dir)
421 script_dir = encode(script_dir)
422
423 setup = {
424 "mode": options.mode,
425 "address": options.address,
426 "wait_for_client": options.wait_for_client,
427 "log_to": options.log_to,
428 "adapter_access_token": options.adapter_access_token,
429 }
430 setup = encode(json.dumps(setup))
431
432 python_code = """
433import codecs;
434import json;
435import sys;
436
437decode = lambda s: codecs.utf_8_decode(bytearray(s))[0] if s is not None else None;
438
439script_dir = decode({script_dir});
440setup = json.loads(decode({setup}));
441
442sys.path.insert(0, script_dir);
443import attach_pid_injected;
444del sys.path[0];
445
446attach_pid_injected.attach(setup);
447"""
448 python_code = (
449 python_code.replace("\r", "")
450 .replace("\n", "")
451 .format(script_dir=script_dir, setup=setup)
452 )
453
454 # attempt pep 768 style code injection
455 if (not options.disable_sys_remote_exec) and hasattr(sys, "remote_exec"):
456 tmp_file_path = ""
457 try:
458 import tempfile
459
460 with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
461 tmp_file_path = tmp_file.name
462 log.info(
463 "Attempting to inject code at '{tmp_file_path}' using sys.remote_exec()",
464 tmp_file_path=tmp_file_path,
465 )
466 tmp_file.write(python_code.encode())
467 tmp_file.write(
468 """import os;os.remove("{tmp_file_path}");""".format(
469 tmp_file_path=tmp_file_path
470 ).encode()

Callers

nothing calls this directly

Calls 8

infoMethod · 0.80
dumpsMethod · 0.80
writeMethod · 0.45
flushMethod · 0.45
closeMethod · 0.45
removeMethod · 0.45
joinMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…