-
Notifications
You must be signed in to change notification settings - Fork 7
/
coda_interactive.cpp
438 lines (391 loc) · 8.62 KB
/
coda_interactive.cpp
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//writer [email protected]
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <signal.h>
#include <setjmp.h>
#include <iostream>
#include <string>
#include "coda.h"
#include "coda_readline.h"
typedef int (*COMMAND)(CoreObject*,int,char**);
struct command_struct{
const char *command_name;
COMMAND command;
const char *help;
};
extern command_struct cmds[];
int command_invalid(CoreObject*, int , char**)
{
std::cout << "Invalid command " << std::endl;
std::cout << "type 'help'" << std::endl;
return 0;
}
int command_help(CoreObject*, int argc, char**)
{
if (argc > 0)
{
std::cout << "Invalid argument" << std::endl;
return 1;
}
int ndx = 0;
while(cmds[ndx].command != command_invalid)
{
std::cout << std::setw(10) << std::left
<< std::setfill(' ')
<< cmds[ndx].command_name << " - " << cmds[ndx].help
<< std::endl;
++ndx;
}
return 0;
}
int command_dis(CoreObject* co, int argc, char** argv)
{
if (argc > 1)
{
std::cout << "Invalid argument" << std::endl;
return 1;
}
if (argc > 0)
{
uint64_t addr;
char *saveptr = 0;
addr = strtoul(argv[0],&saveptr,0);
if (*saveptr != '\0')
{
std::cout << "Invalid argument" << std::endl;
return 1;
}
co->ShowDisassembly(addr);
return 0;
}
else
std::cout << "Argument missing" << std::endl;
return 1;
}
int command_quit(CoreObject*, int, char**)
{
exit(0);
}
int command_bt(CoreObject* co, int argc, char**)
{
if (argc > 0)
{
std::cout << "Invalid argument" << std::endl;
return 1;
}
co->ShowBT();
return 0;
}
int command_btregs(CoreObject* co, int argc, char**)
{
if (argc > 0)
{
std::cout << "Invalid argument" << std::endl;
return 1;
}
co->ShowBT(true);
return 0;
}
int command_mmap(CoreObject* co, int argc, char**)
{
if (argc > 0)
{
std::cout << "Invalid argument" << std::endl;
return 1;
}
co->ShowMemoryMap();
return 0;
}
int command_demangle(CoreObject* co, int argc, char** argv)
{
if (argc > 1)
{
std::cout << "Invalid argument" << std::endl;
return 1;
}
else if (argc < 1)
{
std::cout << "Argument missing" << std::endl;
return 1;
}
if (strcmp(argv[0],"1") == 0)
{
co->EnableDemangle();
std::cout << "Demangling enabled." << std::endl;
return 0;
}
else if (strcmp(argv[0],"0") == 0)
{
co->DisableDemangle();
std::cout << "Demangling disabled." << std::endl;
return 0;
}
std::cout << "Invalid argument." << std::endl;
return 1;
}
int command_thread(CoreObject* co, int argc, char** argv)
{
if (argc > 1)
{
std::cout << "Invalid argument" << std::endl;
return 1;
}
size_t threadno = 0;
if (argc > 0)
{
char *saveptr = 0;
threadno = strtoul(argv[0],&saveptr,0);
if (*saveptr != '\0')
{
std::cout << "Invalid argument" << std::endl;
return 1;
}
co->SwitchToThread(threadno);
return 0;
}
co->SwitchToThread();
return 0;
}
int command_threads(CoreObject* co, int argc, char**)
{
if (argc > 0)
{
std::cout << "Invalid argument" << std::endl;
return 1;
}
co->ShowThrList();
return 0;
}
int command_strings(CoreObject* co, int argc, char**argv)
{
if (argc > 1)
{
std::cout << "Invalid argument" << std::endl;
return 1;
}
char *regex = argv[0];
return co->ShowStrings(regex);
}
int handle_arguments(int argc, char**argv,uint64_t *paddr, size_t *punits)
{
if (argc > 2)
{
std::cout << "Invalid argument" << std::endl;
return 1;
}
char *saveptr = 0;
if ((0 < argc) && (argc <= 2))
{
*paddr = strtoul(argv[0],&saveptr,0);
if (*saveptr != '\0')
{
std::cout << "Invalid argument" << std::endl;
return 1;
}
}
else
{
std::cout << "Argument missing" << std::endl;
return 1;
}
if (argc == 2)
{
saveptr = 0;
*punits = strtoul(argv[1],&saveptr,0);
if (*saveptr != '\0')
{
std::cout << "Invalid argument" << std::endl;
return 1;
}
}
return 0;
}
int command_xuint(CoreObject* co, int argc, char**argv)
{
uint64_t addr;
size_t units = 1;
if(handle_arguments(argc,argv,&addr,&units))
return 1;
co->ShowValueAtVA<unsigned int>(addr,units);
return 0;
}
int command_xuchar(CoreObject* co, int argc, char**argv)
{
uint64_t addr;
size_t units = 1;
if(handle_arguments(argc,argv,&addr,&units))
return 1;
co->ShowValueAtVA<unsigned char>(addr,units);
return 0;
}
int command_xulong(CoreObject* co, int argc, char**argv)
{
uint64_t addr;
size_t units = 1;
if(handle_arguments(argc,argv,&addr,&units))
return 1;
co->ShowValueAtVA<unsigned long>(addr,units);
return 0;
}
int command_xint(CoreObject* co, int argc, char**argv)
{
uint64_t addr;
size_t units = 1;
if(handle_arguments(argc,argv,&addr,&units))
return 1;
co->ShowValueAtVA<int>(addr,units);
return 0;
}
int command_xlong(CoreObject* co, int argc, char**argv)
{
uint64_t addr;
size_t units = 1;
if(handle_arguments(argc,argv,&addr,&units))
return 1;
co->ShowValueAtVA<long>(addr,units);
return 0;
}
int command_xaddr(CoreObject* co, int argc, char**argv)
{
uint64_t addr;
size_t units = 1;
if(handle_arguments(argc,argv,&addr,&units))
return 1;
co->ShowValueAtVA<uint64_t>(addr,units);
return 0;
}
command_struct cmds[] = {
{ "help", command_help,
"Help for all interactive commands." },
{ "demangle",command_demangle,
"demangle 0|1" },
{ "dis", command_dis,
"Show disassembly of the function." },
{ "btregs", command_btregs,
"Show backtrace of the selected thread with registers." },
{ "bt", command_bt,
"Show backtrace of the selected thread." },
{ "mmap", command_mmap,
"Show memory map." },
{ "thread", command_thread,
"thread <number>." },
{ "threads", command_threads,
"Show all threads." },
{ "strings", command_strings,
"Extract strings from coredump under probe."
"\n\t\tUsage: strings <pattern>" },
{ "xuint" , command_xuint,
"Extract unsigned int."
"\n\t\tUsage: xuint addr <count>" },
{ "xchar", command_xuchar,
"Extract char."
"\n\t\tUsage: xchar addr <count>" },
{ "xulong", command_xulong,
"Extract unsigned long."
"\n\t\tUsage: xulong addr <count>" },
{ "xint" , command_xint,
"Extract int."
"\n\t\tUsage: xint addr <count>" },
{ "xlong", command_xlong,
"Extract long."
"\n\t\tUsage: xlong addr <count>" },
{ "xaddr", command_xaddr,
"Extract address."
"\n\t\tUsage: xaddr addr <count>" },
{ "q", command_quit,
"Exit program." },
{ "invalid", command_invalid,
"Invalid command."}
};
COMMAND command(char *cmd)
{
int ndx = 0;
while(cmds[ndx].command != command_invalid)
{
if (strcmp (cmd, cmds[ndx].command_name) == 0)
return cmds[ndx].command;
++ndx;
}
return cmds[ndx].command;
}
void handle_input(CoreObject *co, char *str)
{
const int MAX_ARGS = 10 + 1;
int argc = 0;
char *argv[MAX_ARGS] = {0};
char *saveptr;
for( ;argc < MAX_ARGS - 1; ++argc,str = NULL)
{
argv[argc] = coda_strtok(str,&saveptr);
if (argv[argc] == NULL)
break;
}
if (0 == argc)
return;
command(argv[0])(co,argc-1,&argv[1]);
}
char const* cmd_completer(char const *str_to_com, int str_len, int init)
{
static command_struct *traverse = &cmds[0];
if (NULL == str_to_com)
return NULL;
if (init)
traverse = &cmds[0];
while (traverse->command != command_invalid)
{
if (!strncmp(traverse->command_name, str_to_com, str_len))
{
return traverse++->command_name;
}
++traverse;
}
return NULL;
}
void
CoreObject::Switch2InteractiveMode()
{
std::string line;
bool notty = false;
m_interactive_mode = true;
signal(SIGINT, SIG_IGN);
if (!isatty(fileno(stdin)) || !isatty(fileno(stdout)))
{
notty = true;
m_interactive_mode = false;
}
if (m_interactive_mode)
{
set_completer(cmd_completer);
WelcomeMessage();
}
while (1)
{
char *str;
if (notty)
{
std::getline(std::cin,line);
if (std::cin.eof())
break;
str = (char*)line.c_str();
}
else
{
str = coda_readline("coda > ");
if (str == NULL)
break;
coda_add_history(str);
}
try
{
handle_input(this, str);
}
catch (Done &done)
{
}
if (!notty)
free(str);
}
std::cout << std::endl;
exit(0);
}