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