3 * Copyright (c) 2002 - 2007, Blue Static <http://www.bluestatic.org>
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.
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.
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
17 #import "DebuggerWindowController.h"
18 #import "DebuggerConnection.h"
19 #import "NSXMLElementAdditions.h"
20 #import "AppDelegate.h"
22 @interface DebuggerWindowController (Private
)
24 - (void)updateSourceViewer
;
28 @implementation DebuggerWindowController
31 * Initializes the window controller and sets the connection
33 - (id)initWithConnection
:(DebuggerConnection
*)cnx
35 if (self = [super initWithWindowNibName
:@
"Debugger"])
38 expandedRegisters
= [[NSMutableArray alloc
] init
];
44 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
48 // set up the scroller for the source viewer
49 [sourceViewer setMaxSize
:NSMakeSize(FLT_MAX
, FLT_MAX
)];
50 [[sourceViewer textContainer
] setContainerSize
:NSMakeSize(FLT_MAX
, FLT_MAX
)];
51 [[sourceViewer textContainer
] setWidthTracksTextView
:NO
];
52 [sourceViewer setHorizontallyResizable
:YES
];
53 [sourceViewerScroller setHasHorizontalScroller
:YES
];
54 [sourceViewerScroller display
];
58 * Called when the window is going to be closed so we can clean up all of our stuff
60 - (void)windowWillClose
:(NSNotification
*)aNotification
62 [connection windowDidClose
];
66 * Release object members
70 [expandedRegisters release
];
76 * Sets the status and clears any error message
78 - (void)setStatus
:(NSString
*)aStatus
80 [errormsg setHidden
:YES
];
81 [statusmsg setStringValue
:aStatus
];
82 [[self window
] setTitle
:[NSString stringWithFormat
:@
"GDBp @ %@:%d/%@", [connection remoteHost
], [connection port
], [connection session
]]];
84 [stepInButton setEnabled
:NO
];
85 [stepOutButton setEnabled
:NO
];
86 [stepOverButton setEnabled
:NO
];
87 [runButton setEnabled
:NO
];
88 [reconnectButton setEnabled
:NO
];
90 if ([connection isConnected
])
92 if ([aStatus isEqualToString
:@
"Starting"])
94 [stepInButton setEnabled
:YES
];
95 [runButton setEnabled
:YES
];
100 [reconnectButton setEnabled
:YES
];
105 * Sets the status to be "Error" and then displays the error message
107 - (void)setError
:(NSString
*)anError
109 [errormsg setStringValue
:anError
];
110 [self setStatus
:@
"Error"];
111 [errormsg setHidden
:NO
];
115 * Sets the root node element of the stacktrace
117 - (void)setStack
:(NSArray
*)node
127 if ([stack count
] > 1)
129 [stepOutButton setEnabled
:YES
];
131 [stepInButton setEnabled
:YES
];
132 [stepOverButton setEnabled
:YES
];
133 [runButton setEnabled
:YES
];
135 [self updateSourceViewer
];
139 * Sets the stack root element so that the NSOutlineView can display it
141 - (void)setRegister
:(NSXMLDocument
*)elm
144 [_registerController willChangeValueForKey:@"rootElement.children"];
145 [_registerController unbind:@"contentArray"];
146 [_registerController bind:@"contentArray" toObject:elm withKeyPath:@"rootElement.children" options:nil];
147 [_registerController didChangeValueForKey:@"rootElement.children"];
149 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
150 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
151 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
152 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
153 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
154 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
155 // that's okay for us as we just need to replace the entire thing.
156 [registerController setContent
:nil];
157 [registerController setContent
:[[elm rootElement
] children
]];
159 for (int i
= 0; i
< [registerView numberOfRows
]; i
++)
161 int index
= [expandedRegisters indexOfObject
:[[[registerView itemAtRow
:i
] observedObject
] variable
]];
162 if (index
!= NSNotFound
)
164 [registerView expandItem
:[registerView itemAtRow
:i
]];
170 * Forwards the message to run script execution to the connection
172 - (IBAction
)run
:(id)sender
178 * Tells the connection to ask the server to reconnect
180 - (IBAction
)reconnect
:(id)sender
186 * Forwards the message to "step in" to the connection
188 - (IBAction
)stepIn
:(id)sender
194 * Forwards the message to "step out" to the connection
196 - (IBAction
)stepOut
:(id)sender
198 [connection stepOut
];
202 * Forwards the message to "step over" to the connection
204 - (IBAction
)stepOver
:(id)sender
206 [connection stepOver
];
210 * NSTableView delegate method that informs the controller that the stack selection did change and that
211 * we should update the source viewer
213 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
215 [self updateSourceViewer
];
219 * Does the actual updating of the source viewer by reading in the file
221 - (void)updateSourceViewer
223 int selection
= [stackController selectionIndex
];
224 if (selection
== NSNotFound
)
226 [sourceViewer setString
:@
""];
230 // get the filename and then set the text
231 NSString
*filename
= [[stack objectAtIndex
:selection
] valueForKey
:@
"filename"];
232 filename
= [[NSURL URLWithString
:filename
] path
];
233 NSString
*text
= [NSString stringWithContentsOfFile
:filename
];
234 [sourceViewer setString
:text
];
236 // go through the document until we find the NSRange for the line we want
237 int destination
= [[[stack objectAtIndex
:selection
] valueForKey
:@
"lineno"] intValue
];
239 for (int line
= 0; line
< destination
; line
++)
241 rangeIndex
= NSMaxRange([text lineRangeForRange
:NSMakeRange(rangeIndex
, 0)]);
244 // now get the true start/end markers for it
245 unsigned lineStart
, lineEnd
;
246 [text getLineStart
:&lineStart end
:NULL contentsEnd
:&lineEnd forRange
:NSMakeRange(rangeIndex
- 1, 0)];
247 NSRange lineRange
= NSMakeRange(lineStart
, lineEnd
- lineStart
);
249 // colorize it so the user knows which line we're on in the stack
250 [[sourceViewer textStorage
] setAttributes
:[NSDictionary dictionaryWithObjects
:[NSArray arrayWithObjects
:[NSColor redColor
], [NSColor yellowColor
], nil]
251 forKeys
:[NSArray arrayWithObjects
:NSForegroundColorAttributeName
, NSBackgroundColorAttributeName
, nil]]
253 [sourceViewer scrollRangeToVisible
:[text lineRangeForRange
:NSMakeRange(lineStart
, lineEnd
- lineStart
)]];
255 // make sure the font stays Monaco
256 [sourceViewer setFont
:[NSFont fontWithName
:@
"Monaco" size
:10.0]];
260 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
262 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
264 NSLog(@
"notification expanded:%@", notif
);
265 // XXX: This very well may break because NSTreeController sends us a _NSArrayControllerTreeNode object
266 // which is presumably private, and thus this is not a reliable method for getting the object. But
267 // we damn well need it, so f!ck the rules and we're using it. <rdar://problem/5387001>
268 id notifObj
= [[notif userInfo
] objectForKey
:@
"NSObject"];
269 NSXMLElement
*obj
= [notifObj observedObject
];
271 // we're not a leaf but have no children. this must be beyond our depth, so go make us deeper
272 if (![obj isLeaf
] && [[obj children
] count
] < 1)
274 [connection getProperty
:[[obj attributeForName
:@
"fullname"] stringValue
] forElement
:notifObj
];
277 [expandedRegisters addObject
:[obj variable
]];
281 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
283 - (void)outlineViewItemDidCollapse
:(id)notif
285 [expandedRegisters removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] observedObject
] variable
]];
286 NSLog(@
"outlineViewDidCollapse:%@", notif
);
290 * Updates the register view by reinserting a given node back into the outline view
292 - (void)addChildren
:(NSArray
*)children toNode
:(id)node
294 NSLog(@
"addChildren node:%@", node
);
295 // XXX: this may break like in outlineViewItemDidExpand:<rdar://problem/5387001>
296 NSIndexPath
*masterPath
= [node indexPath
];
297 for (int i
= 0; i
< [children count
]; i
++)
299 [registerController insertObject
:[children objectAtIndex
:i
] atArrangedObjectIndexPath
:[masterPath indexPathByAddingIndex
:i
]];
302 [registerController rearrangeObjects
];