diff --git a/src/helper/command.c b/src/helper/command.c index 15cddcdc4..3d496b688 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -260,7 +260,7 @@ int parse_line(char *line, char *words[], int max_words) return nwords; } -void command_print(command_context_t *context, char *format, ...) +static void command_printv(command_context_t *context, char *format, va_list ap) { char *buffer = NULL; int n, size = 0; @@ -269,8 +269,6 @@ void command_print(command_context_t *context, char *format, ...) /* process format string */ for (;;) { - va_list ap; - va_start(ap, format); if (!buffer || (n = vsnprintf(buffer, size, format, ap)) >= size) { /* increase buffer until it fits the whole string */ @@ -279,16 +277,13 @@ void command_print(command_context_t *context, char *format, ...) /* gotta free up */ if (buffer) free(buffer); - va_end(ap); return; } buffer = p; - va_end(ap); continue; } - va_end(ap); break; } @@ -300,25 +295,33 @@ void command_print(command_context_t *context, char *format, ...) return; } - p = buffer; - - /* process lines in buffer */ - do { - char *next = strchr(p, '\n'); - - if (next) - *next++ = 0; - - if (context->output_handler) - context->output_handler(context, p); - - p = next; - } while (p); + context->output_handler(context, buffer); if (buffer) free(buffer); } +void command_print_sameline(command_context_t *context, char *format, ...) +{ + va_list ap; + va_start(ap, format); + command_printv(context, format, ap); + va_end(ap); +} + +void command_print(command_context_t *context, char *format, ...) +{ + char *t=malloc(strlen(format)+2); + strcpy(t, format); + strcat(t, "\n"); + va_list ap; + va_start(ap, format); + command_printv(context, t, ap); + va_end(ap); + free(t); + +} + int find_and_run_command(command_context_t *context, command_t *commands, char *words[], int num_words, int start_word) { command_t *c; @@ -396,10 +399,7 @@ int command_run_line(command_context_t *context, char *line) if (*line && (line[0] == '#')) return ERROR_OK; - if (context->echo) - { - command_print(context, "%s", line); - } + DEBUG("%s", line); nwords = parse_line(line, words, sizeof(words) / sizeof(words[0])); @@ -550,7 +550,6 @@ command_context_t* command_init() context->mode = COMMAND_EXEC; context->commands = NULL; context->current_target = 0; - context->echo = 0; context->output_handler = NULL; context->output_handler_priv = NULL; diff --git a/src/helper/command.h b/src/helper/command.h index de90d4ae4..3acb8b182 100644 --- a/src/helper/command.h +++ b/src/helper/command.h @@ -34,7 +34,6 @@ typedef struct command_context_s enum command_mode mode; struct command_s *commands; int current_target; - int echo; int (*output_handler)(struct command_context_s *context, char* line); void *output_handler_priv; } command_context_t; @@ -58,6 +57,7 @@ extern command_context_t* copy_command_context(command_context_t* context); extern command_context_t* command_init(); extern int command_done(command_context_t *context); extern void command_print(command_context_t *context, char *format, ...); +extern void command_print_sameline(command_context_t *context, char *format, ...); extern int command_run_line(command_context_t *context, char *line); extern int command_run_file(command_context_t *context, FILE *file, enum command_mode mode); diff --git a/src/helper/interpreter.c b/src/helper/interpreter.c index aa331fda1..abf7b0938 100644 --- a/src/helper/interpreter.c +++ b/src/helper/interpreter.c @@ -217,7 +217,6 @@ int handle_field_command(struct command_context_s *cmd_ctx, char *cmd, char **ar int handle_script_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc) { FILE *script_file; - int echo; if (argc != 1) return ERROR_COMMAND_SYNTAX_ERROR; @@ -229,14 +228,9 @@ int handle_script_command(struct command_context_s *cmd_ctx, char *cmd, char **a command_print(cmd_ctx, "couldn't open script file %s", args[0]); return ERROR_OK; } - - echo = cmd_ctx->echo; - cmd_ctx->echo = 1; command_run_file(cmd_ctx, script_file, cmd_ctx->mode); - cmd_ctx->echo = echo; - fclose(script_file); return ERROR_OK; diff --git a/src/helper/log.c b/src/helper/log.c index a5432aa9c..c55fd5723 100644 --- a/src/helper/log.c +++ b/src/helper/log.c @@ -46,20 +46,14 @@ static char *log_strings[5] = "Debug: " }; -void log_printf(enum log_levels level, const char *file, int line, const char *function, const char *format, ...) +static int count = 0; + +static void log_printfv(enum log_levels level, const char *file, int line, const char *function, const char *format, va_list args) { - static int count = 0; - count++; - va_list args; char buffer[512]; log_callback_t *cb; - if (level > debug_level) - return; - - va_start(args, format); vsnprintf(buffer, 512, format, args); - va_end(args); if (level == LOG_OUTPUT) { @@ -76,8 +70,8 @@ void log_printf(enum log_levels level, const char *file, int line, const char *f if (debug_level >= LOG_DEBUG) { /* print with count and time information */ - time_t t=time(NULL)-start; - fprintf(log_output, "%s %d %ld %s:%d %s(): %s\n", log_strings[level+1], count, t, file, line, function, buffer); + int t=(int)(time(NULL)-start); + fprintf(log_output, "%s %d %d %s:%d %s(): %s\n", log_strings[level+1], count, t, file, line, function, buffer); } else { @@ -92,13 +86,40 @@ void log_printf(enum log_levels level, const char *file, int line, const char *f { for (cb = log_callbacks; cb; cb = cb->next) { - va_start(args, format); cb->fn(cb->priv, file, line, function, format, args); - va_end(args); } } } +void log_printf(enum log_levels level, const char *file, int line, const char *function, const char *format, ...) +{ + count++; + if (level > debug_level) + return; + + va_list args; + va_start(args, format); + log_printfv(level, file, line, function, format, args); + va_end(args); + +} + +void log_printfnl(enum log_levels level, const char *file, int line, const char *function, const char *format, ...) +{ + count++; + if (level > debug_level) + return; + + char *t=malloc(strlen(format)+2); + strcpy(t, format); + strcat(t, "\n"); + + va_list args; + va_start(args, format); + log_printfv(level, file, line, function, t, args); + va_end(args); +} + /* change the current debug level on the fly * 0: only ERRORS * 1: + WARNINGS diff --git a/src/helper/log.h b/src/helper/log.h index 6c7c3de68..48b43d8ec 100644 --- a/src/helper/log.h +++ b/src/helper/log.h @@ -46,7 +46,10 @@ enum log_levels extern void log_printf(enum log_levels level, const char *file, int line, const char *function, const char *format, ...) - __attribute__ ((format (printf, 5, 6))); +__attribute__ ((format (printf, 5, 6))); +extern void log_printfnl(enum log_levels level, const char *file, int line, + const char *function, const char *format, ...) +__attribute__ ((format (printf, 5, 6))); extern int log_register_commands(struct command_context_s *cmd_ctx); extern int log_init(struct command_context_s *cmd_ctx); extern int set_log_output(struct command_context_s *cmd_ctx, FILE *output); @@ -71,34 +74,40 @@ extern int debug_level; /* Avoid fn call and building parameter list if we're not outputting the information. * Matters on feeble CPUs for DEBUG/INFO statements that are involved frequently */ + #define DEBUG(expr ...) \ do { if (debug_level >= LOG_DEBUG) \ - log_printf (LOG_DEBUG, __FILE__, __LINE__, __FUNCTION__, expr); \ + log_printfnl (LOG_DEBUG, __FILE__, __LINE__, __FUNCTION__, expr); \ } while(0) #define INFO(expr ...) \ do { if (debug_level >= LOG_INFO) \ - log_printf (LOG_INFO, __FILE__, __LINE__, __FUNCTION__, expr); \ + log_printfnl (LOG_INFO, __FILE__, __LINE__, __FUNCTION__, expr); \ } while(0) #define WARNING(expr ...) \ do { \ - log_printf (LOG_WARNING, __FILE__, __LINE__, __FUNCTION__, expr); \ + log_printfnl (LOG_WARNING, __FILE__, __LINE__, __FUNCTION__, expr); \ } while(0) #define ERROR(expr ...) \ do { \ - log_printf (LOG_ERROR, __FILE__, __LINE__, __FUNCTION__, expr); \ + log_printfnl (LOG_ERROR, __FILE__, __LINE__, __FUNCTION__, expr); \ } while(0) #define USER(expr ...) \ + do { \ + log_printfnl (LOG_USER, __FILE__, __LINE__, __FUNCTION__, expr); \ + } while(0) + +#define USER_SAMELINE(expr ...) \ do { \ log_printf (LOG_USER, __FILE__, __LINE__, __FUNCTION__, expr); \ } while(0) #define OUTPUT(expr ...) \ do { \ - log_printf (LOG_OUTPUT, __FILE__, __LINE__, __FUNCTION__, expr); \ + log_printfnl (LOG_OUTPUT, __FILE__, __LINE__, __FUNCTION__, expr); \ } while(0) diff --git a/src/server/gdb_server.c b/src/server/gdb_server.c index 335a4cd2c..6c9936eec 100644 --- a/src/server/gdb_server.c +++ b/src/server/gdb_server.c @@ -511,16 +511,14 @@ int gdb_output_con(connection_t *connection, char* line) bin_size = strlen(line); - hex_buffer = malloc(bin_size*2 + 4); + hex_buffer = malloc(bin_size*2 + 2); hex_buffer[0] = 'O'; for (i=0; i> 8, (request & 0xffff0000) >> 16); } break; + case TARGET_REQ_DEBUGCHAR: + target_charmsg(target, (request & 0x00ff0000) >> 16); + break; /* case TARGET_REQ_SEMIHOSTING: * break; */ diff --git a/src/target/target_request.h b/src/target/target_request.h index 6aacaed7a..0547c1d3b 100644 --- a/src/target/target_request.h +++ b/src/target/target_request.h @@ -26,6 +26,7 @@ typedef enum target_req_cmd { TARGET_REQ_TRACEMSG, TARGET_REQ_DEBUGMSG, + TARGET_REQ_DEBUGCHAR, /* TARGET_REQ_SEMIHOSTING, */ } target_req_cmd_t;