Adrian Prantl | 888e023e | 2013-09-06 18:10:44 +0000 | [diff] [blame] | 1 | #!/bin/env python |
| 2 | """ |
| 3 | A gdb-compatible frontend for lldb that implements just enough |
| 4 | commands to run the tests in the debuginfo-tests repository with lldb. |
| 5 | """ |
| 6 | |
| 7 | # ---------------------------------------------------------------------- |
| 8 | # Auto-detect lldb python module. |
| 9 | import commands, platform, os, sys |
| 10 | try: |
| 11 | # Just try for LLDB in case PYTHONPATH is already correctly setup. |
| 12 | import lldb |
| 13 | except ImportError: |
| 14 | lldb_python_dirs = list() |
| 15 | # lldb is not in the PYTHONPATH, try some defaults for the current platform. |
| 16 | platform_system = platform.system() |
| 17 | if platform_system == 'Darwin': |
| 18 | # On Darwin, try the currently selected Xcode directory |
| 19 | xcode_dir = commands.getoutput("xcode-select --print-path") |
| 20 | if xcode_dir: |
| 21 | lldb_python_dirs.append(os.path.realpath(xcode_dir + |
| 22 | '/../SharedFrameworks/LLDB.framework/Resources/Python')) |
| 23 | lldb_python_dirs.append(xcode_dir + |
| 24 | '/Library/PrivateFrameworks/LLDB.framework/Resources/Python') |
| 25 | lldb_python_dirs.append( |
| 26 | '/System/Library/PrivateFrameworks/LLDB.framework/Resources/Python') |
| 27 | success = False |
| 28 | for lldb_python_dir in lldb_python_dirs: |
| 29 | if os.path.exists(lldb_python_dir): |
| 30 | if not (sys.path.__contains__(lldb_python_dir)): |
| 31 | sys.path.append(lldb_python_dir) |
| 32 | try: |
| 33 | import lldb |
| 34 | except ImportError: |
| 35 | pass |
| 36 | else: |
| 37 | print 'imported lldb from: "%s"' % (lldb_python_dir) |
| 38 | success = True |
| 39 | break |
| 40 | if not success: |
| 41 | print "error: couldn't locate the 'lldb' module, please set PYTHONPATH correctly" |
| 42 | sys.exit(1) |
| 43 | # ---------------------------------------------------------------------- |
| 44 | |
| 45 | # Command line option handling. |
| 46 | import argparse |
| 47 | parser = argparse.ArgumentParser(description=__doc__) |
| 48 | parser.add_argument('--quiet', '-q', action="store_true", help='ignored') |
| 49 | parser.add_argument('-batch', action="store_true", |
| 50 | help='exit after processing comand line') |
| 51 | parser.add_argument('-n', action="store_true", help='ignore .lldb file') |
| 52 | parser.add_argument('-x', dest='script', type=file, help='execute commands from file') |
| 53 | parser.add_argument("target", help="the program to debug") |
| 54 | args = parser.parse_args() |
| 55 | |
| 56 | |
| 57 | # Create a new debugger instance. |
| 58 | debugger = lldb.SBDebugger.Create() |
| 59 | debugger.SkipLLDBInitFiles(args.n) |
| 60 | |
Adrian Prantl | 2d26cf3 | 2019-06-17 20:06:34 +0000 | [diff] [blame] | 61 | # Make sure to clean up the debugger on exit. |
| 62 | import atexit |
| 63 | def on_exit(): |
| 64 | debugger.Terminate() |
| 65 | atexit.register(on_exit) |
| 66 | |
Adrian Prantl | 888e023e | 2013-09-06 18:10:44 +0000 | [diff] [blame] | 67 | # Don't return from lldb function calls until the process stops. |
| 68 | debugger.SetAsync(False) |
| 69 | |
| 70 | # Create a target from a file and arch. |
Adrian Prantl | ee30f63 | 2013-09-07 20:04:29 +0000 | [diff] [blame] | 71 | arch = os.popen("file "+args.target).read().split()[-1] |
Adrian Prantl | 002c2a8 | 2013-09-07 17:14:37 +0000 | [diff] [blame] | 72 | target = debugger.CreateTargetWithFileAndArch(args.target, arch) |
Adrian Prantl | 888e023e | 2013-09-06 18:10:44 +0000 | [diff] [blame] | 73 | |
| 74 | if not target: |
| 75 | print "Could not create target", args.target |
| 76 | sys.exit(1) |
| 77 | |
| 78 | if not args.script: |
| 79 | print "Interactive mode is not implemented." |
| 80 | sys.exit(1) |
| 81 | |
| 82 | import re |
| 83 | for command in args.script: |
| 84 | # Strip newline and whitespaces and split into words. |
| 85 | cmd = command[:-1].strip().split() |
| 86 | if not cmd: |
| 87 | continue |
| 88 | |
Adrian Prantl | 5ef1c86 | 2013-09-06 22:33:52 +0000 | [diff] [blame] | 89 | print '> %s'% command[:-1] |
Adrian Prantl | 888e023e | 2013-09-06 18:10:44 +0000 | [diff] [blame] | 90 | |
| 91 | try: |
| 92 | if re.match('^r|(run)$', cmd[0]): |
| 93 | error = lldb.SBError() |
| 94 | launchinfo = lldb.SBLaunchInfo([]) |
| 95 | launchinfo.SetWorkingDirectory(os.getcwd()) |
| 96 | process = target.Launch(launchinfo, error) |
Adrian Prantl | 5ef1c86 | 2013-09-06 22:33:52 +0000 | [diff] [blame] | 97 | print error |
Adrian Prantl | 888e023e | 2013-09-06 18:10:44 +0000 | [diff] [blame] | 98 | if not process or error.fail: |
Adrian Prantl | 888e023e | 2013-09-06 18:10:44 +0000 | [diff] [blame] | 99 | state = process.GetState() |
| 100 | print "State = %d" % state |
Adrian Prantl | 5ef1c86 | 2013-09-06 22:33:52 +0000 | [diff] [blame] | 101 | print """ |
| 102 | ERROR: Could not launch process. |
Adrian Prantl | 17a0011 | 2014-10-13 18:04:10 +0000 | [diff] [blame] | 103 | NOTE: There are several reasons why this may happen: |
Adrian Prantl | 5ef1c86 | 2013-09-06 22:33:52 +0000 | [diff] [blame] | 104 | * Root needs to run "DevToolsSecurity --enable". |
Adrian Prantl | 17a0011 | 2014-10-13 18:04:10 +0000 | [diff] [blame] | 105 | * Older versions of lldb cannot launch more than one process simultaneously. |
Adrian Prantl | 5ef1c86 | 2013-09-06 22:33:52 +0000 | [diff] [blame] | 106 | """ |
Adrian Prantl | 888e023e | 2013-09-06 18:10:44 +0000 | [diff] [blame] | 107 | sys.exit(1) |
| 108 | |
| 109 | elif re.match('^b|(break)$', cmd[0]) and len(cmd) == 2: |
| 110 | if re.match('[0-9]+', cmd[1]): |
| 111 | # b line |
| 112 | mainfile = target.FindFunctions('main')[0].compile_unit.file |
| 113 | print target.BreakpointCreateByLocation(mainfile, int(cmd[1])) |
| 114 | else: |
| 115 | # b file:line |
Adrian Prantl | 5ef1c86 | 2013-09-06 22:33:52 +0000 | [diff] [blame] | 116 | file, line = cmd[1].split(':') |
Adrian Prantl | 888e023e | 2013-09-06 18:10:44 +0000 | [diff] [blame] | 117 | print target.BreakpointCreateByLocation(file, int(line)) |
| 118 | |
| 119 | elif re.match('^ptype$', cmd[0]) and len(cmd) == 2: |
| 120 | # GDB's ptype has multiple incarnations depending on its |
| 121 | # argument (global variable, function, type). The definition |
| 122 | # here is for looking up the signature of a function and only |
| 123 | # if that fails it looks for a type with that name. |
| 124 | # Type lookup in LLDB would be "image lookup --type". |
| 125 | for elem in target.FindFunctions(cmd[1]): |
| 126 | print elem.function.type |
| 127 | continue |
| 128 | print target.FindFirstType(cmd[1]) |
| 129 | |
| 130 | elif re.match('^po$', cmd[0]) and len(cmd) > 1: |
Adrian Prantl | 75c4779 | 2014-02-21 00:17:02 +0000 | [diff] [blame] | 131 | try: |
| 132 | opts = lldb.SBExpressionOptions() |
| 133 | opts.SetFetchDynamicValue(True) |
| 134 | opts.SetCoerceResultToId(True) |
| 135 | print target.EvaluateExpression(' '.join(cmd[1:]), opts) |
| 136 | except: |
| 137 | # FIXME: This is a fallback path for the lab.llvm.org |
| 138 | # buildbot running OS X 10.7; it should be removed. |
| 139 | thread = process.GetThreadAtIndex(0) |
| 140 | frame = thread.GetFrameAtIndex(0) |
| 141 | print frame.EvaluateExpression(' '.join(cmd[1:])) |
Adrian Prantl | 888e023e | 2013-09-06 18:10:44 +0000 | [diff] [blame] | 142 | |
| 143 | elif re.match('^p|(print)$', cmd[0]) and len(cmd) > 1: |
Adrian Prantl | 5ef1c86 | 2013-09-06 22:33:52 +0000 | [diff] [blame] | 144 | thread = process.GetThreadAtIndex(0) |
| 145 | frame = thread.GetFrameAtIndex(0) |
| 146 | print frame.EvaluateExpression(' '.join(cmd[1:])) |
Adrian Prantl | 888e023e | 2013-09-06 18:10:44 +0000 | [diff] [blame] | 147 | |
Adrian Prantl | c26059f | 2017-04-17 17:57:01 +0000 | [diff] [blame] | 148 | elif re.match('^n|(next)$', cmd[0]): |
| 149 | thread = process.GetThreadAtIndex(0) |
| 150 | thread.StepOver() |
| 151 | |
Adrian Prantl | 888e023e | 2013-09-06 18:10:44 +0000 | [diff] [blame] | 152 | elif re.match('^q|(quit)$', cmd[0]): |
| 153 | sys.exit(0) |
| 154 | |
| 155 | else: |
| 156 | print debugger.HandleCommand(' '.join(cmd)) |
| 157 | |
Adrian Prantl | 971ad59 | 2014-10-13 16:34:31 +0000 | [diff] [blame] | 158 | except SystemExit: |
Adrian Prantl | 971ad59 | 2014-10-13 16:34:31 +0000 | [diff] [blame] | 159 | raise |
Adrian Prantl | 888e023e | 2013-09-06 18:10:44 +0000 | [diff] [blame] | 160 | except: |
| 161 | print 'Could not handle the command "%s"' % ' '.join(cmd) |
| 162 | |