xref: /xnu-10063.121.3/tools/lldbmacros/usertaskgdbserver.py (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributionsfrom xnu import *
2*2c2f96dcSApple OSS Distributionsimport logging
3*2c2f96dcSApple OSS Distributions_usertaskdebugging_availabe = False
4*2c2f96dcSApple OSS Distributionstry:
5*2c2f96dcSApple OSS Distributions    from usertaskdebugging import userprocess
6*2c2f96dcSApple OSS Distributions    from usertaskdebugging import gdbserver
7*2c2f96dcSApple OSS Distributions    _usertaskdebugging_availabe = True
8*2c2f96dcSApple OSS Distributionsexcept ImportError:
9*2c2f96dcSApple OSS Distributions    pass
10*2c2f96dcSApple OSS Distributions
11*2c2f96dcSApple OSS Distributionsdef setupLogging(debug_level):
12*2c2f96dcSApple OSS Distributions    log_level = debug_level
13*2c2f96dcSApple OSS Distributions    log_filename = "/tmp/kdbserver.log"
14*2c2f96dcSApple OSS Distributions    logging.basicConfig(level=log_level,
15*2c2f96dcSApple OSS Distributions                      format='%(asctime)s %(module)s %(levelname)s: %(message)s',
16*2c2f96dcSApple OSS Distributions                      datefmt='%Y-%m-%d %H:%M:%S')
17*2c2f96dcSApple OSS Distributions
18*2c2f96dcSApple OSS Distributions
19*2c2f96dcSApple OSS Distributions@lldb_command('beginusertaskdebugging', 'DW')
20*2c2f96dcSApple OSS Distributionsdef DoUserTaskDebuggingServer(cmd_args = [], cmd_options ={}):
21*2c2f96dcSApple OSS Distributions    """ starts a gdb protocol server that is backed by <task_t> in kernel debugging session.
22*2c2f96dcSApple OSS Distributions        Usage: (lldb) beginusertaskdebugging <task_t>
23*2c2f96dcSApple OSS Distributions        options: -D for debug level logging
24*2c2f96dcSApple OSS Distributions                 -W for warning level logging.
25*2c2f96dcSApple OSS Distributions        default is error level logging
26*2c2f96dcSApple OSS Distributions    """
27*2c2f96dcSApple OSS Distributions    if not _usertaskdebugging_availabe:
28*2c2f96dcSApple OSS Distributions        print("You do not have the usertask debugging files available. ")
29*2c2f96dcSApple OSS Distributions        return
30*2c2f96dcSApple OSS Distributions    log_level = logging.INFO
31*2c2f96dcSApple OSS Distributions    if '-D' in cmd_options:
32*2c2f96dcSApple OSS Distributions        log_level = logging.DEBUG
33*2c2f96dcSApple OSS Distributions    elif '-W' in cmd_options:
34*2c2f96dcSApple OSS Distributions        log_level = logging.WARNING
35*2c2f96dcSApple OSS Distributions
36*2c2f96dcSApple OSS Distributions    setupLogging(debug_level=log_level)
37*2c2f96dcSApple OSS Distributions    if not cmd_args:
38*2c2f96dcSApple OSS Distributions        raise ArgumentError("Please provide valid task argument.")
39*2c2f96dcSApple OSS Distributions
40*2c2f96dcSApple OSS Distributions    t = kern.GetValueFromAddress(cmd_args[0], 'task_t')
41*2c2f96dcSApple OSS Distributions
42*2c2f96dcSApple OSS Distributions    up = userprocess.UserProcess(t)
43*2c2f96dcSApple OSS Distributions    gbs = gdbserver.GDBServer(up)
44*2c2f96dcSApple OSS Distributions    print("Starting debug session for %s at localhost:%d." % (GetProcNameForTask(t), gbs.portnum))
45*2c2f96dcSApple OSS Distributions    gbs.run()
46*2c2f96dcSApple OSS Distributions    print("stopped the debug session")
47