-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsimple_debug.rl
More file actions
148 lines (128 loc) · 3.17 KB
/
simple_debug.rl
File metadata and controls
148 lines (128 loc) · 3.17 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
/**
* debugger state machine (for JSCDebugger)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef DEBUG_PARSER
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
namespace JSCDebug
{
class JSCDebugger {
// control execution
public:
const unsigned char* evaluateInCurrentFrame(const unsigned char *scriptSource, unsigned *outLen) { *outLen = strlen((char *)scriptSource); return scriptSource; };
void continueProgram() {};
void stepIntoStatement() {};
void stepOverStatement() {};
void stepOutOfFunction() {};
};
}
#endif
extern "C" {
enum {
kInvalidCommand = 0,
kNextCommand,
kStepIntoCommand,
kBacktraceCommand,
kBreakpointCommand,
kEvalCommand,
kPrintCommand,
kContinueCommand,
kRepeatLastCommand
};
void performAction(int command, char **args, JSCDebug::JSCDebugger *debugger, int socket);
int parseInput(int file, JSCDebug::JSCDebugger *debugger);
static bool shouldExit = false;
%%{
machine simple_debug;
# commands
step = ('s' | 'step' | 'n');
cont = ('c' | 'continue' | 'cont');
breakpoint = ('b' | 'breakpoint');
step_into = ('si');
backtrace = ('bt' | 'backtrace');
eval = ('e' | 'eval');
print_cmd = ('p' | 'print');
newline = ('\n');
action markArgsBegin {
argsBegin = p;
}
action markArgsEnd {
if (args) {
free(args);
}
ssize_t len = p - argsBegin;
args = (char *)calloc(len + 1, 1);
memcpy(args, argsBegin, len);
}
main := |*
step newline => { performAction(kNextCommand, NULL, debugger, file); };
cont newline => { performAction(kContinueCommand, NULL, debugger, file); fbreak; };
eval ' ' (any - newline)+ > markArgsBegin % markArgsEnd newline => { performAction(kEvalCommand, &args, debugger, file); };
[ ]* newline;
*|;
}%%
%% write data;
void performAction(int command, char **args, JSCDebug::JSCDebugger *debugger, int socket)
{
switch (command) {
case kNextCommand:
printf("step over\n");
debugger->stepOverStatement();
break;
case kStepIntoCommand:
printf("step into\n");
debugger->stepIntoStatement();
break;
case kContinueCommand:
debugger->continueProgram();
shouldExit = true;
break;
case kEvalCommand:
printf("eval: '%s'\n", *args);
unsigned outLen;
const unsigned char *result = debugger->evaluateInCurrentFrame((unsigned char *)*args, &outLen);
write(socket, result, outLen);
write(socket, "\n>> ", 4);
free(*args); *args = NULL;
break;
}
}
int parseInput(int file, JSCDebug::JSCDebugger *debugger)
{
#define BUF_SIZE 128
char buff[BUF_SIZE];
memset(buff, '\0', BUF_SIZE);
// machine state
int cs, act;
char *ts, *te = 0;
// arguments storage
char *args = NULL, *argsBegin = NULL;
%% write init;
ssize_t readBytes;
while (!shouldExit && (readBytes = read(file, buff, BUF_SIZE))) {
char *p = buff, *eof = buff + readBytes;
char *pe = eof;
%% write exec;
if (cs == simple_debug_error) {
printf("parser error @ '%s'\n", p);
return -1;
}
}
return 0;
}
} // extern "C"
#ifdef DEBUG_PARSER
int main(int argc, char **argv) {
if (argc > 0) {
int file = open(argv[1], O_RDONLY);
JSCDebug::JSCDebugger *debugger = new JSCDebug::JSCDebugger();
parseInput(file, debugger);
close(file);
}
}
#endif