From 2d269caee2ae00be126f9bbbac83885f3ad334ff Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 30 Nov 2008 20:19:28 -0500 Subject: [PATCH] Start using our new StackFrame and StackController classes as struts * Source/DebuggerController.h: Add the stackController ivar * Source/DebuggerController.m: (init): Initialize the stackController (dealloc): Release stackController (stepIn:): Pop and push the stackController appropriately (stepOut:): ditto (stepOver:): ditto * Source/GDBpConnection.h: -[stepIn] -[stepOut] and -[stepOver] now all return StackFrame objects * Source/GDBpConnection.m: (stepIn): Generate and return the frame (stepOut): ditto (stepOver): ditto (createStackFrame): New private method * Source/StackController.m+h: (peek): New method * Source/StackFrame.m+h: (isShiftedFrame:): New method (description): Implemented --- Source/DebuggerController.h | 3 +++ Source/DebuggerController.m | 21 ++++++++++++++++--- Source/GDBpConnection.h | 7 ++++--- Source/GDBpConnection.m | 40 ++++++++++++++++++++++++++++++++++--- Source/StackController.h | 1 + Source/StackController.m | 8 ++++++++ Source/StackFrame.h | 2 ++ Source/StackFrame.m | 17 ++++++++++++++++ 8 files changed, 90 insertions(+), 9 deletions(-) diff --git a/Source/DebuggerController.h b/Source/DebuggerController.h index ee98d02..bebcda1 100644 --- a/Source/DebuggerController.h +++ b/Source/DebuggerController.h @@ -16,6 +16,7 @@ #import #import "BSSourceView.h" +#import "StackController.h" @class GDBpConnection; @@ -23,6 +24,8 @@ { GDBpConnection *connection; + StackController *stackController; + IBOutlet NSArrayController *stackController2; NSArray *stack; diff --git a/Source/DebuggerController.m b/Source/DebuggerController.m index e0669bb..f1e1569 100644 --- a/Source/DebuggerController.m +++ b/Source/DebuggerController.m @@ -36,6 +36,8 @@ { if (self = [super initWithWindowNibName:@"Debugger"]) { + stackController = [[StackController alloc] init]; + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; connection = [[GDBpConnection alloc] initWithWindowController:self port:[defaults integerForKey:@"Port"] @@ -54,6 +56,7 @@ { [connection release]; [expandedRegisters release]; + [stackController release]; [super dealloc]; } @@ -203,7 +206,11 @@ */ - (IBAction)stepIn:(id)sender { - [connection stepIn]; + StackFrame *frame = [connection stepIn]; + if ([frame isShiftedFrame:[stackController peek]]) + [stackController pop]; + [stackController push:frame]; + NSLog(@"stack = %@", stackController.stack); } /** @@ -211,7 +218,10 @@ */ - (IBAction)stepOut:(id)sender { - [connection stepOut]; + StackFrame *frame = [connection stepOut]; + [stackController pop]; + [stackController push:frame]; + NSLog(@"stack = %@", stackController.stack); } /** @@ -219,7 +229,12 @@ */ - (IBAction)stepOver:(id)sender { - [connection stepOver]; + StackFrame *frame = [connection stepOver]; + + [stackController pop]; + [stackController push:frame]; + + NSLog(@"stack = %@", stackController.stack); } /** diff --git a/Source/GDBpConnection.h b/Source/GDBpConnection.h index eb7f747..36e4c01 100644 --- a/Source/GDBpConnection.h +++ b/Source/GDBpConnection.h @@ -18,6 +18,7 @@ #import "DebuggerController.h" #import "SocketWrapper.h" #import "Breakpoint.h" +#import "StackFrame.h" @interface GDBpConnection : NSObject { @@ -45,9 +46,9 @@ // communication - (void)reconnect; - (void)run; -- (void)stepIn; -- (void)stepOut; -- (void)stepOver; +- (StackFrame *)stepIn; +- (StackFrame *)stepOut; +- (StackFrame *)stepOver; - (void)addBreakpoint:(Breakpoint *)bp; - (void)removeBreakpoint:(Breakpoint *)bp; - (void)refreshStatus; diff --git a/Source/GDBpConnection.m b/Source/GDBpConnection.m index 10fa28a..2219bb1 100644 --- a/Source/GDBpConnection.m +++ b/Source/GDBpConnection.m @@ -20,6 +20,7 @@ @interface GDBpConnection (Private) - (NSString *)createCommand:(NSString *)cmd; - (NSXMLDocument *)processData:(NSString *)data; +- (StackFrame *)createStackFrame; @end @implementation GDBpConnection @@ -171,31 +172,43 @@ /** * Tells the debugger to step into the current command. */ -- (void)stepIn +- (StackFrame *)stepIn { [socket send:[self createCommand:@"step_into"]]; [socket receive]; + + StackFrame *frame = [self createStackFrame]; [self refreshStatus]; + + return frame; } /** * Tells the debugger to step out of the current context */ -- (void)stepOut +- (StackFrame *)stepOut { [socket send:[self createCommand:@"step_out"]]; [socket receive]; + + StackFrame *frame = [self createStackFrame]; [self refreshStatus]; + + return frame; } /** * Tells the debugger to step over the current function */ -- (void)stepOver +- (StackFrame *)stepOver { [socket send:[self createCommand:@"step_over"]]; [socket receive]; + + StackFrame *frame = [self createStackFrame]; [self refreshStatus]; + + return frame; } /** @@ -320,4 +333,25 @@ return [doc autorelease]; } +/** + * Creates a StackFrame based on the current position in the debugger + */ +- (StackFrame *)createStackFrame +{ + [socket send:[self createCommand:@"stack_get -d 0"]]; + NSXMLDocument *doc = [self processData:[socket receive]]; + + NSXMLElement *xmlframe = [[[doc rootElement] children] objectAtIndex:0]; + StackFrame *frame = [[StackFrame alloc] + initWithIndex:[[[xmlframe attributeForName:@"level"] stringValue] intValue] + withFilename:[[xmlframe attributeForName:@"filename"] stringValue] + withSource:nil + atLine:[[[xmlframe attributeForName:@"lineno"] stringValue] intValue] + inFunction:[[xmlframe attributeForName:@"where"] stringValue] + withContexts:nil + ]; + + return [frame autorelease]; +} + @end diff --git a/Source/StackController.h b/Source/StackController.h index fe58dda..c77883f 100644 --- a/Source/StackController.h +++ b/Source/StackController.h @@ -27,6 +27,7 @@ @property(readonly) NSMutableArray *stack; +- (StackFrame *)peek; - (StackFrame *)pop; - (void)push:(StackFrame *)frame; diff --git a/Source/StackController.m b/Source/StackController.m index 2caea7d..0109c41 100644 --- a/Source/StackController.m +++ b/Source/StackController.m @@ -42,6 +42,14 @@ [super dealloc]; } +/** + * Returns a reference to the top of the stack + */ +- (StackFrame *)peek +{ + return [stack lastObject]; +} + /** * Pops the current frame off the stack and returns the frame */ diff --git a/Source/StackFrame.h b/Source/StackFrame.h index d327e6c..1d080d9 100644 --- a/Source/StackFrame.h +++ b/Source/StackFrame.h @@ -64,4 +64,6 @@ inFunction:(NSString *)function withContexts:(NSDictionary *)contexts; +- (BOOL)isShiftedFrame:(StackFrame *)frame; + @end diff --git a/Source/StackFrame.m b/Source/StackFrame.m index f5cd05a..140387c 100644 --- a/Source/StackFrame.m +++ b/Source/StackFrame.m @@ -52,4 +52,21 @@ return self; } +/** + * Determines whether or not the given frame was shifted, rather than jumped. Essentially, + * this checks if it's in the same file/function. + */ +- (BOOL)isShiftedFrame:(StackFrame *)frame +{ + return ([filename isEqualToString:frame.filename]); +} + +/** + * Returns a human-readable representation + */ +- (NSString *)description +{ + return [NSString stringWithFormat:@"#%d %@ [%@:%d]", index, function, filename, lineNumber]; +} + @end -- 2.22.5