()
| 411 | |
| 412 | |
| 413 | def 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 = """ |
| 433 | import codecs; |
| 434 | import json; |
| 435 | import sys; |
| 436 | |
| 437 | decode = lambda s: codecs.utf_8_decode(bytearray(s))[0] if s is not None else None; |
| 438 | |
| 439 | script_dir = decode({script_dir}); |
| 440 | setup = json.loads(decode({setup})); |
| 441 | |
| 442 | sys.path.insert(0, script_dir); |
| 443 | import attach_pid_injected; |
| 444 | del sys.path[0]; |
| 445 | |
| 446 | attach_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() |
nothing calls this directly
no test coverage detected
searching dependent graphs…