1 |
|
/* |
2 |
< |
* mon.cpp - mon main program |
2 |
> |
* mon.cpp - cxmon main program |
3 |
|
* |
4 |
< |
* mon (C) 1997-2000 Christian Bauer, Marc Hellwig |
4 |
> |
* cxmon (C) 1997-2004 Christian Bauer, Marc Hellwig |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
24 |
|
#include <stdlib.h> |
25 |
|
#include <signal.h> |
26 |
|
#include <ctype.h> |
27 |
+ |
#include <string> |
28 |
+ |
#include <map> |
29 |
|
|
30 |
|
#if defined(HAVE_READLINE_H) |
31 |
|
extern "C" { |
49 |
|
|
50 |
|
#include "mon.h" |
51 |
|
#include "mon_cmd.h" |
52 |
+ |
#include "mon_lowmem.h" |
53 |
|
|
54 |
|
#ifndef VERSION |
55 |
< |
#define VERSION "2" |
55 |
> |
#define VERSION "3" |
56 |
|
#endif |
57 |
|
|
58 |
|
|
66 |
|
FILE *monin, *monout, *monerr; |
67 |
|
|
68 |
|
// Input line |
69 |
< |
static char input[INPUT_LENGTH]; |
69 |
> |
static char *input; |
70 |
|
static char *in_ptr; |
71 |
|
char *mon_args_ptr; |
72 |
|
|
73 |
|
// Current address, value of '.' in expressions |
74 |
< |
uint32 mon_dot_address; |
74 |
> |
uintptr mon_dot_address; |
75 |
|
|
76 |
|
// Current value of ':' in expression |
77 |
|
static uint32 colon_value; |
78 |
|
|
79 |
|
|
80 |
|
// Scanner variables |
81 |
< |
enum Token mon_token; // Last token read |
82 |
< |
uint32 mon_number; // Contains the number if mon_token==T_NUMBER |
83 |
< |
char mon_string[INPUT_LENGTH]; // Contains the string if mon_token==T_STRING |
84 |
< |
char mon_name[INPUT_LENGTH]; // Contains the variable name if mon_token==T_NAME |
81 |
> |
enum Token mon_token; // Last token read |
82 |
> |
uintptr mon_number; // Contains the number if mon_token==T_NUMBER |
83 |
> |
char *mon_string; // Contains the string if mon_token==T_STRING |
84 |
> |
char *mon_name; // Contains the variable name if mon_token==T_NAME |
85 |
|
|
86 |
|
|
87 |
|
// List of installed commands |
88 |
|
struct CmdSpec { |
89 |
< |
const char *name; // Name of command |
90 |
< |
void (*func)(void); // Function that executes this command |
89 |
> |
const char *name; // Name of command |
90 |
> |
void (*func)(); // Function that executes this command |
91 |
|
}; |
92 |
|
|
93 |
< |
static CmdSpec *cmds; // Array of CmdSpecs |
94 |
< |
static int num_cmds; // Number of installed commands |
95 |
< |
static char *cmd_help; // Help text for commands |
93 |
> |
static CmdSpec *cmds; // Array of CmdSpecs |
94 |
> |
static int num_cmds; // Number of installed commands |
95 |
> |
static char *cmd_help; // Help text for commands |
96 |
|
|
97 |
|
|
98 |
|
// List of variables |
99 |
< |
struct Variable { |
100 |
< |
Variable *next; // Pointer to next variable (must be first element of struct) |
98 |
< |
char *name; // Variable name |
99 |
< |
uint32 value; // Variable value |
100 |
< |
}; |
101 |
< |
|
102 |
< |
static Variable *first_var; // Pointer to first variable |
99 |
> |
typedef std::map<std::string, uintptr> var_map; |
100 |
> |
static var_map vars; |
101 |
|
|
102 |
|
|
103 |
|
// Prototypes |
104 |
< |
static void init_abort(void); |
105 |
< |
static void exit_abort(void); |
104 |
> |
static void init_abort(); |
105 |
> |
static void exit_abort(); |
106 |
|
|
107 |
|
static void read_line(char *prompt); // Scanner |
108 |
< |
static char get_char(void); |
108 |
> |
static char get_char(); |
109 |
|
static void put_back(char c); |
110 |
< |
static enum Token get_hex_number(uint32 &i); |
111 |
< |
static enum Token get_dec_number(uint32 &i); |
112 |
< |
static enum Token get_char_number(uint32 &i); |
113 |
< |
static enum Token get_string(char *str); |
114 |
< |
static enum Token get_hex_or_name(uint32 &i, char *name); |
115 |
< |
|
116 |
< |
static bool eor_expr(uint32 *number); // Parser |
117 |
< |
static bool and_expr(uint32 *number); |
118 |
< |
static bool shift_expr(uint32 *number); |
119 |
< |
static bool add_expr(uint32 *number); |
120 |
< |
static bool mul_expr(uint32 *number); |
121 |
< |
static bool factor(uint32 *number); |
124 |
< |
static Variable *lookup_var(const char *s); |
125 |
< |
static Variable *insert_var(const char *s); |
126 |
< |
static void remove_var(const char *s); |
110 |
> |
static enum Token get_hex_number(uintptr &i); |
111 |
> |
static enum Token get_dec_number(uintptr &i); |
112 |
> |
static enum Token get_char_number(uintptr &i); |
113 |
> |
static enum Token get_string(char *&str); |
114 |
> |
static enum Token get_hex_or_name(uintptr &i, char *&name); |
115 |
> |
|
116 |
> |
static bool eor_expr(uintptr *number); // Parser |
117 |
> |
static bool and_expr(uintptr *number); |
118 |
> |
static bool shift_expr(uintptr *number); |
119 |
> |
static bool add_expr(uintptr *number); |
120 |
> |
static bool mul_expr(uintptr *number); |
121 |
> |
static bool factor(uintptr *number); |
122 |
|
|
123 |
|
|
124 |
|
/* |
125 |
|
* Add command to mon |
126 |
|
*/ |
127 |
|
|
128 |
< |
void mon_add_command(const char *name, void (*func)(void), const char *help_text) |
128 |
> |
void mon_add_command(const char *name, void (*func)(), const char *help_text) |
129 |
|
{ |
130 |
|
num_cmds++; |
131 |
|
if (cmds) |
170 |
|
was_aborted = true; |
171 |
|
} |
172 |
|
|
173 |
< |
static void init_abort(void) |
173 |
> |
static void init_abort() |
174 |
|
{ |
175 |
|
was_aborted = false; |
176 |
|
sigemptyset(&my_sa.sa_mask); |
184 |
|
sigaction(SIGINT, &my_sa, NULL); |
185 |
|
} |
186 |
|
|
187 |
< |
static void exit_abort(void) |
187 |
> |
static void exit_abort() |
188 |
|
{ |
189 |
|
my_sa.sa_handler = SIG_DFL; |
190 |
|
sigaction(SIGINT, &my_sa, NULL); |
191 |
|
} |
192 |
|
|
193 |
< |
bool mon_aborted(void) |
193 |
> |
bool mon_aborted() |
194 |
|
{ |
195 |
|
bool ret = was_aborted; |
196 |
|
was_aborted = false; |
202 |
|
* Access to buffer |
203 |
|
*/ |
204 |
|
|
205 |
< |
uint32 (*mon_read_byte)(uint32 adr); |
205 |
> |
uint32 (*mon_read_byte)(uintptr adr); |
206 |
|
|
207 |
< |
uint32 mon_read_byte_buffer(uint32 adr) |
207 |
> |
uint32 mon_read_byte_buffer(uintptr adr) |
208 |
|
{ |
209 |
|
return mem[adr % mon_mem_size]; |
210 |
|
} |
211 |
|
|
212 |
< |
uint32 mon_read_byte_real(uint32 adr) |
212 |
> |
uint32 mon_read_byte_real(uintptr adr) |
213 |
|
{ |
214 |
|
return *(uint8 *)adr; |
215 |
|
} |
216 |
|
|
217 |
< |
void (*mon_write_byte)(uint32 adr, uint32 b); |
217 |
> |
void (*mon_write_byte)(uintptr adr, uint32 b); |
218 |
|
|
219 |
< |
void mon_write_byte_buffer(uint32 adr, uint32 b) |
219 |
> |
void mon_write_byte_buffer(uintptr adr, uint32 b) |
220 |
|
{ |
221 |
|
mem[adr % mon_mem_size] = b; |
222 |
|
} |
223 |
|
|
224 |
< |
void mon_write_byte_real(uint32 adr, uint32 b) |
224 |
> |
void mon_write_byte_real(uintptr adr, uint32 b) |
225 |
|
{ |
226 |
|
*(uint8 *)adr = b; |
227 |
|
} |
228 |
|
|
229 |
< |
uint32 mon_read_half(uint32 adr) |
229 |
> |
uint32 mon_read_half(uintptr adr) |
230 |
|
{ |
231 |
|
return (mon_read_byte(adr) << 8) | mon_read_byte(adr+1); |
232 |
|
} |
233 |
|
|
234 |
< |
void mon_write_half(uint32 adr, uint32 w) |
234 |
> |
void mon_write_half(uintptr adr, uint32 w) |
235 |
|
{ |
236 |
|
mon_write_byte(adr, w >> 8); |
237 |
|
mon_write_byte(adr+1, w); |
238 |
|
} |
239 |
|
|
240 |
< |
uint32 mon_read_word(uint32 adr) |
240 |
> |
uint32 mon_read_word(uintptr adr) |
241 |
|
{ |
242 |
|
return (mon_read_byte(adr) << 24) | (mon_read_byte(adr+1) << 16) | (mon_read_byte(adr+2) << 8) | mon_read_byte(adr+3); |
243 |
|
} |
244 |
|
|
245 |
< |
void mon_write_word(uint32 adr, uint32 l) |
245 |
> |
void mon_write_word(uintptr adr, uint32 l) |
246 |
|
{ |
247 |
|
mon_write_byte(adr, l >> 24); |
248 |
|
mon_write_byte(adr+1, l >> 16); |
258 |
|
static void read_line(char *prompt) |
259 |
|
{ |
260 |
|
#ifdef HAVE_LIBREADLINE |
261 |
< |
static char *line_read = NULL; |
262 |
< |
|
263 |
< |
if (line_read) { |
264 |
< |
free(line_read); |
265 |
< |
line_read = NULL; |
261 |
> |
if (input) |
262 |
> |
free(input); |
263 |
> |
input = readline(prompt); |
264 |
> |
|
265 |
> |
if (input) { |
266 |
> |
if (*input) |
267 |
> |
add_history(input); |
268 |
> |
} else { |
269 |
> |
// EOF, quit cxmon |
270 |
> |
input = (char *)malloc(2); |
271 |
> |
input[0] = 'x'; |
272 |
> |
input[1] = 0; |
273 |
> |
fprintf(monout, "x\n"); |
274 |
|
} |
275 |
|
|
276 |
< |
line_read = readline(prompt); |
274 |
< |
|
275 |
< |
if (line_read && *line_read) |
276 |
< |
add_history(line_read); |
277 |
< |
|
278 |
< |
strncpy(in_ptr = input, line_read, INPUT_LENGTH); |
279 |
< |
input[INPUT_LENGTH-1] = 0; |
276 |
> |
in_ptr = input; |
277 |
|
#else |
278 |
+ |
static const unsigned INPUT_LENGTH = 256; |
279 |
+ |
if (!input) |
280 |
+ |
input = (char *)malloc(INPUT_LENGTH); |
281 |
|
fprintf(monout, prompt); |
282 |
|
fflush(monout); |
283 |
|
fgets(in_ptr = input, INPUT_LENGTH, monin); |
292 |
|
* Read a character from the input line |
293 |
|
*/ |
294 |
|
|
295 |
< |
static char get_char(void) |
295 |
> |
static char get_char() |
296 |
|
{ |
297 |
|
return *in_ptr++; |
298 |
|
} |
312 |
|
* Scanner: Get a token from the input line |
313 |
|
*/ |
314 |
|
|
315 |
< |
enum Token mon_get_token(void) |
315 |
> |
enum Token mon_get_token() |
316 |
|
{ |
317 |
< |
char c; |
317 |
> |
char c = get_char(); |
318 |
|
|
319 |
|
// Skip spaces |
320 |
< |
while ((c = get_char()) == ' ') ; |
320 |
> |
while (isspace(c)) |
321 |
> |
c = get_char(); |
322 |
|
|
323 |
|
switch (c) { |
324 |
|
case 0: |
391 |
|
} |
392 |
|
} |
393 |
|
|
394 |
< |
static enum Token get_hex_number(uint32 &i) |
394 |
> |
static enum Token get_hex_number(uintptr &i) |
395 |
|
{ |
396 |
|
char c = get_char(); |
397 |
|
|
400 |
|
return T_NULL; |
401 |
|
|
402 |
|
do { |
403 |
+ |
c = tolower(c); |
404 |
|
if (c < 'a') |
405 |
|
i = (i << 4) + (c - '0'); |
406 |
|
else |
416 |
|
} |
417 |
|
} |
418 |
|
|
419 |
< |
static enum Token get_dec_number(uint32 &i) |
419 |
> |
static enum Token get_dec_number(uintptr &i) |
420 |
|
{ |
421 |
|
char c = get_char(); |
422 |
|
|
437 |
|
} |
438 |
|
} |
439 |
|
|
440 |
< |
static enum Token get_char_number(uint32 &i) |
440 |
> |
static enum Token get_char_number(uintptr &i) |
441 |
|
{ |
442 |
|
char c; |
443 |
|
|
452 |
|
return T_NULL; |
453 |
|
} |
454 |
|
|
455 |
< |
static enum Token get_string(char *str) |
455 |
> |
static enum Token get_string(char *&str) |
456 |
|
{ |
457 |
< |
char c; |
457 |
> |
// Remember start of string |
458 |
> |
char *old_in_ptr = in_ptr; |
459 |
|
|
460 |
+ |
// Determine string length |
461 |
+ |
char c; |
462 |
+ |
unsigned n = 0; |
463 |
|
while ((c = get_char()) != 0) { |
464 |
< |
if (c == '"') { |
465 |
< |
*str = 0; |
466 |
< |
return T_STRING; |
467 |
< |
} |
468 |
< |
*str++ = c; |
464 |
> |
n++; |
465 |
> |
if (c == '"') |
466 |
> |
break; |
467 |
> |
} |
468 |
> |
if (c == 0) { |
469 |
> |
mon_error("Unterminated string"); |
470 |
> |
return T_NULL; |
471 |
|
} |
472 |
|
|
473 |
< |
mon_error("Unterminated string"); |
474 |
< |
return T_NULL; |
473 |
> |
// Allocate new buffer (n: size needed including terminating 0) |
474 |
> |
str = (char *)realloc(str, n); |
475 |
> |
|
476 |
> |
// Copy string to buffer |
477 |
> |
char *p = str; |
478 |
> |
in_ptr = old_in_ptr; |
479 |
> |
while (--n) |
480 |
> |
*p++ = get_char(); |
481 |
> |
*p++ = 0; |
482 |
> |
get_char(); // skip closing '"' |
483 |
> |
return T_STRING; |
484 |
|
} |
485 |
|
|
486 |
< |
static enum Token get_hex_or_name(uint32 &i, char *name) |
486 |
> |
static enum Token get_hex_or_name(uintptr &i, char *&name) |
487 |
|
{ |
488 |
+ |
// Remember start of token |
489 |
|
char *old_in_ptr = in_ptr; |
472 |
– |
char c; |
490 |
|
|
491 |
|
// Try hex number first |
492 |
|
if (get_hex_number(i) == T_NUMBER) |
493 |
|
return T_NUMBER; |
494 |
|
|
495 |
< |
// Not a hex number, must be a variable name |
495 |
> |
// Not a hex number, must be a variable name; determine its length |
496 |
|
in_ptr = old_in_ptr; |
497 |
< |
c = get_char(); |
497 |
> |
char c = get_char(); |
498 |
> |
unsigned n = 1; |
499 |
|
do { |
500 |
< |
*name++ = c; |
500 |
> |
n++; |
501 |
|
c = get_char(); |
502 |
|
} while (isalnum(c)); |
503 |
|
|
504 |
< |
*name = 0; |
505 |
< |
put_back(c); |
504 |
> |
// Allocate new buffer (n: size needed including terminating 0) |
505 |
> |
name = (char *)realloc(name, n); |
506 |
> |
|
507 |
> |
// Copy name to buffer |
508 |
> |
in_ptr = old_in_ptr; |
509 |
> |
char *p = name; |
510 |
> |
while (--n) |
511 |
> |
*p++ = get_char(); |
512 |
> |
*p = 0; |
513 |
|
return T_NAME; |
514 |
|
} |
515 |
|
|
519 |
|
* true: OK, false: Error |
520 |
|
*/ |
521 |
|
|
522 |
< |
bool mon_expression(uint32 *number) |
522 |
> |
bool mon_expression(uintptr *number) |
523 |
|
{ |
524 |
< |
uint32 accu, expr; |
524 |
> |
uintptr accu, expr; |
525 |
|
|
526 |
|
if (!eor_expr(&accu)) |
527 |
|
return false; |
547 |
|
* true: OK, false: Error |
548 |
|
*/ |
549 |
|
|
550 |
< |
static bool eor_expr(uint32 *number) |
550 |
> |
static bool eor_expr(uintptr *number) |
551 |
|
{ |
552 |
< |
uint32 accu, expr; |
552 |
> |
uintptr accu, expr; |
553 |
|
|
554 |
|
if (!and_expr(&accu)) |
555 |
|
return false; |
575 |
|
* true: OK, false: Error |
576 |
|
*/ |
577 |
|
|
578 |
< |
static bool and_expr(uint32 *number) |
578 |
> |
static bool and_expr(uintptr *number) |
579 |
|
{ |
580 |
< |
uint32 accu, expr; |
580 |
> |
uintptr accu, expr; |
581 |
|
|
582 |
|
if (!shift_expr(&accu)) |
583 |
|
return false; |
603 |
|
* true: OK, false: Error |
604 |
|
*/ |
605 |
|
|
606 |
< |
static bool shift_expr(uint32 *number) |
606 |
> |
static bool shift_expr(uintptr *number) |
607 |
|
{ |
608 |
< |
uint32 accu, expr; |
608 |
> |
uintptr accu, expr; |
609 |
|
|
610 |
|
if (!add_expr(&accu)) |
611 |
|
return false; |
638 |
|
* true: OK, false: Error |
639 |
|
*/ |
640 |
|
|
641 |
< |
static bool add_expr(uint32 *number) |
641 |
> |
static bool add_expr(uintptr *number) |
642 |
|
{ |
643 |
< |
uint32 accu, expr; |
643 |
> |
uintptr accu, expr; |
644 |
|
|
645 |
|
if (!mul_expr(&accu)) |
646 |
|
return false; |
673 |
|
* true: OK, false: Error |
674 |
|
*/ |
675 |
|
|
676 |
< |
static bool mul_expr(uint32 *number) |
676 |
> |
static bool mul_expr(uintptr *number) |
677 |
|
{ |
678 |
< |
uint32 accu, fact; |
678 |
> |
uintptr accu, fact; |
679 |
|
|
680 |
|
if (!factor(&accu)) |
681 |
|
return false; |
723 |
|
* true: OK, false: Error |
724 |
|
*/ |
725 |
|
|
726 |
< |
static bool factor(uint32 *number) |
726 |
> |
static bool factor(uintptr *number) |
727 |
|
{ |
728 |
|
switch (mon_token) { |
729 |
|
case T_NUMBER: |
732 |
|
return true; |
733 |
|
|
734 |
|
case T_NAME:{ |
735 |
< |
Variable *var; |
736 |
< |
if ((var = lookup_var(mon_name)) != NULL) { |
737 |
< |
*number = var->value; |
735 |
> |
var_map::const_iterator v = vars.find(mon_name); |
736 |
> |
if (v == vars.end()) |
737 |
> |
return false; |
738 |
> |
else { |
739 |
> |
*number = v->second; |
740 |
|
mon_get_token(); |
741 |
|
return true; |
742 |
< |
} else |
716 |
< |
return false; |
742 |
> |
} |
743 |
|
} |
744 |
|
|
745 |
|
case T_DOT: |
799 |
|
|
800 |
|
|
801 |
|
/* |
776 |
– |
* Lookup the value of a variable |
777 |
– |
*/ |
778 |
– |
|
779 |
– |
static Variable *lookup_var(const char *s) |
780 |
– |
{ |
781 |
– |
// Lookup variable |
782 |
– |
for (Variable *var=first_var; var; var=var->next) |
783 |
– |
if (!strcmp(s, var->name)) |
784 |
– |
return var; |
785 |
– |
|
786 |
– |
// Not found, error |
787 |
– |
mon_error("Undefined variable"); |
788 |
– |
return NULL; |
789 |
– |
} |
790 |
– |
|
791 |
– |
|
792 |
– |
/* |
793 |
– |
* Insert new variable (or redefine old) |
794 |
– |
*/ |
795 |
– |
|
796 |
– |
static Variable *insert_var(const char *s) |
797 |
– |
{ |
798 |
– |
// Lookup variable |
799 |
– |
for (Variable *var=first_var; var; var=var->next) |
800 |
– |
if (!strcmp(s, var->name)) |
801 |
– |
return var; |
802 |
– |
|
803 |
– |
// Insert new variable |
804 |
– |
Variable *var = new Variable; |
805 |
– |
var->name = strdup(s); |
806 |
– |
var->next = first_var; |
807 |
– |
first_var = var; |
808 |
– |
return var; |
809 |
– |
} |
810 |
– |
|
811 |
– |
|
812 |
– |
/* |
813 |
– |
* Remove variable |
814 |
– |
*/ |
815 |
– |
|
816 |
– |
static void remove_var(const char *s) |
817 |
– |
{ |
818 |
– |
Variable *var, *prev = (Variable *)&first_var; |
819 |
– |
|
820 |
– |
// Lookup variable and remove it |
821 |
– |
for (var=prev->next; var; prev=var, var=var->next) |
822 |
– |
if (!strcmp(s, var->name)) { |
823 |
– |
prev->next = var->next; |
824 |
– |
free(var->name); |
825 |
– |
free(var); |
826 |
– |
return; |
827 |
– |
} |
828 |
– |
} |
829 |
– |
|
830 |
– |
|
831 |
– |
/* |
802 |
|
* Set/clear/show variables |
803 |
|
* set [var[=value]] |
804 |
|
*/ |
805 |
|
|
806 |
< |
static void set_var(void) |
806 |
> |
static void set_var() |
807 |
|
{ |
808 |
|
if (mon_token == T_END) { |
809 |
|
|
810 |
|
// Show all variables |
811 |
< |
if (first_var == NULL) |
811 |
> |
if (vars.empty()) |
812 |
|
fprintf(monout, "No variables defined\n"); |
813 |
< |
else |
814 |
< |
for (Variable *v=first_var; v; v=v->next) |
815 |
< |
fprintf(monout, "%s = %08x\n", v->name, v->value); |
813 |
> |
else { |
814 |
> |
var_map::const_iterator v = vars.begin(), end = vars.end(); |
815 |
> |
for (v=vars.begin(); v!=end; ++v) |
816 |
> |
fprintf(monout, "%s = %08lx\n", v->first.c_str(), v->second); |
817 |
> |
} |
818 |
|
|
819 |
|
} else if (mon_token == T_NAME) { |
820 |
< |
char var_name[256]; |
849 |
< |
strcpy(var_name, mon_name); |
820 |
> |
std::string var_name = mon_name; |
821 |
|
mon_get_token(); |
822 |
|
if (mon_token == T_ASSIGN) { |
823 |
|
|
824 |
|
// Set variable |
825 |
< |
uint32 value; |
825 |
> |
uintptr value; |
826 |
|
mon_get_token(); |
827 |
|
if (!mon_expression(&value)) |
828 |
|
return; |
830 |
|
mon_error("Too many arguments"); |
831 |
|
return; |
832 |
|
} |
833 |
< |
insert_var(var_name)->value = value; |
833 |
> |
vars[var_name] = value; |
834 |
|
|
835 |
|
} else if (mon_token == T_END) { |
836 |
|
|
837 |
|
// Clear variable |
838 |
< |
remove_var(var_name); |
838 |
> |
vars.erase(var_name); |
839 |
|
|
840 |
|
} else |
841 |
|
mon_error("'=' expected"); |
849 |
|
* cv |
850 |
|
*/ |
851 |
|
|
852 |
< |
static void clear_vars(void) |
852 |
> |
static void clear_vars() |
853 |
|
{ |
854 |
< |
Variable *var, *next; |
884 |
< |
for (var=first_var; var; var=next) { |
885 |
< |
free(var->name); |
886 |
< |
next = var->next; |
887 |
< |
free(var); |
888 |
< |
} |
889 |
< |
first_var = NULL; |
854 |
> |
vars.clear(); |
855 |
|
} |
856 |
|
|
857 |
|
|
860 |
|
* h |
861 |
|
*/ |
862 |
|
|
863 |
< |
static void help_or_hunt(void) |
863 |
> |
static void help_or_hunt() |
864 |
|
{ |
865 |
|
if (mon_token != T_END) { |
866 |
|
hunt(); |
877 |
|
* ?? |
878 |
|
*/ |
879 |
|
|
880 |
< |
static void mon_cmd_list(void) |
880 |
> |
static void mon_cmd_list() |
881 |
|
{ |
882 |
|
for (int i=0; i<num_cmds; i++) |
883 |
|
fprintf(monout, "%s ", cmds[i].name); |
890 |
|
* @ [size] |
891 |
|
*/ |
892 |
|
|
893 |
< |
static void reallocate(void) |
893 |
> |
static void reallocate() |
894 |
|
{ |
895 |
< |
uint32 size; |
895 |
> |
uintptr size; |
896 |
|
|
897 |
|
if (mon_use_real_mem) { |
898 |
|
fprintf(monerr, "Cannot reallocate buffer in real mode\n"); |
925 |
|
|
926 |
|
static void apply(int size) |
927 |
|
{ |
928 |
< |
uint32 adr, end_adr, value; |
928 |
> |
uintptr adr, end_adr, value; |
929 |
|
char c; |
930 |
|
|
931 |
|
if (!mon_expression(&adr)) |
939 |
|
return; |
940 |
|
} |
941 |
|
|
942 |
< |
uint32 (*read_func)(uint32 adr); |
943 |
< |
void (*write_func)(uint32 adr, uint32 val); |
942 |
> |
uint32 (*read_func)(uintptr adr); |
943 |
> |
void (*write_func)(uintptr adr, uint32 val); |
944 |
|
switch (size) { |
945 |
|
case 1: |
946 |
|
read_func = mon_read_byte; |
980 |
|
mon_dot_address = adr; |
981 |
|
} |
982 |
|
|
983 |
< |
static void apply_byte(void) |
983 |
> |
static void apply_byte() |
984 |
|
{ |
985 |
|
apply(1); |
986 |
|
} |
987 |
|
|
988 |
< |
static void apply_half(void) |
988 |
> |
static void apply_half() |
989 |
|
{ |
990 |
|
apply(2); |
991 |
|
} |
992 |
|
|
993 |
< |
static void apply_word(void) |
993 |
> |
static void apply_word() |
994 |
|
{ |
995 |
|
apply(4); |
996 |
|
} |
1000 |
|
* Execute command via system() (for ls, rm, etc.) |
1001 |
|
*/ |
1002 |
|
|
1003 |
< |
static void mon_exec(void) |
1003 |
> |
static void mon_exec() |
1004 |
|
{ |
1005 |
|
system(input); |
1006 |
|
} |
1010 |
|
* Change current directory |
1011 |
|
*/ |
1012 |
|
|
1013 |
< |
static void mon_change_dir(void) |
1013 |
> |
void mon_change_dir() |
1014 |
|
{ |
1050 |
– |
char c; |
1015 |
|
in_ptr = input; |
1016 |
< |
while ((c = get_char()) == ' ') ; |
1017 |
< |
while ((c = get_char()) != ' ') ; |
1018 |
< |
while ((c = get_char()) == ' ') ; |
1016 |
> |
char c = get_char(); |
1017 |
> |
while (isspace(c)) |
1018 |
> |
c = get_char(); |
1019 |
> |
while (isgraph(c)) |
1020 |
> |
c = get_char(); |
1021 |
> |
while (isspace(c)) |
1022 |
> |
c = get_char(); |
1023 |
|
put_back(c); |
1024 |
|
if (chdir(in_ptr) != 0) |
1025 |
|
mon_error("Cannot change directory"); |
1030 |
|
* Initialize mon |
1031 |
|
*/ |
1032 |
|
|
1033 |
< |
void mon_init(void) |
1033 |
> |
void mon_init() |
1034 |
|
{ |
1035 |
|
cmds = NULL; |
1036 |
|
num_cmds = 0; |
1046 |
|
mon_add_command("d", disassemble_ppc, "d [start [end]] Disassemble PowerPC code\n"); |
1047 |
|
mon_add_command("d65", disassemble_6502, "d65 [start [end]] Disassemble 6502 code\n"); |
1048 |
|
mon_add_command("d68", disassemble_680x0, "d68 [start [end]] Disassemble 680x0 code\n"); |
1049 |
< |
mon_add_command("d80", disassemble_8080, "d80 [start [end]] Disassemble 8080 code\n"); |
1049 |
> |
mon_add_command("d80", disassemble_z80, "d80 [start [end]] Disassemble Z80 code\n"); |
1050 |
|
mon_add_command("d86", disassemble_80x86_32, "d86 [start [end]] Disassemble 80x86 (32-bit) code\n"); |
1051 |
|
mon_add_command("d8086", disassemble_80x86_16, "d8086 [start [end]] Disassemble 80x86 (16-bit) code\n"); |
1052 |
+ |
mon_add_command("d8664", disassemble_x86_64, "d8664 [start [end]] Disassemble x86-64 code\n"); |
1053 |
+ |
mon_add_command("dm", disassemble_mips, "dm [start [end]] Disassemble MIPS code\n"); |
1054 |
+ |
mon_add_command("dmel", disassemble_mipsel, "dmel [start [end]] Disassemble MIPS (little-endian) code\n"); |
1055 |
|
mon_add_command(":", modify, ": start string Modify memory\n"); |
1056 |
|
mon_add_command("f", fill, "f start end string Fill memory\n"); |
1057 |
|
mon_add_command("y", apply_byte, "y[b|h|w] start end expr Apply expression to memory\n"); |
1075 |
|
|
1076 |
|
mon_read_byte = NULL; |
1077 |
|
mon_write_byte = NULL; |
1078 |
+ |
|
1079 |
+ |
input = NULL; |
1080 |
+ |
mon_string = NULL; |
1081 |
+ |
mon_name = NULL; |
1082 |
|
} |
1083 |
|
|
1084 |
|
|
1086 |
|
* Deinitialize mon |
1087 |
|
*/ |
1088 |
|
|
1089 |
< |
void mon_exit(void) |
1089 |
> |
void mon_exit() |
1090 |
|
{ |
1091 |
< |
free(cmds); |
1092 |
< |
cmds = NULL; |
1091 |
> |
if (cmds) { |
1092 |
> |
free(cmds); |
1093 |
> |
cmds = NULL; |
1094 |
> |
} |
1095 |
|
num_cmds = 0; |
1096 |
|
cmd_help = NULL; |
1097 |
+ |
|
1098 |
+ |
if (input) { |
1099 |
+ |
free(input); |
1100 |
+ |
input = NULL; |
1101 |
+ |
} |
1102 |
+ |
if (mon_string) { |
1103 |
+ |
free(mon_string); |
1104 |
+ |
mon_string = NULL; |
1105 |
+ |
} |
1106 |
+ |
if (mon_name) { |
1107 |
+ |
free(mon_name); |
1108 |
+ |
mon_name = NULL; |
1109 |
+ |
} |
1110 |
|
} |
1111 |
|
|
1112 |
|
|
1117 |
|
void mon(int argc, char **argv) |
1118 |
|
{ |
1119 |
|
bool done = false, interactive = true; |
1130 |
– |
char c, cmd[INPUT_LENGTH]; |
1120 |
|
|
1121 |
|
// Setup input/output streams |
1122 |
|
monin = stdin; |
1124 |
|
monerr = stdout; |
1125 |
|
|
1126 |
|
// Make argc/argv point to the actual arguments |
1127 |
+ |
const char *prg_name = argv[0]; |
1128 |
|
if (argc) |
1129 |
|
argc--; argv++; |
1130 |
|
|
1132 |
|
mon_macos_mode = false; |
1133 |
|
mon_use_real_mem = false; |
1134 |
|
while (argc > 0) { |
1135 |
< |
if (strcmp(argv[0], "-m") == 0) |
1135 |
> |
if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0) { |
1136 |
> |
printf("Usage: %s [-m] [-r] [command...]\n", prg_name); |
1137 |
> |
exit(0); |
1138 |
> |
} else if (strcmp(argv[0], "-m") == 0) |
1139 |
|
mon_macos_mode = true; |
1140 |
|
else if (strcmp(argv[0], "-r") == 0) |
1141 |
|
mon_use_real_mem = true; |
1166 |
|
|
1167 |
|
// Print banner |
1168 |
|
if (interactive) |
1169 |
< |
fprintf(monerr, "\n *** mon V" VERSION " by Christian Bauer and Marc Hellwig ***\n" |
1170 |
< |
" *** Press 'h' for help ***\n\n"); |
1169 |
> |
fprintf(monerr, "\n *** cxmon V" VERSION " by Christian Bauer and Marc Hellwig ***\n" |
1170 |
> |
" *** Press 'h' for help ***\n\n"); |
1171 |
> |
} |
1172 |
> |
|
1173 |
> |
// Clear variables |
1174 |
> |
vars.clear(); |
1175 |
> |
|
1176 |
> |
// In MacOS mode, pull in the lowmem globals as variables |
1177 |
> |
if (mon_macos_mode) { |
1178 |
> |
const lowmem_info *l = lowmem; |
1179 |
> |
while (l->name) { |
1180 |
> |
vars[l->name] = l->addr; |
1181 |
> |
l++; |
1182 |
> |
} |
1183 |
|
} |
1184 |
|
|
1185 |
|
init_abort(); |
1186 |
|
|
1187 |
|
// Read and parse command line |
1188 |
+ |
char *cmd = NULL; |
1189 |
|
while (!done) { |
1190 |
|
if (interactive) { |
1191 |
|
char prompt[16]; |
1192 |
< |
sprintf(prompt, "[%08x]-> ", mon_dot_address); |
1192 |
> |
sprintf(prompt, "[%0*lx]-> ", int(2 * sizeof(mon_dot_address)), mon_dot_address); |
1193 |
|
read_line(prompt); |
1194 |
+ |
if (!input) { |
1195 |
+ |
done = true; |
1196 |
+ |
continue; |
1197 |
+ |
} |
1198 |
|
} else { |
1199 |
|
if (argc == 0) { |
1200 |
|
done = true; |
1201 |
|
break; |
1202 |
|
} else { |
1203 |
< |
strncpy(in_ptr = input, argv[0], INPUT_LENGTH); |
1203 |
> |
unsigned n = strlen(argv[0]) + 1; |
1204 |
> |
input = (char *)realloc(input, n); |
1205 |
> |
strcpy(in_ptr = input, argv[0]); |
1206 |
|
argc--; |
1207 |
|
argv++; |
1208 |
|
} |
1209 |
|
} |
1210 |
|
|
1211 |
|
// Skip leading spaces |
1212 |
< |
while ((c = get_char()) == ' ') ; |
1212 |
> |
char c = get_char(); |
1213 |
> |
while (isspace(c)) |
1214 |
> |
c = get_char(); |
1215 |
> |
put_back(c); |
1216 |
> |
if (!c) |
1217 |
> |
continue; // blank line |
1218 |
|
|
1219 |
|
// Read command word |
1220 |
< |
char *p = cmd; |
1221 |
< |
do { |
1205 |
< |
*p++ = c; |
1220 |
> |
char *p = in_ptr; |
1221 |
> |
while (isgraph(c)) |
1222 |
|
c = get_char(); |
1207 |
– |
} while (isgraph(c)); |
1208 |
– |
*p = 0; |
1223 |
|
put_back(c); |
1224 |
+ |
unsigned n = in_ptr - p; |
1225 |
+ |
cmd = (char *)realloc(cmd, n + 1); |
1226 |
+ |
memcpy(cmd, p, n); |
1227 |
+ |
cmd[n] = 0; |
1228 |
|
|
1229 |
|
// Execute command |
1212 |
– |
if (cmd[0] == 0) // Blank line |
1213 |
– |
continue; |
1230 |
|
if (strcmp(cmd, "x") == 0) { // Exit |
1231 |
|
done = true; |
1232 |
|
continue; |
1242 |
|
cmd_done: ; |
1243 |
|
} |
1244 |
|
|
1245 |
+ |
if (cmd) |
1246 |
+ |
free(cmd); |
1247 |
+ |
|
1248 |
|
exit_abort(); |
1249 |
|
|
1250 |
|
// Free buffer |