File: _test_attach_to_process_linux.py

package info (click to toggle)
pydevd 3.4.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,892 kB
  • sloc: python: 77,580; cpp: 1,873; sh: 374; makefile: 50; ansic: 4
file content (75 lines) | stat: -rw-r--r-- 2,563 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
This module is just for testing concepts. It should be erased later on.

Experiments:

// gdb -p 4957
// call dlopen("/home/fabioz/Desktop/dev/PyDev.Debugger/pydevd_attach_to_process/linux/attach_linux.so", 2)
// call dlsym($1, "hello")
// call hello()


// call open("/home/fabioz/Desktop/dev/PyDev.Debugger/pydevd_attach_to_process/linux/attach_linux.so", 2)
// call mmap(0, 6672, 1 | 2 | 4, 1, 3 , 0)
// add-symbol-file
// cat /proc/pid/maps

// call dlopen("/home/fabioz/Desktop/dev/PyDev.Debugger/pydevd_attach_to_process/linux/attach_linux.so", 1|8)
// call dlsym($1, "hello")
// call hello()
"""

import subprocess
import sys
import os
import time

if __name__ == "__main__":
    linux_dir = os.path.join(os.path.dirname(__file__), "linux")
    os.chdir(linux_dir)
    so_location = os.path.join(linux_dir, "attach_linux.so")
    try:
        os.remove(so_location)
    except:
        pass
    subprocess.call("g++ -shared -o attach_linux.so -fPIC -nostartfiles attach_linux.c".split())
    print("Finished compiling")
    assert os.path.exists("/home/fabioz/Desktop/dev/PyDev.Debugger/pydevd_attach_to_process/linux/attach_linux.so")
    os.chdir(os.path.dirname(linux_dir))
    #     import attach_pydevd
    #     attach_pydevd.main(attach_pydevd.process_command_line(['--pid', str(p.pid)]))
    p = subprocess.Popen([sys.executable, "-u", "_always_live_program.py"])
    print("Size of file: %s" % (os.stat(so_location).st_size))

    # (gdb) set architecture
    # Requires an argument. Valid arguments are i386, i386:x86-64, i386:x64-32, i8086, i386:intel, i386:x86-64:intel, i386:x64-32:intel, i386:nacl, i386:x86-64:nacl, i386:x64-32:nacl, auto.

    cmd = [
        "gdb",
        "--pid",
        str(p.pid),
        "--batch",
    ]

    arch = "i386:x86-64"
    if arch:
        cmd.extend(["--eval-command='set architecture %s'" % arch])

    cmd.extend(
        [
            "--eval-command='call dlopen(\"/home/fabioz/Desktop/dev/PyDev.Debugger/pydevd_attach_to_process/linux/attach_linux.so\", 2)'",
            '--eval-command=\'call (int)DoAttach(1, "print(\\"check11111check\\")", 0)\'',
            # "--eval-command='call (int)SetSysTraceFunc(1, 0)'", -- never call this way, always use "--command='...gdb_threads_settrace.py'",
            # So that threads are all stopped!
        ]
    )

    print(" ".join(cmd))
    time.sleep(0.5)
    env = os.environ.copy()
    env.pop("PYTHONIOENCODING", None)
    env.pop("PYTHONPATH", None)
    p2 = subprocess.call(" ".join(cmd), env=env, shell=True)

    time.sleep(1)
    p.kill()