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