/
GdbConnection.m
365 lines (278 loc) · 10.7 KB
/
GdbConnection.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// GdbConnection.m
// Rhodes Debugger
//
// Created by rhomobile on 12/14/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "GdbConnection.h"
@implementation GdbConnection
@synthesize pid;
@synthesize gdbPid;
@synthesize timer;
- (id) init
{
self = [super init];
if (self != nil) {
[self setPid:@""];
[self setGdbPid:@""];
[self setTimer:nil];
gdbAttached = FALSE;
gdbScriptLoaded = FALSE;
tailController = [[LogController alloc] init];
[tailController setDelegate:self];
}
return self;
}
- (void)statusScanner:(id)userData {
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/sh"];
NSArray *args = [NSArray arrayWithObjects:@"-c",@"ps xo pid,command | grep rhorunner.app | grep iPhone | grep -v grep | grep -v gdb",nil];
[task setArguments: args];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
NSFileHandle *file = [pipe fileHandleForReading];
[task launch];
NSString *output = [[NSString alloc] initWithData:[file readDataToEndOfFile] encoding:NSUTF8StringEncoding];
output = [output stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ( [output length] < 1 && ![gdbController finished]) {
[[gdbController task] terminate];
[[tailController task] terminate];
return;
}
if ( [output length] > 0 && !gdbAttached) {
NSArray *components = [output componentsSeparatedByString:@" "];
pid = [components objectAtIndex:0];
NSString *command = [output stringByReplacingOccurrencesOfString:pid withString:@""];
[self attachGdbTo:[command stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] withPid:pid];
if ( delegate && [delegate respondsToSelector:@selector(connecting:)] ) {
[delegate connecting:self];
}
gdbAttached = TRUE;
return;
}
if( gdbAttached && !gdbScriptLoaded && [gdbController isWaiting] ) {
NSString * cmd = @"source \"";
cmd = [cmd stringByAppendingString:[[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Contents/Resources/ruby\""]];
[gdbController sendCmd:cmd async:false];
system("rm /tmp/ruby-debug*");
[gdbController sendCmd:@"set objc-non-blocking-mode off" async:false];
[gdbController sendCmd:@"redirect_stdout" async:false];
[gdbController sendCmd:@"rb_trace" async:false];
gdbScriptLoaded = true;
[self attachTail];
[self setIsConnected:true];
if ( delegate && [delegate respondsToSelector:@selector(resumed:)] ) {
[delegate resumed:self];
}
if ( delegate && [delegate respondsToSelector:@selector(connected:)] ) {
[delegate connected:self];
}
}
}
- (void)attachGdbTo:(NSString*)file withPid:(NSString*)inPid {
NSTask *ps = [[NSTask alloc] init];
[ps setLaunchPath:@"/usr/bin/gdb"];
NSMutableArray *args = [[NSMutableArray alloc] init];
[args addObject:file];
[args addObject:inPid];
[ps setArguments:args];
[gdbController setTask:ps];
[gdbController setWaitString:@"(gdb)"];
[gdbController launchTask];
}
- (NSString *) getGdbPid {
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/sh"];
NSArray *args = [NSArray arrayWithObjects:@"-c",@"ps xo pid,command | grep rhorunner.app | grep gdb | grep -v grep ",nil];
[task setArguments: args];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
NSFileHandle *file = [pipe fileHandleForReading];
[task launch];
NSString *output = [[NSString alloc] initWithData:[file readDataToEndOfFile] encoding:NSUTF8StringEncoding];
output = [output stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSArray *components = [output componentsSeparatedByString:@" "];
gdbPid = [components objectAtIndex:0];
return [components objectAtIndex:0];
}
- (void)attachTail {
NSTask *ps = [[NSTask alloc] init];
[ps setLaunchPath:@"/usr/bin/tail"];
NSMutableArray *args = [[NSMutableArray alloc] init];
[args addObject:@"-f"];
[args addObject:[NSString stringWithFormat:@"/tmp/ruby-debug.%@",pid]];
[ps setArguments:args];
[tailController setTask:ps];
[tailController launchTask];
}
#pragma mark -- Debug Connection Implementation --
- (void) terminate {
[[gdbController task] terminate];
}
-(void) setBreakPointInFile:(NSString*)file atLine:(int)line {
NSString *lineString = [NSString stringWithFormat:@"%d",line];
NSString *cmd = [NSString stringWithFormat:@"call (unsigned long)rb_eval_string(\"$_file = '%@'; $_line = %@;\")",file,lineString];
if(![gdbController finished] && gdbScriptLoaded) {
[self pause];
[gdbController sendCmd:cmd async:true];
[gdbController sendCmd:@"call (unsigned long)rb_eval_string(\"puts '-- Breakpoint set'\")" async:true];
[gdbController sendCmd:@"rb_break" async:true];
if ( delegate && [delegate respondsToSelector:@selector(resumed:)] ) {
[delegate resumed:self];
}
}
}
-(void) startWaiting {
timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(statusScanner:) userInfo: nil repeats:true];
[self setWaitForConnection:TRUE];
}
-(void) stopWaiting {
if([self timer]){
[[self timer] invalidate];
}
[self setWaitForConnection:FALSE];
}
- (void) pause {
if(![gdbController isWaiting] && ! [gdbController finished] ) {
NSString * myGdbPid = [self getGdbPid];
NSString *cmd = @"/bin/kill -INT ";
system([[cmd stringByAppendingString:myGdbPid] cStringUsingEncoding:NSASCIIStringEncoding]);
}
}
- (void)step {
if(![gdbController finished]) {
[self pause];
[gdbController sendCmd:@"rb_step" async:true];
if ( delegate && [delegate respondsToSelector:@selector(resumed:)] ) {
[delegate resumed:self];
}
}
}
- (void)stepOut {
if(![gdbController finished]) {
[self pause];
[gdbController sendCmd:@"rb_finish" async:true];
if ( delegate && [delegate respondsToSelector:@selector(resumed:)] ) {
[delegate resumed:self];
}
}
}
-(void) resume {
if(![gdbController finished]) {
[self pause];
[gdbController sendCmd:@"rb_break" async:true];
if ( delegate && [delegate respondsToSelector:@selector(resumed:)] ) {
[delegate resumed:self];
}
}
}
- (void) sendRubyCmd:(NSString *)rubyCmd {
NSString *sanitized = [rubyCmd stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
NSString *cmd = [NSString stringWithFormat:@"eval \"%@\"",sanitized];
[self pause];
[gdbController sendCmd:cmd async:true];
}
- (void) clearBreakPoints {
if(![gdbController finished]) {
[self pause];
[gdbController sendCmd:@"rb_cont" async:true];
}
}
#pragma mark -- Delegate Methods --
- (NSAttributedString *)logController:(id)sender willAppendNewString:(NSString *)plainString
{
// NSLog(@"Colorizing string: %@", plainString);
if( !plainString ) {
return [[NSAttributedString alloc] initWithString:@""];
}
// First, we need a mutable attributed copy of it.
// It'd be nice to use something like [NSMutableAttributedString mutableAttributedStringWithString:]
// but that method doesn't exist. Make due with what I can.
NSMutableAttributedString *colorizedString = [[NSMutableAttributedString alloc] initWithString:plainString];
// We'll do all the math on plainString, do all the modifications on colorizedString.
NSUInteger startOfLine=0, endOfLine=0;
NSUInteger endOfString = [plainString length];
NSRange restOfString=NSMakeRange(0, endOfString);
// Go through plainString one line at a time
for( startOfLine=0; endOfLine < endOfString; startOfLine=endOfLine)
{
// Find the next \n
NSRange nextNewLineRange = [plainString
rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]
options:NSLiteralSearch range:restOfString];
// If we can't find one, just pretend it's at the end
if( nextNewLineRange.location == NSNotFound )
nextNewLineRange.location = restOfString.location+restOfString.length;
// The end of the line is right after the \n
endOfLine = nextNewLineRange.location + nextNewLineRange.length;
NSRange thisLineRange = NSMakeRange(startOfLine, endOfLine-startOfLine);
NSString *thisLine = [plainString substringWithRange:thisLineRange];
restOfString = NSMakeRange(endOfLine,endOfString-endOfLine);
// The entire line will be black Monaco 10pt font
NSMutableDictionary *attribDict = [NSMutableDictionary dictionary];
[attribDict setObject:[NSFont fontWithName:@"Monaco" size:10.0f]
forKey:NSFontAttributeName];
[attribDict setObject:[NSColor blackColor]
forKey:NSForegroundColorAttributeName];
[colorizedString setAttributes:attribDict range:thisLineRange];
if([sender isEqual:gdbController]) {
NSRange gdbRange = [thisLine rangeOfString:@"(gdb)"];
if ( gdbRange.location != NSNotFound)
{
gdbRange.location += startOfLine;
[attribDict setObject:[NSColor blueColor] forKey:NSForegroundColorAttributeName];
[colorizedString addAttributes:attribDict range:gdbRange];
}
} else if ([sender isEqual:tailController]) {
NSRange gdbRange = [thisLine rangeOfString:@"--"];
if ( gdbRange.location == 0) {
[attribDict setObject:[NSColor purpleColor] forKey:NSForegroundColorAttributeName];
[colorizedString setAttributes:attribDict range:thisLineRange];
}
NSRange breakRange = [thisLine rangeOfString:@"--Break"];
NSRange stepRange = [thisLine rangeOfString:@"--Step"];
if ( breakRange.location != NSNotFound || stepRange.location != NSNotFound) {
NSString *stopLine = [[thisLine componentsSeparatedByString:@":"] lastObject];
NSRange evalRange = [thisLine rangeOfString:@"(eval)"];
if(evalRange.location == NSNotFound) {
NSString *file = [[[[thisLine componentsSeparatedByString:@" at "] lastObject] componentsSeparatedByString:@":"] objectAtIndex:0];
if ( delegate && [delegate respondsToSelector:@selector(stopInFile:atLine:sender:)] ) {
[delegate stopInFile:file atLine:[stopLine intValue] sender:self];
}
}
}
}
}
if([sender isEqual: tailController]) {
if ( delegate && [delegate respondsToSelector:@selector(rubyStdout:sender:)] ) {
[delegate rubyStdout:[colorizedString mutableString] sender:self];
}
}
// Finally, return our colorized version.
return colorizedString;
}
- (void) logControllerTaskDidFinish:(id)sender {
if([sender isEqual:gdbController]) {
gdbAttached = FALSE;
gdbScriptLoaded = FALSE;
if ( delegate && [delegate respondsToSelector:@selector(disconnected:)] ) {
[delegate disconnected:self];
}
}
}
- (void) logControllerTaskIsWaiting:(id)sender {
if ( delegate && [delegate respondsToSelector:@selector(paused:)] ) {
[delegate paused:self];
}
}
#pragma mark -- Actions --
- (IBAction)gdbInput:(id)sender {
if ([[sender stringValue] length] < 1) {return;}
[self pause];
if(![gdbController finished]) {
[gdbController sendCmd:[sender stringValue] async:true];
}
[sender setStringValue:@""];
}
@end