]> src.bluestatic.org Git - macgdbp.git/blob - Source/DebuggerController.m
Specify CODE_SIGN_STYLE=Manual .
[macgdbp.git] / Source / DebuggerController.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2011, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "DebuggerController.h"
18
19 #import "AppDelegate.h"
20 #import "BSSourceView.h"
21 #import "BreakpointController.h"
22 #import "BreakpointManager.h"
23 #import "DebuggerBackEnd.h"
24 #import "DebuggerModel.h"
25 #import "EvalController.h"
26 #import "FileAccessController.h"
27 #import "PreferenceNames.h"
28 #import "StackFrame.h"
29
30 @interface DebuggerController (Private)
31 - (void)updateSourceViewer;
32 - (void)expandVariables;
33 @end
34
35 @implementation DebuggerController {
36 DebuggerModel* _model;
37
38 DebuggerBackEnd* _connection;
39
40 BreakpointController* _breakpointsController;
41 EvalController* _evalController;
42
43 NSMutableSet* _expandedVariables;
44 VariableNode* _selectedVariable;
45 }
46
47 /**
48 * Initializes the window controller and sets the connection using preference
49 * values
50 */
51 - (id)init
52 {
53 if (self = [super initWithWindowNibName:@"Debugger"])
54 {
55 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
56
57 _model = [[DebuggerModel alloc] init];
58 [_model addObserver:self
59 forKeyPath:@"connected"
60 options:NSKeyValueObservingOptionNew
61 context:nil];
62
63 _connection = [[DebuggerBackEnd alloc] initWithModel:_model
64 port:[defaults integerForKey:kPrefPort]
65 autoAttach:[defaults boolForKey:kPrefDebuggerAttached]];
66 _model.breakpointManager.connection = _connection;
67
68 [_model addObserver:self
69 forKeyPath:@"status"
70 options:NSKeyValueObservingOptionNew
71 context:nil];
72
73 _expandedVariables = [[NSMutableSet alloc] init];
74 [[self window] makeKeyAndOrderFront:nil];
75 [[self window] setDelegate:self];
76
77 if ([defaults boolForKey:kPrefInspectorWindowVisible])
78 [_inspector orderFront:self];
79 }
80 return self;
81 }
82
83 /**
84 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
85 */
86 - (void)awakeFromNib
87 {
88 // Exclude from the Windows menu because there is an explicit entry.
89 [[self window] setExcludedFromWindowsMenu:YES];
90
91 // Connect to XIB properties.
92 [_sourceViewer setDelegate:self];
93 [_stackArrayController setSortDescriptors:@[ [[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES] ]];
94 [_stackArrayController addObserver:self
95 forKeyPath:@"selectedObjects"
96 options:NSKeyValueObservingOptionNew
97 context:nil];
98 [_stackArrayController addObserver:self
99 forKeyPath:@"selection.source"
100 options:NSKeyValueObservingOptionNew
101 context:nil];
102 [_variablesTreeController addObserver:self
103 forKeyPath:@"arrangedObjects"
104 options:NSKeyValueObservingOptionNew
105 context:nil];
106 self.connection.autoAttach = [_attachedCheckbox state] == NSOnState;
107
108 // Load view controllers into the tab views.
109 _breakpointsController = [[BreakpointController alloc] initWithBreakpointManager:_model.breakpointManager
110 sourceView:_sourceViewer];
111 [[self.tabView tabViewItemAtIndex:1] setView:_breakpointsController.view];
112
113 _evalController = [[EvalController alloc] initWithBackEnd:_connection];
114 [[self.tabView tabViewItemAtIndex:2] setView:_evalController.view];
115
116 // When the segment control's selection changes, update the tab view.
117 [[_segmentControl cell] addObserver:self
118 forKeyPath:@"selectedSegment"
119 options:0
120 context:nil];
121 // When the segment control's superview changes, recalculate the spacer
122 // segment widths.
123 [[_segmentControl superview] addObserver:self
124 forKeyPath:@"frame"
125 options:0
126 context:nil];
127
128 NSUInteger selectedSegment =
129 [[[NSUserDefaults standardUserDefaults] valueForKey:kPrefSelectedDebuggerSegment] intValue];
130 [[_segmentControl cell] setSelectedSegment:selectedSegment];
131 [self updateSegmentControl];
132 }
133
134 /**
135 * Key-value observation routine.
136 */
137 - (void)observeValueForKeyPath:(NSString*)keyPath
138 ofObject:(id)object
139 change:(NSDictionary<NSString*,id>*)change
140 context:(void*)context {
141 if (object == _stackArrayController && [keyPath isEqualToString:@"selectedObjects"]) {
142 for (StackFrame* frame in _stackArrayController.selectedObjects)
143 [_connection loadStackFrame:frame];
144 } else if (object == _stackArrayController && [keyPath isEqualToString:@"selection.source"]) {
145 [self updateSourceViewer];
146 } else if (object == _variablesTreeController) {
147 [self expandVariables];
148 } else if (object == _model) {
149 if ([keyPath isEqualToString:@"connected"]) {
150 if ([change[NSKeyValueChangeNewKey] boolValue]) {
151 [self debuggerConnected];
152 } else {
153 [self debuggerDisconnected];
154 }
155 }
156 } else if (object == _segmentControl.cell) {
157 [[NSUserDefaults standardUserDefaults] setValue:@(_segmentControl.selectedSegment)
158 forKey:kPrefSelectedDebuggerSegment];
159 [_tabView selectTabViewItemAtIndex:_segmentControl.selectedSegment - 1];
160 } else if (object == _segmentControl.superview) {
161 [self updateSegmentControl];
162 } else {
163 [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
164 }
165 }
166
167 /**
168 * Validates the menu items for the "Debugger" menu
169 */
170 - (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)anItem
171 {
172 SEL action = [anItem action];
173
174 if (action == @selector(stepOut:)) {
175 return _model.connected && _model.stackDepth > 1;
176 } else if (action == @selector(stepIn:) ||
177 action == @selector(stepOver:) ||
178 action == @selector(run:) ||
179 action == @selector(stop:)) {
180 return _model.connected;
181 }
182 return [[self window] validateUserInterfaceItem:anItem];
183 }
184
185 /**
186 * Shows the inspector window
187 */
188 - (IBAction)showInspectorWindow:(id)sender
189 {
190 if (![_inspector isVisible])
191 [_inspector makeKeyAndOrderFront:sender];
192 else
193 [_inspector orderOut:sender];
194 }
195
196 /**
197 * Runs the eval window sheet.
198 */
199 - (IBAction)showEvalWindow:(id)sender
200 {
201 [self.segmentControl setSelectedSegment:3];
202 }
203
204 /**
205 * Delegate function for GDBpConnection for when the debugger connects.
206 */
207 - (void)debuggerConnected
208 {
209 if (!self.connection.autoAttach)
210 return;
211 if ([[NSUserDefaults standardUserDefaults] boolForKey:kPrefBreakOnFirstLine])
212 [self stepIn:self];
213 // Do not cache the file between debugger executions.
214 _sourceViewer.file = nil;
215 [_expandedVariables removeAllObjects];
216 }
217
218 /**
219 * Called once the debugger disconnects.
220 */
221 - (void)debuggerDisconnected
222 {
223 // Invalidate the marked line so we don't look like we're still running.
224 _sourceViewer.markedLine = -1;
225 [_sourceViewer setNeedsDisplay:YES];
226 }
227
228 /**
229 * Forwards the message to run script execution to the connection
230 */
231 - (IBAction)run:(id)sender
232 {
233 [_connection run];
234 }
235
236 - (IBAction)attachedToggled:(id)sender
237 {
238 _connection.autoAttach = [sender state] == NSOnState;
239 }
240
241 /**
242 * Forwards the message to "step in" to the connection
243 */
244 - (IBAction)stepIn:(id)sender
245 {
246 if ([[_variablesTreeController selectedObjects] count] > 0)
247 _selectedVariable = [[_variablesTreeController selectedObjects] objectAtIndex:0];
248
249 [_connection stepIn];
250 }
251
252 /**
253 * Forwards the message to "step out" to the connection
254 */
255 - (IBAction)stepOut:(id)sender
256 {
257 if ([[_variablesTreeController selectedObjects] count] > 0)
258 _selectedVariable = [[_variablesTreeController selectedObjects] objectAtIndex:0];
259
260 [_connection stepOut];
261 }
262
263 /**
264 * Forwards the message to "step over" to the connection
265 */
266 - (IBAction)stepOver:(id)sender
267 {
268 if ([[_variablesTreeController selectedObjects] count] > 0)
269 _selectedVariable = [[_variablesTreeController selectedObjects] objectAtIndex:0];
270
271 [_connection stepOver];
272 }
273
274 /**
275 * Forwards the detach/"stop" message to the back end.
276 */
277 - (IBAction)stop:(id)sender
278 {
279 [_connection stop];
280 }
281
282 /**
283 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
284 */
285 - (void)outlineViewItemDidExpand:(NSNotification*)notif
286 {
287 NSTreeNode* node = [[notif userInfo] objectForKey:@"NSObject"];
288 [_expandedVariables addObject:[[node representedObject] fullName]];
289
290 [_connection loadVariableNode:[node representedObject]
291 forStackFrame:[[_stackArrayController selectedObjects] lastObject]];
292 [self expandVariables];
293 }
294
295 /**
296 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
297 */
298 - (void)outlineViewItemDidCollapse:(NSNotification*)notif
299 {
300 [_expandedVariables removeObject:[[[[notif userInfo] objectForKey:@"NSObject"] representedObject] fullName]];
301 }
302
303 #pragma mark Private
304
305 /**
306 * Does the actual updating of the source viewer by reading in the file
307 */
308 - (void)updateSourceViewer
309 {
310 StackFrame* frame = _stackArrayController.selectedObjects.firstObject;
311 if (!frame)
312 return;
313
314 if (!frame.loaded && self.model.connected) {
315 [_connection loadStackFrame:frame];
316 return;
317 }
318
319 // Get the filename.
320 NSString* filename = [[NSURL URLWithString:frame.filename] path];
321 if ([filename isEqualToString:@""])
322 return;
323
324 if (![_sourceViewer.file isEqualToString:filename]) {
325 // Replace the source if necessary.
326 if (frame.source) {
327 [_sourceViewer setString:frame.source asFile:filename];
328 } else {
329 [_sourceViewer setFile:filename];
330 }
331
332 NSSet<NSNumber*>* breakpoints = [_model.breakpointManager breakpointsForFile:filename];
333 [_sourceViewer setMarkers:breakpoints];
334 }
335
336 [_sourceViewer setMarkedLine:frame.lineNumber];
337 [_sourceViewer scrollToLine:frame.lineNumber];
338 }
339
340 /**
341 * Expands the variables based on the stored set
342 */
343 - (void)expandVariables
344 {
345 NSString* selection = [_selectedVariable fullName];
346
347 for (NSInteger i = 0; i < [_variablesOutlineView numberOfRows]; i++) {
348 NSTreeNode* node = [_variablesOutlineView itemAtRow:i];
349 NSString* fullName = [[node representedObject] fullName];
350
351 // see if it needs expanding
352 if ([_expandedVariables containsObject:fullName])
353 [_variablesOutlineView expandItem:node];
354
355 // select it if we had it selected before
356 if ([fullName isEqualToString:selection])
357 [_variablesTreeController setSelectionIndexPath:[node indexPath]];
358 }
359 }
360
361 /**
362 * Sets the widths of the segmented control.
363 */
364 - (void)updateSegmentControl {
365 NSRect containerFrame = [[_segmentControl superview] frame];
366 CGFloat containerWidth = NSWidth(containerFrame);
367 CGFloat segmentSizes = 0;
368 for (NSInteger i = 1; i < [_segmentControl segmentCount] - 1; ++i) {
369 segmentSizes += [_segmentControl widthForSegment:i];
370 }
371 CGFloat spacerWidth = (containerWidth - segmentSizes) / 2;
372 [_segmentControl setWidth:spacerWidth forSegment:0];
373 [_segmentControl setWidth:spacerWidth forSegment:[_segmentControl segmentCount] - 1];
374
375 [_segmentControl setFrame:NSMakeRect(-5, NSHeight(containerFrame) - 27, containerWidth + 10, 30)];
376 }
377
378 #pragma mark BSSourceView Delegate
379
380 /**
381 * The gutter was clicked, which indicates that a breakpoint needs to be changed
382 */
383 - (void)gutterClickedAtLine:(int)line forFile:(NSString*)file
384 {
385 BreakpointManager* manager = _model.breakpointManager;
386 Breakpoint* breakpoint = [Breakpoint breakpointAtLine:line inFile:file];
387
388 if ([manager hasBreakpoint:breakpoint]) {
389 [manager removeBreakpoint:breakpoint];
390 } else {
391 [manager addBreakpoint:breakpoint];
392 }
393
394 [_sourceViewer setMarkers:[manager breakpointsForFile:file]];
395 }
396
397 - (void)error:(NSError*)error whileHighlightingFile:(NSString*)file
398 {
399 #if USE_APP_SANDBOX
400 if (error.code == NSFileReadNoPermissionError) {
401 [FileAccessController showFileAccessDialog];
402 }
403 #endif // USE_APP_SANDBOX
404 }
405
406 #pragma mark NSSplitView Delegate
407
408 - (CGFloat)splitView:(NSSplitView*)splitView
409 constrainSplitPosition:(CGFloat)proposedPosition
410 ofSubviewAt:(NSInteger)dividerIndex
411 {
412 const NSSize splitViewSize = splitView.bounds.size;
413 const CGFloat minimumSplit = 150;
414 const CGFloat maximumSplit = (splitView.isVertical ? splitViewSize.width : splitViewSize.height) - minimumSplit;
415
416 if (proposedPosition < minimumSplit)
417 return minimumSplit;
418 if (proposedPosition > maximumSplit)
419 return maximumSplit;
420 return proposedPosition;
421 }
422
423 @end