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