1#!/usr/bin/env python 2##===-- lui.py -----------------------------------------------*- Python -*-===## 3## 4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5# See https://llvm.org/LICENSE.txt for license information. 6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7## 8##===----------------------------------------------------------------------===## 9 10 11import curses 12 13import lldb 14import lldbutil 15 16from optparse import OptionParser 17import os 18import signal 19import sys 20 21try: 22 import queue 23except ImportError: 24 import Queue as queue 25 26import debuggerdriver 27import cui 28 29import breakwin 30import commandwin 31import eventwin 32import sourcewin 33import statuswin 34 35event_queue = None 36 37 38def handle_args(driver, argv): 39 parser = OptionParser() 40 parser.add_option( 41 "-p", 42 "--attach", 43 dest="pid", 44 help="Attach to specified Process ID", 45 type="int") 46 parser.add_option( 47 "-c", 48 "--core", 49 dest="core", 50 help="Load specified core file", 51 type="string") 52 53 (options, args) = parser.parse_args(argv) 54 55 if options.pid is not None: 56 try: 57 pid = int(options.pid) 58 driver.attachProcess(ui, pid) 59 except ValueError: 60 print("Error: expecting integer PID, got '%s'" % options.pid) 61 elif options.core is not None: 62 if not os.path.exists(options.core): 63 raise Exception( 64 "Specified core file '%s' does not exist." % 65 options.core) 66 driver.loadCore(options.core) 67 elif len(args) == 2: 68 if not os.path.isfile(args[1]): 69 raise Exception("Specified target '%s' does not exist" % args[1]) 70 driver.createTarget(args[1]) 71 elif len(args) > 2: 72 if not os.path.isfile(args[1]): 73 raise Exception("Specified target '%s' does not exist" % args[1]) 74 driver.createTarget(args[1], args[2:]) 75 76 77def sigint_handler(signal, frame): 78 global debugger 79 debugger.terminate() 80 81 82class LLDBUI(cui.CursesUI): 83 84 def __init__(self, screen, event_queue, driver): 85 super(LLDBUI, self).__init__(screen, event_queue) 86 87 self.driver = driver 88 89 h, w = self.screen.getmaxyx() 90 91 command_win_height = 20 92 break_win_width = 60 93 94 self.status_win = statuswin.StatusWin(0, h - 1, w, 1) 95 h -= 1 96 self.command_win = commandwin.CommandWin( 97 driver, 0, h - command_win_height, w, command_win_height) 98 h -= command_win_height 99 self.source_win = sourcewin.SourceWin(driver, 0, 0, 100 w - break_win_width - 1, h) 101 self.break_win = breakwin.BreakWin(driver, w - break_win_width, 0, 102 break_win_width, h) 103 104 self.wins = [self.status_win, 105 # self.event_win, 106 self.source_win, 107 self.break_win, 108 self.command_win, 109 ] 110 111 self.focus = len(self.wins) - 1 # index of command window; 112 113 def handleEvent(self, event): 114 # hack 115 if isinstance(event, int): 116 if event == curses.KEY_F10: 117 self.driver.terminate() 118 if event == 20: # ctrl-T 119 def foo(cmd): 120 ret = lldb.SBCommandReturnObject() 121 self.driver.getCommandInterpreter().HandleCommand(cmd, ret) 122 foo('target create a.out') 123 foo('b main') 124 foo('run') 125 super(LLDBUI, self).handleEvent(event) 126 127 128def main(screen): 129 signal.signal(signal.SIGINT, sigint_handler) 130 131 global event_queue 132 event_queue = queue.Queue() 133 134 global debugger 135 debugger = lldb.SBDebugger.Create() 136 137 driver = debuggerdriver.createDriver(debugger, event_queue) 138 view = LLDBUI(screen, event_queue, driver) 139 140 driver.start() 141 142 # hack to avoid hanging waiting for prompts! 143 driver.handleCommand("settings set auto-confirm true") 144 145 handle_args(driver, sys.argv) 146 view.eventLoop() 147 148if __name__ == "__main__": 149 try: 150 curses.wrapper(main) 151 except KeyboardInterrupt: 152 exit() 153