Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 273 lines (236 sloc) 6.114 kb
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
/*
* MacRuby Debugger Connector.
*
* This file is covered by the Ruby license. See COPYING for more details.
*
* Copyright (C) 2012, The MacRuby Team. All rights reserved.
* Copyright (C) 2010-2011, Apple Inc. All rights reserved.
*/

#import "MacRubyDebuggerConnector.h"

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

@implementation MacRubyDebuggerConnector

- (NSData *)_recv
{
    assert(_socket != nil);
    return [_socket availableData];
}

- (const char *)_recvCString
{
    return (const char *)[[self _recv] bytes];
}

- (NSString *)_recvString
{
    return [[NSString alloc] initWithData:[self _recv]
encoding:NSASCIIStringEncoding];
}

- (void)_send:(NSData *)data
{
    assert(_socket != nil);
    [_socket writeData:data];
}

- (void)_sendCString:(const char *)str
{
    [self _send:[NSData dataWithBytes:str length:strlen(str)]];
}

-(void)_readLocation
{
    _location = [self _recvString];
    if ([_location length] == 0) {
_location = nil;
    }
}

- (id)initWithInterpreterPath:(NSString *)ipath arguments:(NSArray *)arguments
{
    self = [super init];
    if (self != nil) {
// Generate the socket path.
NSString *tmpdir = NSTemporaryDirectory();
assert(tmpdir != nil);
char path[PATH_MAX];
snprintf(path, sizeof path, "%s/macrubyd-XXXXXX",
[tmpdir fileSystemRepresentation]);
assert(mktemp(path) != NULL);
_socketPath = [[NSFileManager defaultManager]
stringWithFileSystemRepresentation:path length:strlen(path)];

// Setup arguments.
_arguments = [NSMutableArray new];
[_arguments addObject:@"--debug-mode"];
[_arguments addObject:_socketPath];
[_arguments addObjectsFromArray:arguments];

_interpreterPath = ipath;
_task = nil;
_socket = nil;
_location = nil;
    }
    return self;
}

- (void)finalize
{
    [super finalize];
    [self stopExecution];
}

- (void)startExecution
{
    if (_task == nil || ![_task isRunning]) {
// Create task.
_task = [NSTask new];
[_task setLaunchPath:_interpreterPath];
[_task setArguments:_arguments];

// Launch the remote process.
[[NSFileManager defaultManager] removeItemAtPath:_socketPath error:nil];
[_task launch];

// Wait until the socket file is available.
while (true) {
if ([[NSFileManager defaultManager] fileExistsAtPath:_socketPath]) {
break;
}
if (![_task isRunning]) {
// Something wrong happened at the very beginning, most likely
// a command-line argument error.
_task = nil;
return;
}
assert([_task isRunning]); // XXX raise exception
usleep(500000);
}

// Create the socket.
const int fd = socket(PF_LOCAL, SOCK_STREAM, 0);
if (fd == -1) {
// XXX should raise exception.
perror("socket()");
exit(1);
}

// Prepare the name.
struct sockaddr_un name;
name.sun_family = PF_LOCAL;
strncpy(name.sun_path, [_socketPath fileSystemRepresentation],
sizeof(name.sun_path));

// Connect.
if (connect(fd, (struct sockaddr *)&name, SUN_LEN(&name)) == -1) {
// XXX should raise exception.
perror("connect()");
exit(1);
}

// Good!
_socket = [[NSFileHandle alloc] initWithFileDescriptor:fd];
[self _readLocation];
    }
}

- (void)continueExecution
{
    [self _sendCString:"continue"];
    [self _readLocation];
}

- (void)stepExecution
{
    [self _sendCString:"next"];
    [self _readLocation];
}

- (void)stopExecution
{
    if (_task != nil) {
[_task terminate];
_task = nil;
    }
    _location = nil;
    [[NSFileManager defaultManager] removeItemAtPath:_socketPath error:nil];
}

- (NSString *)location
{
    return _location;
}

- (NSArray *)localVariables
{
    return nil;
}

- (NSArray *)backtrace
{
    [self _sendCString:"backtrace"];
    return [[self _recvString] componentsSeparatedByString:@"\n"];
}

- (void)setFrame:(unsigned int)frame
{
    char buf[512];
    snprintf(buf, sizeof buf, "frame %d", frame);
    [self _sendCString:buf];
}

- (breakpoint_t)addBreakPointAtPath:(NSString *)path line:(unsigned int)line
condition:(NSString *)condition
{
    char buf[512];
    snprintf(buf, sizeof buf, "break %s:%d", [path fileSystemRepresentation],
line);
    [self _sendCString:buf];
    return [[self _recvString] integerValue];
}

- (void)enableBreakPoint:(breakpoint_t)bp
{
    char buf[512];
    snprintf(buf, sizeof buf, "enable %d", bp);
    [self _sendCString:buf];
}

- (void)disableBreakPoint:(breakpoint_t)bp
{
    char buf[512];
    snprintf(buf, sizeof buf, "disable %d", bp);
    [self _sendCString:buf];
}

- (void)deleteBreakPoint:(breakpoint_t)bp
{
    char buf[512];
    snprintf(buf, sizeof buf, "delete %d", bp);
    [self _sendCString:buf];
}

- (void)setCondition:(NSString *)condition forBreakPoint:(breakpoint_t)bp
{
    char buf[512];
    snprintf(buf, sizeof buf, "condition %d %s", bp, [condition UTF8String]);
    [self _sendCString:buf];
}

- (NSDictionary *)_parseBreakPointDescription:(NSString *)desc
{
    NSArray *ary = [desc componentsSeparatedByString:@","];
    if ([ary count] == 0) {
return nil;
    }
    NSMutableDictionary *dict = [NSMutableDictionary new];
    unsigned i, count;
    for (i = 0, count = [ary count]; i < count; i++) {
NSArray *fields = [[ary objectAtIndex:i]
componentsSeparatedByString:@"="];
if ([fields count] == 2) {
NSString *key = [fields objectAtIndex:0];
NSString *val = [fields objectAtIndex:1];
[dict setObject:val forKey:key];
}
    }
    if ([dict count] == 0) {
return nil;
    }
    return dict;
}


- (NSArray *)allBreakPoints
{
    [self _sendCString:"info breakpoints"];
    NSArray *ary = [[self _recvString] componentsSeparatedByString:@"\n"];
    NSMutableArray *ary2 = [NSMutableArray new];
    unsigned i, count;
    for (i = 0, count = [ary count]; i < count; i++) {
NSString *desc = [ary objectAtIndex:i];
NSDictionary *dict = [self _parseBreakPointDescription:desc];
if (dict != nil) {
[ary2 addObject:dict];
}
    }
    return ary2;
}

- (NSString *)evaluateExpression:(NSString *)expression
{
    char buf[512];
    snprintf(buf, sizeof buf, "eval %s", [expression UTF8String]);
    [self _sendCString:buf];
    return [self _recvString];
}

@end
Something went wrong with that request. Please try again.