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)initWithPort
:(int)aPort session
:(NSString
*)aSession
35 if (self = [super initWithWindowNibName
:@
"Debugger"])
37 connection
= [[DebuggerConnection alloc
] initWithWindowController
:self port
:aPort session
:aSession
];
38 expandedRegisters
= [[NSMutableArray alloc
] init
];
39 [[self window
] makeKeyAndOrderFront
:nil];
45 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
49 // set up the scroller for the source viewer
50 [sourceViewer setMaxSize
:NSMakeSize(FLT_MAX
, FLT_MAX
)];
51 [[sourceViewer textContainer
] setContainerSize
:NSMakeSize(FLT_MAX
, FLT_MAX
)];
52 [[sourceViewer textContainer
] setWidthTracksTextView
:NO
];
53 [sourceViewer setHorizontallyResizable
:YES
];
54 [sourceViewerScroller setHasHorizontalScroller
:YES
];
55 [sourceViewerScroller display
];
57 [self setStatus
:@
"Connecting"];
61 * Release object members
65 [expandedRegisters release
];
71 * Sets the status and clears any error message
73 - (void)setStatus
:(NSString
*)aStatus
75 [errormsg setHidden
:YES
];
76 [statusmsg setStringValue
:aStatus
];
77 [[self window
] setTitle
:[NSString stringWithFormat
:@
"GDBp @ %@:%d/%@", [connection remoteHost
], [connection port
], [connection session
]]];
79 [stepInButton setEnabled
:NO
];
80 [stepOutButton setEnabled
:NO
];
81 [stepOverButton setEnabled
:NO
];
82 [runButton setEnabled
:NO
];
83 [reconnectButton setEnabled
:NO
];
85 if ([connection isConnected
])
87 if ([aStatus isEqualToString
:@
"Starting"])
89 [stepInButton setEnabled
:YES
];
90 [runButton setEnabled
:YES
];
95 [reconnectButton setEnabled
:YES
];
100 * Sets the status to be "Error" and then displays the error message
102 - (void)setError
:(NSString
*)anError
104 [errormsg setStringValue
:anError
];
105 [self setStatus
:@
"Error"];
106 [errormsg setHidden
:NO
];
110 * Sets the root node element of the stacktrace
112 - (void)setStack
:(NSArray
*)node
122 if ([stack count
] > 1)
124 [stepOutButton setEnabled
:YES
];
126 [stepInButton setEnabled
:YES
];
127 [stepOverButton setEnabled
:YES
];
128 [runButton setEnabled
:YES
];
130 [self updateSourceViewer
];
134 * Sets the stack root element so that the NSOutlineView can display it
136 - (void)setRegister
:(NSXMLDocument
*)elm
139 [_registerController willChangeValueForKey:@"rootElement.children"];
140 [_registerController unbind:@"contentArray"];
141 [_registerController bind:@"contentArray" toObject:elm withKeyPath:@"rootElement.children" options:nil];
142 [_registerController didChangeValueForKey:@"rootElement.children"];
144 // XXX: Doing anything short of this will cause bindings to crash spectacularly for no reason whatsoever, and
145 // in seemingly arbitrary places. The class that crashes is _NSKeyValueObservationInfoCreateByRemoving.
146 // http://boredzo.org/blog/archives/2006-01-29/have-you-seen-this-crash says that this means nothing is
147 // being observed, but I doubt that he was using an NSOutlineView which seems to be one f!cking piece of
148 // sh!t when used with NSTreeController. http://www.cocoadev.com/index.pl?NSTreeControllerBugOrDeveloperError
149 // was the inspiration for this fix (below) but the author says that inserting does not work too well, but
150 // that's okay for us as we just need to replace the entire thing.
151 [registerController setContent
:nil];
152 [registerController setContent
:[[elm rootElement
] children
]];
154 /*for (NSTreeNode *node in expandedRegisters)
156 [registerView expandItem:node];
158 NSLog(@
"expanded items = %@", expandedRegisters
);
162 * Forwards the message to run script execution to the connection
164 - (IBAction
)run
:(id)sender
170 * Tells the connection to ask the server to reconnect
172 - (IBAction
)reconnect
:(id)sender
178 * Forwards the message to "step in" to the connection
180 - (IBAction
)stepIn
:(id)sender
186 * Forwards the message to "step out" to the connection
188 - (IBAction
)stepOut
:(id)sender
190 [connection stepOut
];
194 * Forwards the message to "step over" to the connection
196 - (IBAction
)stepOver
:(id)sender
198 [connection stepOver
];
202 * NSTableView delegate method that informs the controller that the stack selection did change and that
203 * we should update the source viewer
205 - (void)tableViewSelectionDidChange
:(NSNotification
*)notif
207 [self updateSourceViewer
];
211 * Does the actual updating of the source viewer by reading in the file
213 - (void)updateSourceViewer
215 int selection
= [stackController selectionIndex
];
216 if (selection
== NSNotFound
)
218 [sourceViewer setString
:@
""];
222 // get the filename and then set the text
223 NSString
*filename
= [[stack objectAtIndex
:selection
] valueForKey
:@
"filename"];
224 filename
= [[NSURL URLWithString
:filename
] path
];
225 NSString
*text
= [NSString stringWithContentsOfFile
:filename
];
226 [sourceViewer setString
:text
];
228 // go through the document until we find the NSRange for the line we want
229 int destination
= [[[stack objectAtIndex
:selection
] valueForKey
:@
"lineno"] intValue
];
231 for (int line
= 0; line
< destination
; line
++)
233 rangeIndex
= NSMaxRange([text lineRangeForRange
:NSMakeRange(rangeIndex
, 0)]);
236 // now get the true start/end markers for it
237 unsigned lineStart
, lineEnd
;
238 [text getLineStart
:&lineStart end
:NULL contentsEnd
:&lineEnd forRange
:NSMakeRange(rangeIndex
- 1, 0)];
239 NSRange lineRange
= NSMakeRange(lineStart
, lineEnd
- lineStart
);
241 // colorize it so the user knows which line we're on in the stack
242 [[sourceViewer textStorage
] setAttributes
:[NSDictionary dictionaryWithObjects
:[NSArray arrayWithObjects
:[NSColor redColor
], [NSColor yellowColor
], nil]
243 forKeys
:[NSArray arrayWithObjects
:NSForegroundColorAttributeName
, NSBackgroundColorAttributeName
, nil]]
245 [sourceViewer scrollRangeToVisible
:[text lineRangeForRange
:NSMakeRange(lineStart
, lineEnd
- lineStart
)]];
247 // make sure the font stays Monaco
248 [sourceViewer setFont
:[NSFont fontWithName
:@
"Monaco" size
:10.0]];
252 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
254 - (void)outlineViewItemDidExpand
:(NSNotification
*)notif
256 NSTreeNode
*node
= [[notif userInfo
] objectForKey
:@
"NSObject"];
258 // we're not a leaf but have no children. this must be beyond our depth, so go make us deeper
259 if (![node isLeaf
] && [[node childNodes
] count
] < 1)
261 [connection getProperty
:[[[node representedObject
] attributeForName
:@
"fullname"] stringValue
] forNode
:node
];
264 [expandedRegisters addObject
:[[node representedObject
] variable
]];
268 * Called when an item was collapsed. This allows us to remove it from the list of expanded items
270 - (void)outlineViewItemDidCollapse
:(NSNotification
*)notif
272 [expandedRegisters removeObject
:[[[[notif userInfo
] objectForKey
:@
"NSObject"] representedObject
] variable
]];
276 * Updates the register view by reinserting a given node back into the outline view
278 - (void)addChildren
:(NSArray
*)children toNode
:(NSTreeNode
*)node
280 NSIndexPath
*masterPath
= [node indexPath
];
281 for (int i
= 0; i
< [children count
]; i
++)
283 [registerController insertObject
:[children objectAtIndex
:i
] atArrangedObjectIndexPath
:[masterPath indexPathByAddingIndex
:i
]];
286 [registerController rearrangeObjects
];