|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#include "dwarf.c"
|
|
|
|
#include "util.c"
|
|
|
|
#include "command.c"
|
|
|
|
|
|
|
|
// TODO: read the directory table, get full path as comp_dir + directory_table (can be ".." or w/e) + filename
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc != 2) {
|
|
|
|
printf("Usage: %s executable\n", argv[0]);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct mi_process process = process_create(argv[1]);
|
|
|
|
printf("pid of child = %d\n", process.pid);
|
|
|
|
|
|
|
|
size_t max_command_length = 1023;
|
|
|
|
int command_length = 0;
|
|
|
|
char *command = malloc(max_command_length + 1);
|
|
|
|
char *last_command = malloc(max_command_length + 1);
|
|
|
|
|
|
|
|
parse_debug_info(process.elf, &process.debug);
|
|
|
|
|
|
|
|
process.base_address = 0x555555554000UL; // get_executable_base_address(file, proc.pid);
|
|
|
|
process.main_address = get_address_of_subroutine(process, "main");
|
|
|
|
|
|
|
|
printf("Base address: %#lx\n", process.base_address);
|
|
|
|
printf("Main address: %#lx\n", process.main_address);
|
|
|
|
|
|
|
|
printf("> ");
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
parse_debug_line(process.elf, 0, 0, 0, &process.debug.sp_count, &process.source_file_count, &process.source_dirs_count);
|
|
|
|
process.debug.sp_table = malloc(process.debug.sp_count * sizeof(struct mi_sourcepoint));
|
|
|
|
process.source_directories = malloc(process.source_dirs_count * sizeof(char *));
|
|
|
|
process.source_files = malloc(process.source_file_count * sizeof(struct mi_sourcefile));
|
|
|
|
parse_debug_line(process.elf, process.debug.sp_table, process.source_files, process.source_directories, 0, 0, 0);
|
|
|
|
|
|
|
|
while ((command_length = getline(&command, &max_command_length, stdin))) {
|
|
|
|
if (command_length == 1) {
|
|
|
|
memset(command, 0, max_command_length);
|
|
|
|
memcpy(command, last_command, command_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 == strncmp(command, "exit\n", command_length) || 0 == strncmp(command, "q\n", command_length)) {
|
|
|
|
break;
|
|
|
|
} else if (0 == strncmp(command, "regs\n", command_length)) {
|
|
|
|
command_regs(process);
|
|
|
|
} else if (0 == strncmp(command, "step\n", command_length)) {
|
|
|
|
command_step(process);
|
|
|
|
} else if (0 == strncmp(command, "start\n", command_length)) {
|
|
|
|
command_start(process);
|
|
|
|
} else if (0 == strncmp(command, "next\n", command_length)) {
|
|
|
|
command_next(process);
|
|
|
|
} else if (0 == strncmp(command, "line\n", command_length)) {
|
|
|
|
command_list(process);
|
|
|
|
} else if (0 == strncmp(command, "cont\n", command_length)) {
|
|
|
|
command_cont(process);
|
|
|
|
} else if (0 == strncmp(command, "stop\n", command_length)) {
|
|
|
|
command_stop(process);
|
|
|
|
} else {
|
|
|
|
printf("Unknown command: %*s", command_length, command);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(last_command, 0, max_command_length);
|
|
|
|
memcpy(last_command, command, command_length);
|
|
|
|
|
|
|
|
printf("> ");
|
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|