debugger.c (2649B)
1 /* debugger.c 2 * 3 * Copyright 2011 Brian Swetland <swetland@frotz.net> 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <stdarg.h> 21 #include <unistd.h> 22 #include <string.h> 23 #include <signal.h> 24 #include <fcntl.h> 25 #include <getopt.h> 26 27 #include <fw/types.h> 28 #include "rswdp.h" 29 30 #include "linenoise.h" 31 #include "debugger.h" 32 33 unsigned log_flags = 0; 34 35 void linenoiseInit(void); 36 37 static const char *scriptfile = NULL; 38 39 void gdb_console_puts(const char *msg); 40 41 void xprintf(xpchan ch, const char *fmt, ...) { 42 char line[256]; 43 va_list ap; 44 va_start(ap, fmt); 45 vsnprintf(line, 256, fmt, ap); 46 va_end(ap); 47 linenoisePause(); 48 if (write(1, line, strlen(line)) < 0) ; 49 linenoiseResume(); 50 gdb_console_puts(line); 51 } 52 53 static void handler(int n) { 54 swdp_interrupt(); 55 } 56 57 static void usage(int argc, char **argv) { 58 fprintf(stderr, "usage: %s [-h] [-f script]\n", argv[0]); 59 60 exit(1); 61 } 62 63 int main(int argc, char **argv) { 64 char lastline[1024]; 65 char *line; 66 67 /* args */ 68 for(;;) { 69 int c; 70 int option_index = 0; 71 72 static struct option long_options[] = { 73 {"help", 0, 0, 'h'}, 74 {"script", 1, 0, 'f'}, 75 {0, 0, 0, 0}, 76 }; 77 78 c = getopt_long(argc, argv, "f:h", long_options, &option_index); 79 if(c == -1) 80 break; 81 82 switch(c) { 83 case 'f': 84 scriptfile = optarg; 85 break; 86 case 'h': 87 usage(argc, argv); 88 break; 89 default: 90 usage(argc, argv); 91 break; 92 } 93 } 94 95 lastline[0] = 0; 96 97 if (swdp_open()) { 98 fprintf(stderr,"could not find device\n"); 99 return -1; 100 } 101 102 signal(SIGINT, handler); 103 104 // swdp_enable_tracing(1); 105 106 /* 107 * if the user passed in a script file, pass it to the debug 108 * command handler before starting the main loop 109 */ 110 if (scriptfile != NULL) { 111 char *buf = malloc(sizeof("script ") + strlen(scriptfile) + 1); 112 113 strcpy(buf, "script "); 114 strcat(buf, scriptfile); 115 116 debugger_command(buf); 117 118 free(buf); 119 } 120 121 linenoiseInit(); 122 debugger_init(); 123 124 while ((line = linenoise("debugger> ")) != NULL) { 125 if (line[0] == 0) { 126 strcpy(line, lastline); 127 } else { 128 linenoiseHistoryAdd(line); 129 strcpy(lastline, line); 130 } 131 debugger_command(line); 132 } 133 return 0; 134 }