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