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"
21 @interface DebuggerWindowController (Private
)
23 - (void)updateSourceViewer
;
27 @implementation DebuggerWindowController
30 * Initializes the window controller and sets the connection
32 - (id)initWithConnection
: (DebuggerConnection
*)cnx
34 if (self = [super initWithWindowNibName
: @
"Debugger"])
36 _connection
= [cnx retain
];
42 * Before the display get's comfortable, set up the NSTextView to scroll horizontally
46 // set up the scroller for the source viewer
47 [_sourceViewer setMaxSize
: NSMakeSize(FLT_MAX
, FLT_MAX
)];
48 [[_sourceViewer textContainer
] setContainerSize
: NSMakeSize(FLT_MAX
, FLT_MAX
)];
49 [[_sourceViewer textContainer
] setWidthTracksTextView
: NO
];
50 [_sourceViewer setHorizontallyResizable
: YES
];
51 [_sourceViewerScroller setHasHorizontalScroller
: YES
];
52 [_sourceViewerScroller display
];
56 * Release object members
60 [_connection release
];
66 * Sets the status and clears any error message
68 - (void)setStatus
: (NSString
*)status
70 [_error setHidden
: YES
];
71 [_status setStringValue
: status
];
72 [[self window
] setTitle
: [NSString stringWithFormat
: @
"GDBp @ %@:%d/%@", [_connection remoteHost
], [_connection port
], [_connection session
]]];
74 [_stepInButton setEnabled
: NO
];
75 [_stepOutButton setEnabled
: NO
];
76 [_stepOverButton setEnabled
: NO
];
77 [_runButton setEnabled
: NO
];
79 if ([_connection isConnected
])
81 if ([status isEqualToString
: @
"Starting"])
83 [_stepInButton setEnabled
: YES
];
84 [_runButton setEnabled
: YES
];
90 * Sets the status to be "Error" and then displays the error message
92 - (void)setError
: (NSString
*)error
94 [_error setStringValue
: error
];
95 [self setStatus
: @
"Error"];
96 [_error setHidden
: NO
];
100 * Sets the root node element of the stacktrace
102 - (void)setStack
: (NSArray
*)stack
112 if ([_stack count
] > 1)
114 [_stepOutButton setEnabled
: YES
];
116 [_stepInButton setEnabled
: YES
];
117 [_stepOverButton setEnabled
: YES
];
118 [_runButton setEnabled
: YES
];
120 [self updateSourceViewer
];
124 * Sets the stack root element so that the NSOutlineView can display it
126 - (void)setRegister
: (NSXMLElement
*)elm
128 if (_register
!= nil)
138 * Forwards the message to run script execution to the connection
140 - (IBAction
)run
: (id)sender
146 * Forwards the message to "step in" to the connection
148 - (IBAction
)stepIn
: (id)sender
150 [_connection stepIn
];
154 * Forwards the message to "step out" to the connection
156 - (IBAction
)stepOut
: (id)sender
158 [_connection stepOut
];
162 * Forwards the message to "step over" to the connection
164 - (IBAction
)stepOver
: (id)sender
166 [_connection stepOver
];
170 * NSTableView delegate method that informs the controller that the stack selection did change and that
171 * we should update the source viewer
173 - (void)tableViewSelectionDidChange
: (NSNotification
*)notif
175 NSLog(@
"selection changed");
176 [self updateSourceViewer
];
179 * Does the actual updating of the source viewer by reading in the file
181 - (void)updateSourceViewer
183 int selection
= [_stackController selectionIndex
];
184 if (selection
== NSNotFound
)
186 [_sourceViewer setString
: @
""];
190 // get the filename and then set the text
191 NSString
*filename
= [[_stack objectAtIndex
: selection
] valueForKey
: @
"filename"];
192 filename
= [[NSURL URLWithString
: filename
] path
];
193 NSString
*text
= [NSString stringWithContentsOfFile
: filename
];
194 [_sourceViewer setString
: text
];
196 // go through the document until we find the NSRange for the line we want
197 int destination
= [[[_stack objectAtIndex
: selection
] valueForKey
: @
"lineno"] intValue
];
199 for (int line
= 0; line
< destination
; line
++)
201 rangeIndex
= NSMaxRange([text lineRangeForRange
: NSMakeRange(rangeIndex
, 0)]);
204 // now get the true start/end markers for it
205 unsigned lineStart
, lineEnd
;
206 [text getLineStart
: &lineStart end
: NULL contentsEnd
: &lineEnd forRange
: NSMakeRange(rangeIndex
- 1, 0)];
207 NSRange lineRange
= NSMakeRange(lineStart
, lineEnd
- lineStart
);
209 // colorize it so the user knows which line we're on in the stack
210 [[_sourceViewer textStorage
] setAttributes
: [NSDictionary dictionaryWithObjects
: [NSArray arrayWithObjects
: [NSColor redColor
], [NSColor yellowColor
], nil]
211 forKeys
: [NSArray arrayWithObjects
: NSForegroundColorAttributeName
, NSBackgroundColorAttributeName
, nil]]
213 [_sourceViewer scrollRangeToVisible
: [text lineRangeForRange
: NSMakeRange(lineStart
, lineEnd
- lineStart
)]];
215 // make sure the font stays Monaco
216 [_sourceViewer setFont
: [NSFont fontWithName
: @
"Monaco" size
: 10.0]];
220 * Called whenver an item is expanded. This allows us to determine if we need to fetch deeper
222 - (void)outlineViewItemDidExpand
: (NSNotification
*)notif
224 // XXX: This very well may break because NSTreeController sends us a _NSArrayControllerTreeNode object
225 // which is presumably private, and thus this is not a reliable method for getting the object. But
226 // we damn well need it, so f!ck the rules and we're using it. <rdar://problem/5387001>
227 id notifObj
= [[notif userInfo
] objectForKey
: @
"NSObject"];
228 NSXMLElement
*obj
= [notifObj observedObject
];
230 // we're not a leaf but have no children. this must be beyond our depth, so go make us deeper
231 if (![obj isLeaf
] && [[obj children
] count
] < 1)
233 [_connection getProperty
: [[obj attributeForName
: @
"fullname"] stringValue
] forElement
: notifObj
];
238 * Updates the register view by reinserting a given node back into the outline view
240 - (void)addChildren
: (NSArray
*)children toNode
: (id)node
242 // XXX: this may break like in outlineViewItemDidExpand: <rdar://problem/5387001>
243 NSIndexPath
*masterPath
= [node indexPath
];
244 for (int i
= 0; i
< [children count
]; i
++)
246 [_registerController insertObject
: [children objectAtIndex
: i
] atArrangedObjectIndexPath
: [masterPath indexPathByAddingIndex
: i
]];
249 [_registerController rearrangeObjects
];