소스 검색

Porting a bugfix from VM + shell_cmds refactoring

Yann Weber 1 년 전
부모
커밋
6886694530
2개의 변경된 파일173개의 추가작업 그리고 159개의 파일을 삭제
  1. 155
    2
      shell_cmds.c
  2. 18
    157
      shell_cmds.h

+ 155
- 2
shell_cmds.c 파일 보기

@@ -134,7 +134,7 @@ void asmsh_cmd_args_free(asmsh_cmd_args_t *args)
134 134
 }
135 135
 
136 136
 
137
-int asmsh_cmd_bcode(asmsh_t *sh, char *argbuf, int bufsz, int argc, char **args)
137
+int asmsh_cmd_bcode_(asmsh_t *sh, char *argbuf, int bufsz, int argc, char **args)
138 138
 {
139 139
 	asmsh_bytecode_t bcode;
140 140
 	char _buf[256], *buf;
@@ -176,7 +176,6 @@ int asmsh_cmd_bcode(asmsh_t *sh, char *argbuf, int bufsz, int argc, char **args)
176 176
 			}
177 177
 		}
178 178
 		code[sz]='\0';
179
-dprintf(2, "INSTR='%s'\n", code);
180 179
 		if(asmsh_asmc_compile(sh->cctx, code, &bcode) < 0)
181 180
 		{
182 181
 			return -1;
@@ -202,3 +201,157 @@ dprintf(2, "INSTR='%s'\n", code);
202 201
 	return 0;
203 202
 }
204 203
 
