mdebug

cortex m series debugger
git clone http://frotz.net/git/mdebug.git
Log | Files | Refs | README | LICENSE

debugger.c (2723B)


      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 			{"pico", 0, 0, 'p'},
     76 			{0, 0, 0, 0},
     77 		};
     78 
     79 		c = getopt_long(argc, argv, "f:h", long_options, &option_index);
     80 		if(c == -1)
     81 			break;
     82 
     83 		switch(c) {
     84 			case 'f':
     85 				scriptfile = optarg;
     86 				break;
     87 			case 'h':
     88 				usage(argc, argv);
     89 				break;
     90 			case 'p':
     91 				debug_target("pico");
     92 				break;
     93 			default:
     94 				usage(argc, argv);
     95 				break;
     96 		}
     97 	}
     98 
     99 	lastline[0] = 0;
    100 
    101 	if (swdp_open()) {
    102 		fprintf(stderr,"could not find device\n");
    103 		return -1;
    104 	}
    105 
    106 	signal(SIGINT, handler);
    107 
    108 //	swdp_enable_tracing(1);
    109 
    110 	/*
    111 	 * if the user passed in a script file, pass it to the debug
    112 	 * command handler before starting the main loop
    113 	 */
    114 	if (scriptfile != NULL) {
    115 		char *buf = malloc(sizeof("script ") + strlen(scriptfile) + 1);
    116 
    117 		strcpy(buf, "script ");
    118 		strcat(buf, scriptfile);
    119 
    120 		debugger_command(buf);
    121 
    122 		free(buf);
    123 	}
    124 
    125 	linenoiseInit();
    126 	debugger_init();
    127 
    128 	while ((line = linenoise("debugger> ")) != NULL) {
    129 		if (line[0] == 0) {
    130 			strcpy(line, lastline);
    131 		} else {
    132 			linenoiseHistoryAdd(line);
    133 			strcpy(lastline, line);
    134 		}
    135 		debugger_command(line);
    136 	}
    137 	return 0;
    138 }