1
2# LLDB UI state in the Vim user interface.
3
4import os, re, sys
5import lldb
6import vim
7from vim_panes import *
8from vim_signs import *
9
10def is_same_file(a, b):
11  """ returns true if paths a and b are the same file """
12  a = os.path.realpath(a)
13  b = os.path.realpath(b)
14  return a in b or b in a
15
16class UI:
17  def __init__(self):
18    """ Declare UI state variables """
19
20    # Default panes to display
21    self.defaultPanes = ['breakpoints', 'backtrace', 'locals', 'threads', 'registers', 'disassembly']
22
23    # map of tuples (filename, line) --> SBBreakpoint
24    self.markedBreakpoints = {}
25
26    # Currently shown signs
27    self.breakpointSigns = {}
28    self.pcSigns = []
29
30    # Container for panes
31    self.paneCol = PaneLayout()
32
33    # All possible LLDB panes
34    self.backtracePane = BacktracePane(self.paneCol)
35    self.threadPane = ThreadPane(self.paneCol)
36    self.disassemblyPane = DisassemblyPane(self.paneCol)
37    self.localsPane = LocalsPane(self.paneCol)
38    self.registersPane = RegistersPane(self.paneCol)
39    self.breakPane = BreakpointsPane(self.paneCol)
40
41  def activate(self):
42    """ Activate UI: display default set of panes """
43    self.paneCol.prepare(self.defaultPanes)
44
45  def get_user_buffers(self, filter_name=None):
46    """ Returns a list of buffers that are not a part of the LLDB UI. That is, they
47        are not contained in the PaneLayout object self.paneCol.
48    """
49    ret = []
50    for w in vim.windows:
51      b = w.buffer
52      if not self.paneCol.contains(b.name):
53        if filter_name is None or filter_name in b.name:
54          ret.append(b)
55    return ret
56
57  def update_pc(self, process, buffers, goto_file):
58    """ Place the PC sign on the PC location of each thread's selected frame """
59
60    def GetPCSourceLocation(thread):
61      """ Returns a tuple (thread_index, file, line, column) that represents where
62          the PC sign should be placed for a thread.
63      """
64
65      frame = thread.GetSelectedFrame()
66      frame_num = frame.GetFrameID()
67      le = frame.GetLineEntry()
68      while not le.IsValid() and frame_num < thread.GetNumFrames():
69        frame_num += 1
70        le = thread.GetFrameAtIndex(frame_num).GetLineEntry()
71
72      if le.IsValid():
73        path = os.path.join(le.GetFileSpec().GetDirectory(), le.GetFileSpec().GetFilename())
74        return (thread.GetIndexID(), path, le.GetLine(), le.GetColumn())
75      return None
76
77
78    # Clear all existing PC signs
79    del_list = []
80    for sign in self.pcSigns:
81      sign.hide()
82      del_list.append(sign)
83    for sign in del_list:
84      self.pcSigns.remove(sign)
85      del sign
86
87    # Select a user (non-lldb) window
88    if not self.paneCol.selectWindow(False):
89      # No user window found; avoid clobbering by splitting
90      vim.command(":vsp")
91
92    # Show a PC marker for each thread
93    for thread in process:
94      loc = GetPCSourceLocation(thread)
95      if not loc:
96        # no valid source locations for PCs. hide all existing PC markers
97        continue
98
99      buf = None
100      (tid, fname, line, col) = loc
101      buffers = self.get_user_buffers(fname)
102      is_selected = thread.GetIndexID() == process.GetSelectedThread().GetIndexID()
103      if len(buffers) == 1:
104        buf = buffers[0]
105        if buf != vim.current.buffer:
106          # Vim has an open buffer to the required file: select it
107          vim.command('execute ":%db"' % buf.number)
108      elif is_selected and vim.current.buffer.name not in fname and os.path.exists(fname) and goto_file:
109        # FIXME: If current buffer is modified, vim will complain when we try to switch away.
110        #        Find a way to detect if the current buffer is modified, and...warn instead?
111        vim.command('execute ":e %s"' % fname)
112        buf = vim.current.buffer
113      elif len(buffers) > 1 and goto_file:
114        #FIXME: multiple open buffers match PC location
115        continue
116      else:
117        continue
118
119      self.pcSigns.append(PCSign(buf, line, is_selected))
120
121      if is_selected and goto_file:
122        # if the selected file has a PC marker, move the cursor there too
123        curname = vim.current.buffer.name
124        if curname is not None and is_same_file(curname, fname):
125          move_cursor(line, 0)
126        elif move_cursor:
127          print "FIXME: not sure where to move cursor because %s != %s " % (vim.current.buffer.name, fname)
128
129  def update_breakpoints(self, target, buffers):
130    """ Decorates buffer with signs corresponding to breakpoints in target. """
131
132    def GetBreakpointLocations(bp):
133      """ Returns a list of tuples (resolved, filename, line) where a breakpoint was resolved. """
134      if not bp.IsValid():
135        sys.stderr.write("breakpoint is invalid, no locations")
136        return []
137
138      ret = []
139      numLocs = bp.GetNumLocations()
140      for i in range(numLocs):
141        loc = bp.GetLocationAtIndex(i)
142        desc = get_description(loc, lldb.eDescriptionLevelFull)
143        match = re.search('at\ ([^:]+):([\d]+)', desc)
144        try:
145          lineNum = int(match.group(2).strip())
146          ret.append((loc.IsResolved(), match.group(1), lineNum))
147        except ValueError as e:
148          sys.stderr.write("unable to parse breakpoint location line number: '%s'" % match.group(2))
149          sys.stderr.write(str(e))
150
151      return ret
152
153
154    if target is None or not target.IsValid():
155      return
156
157    needed_bps = {}
158    for bp_index in range(target.GetNumBreakpoints()):
159      bp = target.GetBreakpointAtIndex(bp_index)
160      locations = GetBreakpointLocations(bp)
161      for (is_resolved, file, line) in GetBreakpointLocations(bp):
162        for buf in buffers:
163          if file in buf.name:
164            needed_bps[(buf, line, is_resolved)] = bp
165
166    # Hide any signs that correspond with disabled breakpoints
167    del_list = []
168    for (b, l, r) in self.breakpointSigns:
169      if (b, l, r) not in needed_bps:
170        self.breakpointSigns[(b, l, r)].hide()
171        del_list.append((b, l, r))
172    for d in del_list:
173      del self.breakpointSigns[d]
174
175    # Show any signs for new breakpoints
176    for (b, l, r) in needed_bps:
177      bp = needed_bps[(b, l, r)]
178      if self.haveBreakpoint(b.name, l):
179        self.markedBreakpoints[(b.name, l)].append(bp)
180      else:
181        self.markedBreakpoints[(b.name, l)] = [bp]
182
183      if (b, l, r) not in self.breakpointSigns:
184        s = BreakpointSign(b, l, r)
185        self.breakpointSigns[(b, l, r)] = s
186
187  def update(self, target, status, controller, goto_file=False):
188    """ Updates debugger info panels and breakpoint/pc marks and prints
189        status to the vim status line. If goto_file is True, the user's
190        cursor is moved to the source PC location in the selected frame.
191    """
192
193    self.paneCol.update(target, controller)
194    self.update_breakpoints(target, self.get_user_buffers())
195
196    if target is not None and target.IsValid():
197      process = target.GetProcess()
198      if process is not None and process.IsValid():
199        self.update_pc(process, self.get_user_buffers, goto_file)
200
201    if status is not None and len(status) > 0:
202      print status
203
204  def haveBreakpoint(self, file, line):
205    """ Returns True if we have a breakpoint at file:line, False otherwise  """
206    return (file, line) in self.markedBreakpoints
207
208  def getBreakpoints(self, fname, line):
209    """ Returns the LLDB SBBreakpoint object at fname:line """
210    if self.haveBreakpoint(fname, line):
211      return self.markedBreakpoints[(fname, line)]
212    else:
213      return None
214
215  def deleteBreakpoints(self, name, line):
216    del self.markedBreakpoints[(name, line)]
217
218  def showWindow(self, name):
219    """ Shows (un-hides) window pane specified by name """
220    if not self.paneCol.havePane(name):
221      sys.stderr.write("unknown window: %s" % name)
222      return False
223    self.paneCol.prepare([name])
224    return True
225
226  def hideWindow(self, name):
227    """ Hides window pane specified by name """
228    if not self.paneCol.havePane(name):
229      sys.stderr.write("unknown window: %s" % name)
230      return False
231    self.paneCol.hide([name])
232    return True
233
234global ui
235ui = UI()
236