204
+
205
+
206
+int asmsh_cmd_quit(asmsh_t *sh, asmsh_cmd_args_t *args)
207
+{
208
+	asmsh_cleanup(sh);
209
+	return 1;
210
+}
211
+
212
+
213
+int asmsh_cmd_bcode(asmsh_t *sh, asmsh_cmd_args_t *args)
214
+{
215
+	char str[256];
216
+	int ret;
217
+
218
+	ret = asmsh_cmd_bcode_(sh, str, 256, args->argc, args->args);
219
+	if(ret)
220
+	{
221
+		return ret;
222
+	}
223
+	printf("%s\n", str);
224
+	return 0;
225
+}
226
+
227
+int asmsh_cmd_maps(asmsh_t *sh, asmsh_cmd_args_t *args)
228
+{
229
+	if(asmsh_env_update_maps(sh->env) < 0)
230
+	{
231
+		perror("Unable to update maps info");
232
+		return -1;
233
+	}
234
+	printf("%12s-%-12s perm %-8s %-6s %-10s %s\n", "start", "stop", "offset", "device",
235
+			"inode", "path");
236
+	for(int i=0; i<sh->env->mmap.size; i++)
237
+	{
238
+		const child_mmap_t *m = &sh->env->mmap.maps[i];
239
+		printf("%012llx-%012llx %c%c%c  %08lx  %02lx:%02lx %10lu %s\n",
240
+				(unsigned long long int)m->start,
241
+				(unsigned long long int)m->stop,
242
+				m->perm & PROT_READ ?'r':'-',
243
+				m->perm & PROT_WRITE?'w':'-',
244
+				m->perm & PROT_EXEC ?'x':'-',
245
+				m->offset,
246
+				(m->device & 0xFF00)>>8,
247
+				m->device & 0xFF,
248
+				m->inode,
249
+				m->pathname);
250
+	}
251
+	return 0;
252
+}
253
+
254
+int asmsh_cmd_print_regs(asmsh_t *sh, asmsh_cmd_args_t *args)
255
+{
256
+	asmsh_env_t *env = sh->env;
257
+	asmsh_env_update_regs(env);
258
+	struct user_regs_struct *r = &env->regs;
259
+	
260
+#define FLG(b, l) ( (r->eflags & (1<<b))?l:'-' )
261
+
262
+	printf("rax: %016llx rbx: %016llx rcx: %016llx rdx: %016llx\n\
263
+rbp: %016llx rsi: %016llx rdi: %016llx rsp: %016llx\n\
264
+ r8: %016llx  r9: %016llx r10: %016llx r11: %016llx\n\
265
+r12: %016llx r13: %016llx r14: %016llx r15: %016llx\n\
266
+rip: %016llx flg: %016llx\n\
267
+cs: %04llx ds: %04llx es: %04llx fs:%04llx gs: %04llx ss:%04llx\n\
268
+flags: %c%c%c%c|%c%c%c\n\
269
+       ODSZ|APC\n\
270
+",	r->rax, r->rbx, r->rcx, r->rdx,\
271
+	r->rbp, r->rsi, r->rdi, r->rsp,\
272
+	r->r8, r->r9, r->r10, r->r11,\
273
+	r->r12, r->r13, r->r14, r->r15,\
274
+	r->rip, r->eflags,\
275
+	r->cs, r->ds, r->es, r->fs, r->gs, r->ss,
276
+	FLG(11,'O'), FLG(10, 'D'), FLG(7, 'S'), FLG(6, 'Z'),
277
+	FLG(4, 'A'), FLG(2, 'P'), FLG(0, 'C'));
278
+
279
+#undef FLG
280
+	return 0;
281
+}
282
+
283
+int asmsh_cmd_syscalls(asmsh_t *sh, asmsh_cmd_args_t *args)
284
+{
285
+	const int sz = sizeof(syscall_infos)/sizeof(*syscall_infos);
286
+	int printed;
287
+	char buf[512];
288
+
289
+	printed = 0;
290
+	for(int i=0; i<sz; i++)
291
+	{
292
+		int ret = snprintf(buf, 512, "%3d 0x%03x %20s ",
293
+			syscall_infos[i].nr,
294
+			syscall_infos[i].nr,
295
+			syscall_infos[i].name);
296
+		buf[ret] = '\0';
297
+		printed += ret;
298
+		if(printed > 80)
299
+		{
300
+			printf("\n");
301
+			printed = ret;
302
+		}
303
+		printf(buf);
304
+	}
305
+	printf("\n");
306
+	return 0;
307
+}
308
+
309
+int asmsh_cmd_reset(asmsh_t *sh, asmsh_cmd_args_t *args)
310
+{
311
+	char *childpath = sh->child_path?strdup(sh->child_path):NULL;
312
+	asmsh_cleanup(sh);
313
+	int ret = asmsh_init(sh, childpath)<0?2:0;
314
+	if(childpath) { free(childpath); }
315
+	return ret;
316
+}
317
+
318
+// Display CPU flag resister values
319
+int asmsh_cmd_flags(asmsh_t *sh, asmsh_cmd_args_t *args)
320
+{
321
+	printf("Flags :\n");
322
+#define printFLG(name, b) printf(" (%2d)%16s : %d\n", b, name,\
323
+		(sh->env->regs.eflags & (1<<b))?1:0);
324
+	printFLG("Overflow", 11);
325
+	printFLG("Direction", 10);
326
+	printFLG("Sign", 7);
327
+	printFLG("Zero", 6);
328
+	printFLG("Auxiliary carry", 4);
329
+	printFLG("Parity", 2);
330
+	printFLG("Carry", 0);
331
+#undef printFLG
332
+#define EFLG(b,n) ((sh->env->regs.eflags & (1<<b))?n:'-')
333
+	printf("%c%c%c%c %c%c%c\n",
334
+			EFLG(11,'O'), EFLG(10, 'D'), EFLG(7,'S'), EFLG(6,'Z'),
335
+			EFLG(4,'A'), EFLG(2, 'P'), EFLG(0, 'C'));
336
+	return 0;
337
+}
338
+
339
+// Print help (command list + help)
340
+int asmsh_cmd_help_(asmsh_t *sh, asmsh_cmd_args_t *args)
341
+{
342
+	char buf[4096], abuf[64];
343
+	int ret;
344
+
345
+	ret = snprintf(buf, 4096, "Available commands :\n");
346
+	for(const asmsh_cmd_t *cmd = asmsh_CMDS; cmd->str; cmd++)
347
+	{
348
+		snprintf(abuf, 64, "%s %s", cmd->sms, cmd->usage);
349
+		ret += snprintf(buf+ret, 4096-ret, "  %-18s : %s\n", 
350
+				abuf, cmd->desc);
351
+	}
352
+	dprintf(2, buf);
353
+	return 0;
354
+}
355
+
356
+
357
+

+ 18
- 157
shell_cmds.h 파일 보기

@@ -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

Loading…
취소
저장