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