2008-02-25 11:48:04 -06:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2005 by Dominic Rath *
|
|
|
|
* Dominic.Rath@gmx.de *
|
|
|
|
* *
|
2009-07-17 14:54:25 -05:00
|
|
|
* Copyright (C) 2007,2008 Øyvind Harboe *
|
2008-07-25 01:54:17 -05:00
|
|
|
* oyvind.harboe@zylin.com *
|
|
|
|
* *
|
2008-09-20 05:50:53 -05:00
|
|
|
* Copyright (C) 2008 by Spencer Oliver *
|
|
|
|
* spen@spen-soft.co.uk *
|
|
|
|
* *
|
2008-02-25 11:48:04 -06:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2008-03-13 10:46:40 -05:00
|
|
|
#include "time_support.h"
|
2009-05-11 00:50:21 -05:00
|
|
|
// @todo the inclusion of server.h here is a layering violation
|
2008-12-15 03:43:26 -06:00
|
|
|
#include "server.h"
|
2008-02-25 11:48:04 -06:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
2008-05-09 14:03:03 -05:00
|
|
|
|
2009-05-10 22:35:25 -05:00
|
|
|
#ifdef _DEBUG_FREE_SPACE_
|
|
|
|
#ifdef HAVE_MALLOC_H
|
2008-05-07 13:05:58 -05:00
|
|
|
#include <malloc.h>
|
2009-05-10 22:35:25 -05:00
|
|
|
#else
|
|
|
|
#error "malloc.h is required to use --enable-malloc-logging"
|
|
|
|
#endif
|
2008-05-08 13:07:37 -05:00
|
|
|
#endif
|
2008-02-25 11:48:04 -06:00
|
|
|
|
|
|
|
int debug_level = -1;
|
|
|
|
|
|
|
|
static FILE* log_output;
|
2009-11-13 05:00:51 -06:00
|
|
|
static struct log_callback *log_callbacks = NULL;
|
2008-02-25 11:48:04 -06:00
|
|
|
|
2008-07-18 05:20:10 -05:00
|
|
|
static long long last_time;
|
|
|
|
static long long current_time;
|
|
|
|
|
2008-03-13 03:17:01 -05:00
|
|
|
static long long start;
|
2008-02-25 11:48:04 -06:00
|
|
|
|
|
|
|
static char *log_strings[5] =
|
|
|
|
{
|
2008-12-19 13:40:38 -06:00
|
|
|
"User : ",
|
|
|
|
"Error: ",
|
|
|
|
"Warn : ", /* want a space after each colon, all same width, colons aligned */
|
|
|
|
"Info : ",
|
|
|
|
"Debug: "
|
2008-02-25 11:48:04 -06:00
|
|
|
};
|
|
|
|
|
2008-12-19 13:40:38 -06:00
|
|
|
|
2008-02-28 02:11:18 -06:00
|
|
|
static int count = 0;
|
|
|
|
|
2009-09-25 06:11:39 -05:00
|
|
|
|
|
|
|
static struct store_log_forward * log_head = NULL;
|
|
|
|
static int log_forward_count = 0;
|
|
|
|
|
|
|
|
struct store_log_forward
|
|
|
|
{
|
|
|
|
struct store_log_forward * next;
|
|
|
|
const char * file;
|
|
|
|
int line;
|
|
|
|
const char * function;
|
|
|
|
const char * string;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* either forward the log to the listeners or store it for possible forwarding later */
|
2009-11-15 05:42:45 -06:00
|
|
|
static void log_forward(const char *file, unsigned line, const char *function, const char *string)
|
2009-09-25 06:11:39 -05:00
|
|
|
{
|
|
|
|
if (log_forward_count==0)
|
|
|
|
{
|
2009-11-13 05:00:51 -06:00
|
|
|
struct log_callback *cb, *next;
|
2009-09-25 06:11:39 -05:00
|
|
|
cb = log_callbacks;
|
|
|
|
/* DANGER!!!! the log callback can remove itself!!!! */
|
|
|
|
while (cb)
|
|
|
|
{
|
|
|
|
next = cb->next;
|
|
|
|
cb->fn(cb->priv, file, line, function, string);
|
|
|
|
cb = next;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
struct store_log_forward *log = malloc(sizeof (struct store_log_forward));
|
|
|
|
log->file = strdup(file);
|
|
|
|
log->line = line;
|
|
|
|
log->function = strdup(function);
|
|
|
|
log->string = strdup(string);
|
|
|
|
log->next = NULL;
|
|
|
|
if (log_head==NULL)
|
|
|
|
log_head = log;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* append to tail */
|
|
|
|
struct store_log_forward * t;
|
|
|
|
t = log_head;
|
|
|
|
while (t->next!=NULL)
|
|
|
|
{
|
|
|
|
t = t->next;
|
|
|
|
}
|
|
|
|
t->next = log;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_try(void)
|
|
|
|
{
|
|
|
|
log_forward_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_catch(void)
|
|
|
|
{
|
|
|
|
assert(log_forward_count>0);
|
|
|
|
log_forward_count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_rethrow(void)
|
|
|
|
{
|
|
|
|
log_catch();
|
|
|
|
if (log_forward_count==0)
|
|
|
|
{
|
|
|
|
struct store_log_forward *log;
|
|
|
|
|
|
|
|
log = log_head;
|
|
|
|
while (log != NULL)
|
|
|
|
{
|
|
|
|
log_forward(log->file, log->line, log->function, log->string);
|
|
|
|
|
|
|
|
struct store_log_forward *t=log;
|
|
|
|
log = log->next;
|
|
|
|
|
|
|
|
free((void *)t->file);
|
|
|
|
free((void *)t->function);
|
|
|
|
free((void *)t->string);
|
|
|
|
free(t);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log_head = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-29 06:16:02 -06:00
|
|
|
/* The log_puts() serves to somewhat different goals:
|
2008-09-12 02:09:38 -05:00
|
|
|
*
|
2008-02-29 05:23:26 -06:00
|
|
|
* - logging
|
|
|
|
* - feeding low-level info to the user in GDB or Telnet
|
2008-09-12 02:09:38 -05:00
|
|
|
*
|
2008-02-29 05:23:26 -06:00
|
|
|
* The latter dictates that strings without newline are not logged, lest there
|
2008-09-12 02:09:38 -05:00
|
|
|
* will be *MANY log lines when sending one char at the time(e.g.
|
2008-02-29 05:23:26 -06:00
|
|
|
* target_request.c).
|
2008-09-12 02:09:38 -05:00
|
|
|
*
|
2008-02-29 05:23:26 -06:00
|
|
|
*/
|
2008-02-29 05:16:38 -06:00
|
|
|
static void log_puts(enum log_levels level, const char *file, int line, const char *function, const char *string)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
2008-04-09 00:55:23 -05:00
|
|
|
char *f;
|
2008-03-25 10:45:17 -05:00
|
|
|
if (level == LOG_LVL_OUTPUT)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
|
|
|
/* do not prepend any headers, just print out what we were given and return */
|
2008-02-29 05:16:38 -06:00
|
|
|
fputs(string, log_output);
|
2008-02-25 11:48:04 -06:00
|
|
|
fflush(log_output);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-09 00:55:23 -05:00
|
|
|
f = strrchr(file, '/');
|
2008-02-25 11:48:04 -06:00
|
|
|
if (f != NULL)
|
|
|
|
file = f + 1;
|
|
|
|
|
2009-06-23 17:38:12 -05:00
|
|
|
if (strchr(string, '\n') != NULL)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
if (debug_level >= LOG_LVL_DEBUG)
|
2008-02-29 05:23:26 -06:00
|
|
|
{
|
|
|
|
/* print with count and time information */
|
2009-06-23 17:42:54 -05:00
|
|
|
int t = (int)(timeval_ms()-start);
|
2009-05-10 22:35:25 -05:00
|
|
|
#ifdef _DEBUG_FREE_SPACE_
|
2008-05-07 13:05:58 -05:00
|
|
|
struct mallinfo info;
|
|
|
|
info = mallinfo();
|
2008-05-08 13:07:37 -05:00
|
|
|
#endif
|
2008-12-19 13:40:38 -06:00
|
|
|
fprintf(log_output, "%s%d %d %s:%d %s()"
|
2009-05-10 22:35:25 -05:00
|
|
|
#ifdef _DEBUG_FREE_SPACE_
|
2008-05-08 13:07:37 -05:00
|
|
|
" %d"
|
|
|
|
#endif
|
2009-06-23 17:44:17 -05:00
|
|
|
": %s", log_strings[level + 1], count, t, file, line, function,
|
2009-05-10 22:35:25 -05:00
|
|
|
#ifdef _DEBUG_FREE_SPACE_
|
2008-05-08 13:07:37 -05:00
|
|
|
info.fordblks,
|
|
|
|
#endif
|
|
|
|
string);
|
2008-02-29 05:23:26 -06:00
|
|
|
}
|
2009-06-23 17:35:09 -05:00
|
|
|
else if (server_use_pipes == 0)
|
2008-02-29 05:23:26 -06:00
|
|
|
{
|
2008-12-15 03:43:26 -06:00
|
|
|
/* if we are using gdb through pipes then we do not want any output
|
|
|
|
* to the pipe otherwise we get repeated strings */
|
|
|
|
if (strcmp(string, "\n") != 0)
|
2008-08-13 07:13:18 -05:00
|
|
|
{
|
|
|
|
/* print human readable output - but skip empty lines */
|
|
|
|
fprintf(log_output, "%s%s",
|
2009-06-23 17:44:17 -05:00
|
|
|
(level > LOG_LVL_USER)?log_strings[level + 1]:"", string);
|
2008-08-13 07:13:18 -05:00
|
|
|
}
|
2008-02-29 05:23:26 -06:00
|
|
|
}
|
|
|
|
} else
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
2008-09-12 02:09:38 -05:00
|
|
|
/* only entire lines are logged. Otherwise it's
|
2008-02-29 05:23:26 -06:00
|
|
|
* single chars intended for the log callbacks. */
|
2008-02-25 11:48:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fflush(log_output);
|
2008-09-12 02:09:38 -05:00
|
|
|
|
2008-03-25 10:45:17 -05:00
|
|
|
/* Never forward LOG_LVL_DEBUG, too verbose and they can be found in the log if need be */
|
|
|
|
if (level <= LOG_LVL_INFO)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
2009-09-25 06:11:39 -05:00
|
|
|
log_forward(file, line, function, string);
|
2008-02-25 11:48:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-25 06:11:39 -05:00
|
|
|
|
2009-11-15 05:42:45 -06:00
|
|
|
void log_printf(enum log_levels level, const char *file, unsigned line, const char *function, const char *format, ...)
|
2008-02-28 02:11:18 -06:00
|
|
|
{
|
2008-02-29 05:16:38 -06:00
|
|
|
char *string;
|
2008-04-09 00:55:23 -05:00
|
|
|
va_list ap;
|
2008-02-29 05:16:38 -06:00
|
|
|
|
2008-02-28 02:11:18 -06:00
|
|
|
count++;
|
|
|
|
if (level > debug_level)
|
|
|
|
return;
|
|
|
|
|
2008-02-29 05:16:38 -06:00
|
|
|
va_start(ap, format);
|
|
|
|
|
2008-03-07 04:55:59 -06:00
|
|
|
string = alloc_vprintf(format, ap);
|
2008-02-29 05:16:38 -06:00
|
|
|
if (string != NULL)
|
|
|
|
{
|
|
|
|
log_puts(level, file, line, function, string);
|
|
|
|
free(string);
|
|
|
|
}
|
2008-09-12 02:09:38 -05:00
|
|
|
|
2008-02-29 05:16:38 -06:00
|
|
|
va_end(ap);
|
2008-02-28 02:11:18 -06:00
|
|
|
}
|
|
|
|
|
2009-11-15 05:42:45 -06:00
|
|
|
void log_printf_lf(enum log_levels level, const char *file, unsigned line, const char *function, const char *format, ...)
|
2008-02-28 02:11:18 -06:00
|
|
|
{
|
2008-02-29 05:16:38 -06:00
|
|
|
char *string;
|
2008-04-09 00:55:23 -05:00
|
|
|
va_list ap;
|
2008-02-29 05:16:38 -06:00
|
|
|
|
2008-02-28 02:11:18 -06:00
|
|
|
count++;
|
|
|
|
if (level > debug_level)
|
|
|
|
return;
|
2008-09-12 02:09:38 -05:00
|
|
|
|
2008-02-29 05:16:38 -06:00
|
|
|
va_start(ap, format);
|
2008-09-12 02:09:38 -05:00
|
|
|
|
2008-03-07 04:55:59 -06:00
|
|
|
string = alloc_vprintf(format, ap);
|
2008-02-29 05:16:38 -06:00
|
|
|
if (string != NULL)
|
|
|
|
{
|
2008-03-07 04:55:59 -06:00
|
|
|
strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one char longer */
|
2008-02-29 05:16:38 -06:00
|
|
|
log_puts(level, file, line, function, string);
|
|
|
|
free(string);
|
|
|
|
}
|
2008-09-12 02:09:38 -05:00
|
|
|
|
2008-02-29 05:16:38 -06:00
|
|
|
va_end(ap);
|
2008-02-28 02:11:18 -06:00
|
|
|
}
|
|
|
|
|
2008-02-25 11:48:04 -06:00
|
|
|
/* change the current debug level on the fly
|
|
|
|
* 0: only ERRORS
|
|
|
|
* 1: + WARNINGS
|
|
|
|
* 2: + INFORMATIONAL MSGS
|
|
|
|
* 3: + DEBUG MSGS
|
|
|
|
*/
|
2009-11-10 01:56:52 -06:00
|
|
|
COMMAND_HANDLER(handle_debug_level_command)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
2009-11-15 06:57:12 -06:00
|
|
|
if (CMD_ARGC == 1)
|
2009-06-11 20:39:57 -05:00
|
|
|
{
|
|
|
|
unsigned new_level;
|
2009-11-15 10:15:59 -06:00
|
|
|
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], new_level);
|
2009-06-11 20:39:57 -05:00
|
|
|
debug_level = MIN(new_level, LOG_LVL_DEBUG);
|
|
|
|
}
|
2009-11-15 06:57:12 -06:00
|
|
|
else if (CMD_ARGC > 1)
|
2009-06-11 20:39:57 -05:00
|
|
|
return ERROR_COMMAND_SYNTAX_ERROR;
|
2008-02-25 11:48:04 -06:00
|
|
|
|
2008-12-15 03:43:26 -06:00
|
|
|
if (debug_level >= LOG_LVL_DEBUG && server_use_pipes == 1)
|
|
|
|
{
|
|
|
|
/* if we are enabling debug info then we need to write to a log file
|
|
|
|
* otherwise the pipe will get full and cause issues with gdb */
|
|
|
|
FILE* file = fopen("openocd.log", "w");
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
log_output = file;
|
|
|
|
LOG_WARNING("enabling log output as we are using pipes");
|
|
|
|
}
|
|
|
|
}
|
2008-12-19 13:40:38 -06:00
|
|
|
|
2009-11-15 07:57:37 -06:00
|
|
|
command_print(CMD_CTX, "debug_level: %i", debug_level);
|
2009-06-21 16:20:10 -05:00
|
|
|
|
2008-02-25 11:48:04 -06:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-10 01:56:52 -06:00
|
|
|
COMMAND_HANDLER(handle_log_output_command)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
2009-11-15 06:57:12 -06:00
|
|
|
if (CMD_ARGC == 1)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
2009-11-15 10:15:59 -06:00
|
|
|
FILE* file = fopen(CMD_ARGV[0], "w");
|
2008-09-12 02:09:38 -05:00
|
|
|
|
2008-02-25 11:48:04 -06:00
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
log_output = file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-20 17:52:18 -06:00
|
|
|
static struct command_registration log_command_handlers[] = {
|
|
|
|
{
|
|
|
|
.name = "log_output",
|
|
|
|
.handler = &handle_log_output_command,
|
|
|
|
.mode = COMMAND_ANY,
|
|
|
|
.help = "redirect logging to a file (default: stderr)",
|
|
|
|
.usage = "<file_name>",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "debug_level",
|
|
|
|
.handler = &handle_debug_level_command,
|
|
|
|
.mode = COMMAND_ANY,
|
|
|
|
.help = "sets the verbosity level of debugging output",
|
|
|
|
.usage = "<level:0-3>",
|
|
|
|
},
|
|
|
|
COMMAND_REGISTRATION_DONE
|
|
|
|
};
|
|
|
|
|
2009-11-13 15:25:47 -06:00
|
|
|
int log_register_commands(struct command_context *cmd_ctx)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
2008-03-13 03:17:01 -05:00
|
|
|
start = timeval_ms();
|
2008-02-25 11:48:04 -06:00
|
|
|
|
2009-11-20 17:52:18 -06:00
|
|
|
return register_commands(cmd_ctx, NULL, log_command_handlers);
|
2008-02-25 11:48:04 -06:00
|
|
|
}
|
|
|
|
|
2009-11-13 15:25:47 -06:00
|
|
|
int log_init(struct command_context *cmd_ctx)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
|
|
|
/* set defaults for daemon configuration, if not set by cmdline or cfgfile */
|
|
|
|
if (debug_level == -1)
|
2008-03-25 10:45:17 -05:00
|
|
|
debug_level = LOG_LVL_INFO;
|
2008-09-12 02:09:38 -05:00
|
|
|
|
2008-02-25 11:48:04 -06:00
|
|
|
if (log_output == NULL)
|
|
|
|
{
|
|
|
|
log_output = stderr;
|
|
|
|
}
|
2008-09-12 02:09:38 -05:00
|
|
|
|
2009-06-23 17:42:54 -05:00
|
|
|
start = last_time = timeval_ms();
|
2008-09-12 02:09:38 -05:00
|
|
|
|
2008-02-25 11:48:04 -06:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
2008-09-12 02:09:38 -05:00
|
|
|
|
2009-11-13 15:25:47 -06:00
|
|
|
int set_log_output(struct command_context *cmd_ctx, FILE *output)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
|
|
|
log_output = output;
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add/remove log callback handler */
|
|
|
|
int log_add_callback(log_callback_fn fn, void *priv)
|
|
|
|
{
|
2009-11-13 05:00:51 -06:00
|
|
|
struct log_callback *cb;
|
2008-02-25 11:48:04 -06:00
|
|
|
|
|
|
|
/* prevent the same callback to be registered more than once, just for sure */
|
|
|
|
for (cb = log_callbacks; cb; cb = cb->next)
|
|
|
|
{
|
|
|
|
if (cb->fn == fn && cb->priv == priv)
|
|
|
|
return ERROR_INVALID_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* alloc memory, it is safe just to return in case of an error, no need for the caller to check this */
|
2009-11-13 05:00:51 -06:00
|
|
|
if ((cb = malloc(sizeof(struct log_callback))) == NULL)
|
2008-02-25 11:48:04 -06:00
|
|
|
return ERROR_BUF_TOO_SMALL;
|
|
|
|
|
|
|
|
/* add item to the beginning of the linked list */
|
|
|
|
cb->fn = fn;
|
|
|
|
cb->priv = priv;
|
|
|
|
cb->next = log_callbacks;
|
|
|
|
log_callbacks = cb;
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int log_remove_callback(log_callback_fn fn, void *priv)
|
|
|
|
{
|
2009-11-13 05:00:51 -06:00
|
|
|
struct log_callback *cb, **p;
|
2008-02-25 11:48:04 -06:00
|
|
|
|
2008-02-25 14:18:26 -06:00
|
|
|
for (p = &log_callbacks; (cb = *p); p = &(*p)->next)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
2008-02-29 06:16:02 -06:00
|
|
|
if (cb->fn == fn && cb->priv == priv)
|
|
|
|
{
|
|
|
|
*p = cb->next;
|
2008-02-25 11:48:04 -06:00
|
|
|
free(cb);
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no such item */
|
|
|
|
return ERROR_INVALID_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return allocated string w/printf() result */
|
2008-03-07 04:55:59 -06:00
|
|
|
char *alloc_vprintf(const char *fmt, va_list ap)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
2009-11-24 14:24:06 -06:00
|
|
|
va_list ap_copy;
|
|
|
|
int len;
|
|
|
|
char *string;
|
2008-03-06 06:01:17 -06:00
|
|
|
|
2009-11-24 14:24:06 -06:00
|
|
|
/* determine the length of the buffer needed */
|
|
|
|
va_copy(ap_copy, ap);
|
|
|
|
len = vsnprintf(NULL, 0, fmt, ap_copy);
|
|
|
|
va_end(ap_copy);
|
2008-03-06 06:01:17 -06:00
|
|
|
|
2009-11-24 14:24:06 -06:00
|
|
|
/* allocate and make room for terminating zero. */
|
|
|
|
/* FIXME: The old version always allocated at least one byte extra and
|
|
|
|
* other code depend on that. They should be probably be fixed, but for
|
|
|
|
* now reserve the extra byte. */
|
|
|
|
string = malloc(len + 2);
|
|
|
|
if (string == NULL)
|
|
|
|
return NULL;
|
2008-02-29 05:16:38 -06:00
|
|
|
|
2009-11-24 14:24:06 -06:00
|
|
|
/* do the real work */
|
|
|
|
vsnprintf(string, len + 1, fmt, ap);
|
2008-09-12 02:09:38 -05:00
|
|
|
|
2008-02-29 06:16:02 -06:00
|
|
|
return string;
|
2008-02-25 11:48:04 -06:00
|
|
|
}
|
2008-03-07 04:55:59 -06:00
|
|
|
|
|
|
|
char *alloc_printf(const char *format, ...)
|
|
|
|
{
|
|
|
|
char *string;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
string = alloc_vprintf(format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return string;
|
|
|
|
}
|
2008-07-18 05:20:10 -05:00
|
|
|
|
|
|
|
/* Code must return to the server loop before 1000ms has returned or invoke
|
|
|
|
* this function.
|
2008-09-12 02:09:38 -05:00
|
|
|
*
|
2008-07-18 05:20:10 -05:00
|
|
|
* The GDB connection will time out if it spends >2000ms and you'll get nasty
|
|
|
|
* error messages from GDB:
|
2008-09-12 02:09:38 -05:00
|
|
|
*
|
2008-07-18 05:20:10 -05:00
|
|
|
* Ignoring packet error, continuing...
|
|
|
|
* Reply contains invalid hex digit 116
|
|
|
|
*
|
|
|
|
* While it is possible use "set remotetimeout" to more than the default 2000ms
|
|
|
|
* in GDB, OpenOCD guarantees that it sends keep-alive packages on the
|
|
|
|
* GDB protocol and it is a bug in OpenOCD not to either return to the server
|
|
|
|
* loop or invoke keep_alive() every 1000ms.
|
2008-09-12 02:09:38 -05:00
|
|
|
*
|
2008-07-18 05:20:10 -05:00
|
|
|
* This function will send a keep alive packet if >500ms has passed since last time
|
|
|
|
* it was invoked.
|
2008-09-12 02:09:38 -05:00
|
|
|
*
|
2008-09-08 02:17:48 -05:00
|
|
|
* Note that this function can be invoked often, so it needs to be relatively
|
|
|
|
* fast when invoked more often than every 500ms.
|
2008-09-12 02:09:38 -05:00
|
|
|
*
|
2008-07-18 05:20:10 -05:00
|
|
|
*/
|
|
|
|
void keep_alive()
|
|
|
|
{
|
2009-06-23 17:42:54 -05:00
|
|
|
current_time = timeval_ms();
|
2009-06-23 17:45:47 -05:00
|
|
|
if (current_time-last_time > 1000)
|
2008-07-18 05:20:10 -05:00
|
|
|
{
|
2009-05-27 15:30:17 -05:00
|
|
|
extern int gdb_actual_connections;
|
|
|
|
|
|
|
|
if (gdb_actual_connections)
|
|
|
|
LOG_WARNING("keep_alive() was not invoked in the "
|
|
|
|
"1000ms timelimit. GDB alive packet not "
|
|
|
|
"sent! (%lld). Workaround: increase "
|
|
|
|
"\"set remotetimeout\" in GDB",
|
|
|
|
current_time-last_time);
|
|
|
|
else
|
|
|
|
LOG_DEBUG("keep_alive() was not invoked in the "
|
|
|
|
"1000ms timelimit (%lld). This may cause "
|
|
|
|
"trouble with GDB connections.",
|
|
|
|
current_time-last_time);
|
2008-09-12 02:09:38 -05:00
|
|
|
}
|
2009-06-23 17:45:47 -05:00
|
|
|
if (current_time-last_time > 500)
|
2008-07-18 05:20:10 -05:00
|
|
|
{
|
|
|
|
/* this will keep the GDB connection alive */
|
|
|
|
LOG_USER_N("%s", "");
|
2008-09-08 02:17:48 -05:00
|
|
|
|
2008-09-12 02:09:38 -05:00
|
|
|
/* DANGER!!!! do not add code to invoke e.g. target event processing,
|
|
|
|
* jim timer processing, etc. it can cause infinite recursion +
|
|
|
|
* jim event callbacks need to happen at a well defined time,
|
|
|
|
* not anywhere keep_alive() is invoked.
|
|
|
|
*
|
|
|
|
* These functions should be invoked at a well defined spot in server.c
|
|
|
|
*/
|
2008-09-08 02:17:48 -05:00
|
|
|
|
2009-06-23 17:42:54 -05:00
|
|
|
last_time = current_time;
|
2008-07-18 05:20:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset keep alive timer without sending message */
|
|
|
|
void kept_alive()
|
|
|
|
{
|
2009-06-23 17:42:54 -05:00
|
|
|
current_time = timeval_ms();
|
|
|
|
last_time = current_time;
|
2008-07-18 05:20:10 -05:00
|
|
|
}
|
2008-08-19 07:17:31 -05:00
|
|
|
|
|
|
|
/* if we sleep for extended periods of time, we must invoke keep_alive() intermittantly */
|
2009-11-15 05:42:45 -06:00
|
|
|
void alive_sleep(uint64_t ms)
|
2008-08-19 07:17:31 -05:00
|
|
|
{
|
2009-11-15 05:42:45 -06:00
|
|
|
uint64_t napTime = 10;
|
|
|
|
for (uint64_t i = 0; i < ms; i += napTime)
|
2008-08-19 07:17:31 -05:00
|
|
|
{
|
2009-11-15 05:42:45 -06:00
|
|
|
uint64_t sleep_a_bit = ms - i;
|
2009-06-23 17:45:47 -05:00
|
|
|
if (sleep_a_bit > napTime)
|
2009-06-23 17:42:54 -05:00
|
|
|
sleep_a_bit = napTime;
|
2009-11-15 05:42:45 -06:00
|
|
|
|
|
|
|
usleep(sleep_a_bit * 1000);
|
2008-08-19 07:17:31 -05:00
|
|
|
keep_alive();
|
|
|
|
}
|
|
|
|
}
|
2008-10-09 03:08:29 -05:00
|
|
|
|
2009-11-15 05:42:45 -06:00
|
|
|
void busy_sleep(uint64_t ms)
|
2008-10-09 03:08:29 -05:00
|
|
|
{
|
2009-11-15 05:42:45 -06:00
|
|
|
uint64_t then = timeval_ms();
|
|
|
|
while (timeval_ms() - then < ms)
|
2008-10-09 03:08:29 -05:00
|
|
|
{
|
|
|
|
/* busy wait */
|
|
|
|
}
|
|
|
|
}
|