Force a re-serialization of Breakpoints on decode.
[macgdbp.git] / Source / ProtocolClient.m
1 /*
2 * MacGDBp
3 * Copyright (c) 2013, Blue Static <http://www.bluestatic.org>
4 *
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.
8 *
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.
12 *
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
15 */
16
17 #import "ProtocolClient.h"
18
19 #import "AppDelegate.h"
20 #import "LoggingController.h"
21
22 @implementation ProtocolClient {
23 // The object responsible for the actual communication with the debug server.
24 MessageQueue* _messageQueue;
25
26 // The delegate of this class, which receives high-level messages about the
27 // state of the debugger.
28 id<ProtocolClientDelegate> _delegate; // weak
29
30 // A map between transaction ID and handler block for that message.
31 NSMutableDictionary<NSNumber*, ProtocolClientMessageHandler>* _dispatchTable;
32
33 // The next transaction ID to assign.
34 int _nextID;
35
36 // Records the last read and written transaction IDs. These are only used in
37 // creating LogEntry objects.
38 NSInteger _lastReadID;
39 NSInteger _lastWrittenID;
40 }
41
42 - (id)initWithDelegate:(id<ProtocolClientDelegate>)delegate {
43 if ((self = [super init])) {
44 _delegate = delegate;
45 _dispatchTable = [[NSMutableDictionary alloc] init];
46 }
47 return self;
48 }
49
50 - (BOOL)isConnected {
51 return [_messageQueue isConnected];
52 }
53
54 - (void)connectOnPort:(NSUInteger)port {
55 assert(!_messageQueue);
56 _messageQueue = [[MessageQueue alloc] initWithPort:port delegate:self];
57 [_messageQueue connect];
58 }
59
60 - (void)disconnect {
61 [_messageQueue disconnect];
62 }
63
64 - (void)sendCommandWithFormat:(NSString*)format, ... {
65 // Collect varargs and format command.
66 va_list args;
67 va_start(args, format);
68 NSString* command = [[NSString alloc] initWithFormat:format arguments:args];
69 va_end(args);
70
71 [self sendCommandWithFormat:command handler:^(NSXMLDocument* message){}];
72 }
73
74 - (void)sendCommandWithFormat:(NSString*)format
75 handler:(ProtocolClientMessageHandler)handler, ... {
76 // Collect varargs and format command.
77 va_list args;
78 va_start(args, handler);
79 NSString* command = [[NSString alloc] initWithFormat:format arguments:args];
80 va_end(args);
81
82 int transaction = _nextID++;
83 NSString* taggedCommand = [NSString stringWithFormat:@"%@ -i %d", command, transaction];
84
85 assert(_messageQueue);
86 [_dispatchTable setObject:[handler copy] forKey:@(transaction)];
87 [_messageQueue sendMessage:taggedCommand];
88 }
89
90 - (void)sendCustomCommandWithFormat:(NSString*)format
91 handler:(ProtocolClientMessageHandler)handler, ... {
92 // Collect varargs and format command.
93 va_list args;
94 va_start(args, handler);
95 NSString* command = [[NSString alloc] initWithFormat:format arguments:args];
96 va_end(args);
97
98 int transaction = _nextID++;
99 NSString* taggedCommand =
100 [command stringByReplacingOccurrencesOfString:@"{txn}"
101 withString:[NSString stringWithFormat:@"%d", transaction]];
102
103 assert(_messageQueue);
104 [_dispatchTable setObject:[handler copy] forKey:@(transaction)];
105 [_messageQueue sendMessage:taggedCommand];
106 }
107
108
109 - (NSInteger)transactionIDFromResponse:(NSXMLDocument*)response {
110 return [[[[response rootElement] attributeForName:@"transaction_id"] stringValue] intValue];
111 }
112
113 - (NSInteger)transactionIDFromCommand:(NSString*)command {
114 NSRange occurrence = [command rangeOfString:@"-i "];
115 if (occurrence.location == NSNotFound)
116 return NSNotFound;
117 NSString* transaction = [command substringFromIndex:occurrence.location + occurrence.length];
118 return [transaction intValue];
119 }
120
121 + (NSString*)escapedFilePathURI:(NSString*)path {
122 // Custon GDBp paths are fine.
123 if ([[path substringToIndex:4] isEqualToString:@"gdbp"])
124 return path;
125
126 // Create a temporary URL that will escape all the nasty characters.
127 NSURL* url = [NSURL fileURLWithPath:path];
128 NSString* urlString = [url absoluteString];
129
130 // Remove the host because this is a file:// URL;
131 NSString* host = [url host];
132 if (host)
133 urlString = [urlString stringByReplacingOccurrencesOfString:[url host] withString:@""];
134
135 // Escape % for use in printf-style NSString formatters.
136 urlString = [urlString stringByReplacingOccurrencesOfString:@"%" withString:@"%%"];
137 return urlString;
138 }
139
140 // MessageQueueDelegate ////////////////////////////////////////////////////////
141
142 - (void)messageQueue:(MessageQueue*)queue error:(NSError*)error {
143 NSLog(@"error = %@", error);
144 }
145
146 - (void)messageQueueDidConnect:(MessageQueue*)queue {
147 _nextID = 0;
148 _lastReadID = 0;
149 _lastWrittenID = 0;
150
151 [_delegate debuggerEngineConnected:self];
152 }
153
154 - (void)messageQueueDidDisconnect:(MessageQueue*)queue {
155 _messageQueue = nil;
156 [_dispatchTable removeAllObjects];
157 [_delegate debuggerEngineDisconnected:self];
158 }
159
160 // Callback for when a message has been sent.
161 - (void)messageQueue:(MessageQueue*)queue didSendMessage:(NSString*)message {
162 NSInteger tag = [self transactionIDFromCommand:message];
163 _lastWrittenID = tag;
164
165 LoggingController* logger = [[AppDelegate instance] loggingController];
166 LogEntry* entry = [LogEntry newSendEntry:message];
167 entry.lastReadTransactionID = _lastReadID;
168 entry.lastWrittenTransactionID = _lastWrittenID;
169 [logger recordEntry:entry];
170 }
171
172 // Callback with the message content when one has been receieved.
173 - (void)messageQueue:(MessageQueue*)queue didReceiveMessage:(NSString*)message {
174 // Record this message in the transaction log.
175 LoggingController* logger = [[AppDelegate instance] loggingController];
176 LogEntry* entry = [LogEntry newReceiveEntry:message];
177 entry.lastReadTransactionID = _lastReadID;
178 entry.lastWrittenTransactionID = _lastWrittenID;
179 [logger recordEntry:entry];
180
181 // Parse the XML and test for errors.
182 NSError* error = nil;
183 NSXMLDocument* xml = [[NSXMLDocument alloc] initWithXMLString:message
184 options:NSXMLDocumentTidyXML
185 error:&error];
186 if (error) {
187 [self messageQueue:queue error:error];
188 return;
189 }
190 NSInteger transactionID = [self transactionIDFromResponse:xml];
191
192 _lastReadID = transactionID;
193 entry.lastReadTransactionID = _lastReadID;
194
195 if ([[[xml rootElement] elementsForName:@"error"] count] > 0) {
196 // Handle back-end errors.
197 [_delegate protocolClient:self receivedErrorMessage:xml];
198 } else if ([[[xml rootElement] name] isEqualToString:@"init"]) {
199 // Handle the initial connection message.
200 [_delegate protocolClient:self receivedInitialMessage:xml];
201 } else {
202 // Dispatch the handler for the message.
203 ProtocolClientMessageHandler handler = [_dispatchTable objectForKey:@(transactionID)];
204 handler(xml);
205 [_dispatchTable removeObjectForKey:@(transactionID)];
206 }
207 }
208
209 @end