annotate lldb/utils/lui/sourcewin.py @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents 1d019706d866
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 ##===-- sourcewin.py -----------------------------------------*- Python -*-===##
anatofuz
parents:
diff changeset
2 ##
anatofuz
parents:
diff changeset
3 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 # See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 ##
anatofuz
parents:
diff changeset
7 ##===----------------------------------------------------------------------===##
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 import cui
anatofuz
parents:
diff changeset
10 import curses
anatofuz
parents:
diff changeset
11 import lldb
anatofuz
parents:
diff changeset
12 import lldbutil
anatofuz
parents:
diff changeset
13 import re
anatofuz
parents:
diff changeset
14 import os
anatofuz
parents:
diff changeset
15
anatofuz
parents:
diff changeset
16
anatofuz
parents:
diff changeset
17 class SourceWin(cui.TitledWin):
anatofuz
parents:
diff changeset
18
anatofuz
parents:
diff changeset
19 def __init__(self, driver, x, y, w, h):
anatofuz
parents:
diff changeset
20 super(SourceWin, self).__init__(x, y, w, h, "Source")
anatofuz
parents:
diff changeset
21 self.sourceman = driver.getSourceManager()
anatofuz
parents:
diff changeset
22 self.sources = {}
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 self.filename = None
anatofuz
parents:
diff changeset
25 self.pc_line = None
anatofuz
parents:
diff changeset
26 self.viewline = 0
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 self.breakpoints = {}
anatofuz
parents:
diff changeset
29
anatofuz
parents:
diff changeset
30 self.win.scrollok(1)
anatofuz
parents:
diff changeset
31
anatofuz
parents:
diff changeset
32 self.markerPC = ":) "
anatofuz
parents:
diff changeset
33 self.markerBP = "B> "
anatofuz
parents:
diff changeset
34 self.markerNone = " "
anatofuz
parents:
diff changeset
35
anatofuz
parents:
diff changeset
36 try:
anatofuz
parents:
diff changeset
37 from pygments.formatters import TerminalFormatter
anatofuz
parents:
diff changeset
38 self.formatter = TerminalFormatter()
anatofuz
parents:
diff changeset
39 except ImportError:
anatofuz
parents:
diff changeset
40 #self.win.addstr("\nWarning: no 'pygments' library found. Syntax highlighting is disabled.")
anatofuz
parents:
diff changeset
41 self.lexer = None
anatofuz
parents:
diff changeset
42 self.formatter = None
anatofuz
parents:
diff changeset
43 pass
anatofuz
parents:
diff changeset
44
anatofuz
parents:
diff changeset
45 # FIXME: syntax highlight broken
anatofuz
parents:
diff changeset
46 self.formatter = None
anatofuz
parents:
diff changeset
47 self.lexer = None
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 def handleEvent(self, event):
anatofuz
parents:
diff changeset
50 if isinstance(event, int):
anatofuz
parents:
diff changeset
51 self.handleKey(event)
anatofuz
parents:
diff changeset
52 return
anatofuz
parents:
diff changeset
53
anatofuz
parents:
diff changeset
54 if isinstance(event, lldb.SBEvent):
anatofuz
parents:
diff changeset
55 if lldb.SBBreakpoint.EventIsBreakpointEvent(event):
anatofuz
parents:
diff changeset
56 self.handleBPEvent(event)
anatofuz
parents:
diff changeset
57
anatofuz
parents:
diff changeset
58 if lldb.SBProcess.EventIsProcessEvent(event) and \
anatofuz
parents:
diff changeset
59 not lldb.SBProcess.GetRestartedFromEvent(event):
anatofuz
parents:
diff changeset
60 process = lldb.SBProcess.GetProcessFromEvent(event)
anatofuz
parents:
diff changeset
61 if not process.IsValid():
anatofuz
parents:
diff changeset
62 return
anatofuz
parents:
diff changeset
63 if process.GetState() == lldb.eStateStopped:
anatofuz
parents:
diff changeset
64 self.refreshSource(process)
anatofuz
parents:
diff changeset
65 elif process.GetState() == lldb.eStateExited:
anatofuz
parents:
diff changeset
66 self.notifyExited(process)
anatofuz
parents:
diff changeset
67
anatofuz
parents:
diff changeset
68 def notifyExited(self, process):
anatofuz
parents:
diff changeset
69 self.win.erase()
anatofuz
parents:
diff changeset
70 target = lldbutil.get_description(process.GetTarget())
anatofuz
parents:
diff changeset
71 pid = process.GetProcessID()
anatofuz
parents:
diff changeset
72 ec = process.GetExitStatus()
anatofuz
parents:
diff changeset
73 self.win.addstr(
anatofuz
parents:
diff changeset
74 "\nProcess %s [%d] has exited with exit-code %d" %
anatofuz
parents:
diff changeset
75 (target, pid, ec))
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 def pageUp(self):
anatofuz
parents:
diff changeset
78 if self.viewline > 0:
anatofuz
parents:
diff changeset
79 self.viewline = self.viewline - 1
anatofuz
parents:
diff changeset
80 self.refreshSource()
anatofuz
parents:
diff changeset
81
anatofuz
parents:
diff changeset
82 def pageDown(self):
anatofuz
parents:
diff changeset
83 if self.viewline < len(self.content) - self.height + 1:
anatofuz
parents:
diff changeset
84 self.viewline = self.viewline + 1
anatofuz
parents:
diff changeset
85 self.refreshSource()
anatofuz
parents:
diff changeset
86 pass
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 def handleKey(self, key):
anatofuz
parents:
diff changeset
89 if key == curses.KEY_DOWN:
anatofuz
parents:
diff changeset
90 self.pageDown()
anatofuz
parents:
diff changeset
91 elif key == curses.KEY_UP:
anatofuz
parents:
diff changeset
92 self.pageUp()
anatofuz
parents:
diff changeset
93
anatofuz
parents:
diff changeset
94 def updateViewline(self):
anatofuz
parents:
diff changeset
95 half = self.height / 2
anatofuz
parents:
diff changeset
96 if self.pc_line < half:
anatofuz
parents:
diff changeset
97 self.viewline = 0
anatofuz
parents:
diff changeset
98 else:
anatofuz
parents:
diff changeset
99 self.viewline = self.pc_line - half + 1
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 if self.viewline < 0:
anatofuz
parents:
diff changeset
102 raise Exception(
anatofuz
parents:
diff changeset
103 "negative viewline: pc=%d viewline=%d" %
anatofuz
parents:
diff changeset
104 (self.pc_line, self.viewline))
anatofuz
parents:
diff changeset
105
anatofuz
parents:
diff changeset
106 def refreshSource(self, process=None):
anatofuz
parents:
diff changeset
107 (self.height, self.width) = self.win.getmaxyx()
anatofuz
parents:
diff changeset
108
anatofuz
parents:
diff changeset
109 if process is not None:
anatofuz
parents:
diff changeset
110 loc = process.GetSelectedThread().GetSelectedFrame().GetLineEntry()
anatofuz
parents:
diff changeset
111 f = loc.GetFileSpec()
anatofuz
parents:
diff changeset
112 self.pc_line = loc.GetLine()
anatofuz
parents:
diff changeset
113
anatofuz
parents:
diff changeset
114 if not f.IsValid():
anatofuz
parents:
diff changeset
115 self.win.addstr(0, 0, "Invalid source file")
anatofuz
parents:
diff changeset
116 return
anatofuz
parents:
diff changeset
117
anatofuz
parents:
diff changeset
118 self.filename = f.GetFilename()
anatofuz
parents:
diff changeset
119 path = os.path.join(f.GetDirectory(), self.filename)
anatofuz
parents:
diff changeset
120 self.setTitle(path)
anatofuz
parents:
diff changeset
121 self.content = self.getContent(path)
anatofuz
parents:
diff changeset
122 self.updateViewline()
anatofuz
parents:
diff changeset
123
anatofuz
parents:
diff changeset
124 if self.filename is None:
anatofuz
parents:
diff changeset
125 return
anatofuz
parents:
diff changeset
126
anatofuz
parents:
diff changeset
127 if self.formatter is not None:
anatofuz
parents:
diff changeset
128 from pygments.lexers import get_lexer_for_filename
anatofuz
parents:
diff changeset
129 self.lexer = get_lexer_for_filename(self.filename)
anatofuz
parents:
diff changeset
130
anatofuz
parents:
diff changeset
131 bps = [] if not self.filename in self.breakpoints else self.breakpoints[self.filename]
anatofuz
parents:
diff changeset
132 self.win.erase()
anatofuz
parents:
diff changeset
133 if self.content:
anatofuz
parents:
diff changeset
134 self.formatContent(self.content, self.pc_line, bps)
anatofuz
parents:
diff changeset
135
anatofuz
parents:
diff changeset
136 def getContent(self, path):
anatofuz
parents:
diff changeset
137 content = []
anatofuz
parents:
diff changeset
138 if path in self.sources:
anatofuz
parents:
diff changeset
139 content = self.sources[path]
anatofuz
parents:
diff changeset
140 else:
anatofuz
parents:
diff changeset
141 if os.path.exists(path):
anatofuz
parents:
diff changeset
142 with open(path) as x:
anatofuz
parents:
diff changeset
143 content = x.readlines()
anatofuz
parents:
diff changeset
144 self.sources[path] = content
anatofuz
parents:
diff changeset
145 return content
anatofuz
parents:
diff changeset
146
anatofuz
parents:
diff changeset
147 def formatContent(self, content, pc_line, breakpoints):
anatofuz
parents:
diff changeset
148 source = ""
anatofuz
parents:
diff changeset
149 count = 1
anatofuz
parents:
diff changeset
150 self.win.erase()
anatofuz
parents:
diff changeset
151 end = min(len(content), self.viewline + self.height)
anatofuz
parents:
diff changeset
152 for i in range(self.viewline, end):
anatofuz
parents:
diff changeset
153 line_num = i + 1
anatofuz
parents:
diff changeset
154 marker = self.markerNone
anatofuz
parents:
diff changeset
155 attr = curses.A_NORMAL
anatofuz
parents:
diff changeset
156 if line_num == pc_line:
anatofuz
parents:
diff changeset
157 attr = curses.A_REVERSE
anatofuz
parents:
diff changeset
158 if line_num in breakpoints:
anatofuz
parents:
diff changeset
159 marker = self.markerBP
anatofuz
parents:
diff changeset
160 line = "%s%3d %s" % (marker, line_num, self.highlight(content[i]))
anatofuz
parents:
diff changeset
161 if len(line) >= self.width:
anatofuz
parents:
diff changeset
162 line = line[0:self.width - 1] + "\n"
anatofuz
parents:
diff changeset
163 self.win.addstr(line, attr)
anatofuz
parents:
diff changeset
164 source += line
anatofuz
parents:
diff changeset
165 count = count + 1
anatofuz
parents:
diff changeset
166 return source
anatofuz
parents:
diff changeset
167
anatofuz
parents:
diff changeset
168 def highlight(self, source):
anatofuz
parents:
diff changeset
169 if self.lexer and self.formatter:
anatofuz
parents:
diff changeset
170 from pygments import highlight
anatofuz
parents:
diff changeset
171 return highlight(source, self.lexer, self.formatter)
anatofuz
parents:
diff changeset
172 else:
anatofuz
parents:
diff changeset
173 return source
anatofuz
parents:
diff changeset
174
anatofuz
parents:
diff changeset
175 def addBPLocations(self, locations):
anatofuz
parents:
diff changeset
176 for path in locations:
anatofuz
parents:
diff changeset
177 lines = locations[path]
anatofuz
parents:
diff changeset
178 if path in self.breakpoints:
anatofuz
parents:
diff changeset
179 self.breakpoints[path].update(lines)
anatofuz
parents:
diff changeset
180 else:
anatofuz
parents:
diff changeset
181 self.breakpoints[path] = lines
anatofuz
parents:
diff changeset
182
anatofuz
parents:
diff changeset
183 def removeBPLocations(self, locations):
anatofuz
parents:
diff changeset
184 for path in locations:
anatofuz
parents:
diff changeset
185 lines = locations[path]
anatofuz
parents:
diff changeset
186 if path in self.breakpoints:
anatofuz
parents:
diff changeset
187 self.breakpoints[path].difference_update(lines)
anatofuz
parents:
diff changeset
188 else:
anatofuz
parents:
diff changeset
189 raise "Removing locations that were never added...no good"
anatofuz
parents:
diff changeset
190
anatofuz
parents:
diff changeset
191 def handleBPEvent(self, event):
anatofuz
parents:
diff changeset
192 def getLocations(event):
anatofuz
parents:
diff changeset
193 locs = {}
anatofuz
parents:
diff changeset
194
anatofuz
parents:
diff changeset
195 bp = lldb.SBBreakpoint.GetBreakpointFromEvent(event)
anatofuz
parents:
diff changeset
196
anatofuz
parents:
diff changeset
197 if bp.IsInternal():
anatofuz
parents:
diff changeset
198 # don't show anything for internal breakpoints
anatofuz
parents:
diff changeset
199 return
anatofuz
parents:
diff changeset
200
anatofuz
parents:
diff changeset
201 for location in bp:
anatofuz
parents:
diff changeset
202 # hack! getting the LineEntry via SBBreakpointLocation.GetAddress.GetLineEntry does not work good for
anatofuz
parents:
diff changeset
203 # inlined frames, so we get the description (which does take
anatofuz
parents:
diff changeset
204 # into account inlined functions) and parse it.
anatofuz
parents:
diff changeset
205 desc = lldbutil.get_description(
anatofuz
parents:
diff changeset
206 location, lldb.eDescriptionLevelFull)
anatofuz
parents:
diff changeset
207 match = re.search('at\ ([^:]+):([\d]+)', desc)
anatofuz
parents:
diff changeset
208 try:
anatofuz
parents:
diff changeset
209 path = match.group(1)
anatofuz
parents:
diff changeset
210 line = int(match.group(2).strip())
anatofuz
parents:
diff changeset
211 except ValueError as e:
anatofuz
parents:
diff changeset
212 # bp loc unparsable
anatofuz
parents:
diff changeset
213 continue
anatofuz
parents:
diff changeset
214
anatofuz
parents:
diff changeset
215 if path in locs:
anatofuz
parents:
diff changeset
216 locs[path].add(line)
anatofuz
parents:
diff changeset
217 else:
anatofuz
parents:
diff changeset
218 locs[path] = set([line])
anatofuz
parents:
diff changeset
219 return locs
anatofuz
parents:
diff changeset
220
anatofuz
parents:
diff changeset
221 event_type = lldb.SBBreakpoint.GetBreakpointEventTypeFromEvent(event)
anatofuz
parents:
diff changeset
222 if event_type == lldb.eBreakpointEventTypeEnabled \
anatofuz
parents:
diff changeset
223 or event_type == lldb.eBreakpointEventTypeAdded \
anatofuz
parents:
diff changeset
224 or event_type == lldb.eBreakpointEventTypeLocationsResolved \
anatofuz
parents:
diff changeset
225 or event_type == lldb.eBreakpointEventTypeLocationsAdded:
anatofuz
parents:
diff changeset
226 self.addBPLocations(getLocations(event))
anatofuz
parents:
diff changeset
227 elif event_type == lldb.eBreakpointEventTypeRemoved \
anatofuz
parents:
diff changeset
228 or event_type == lldb.eBreakpointEventTypeLocationsRemoved \
anatofuz
parents:
diff changeset
229 or event_type == lldb.eBreakpointEventTypeDisabled:
anatofuz
parents:
diff changeset
230 self.removeBPLocations(getLocations(event))
anatofuz
parents:
diff changeset
231 elif event_type == lldb.eBreakpointEventTypeCommandChanged \
anatofuz
parents:
diff changeset
232 or event_type == lldb.eBreakpointEventTypeConditionChanged \
anatofuz
parents:
diff changeset
233 or event_type == lldb.eBreakpointEventTypeIgnoreChanged \
anatofuz
parents:
diff changeset
234 or event_type == lldb.eBreakpointEventTypeThreadChanged \
anatofuz
parents:
diff changeset
235 or event_type == lldb.eBreakpointEventTypeInvalidType:
anatofuz
parents:
diff changeset
236 # no-op
anatofuz
parents:
diff changeset
237 pass
anatofuz
parents:
diff changeset
238 self.refreshSource()