|
@@ -80,7 +80,6 @@ void asmsh_cmd_args_free(asmsh_cmd_args_t *args);
|
80
|
80
|
const char *asmsh_cmd_help(asmsh_t *sh);
|
81
|
81
|
|
82
|
82
|
|
83
|
|
-
|
84
|
83
|
/*
|
85
|
84
|
* Commands declaration
|
86
|
85
|
*
|
|
@@ -89,196 +88,58 @@ const char *asmsh_cmd_help(asmsh_t *sh);
|
89
|
88
|
*/
|
90
|
89
|
|
91
|
90
|
// Quit the shell
|
92
|
|
-static int _quit(asmsh_t *sh, asmsh_cmd_args_t *args)
|
93
|
|
-{
|
94
|
|
- asmsh_cleanup(sh);
|
95
|
|
- return 1;
|
96
|
|
-}
|
|
91
|
+int asmsh_cmd_quit(asmsh_t *sh, asmsh_cmd_args_t *args);
|
97
|
92
|
|
98
|
93
|
// Print an instruction bytecode
|
99
|
|
-int asmsh_cmd_bcode(asmsh_t *sh, char *buf, int bufsz, int argc, char **args);
|
100
|
|
-
|
101
|
|
-
|
102
|
|
-static int _bcode(asmsh_t *sh, asmsh_cmd_args_t *args)
|
103
|
|
-{
|
104
|
|
- char str[256];
|
105
|
|
- int ret;
|
106
|
|
-
|
107
|
|
- ret = asmsh_cmd_bcode(sh, str, 256, args->argc, args->args);
|
108
|
|
- if(ret)
|
109
|
|
- {
|
110
|
|
- return ret;
|
111
|
|
- }
|
112
|
|
- printf("%s\n", str);
|
113
|
|
- return 0;
|
114
|
|
-}
|
115
|
|
-
|
116
|
|
-static int _maps(asmsh_t *sh, asmsh_cmd_args_t *args)
|
117
|
|
-{
|
118
|
|
- if(asmsh_env_update_maps(sh->env) < 0)
|
119
|
|
- {
|
120
|
|
- perror("Unable to update maps info");
|
121
|
|
- return -1;
|
122
|
|
- }
|
123
|
|
- printf("%12s-%-12s perm %-8s %-6s %-10s %s\n", "start", "stop", "offset", "device",
|
124
|
|
- "inode", "path");
|
125
|
|
- for(int i=0; i<sh->env->mmap.size; i++)
|
126
|
|
- {
|
127
|
|
- const child_mmap_t *m = &sh->env->mmap.maps[i];
|
128
|
|
- printf("%012llx-%012llx %c%c%c %08lx %02lx:%02lx %10lu %s\n",
|
129
|
|
- (unsigned long long int)m->start,
|
130
|
|
- (unsigned long long int)m->stop,
|
131
|
|
- m->perm & PROT_READ ?'r':'-',
|
132
|
|
- m->perm & PROT_WRITE?'w':'-',
|
133
|
|
- m->perm & PROT_EXEC ?'x':'-',
|
134
|
|
- m->offset,
|
135
|
|
- (m->device & 0xFF00)>>8,
|
136
|
|
- m->device & 0xFF,
|
137
|
|
- m->inode,
|
138
|
|
- m->pathname);
|
139
|
|
- }
|
140
|
|
- return 0;
|
141
|
|
-}
|
|
94
|
+int asmsh_cmd_bcode_(asmsh_t *sh, char *buf, int bufsz, int argc, char **args);
|
|
95
|
+int asmsh_cmd_bcode(asmsh_t *sh, asmsh_cmd_args_t *args);
|
142
|
96
|
|
143
|
|
-// Print the registers
|
144
|
|
-static int _print_regs(asmsh_t *sh, asmsh_cmd_args_t *args)
|
145
|
|
-{
|
146
|
|
- asmsh_env_t *env = sh->env;
|
147
|
|
- asmsh_env_update_regs(env);
|
148
|
|
- struct user_regs_struct *r = &env->regs;
|
149
|
|
-
|
150
|
|
-#define FLG(b, l) ( (r->eflags & (1<<b))?l:'-' )
|
151
|
|
-
|
152
|
|
- printf("rax: %016llx rbx: %016llx rcx: %016llx rdx: %016llx\n\
|
153
|
|
-rbp: %016llx rsi: %016llx rdi: %016llx rsp: %016llx\n\
|
154
|
|
- r8: %016llx r9: %016llx r10: %016llx r11: %016llx\n\
|
155
|
|
-r12: %016llx r13: %016llx r14: %016llx r15: %016llx\n\
|
156
|
|
-rip: %016llx flg: %016llx\n\
|
157
|
|
-cs: %04llx ds: %04llx es: %04llx fs:%04llx gs: %04llx ss:%04llx\n\
|
158
|
|
-flags: %c%c%c%c|%c%c%c\n\
|
159
|
|
- ODSZ|APC\n\
|
160
|
|
-", r->rax, r->rbx, r->rcx, r->rdx,\
|
161
|
|
- r->rbp, r->rsi, r->rdi, r->rsp,\
|
162
|
|
- r->r8, r->r9, r->r10, r->r11,\
|
163
|
|
- r->r12, r->r13, r->r14, r->r15,\
|
164
|
|
- r->rip, r->eflags,\
|
165
|
|
- r->cs, r->ds, r->es, r->fs, r->gs, r->ss,
|
166
|
|
- FLG(11,'O'), FLG(10, 'D'), FLG(7, 'S'), FLG(6, 'Z'),
|
167
|
|
- FLG(4, 'A'), FLG(2, 'P'), FLG(0, 'C'));
|
|
97
|
+int asmsh_cmd_maps(asmsh_t *sh, asmsh_cmd_args_t *args);
|
168
|
98
|
|
169
|
|
-#undef FLG
|
170
|
|
- return 0;
|
171
|
|
-}
|
172
|
|
-
|
173
|
|
-static int _syscalls(asmsh_t *sh, asmsh_cmd_args_t *args)
|
174
|
|
-{
|
175
|
|
- const int sz = sizeof(syscall_infos)/sizeof(*syscall_infos);
|
176
|
|
- int printed;
|
177
|
|
- char buf[512];
|
|
99
|
+int asmsh_cmd_print_regs(asmsh_t *sh, asmsh_cmd_args_t *args);
|
178
|
100
|
|
179
|
|
- printed = 0;
|
180
|
|
- for(int i=0; i<sz; i++)
|
181
|
|
- {
|
182
|
|
- int ret = snprintf(buf, 512, "%3d 0x%03x %20s ",
|
183
|
|
- syscall_infos[i].nr,
|
184
|
|
- syscall_infos[i].nr,
|
185
|
|
- syscall_infos[i].name);
|
186
|
|
- buf[ret] = '\0';
|
187
|
|
- printed += ret;
|
188
|
|
- if(printed > 80)
|
189
|
|
- {
|
190
|
|
- printf("\n");
|
191
|
|
- printed = ret;
|
192
|
|
- }
|
193
|
|
- printf(buf);
|
194
|
|
- }
|
195
|
|
- printf("\n");
|
196
|
|
- return 0;
|
197
|
|
-}
|
|
101
|
+int asmsh_cmd_syscalls(asmsh_t *sh, asmsh_cmd_args_t *args);
|
198
|
102
|
|
199
|
|
-// Reset the shell (restart the child etc)
|
200
|
|
-static int _reset(asmsh_t *sh, asmsh_cmd_args_t *args)
|
201
|
|
-{
|
202
|
|
- char *childpath = strdup(sh->child_path);
|
203
|
|
- asmsh_cleanup(sh);
|
204
|
|
- asmsh_init(sh, childpath);
|
205
|
|
- free(childpath);
|
206
|
|
- return 0;
|
207
|
|
-}
|
|
103
|
+// Reset the shell (restart the child etc)a
|
|
104
|
+int asmsh_cmd_reset(asmsh_t *sh, asmsh_cmd_args_t *args);
|
208
|
105
|
|
209
|
106
|
// Display CPU flag resister values
|
210
|
|
-static int _flags(asmsh_t *sh, asmsh_cmd_args_t *args)
|
211
|
|
-{
|
212
|
|
- printf("Flags :\n");
|
213
|
|
-#define printFLG(name, b) printf(" (%2d)%16s : %d\n", b, name,\
|
214
|
|
- (sh->env->regs.eflags & (1<<b))?1:0);
|
215
|
|
- printFLG("Overflow", 11);
|
216
|
|
- printFLG("Direction", 10);
|
217
|
|
- printFLG("Sign", 7);
|
218
|
|
- printFLG("Zero", 6);
|
219
|
|
- printFLG("Auxiliary carry", 4);
|
220
|
|
- printFLG("Parity", 2);
|
221
|
|
- printFLG("Carry", 0);
|
222
|
|
-#undef printFLG
|
223
|
|
-#define EFLG(b,n) ((sh->env->regs.eflags & (1<<b))?n:'-')
|
224
|
|
- printf("%c%c%c%c %c%c%c\n",
|
225
|
|
- EFLG(11,'O'), EFLG(10, 'D'), EFLG(7,'S'), EFLG(6,'Z'),
|
226
|
|
- EFLG(4,'A'), EFLG(2, 'P'), EFLG(0, 'C'));
|
227
|
|
- return 0;
|
228
|
|
-}
|
|
107
|
+int asmsh_cmd_flags(asmsh_t *sh, asmsh_cmd_args_t *args);
|
229
|
108
|
|
230
|
|
-// Declared because referenced in command list but not implemented
|
231
|
|
-// because the _help function needs a reference to the command list
|
232
|
|
-static int _help(asmsh_t *sh, asmsh_cmd_args_t *args);
|
|
109
|
+int asmsh_cmd_help_(asmsh_t *sh, asmsh_cmd_args_t *args);
|
233
|
110
|
|
234
|
111
|
|
235
|
112
|
/*
|
236
|
113
|
* The list of shell commands
|
237
|
114
|
*/
|
238
|
115
|
static const asmsh_cmd_t asmsh_CMDS[] = {
|
239
|
|
- {".bytecode", _bcode, 2,
|
|
116
|
+ {".bytecode", asmsh_cmd_bcode, 2,
|
240
|
117
|
".b(ytecode)", "",
|
241
|
118
|
"display last instruction bytecode"},
|
242
|
|
- {".flags", _flags, 2,
|
|
119
|
+ {".flags", asmsh_cmd_flags, 2,
|
243
|
120
|
".f(lags)", "",
|
244
|
121
|
"display CPU flags"},
|
245
|
|
- {".help", _help, 2,
|
|
122
|
+ {".help", asmsh_cmd_help_, 2,
|
246
|
123
|
".h(elp)","[cmd]",
|
247
|
124
|
"display this help or the help of specified command"},
|
248
|
|
- {".maps", _maps, 2,
|
|
125
|
+ {".maps", asmsh_cmd_maps, 2,
|
249
|
126
|
".m(aps)", "",
|
250
|
127
|
"display memory maps"},
|
251
|
|
- {".quit", _quit, 2,
|
|
128
|
+ {".quit", asmsh_cmd_quit, 2,
|
252
|
129
|
".q(uit)","",
|
253
|
130
|
"quit asmsh"},
|
254
|
|
- {".regs", _print_regs, 1,
|
|
131
|
+ {".regs", asmsh_cmd_print_regs, 1,
|
255
|
132
|
".(regs)", "",
|
256
|
133
|
"display registers value"},
|
257
|
|
- {".syscalls", _syscalls, 2,
|
|
134
|
+ {".syscalls", asmsh_cmd_syscalls, 2,
|
258
|
135
|
".s(yscalls)", "",
|
259
|
136
|
"print syscalls name & numbers"},
|
260
|
|
- {".reset", _reset, 0,
|
|
137
|
+ {".reset", asmsh_cmd_reset, 0,
|
261
|
138
|
".reset", "",
|
262
|
139
|
"reset the shell"},
|
263
|
140
|
{NULL, NULL, 0},
|
264
|
141
|
};
|
265
|
142
|
|
266
|
143
|
|
267
|
|
-// Print help (command list + help)
|
268
|
|
-static int _help(asmsh_t *sh, asmsh_cmd_args_t *args)
|
269
|
|
-{
|
270
|
|
- char buf[4096], abuf[64];
|
271
|
|
- int ret;
|
272
|
|
-
|
273
|
|
- ret = snprintf(buf, 4096, "Available commands :\n");
|
274
|
|
- for(const asmsh_cmd_t *cmd = asmsh_CMDS; cmd->str; cmd++)
|
275
|
|
- {
|
276
|
|
- snprintf(abuf, 64, "%s %s", cmd->sms, cmd->usage);
|
277
|
|
- ret += snprintf(buf+ret, 4096-ret, " %-18s : %s\n",
|
278
|
|
- abuf, cmd->desc);
|
279
|
|
- }
|
280
|
|
- dprintf(2, buf);
|
281
|
|
- return 0;
|
282
|
|
-}
|
283
|
144
|
|
284
|
145
|
#endif
|