1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
25 ***************************************************************************/
33 #include "time_support.h"
37 #ifdef _DEBUG_FREE_SPACE_
41 #error "malloc.h is required to use --enable-malloc-logging"
47 static FILE *log_output;
48 static struct log_callback *log_callbacks;
50 static long long last_time;
51 static long long current_time;
53 static long long start;
55 static const char * const log_strings[5] = {
58 "Warn : ", /* want a space after each colon, all same width, colons aligned */
65 static struct store_log_forward *log_head;
66 static int log_forward_count;
68 struct store_log_forward {
69 struct store_log_forward *next;
76 /* either forward the log to the listeners or store it for possible forwarding later */
77 static void log_forward(const char *file, unsigned line, const char *function, const char *string)
79 if (log_forward_count == 0) {
80 struct log_callback *cb, *next;
82 /* DANGER!!!! the log callback can remove itself!!!! */
85 cb->fn(cb->priv, file, line, function, string);
89 struct store_log_forward *log = malloc(sizeof(struct store_log_forward));
90 log->file = strdup(file);
92 log->function = strdup(function);
93 log->string = strdup(string);
99 struct store_log_forward *t;
101 while (t->next != NULL)
108 /* The log_puts() serves to somewhat different goals:
111 * - feeding low-level info to the user in GDB or Telnet
113 * The latter dictates that strings without newline are not logged, lest there
114 * will be *MANY log lines when sending one char at the time(e.g.
118 static void log_puts(enum log_levels level,
121 const char *function,
125 if (level == LOG_LVL_OUTPUT) {
126 /* do not prepend any headers, just print out what we were given and return */
127 fputs(string, log_output);
132 f = strrchr(file, '/');
136 if (strlen(string) > 0) {
137 if (debug_level >= LOG_LVL_DEBUG) {
138 /* print with count and time information */
139 int t = (int)(timeval_ms()-start);
140 #ifdef _DEBUG_FREE_SPACE_
141 struct mallinfo info;
144 fprintf(log_output, "%s%d %d %s:%d %s()"
145 #ifdef _DEBUG_FREE_SPACE_
148 ": %s", log_strings[level + 1], count, t, file, line, function,
149 #ifdef _DEBUG_FREE_SPACE_
154 /* if we are using gdb through pipes then we do not want any output
155 * to the pipe otherwise we get repeated strings */
156 fprintf(log_output, "%s%s",
157 (level > LOG_LVL_USER) ? log_strings[level + 1] : "", string);
160 /* Empty strings are sent to log callbacks to keep e.g. gdbserver alive, here we do
166 /* Never forward LOG_LVL_DEBUG, too verbose and they can be found in the log if need be */
167 if (level <= LOG_LVL_INFO)
168 log_forward(file, line, function, string);
171 void log_printf(enum log_levels level,
174 const char *function,
182 if (level > debug_level)
185 va_start(ap, format);
187 string = alloc_vprintf(format, ap);
188 if (string != NULL) {
189 log_puts(level, file, line, function, string);
196 void log_printf_lf(enum log_levels level,
199 const char *function,
207 if (level > debug_level)
210 va_start(ap, format);
212 string = alloc_vprintf(format, ap);
213 if (string != NULL) {
214 strcat(string, "\n"); /* alloc_vprintf guaranteed the buffer to be at least one
216 log_puts(level, file, line, function, string);
223 COMMAND_HANDLER(handle_debug_level_command)
227 COMMAND_PARSE_NUMBER(int, CMD_ARGV[0], new_level);
228 if ((new_level > LOG_LVL_DEBUG) || (new_level < LOG_LVL_SILENT)) {
229 LOG_ERROR("level must be between %d and %d", LOG_LVL_SILENT, LOG_LVL_DEBUG);
230 return ERROR_COMMAND_SYNTAX_ERROR;
232 debug_level = new_level;
233 } else if (CMD_ARGC > 1)
234 return ERROR_COMMAND_SYNTAX_ERROR;
236 command_print(CMD_CTX, "debug_level: %i", debug_level);
241 COMMAND_HANDLER(handle_log_output_command)
244 FILE *file = fopen(CMD_ARGV[0], "w");
253 static struct command_registration log_command_handlers[] = {
255 .name = "log_output",
256 .handler = handle_log_output_command,
258 .help = "redirect logging to a file (default: stderr)",
259 .usage = "file_name",
262 .name = "debug_level",
263 .handler = handle_debug_level_command,
265 .help = "Sets the verbosity level of debugging output. "
266 "0 shows errors only; 1 adds warnings; "
267 "2 (default) adds other info; 3 adds debugging.",
270 COMMAND_REGISTRATION_DONE
273 int log_register_commands(struct command_context *cmd_ctx)
275 return register_commands(cmd_ctx, NULL, log_command_handlers);
280 /* set defaults for daemon configuration,
281 * if not set by cmdline or cfgfile */
282 if (debug_level == -1)
283 debug_level = LOG_LVL_INFO;
285 char *debug_env = getenv("OPENOCD_DEBUG_LEVEL");
286 if (NULL != debug_env) {
288 int retval = parse_int(debug_env, &value);
289 if (ERROR_OK == retval &&
290 debug_level >= LOG_LVL_SILENT &&
291 debug_level <= LOG_LVL_DEBUG)
295 if (log_output == NULL)
298 start = last_time = timeval_ms();
301 int set_log_output(struct command_context *cmd_ctx, FILE *output)
307 /* add/remove log callback handler */
308 int log_add_callback(log_callback_fn fn, void *priv)
310 struct log_callback *cb;
312 /* prevent the same callback to be registered more than once, just for sure */
313 for (cb = log_callbacks; cb; cb = cb->next) {
314 if (cb->fn == fn && cb->priv == priv)
315 return ERROR_COMMAND_SYNTAX_ERROR;
318 /* alloc memory, it is safe just to return in case of an error, no need for the caller to
320 cb = malloc(sizeof(struct log_callback));
322 return ERROR_BUF_TOO_SMALL;
324 /* add item to the beginning of the linked list */
327 cb->next = log_callbacks;
333 int log_remove_callback(log_callback_fn fn, void *priv)
335 struct log_callback *cb, **p;
337 for (p = &log_callbacks; (cb = *p); p = &(*p)->next) {
338 if (cb->fn == fn && cb->priv == priv) {
346 return ERROR_COMMAND_SYNTAX_ERROR;
349 /* return allocated string w/printf() result */
350 char *alloc_vprintf(const char *fmt, va_list ap)
356 /* determine the length of the buffer needed */
357 va_copy(ap_copy, ap);
358 len = vsnprintf(NULL, 0, fmt, ap_copy);
361 /* allocate and make room for terminating zero. */
362 /* FIXME: The old version always allocated at least one byte extra and
363 * other code depend on that. They should be probably be fixed, but for
364 * now reserve the extra byte. */
365 string = malloc(len + 2);
369 /* do the real work */
370 vsnprintf(string, len + 1, fmt, ap);
375 char *alloc_printf(const char *format, ...)
379 va_start(ap, format);
380 string = alloc_vprintf(format, ap);
385 /* Code must return to the server loop before 1000ms has returned or invoke
388 * The GDB connection will time out if it spends >2000ms and you'll get nasty
389 * error messages from GDB:
391 * Ignoring packet error, continuing...
392 * Reply contains invalid hex digit 116
394 * While it is possible use "set remotetimeout" to more than the default 2000ms
395 * in GDB, OpenOCD guarantees that it sends keep-alive packages on the
396 * GDB protocol and it is a bug in OpenOCD not to either return to the server
397 * loop or invoke keep_alive() every 1000ms.
399 * This function will send a keep alive packet if >500ms has passed since last time
402 * Note that this function can be invoked often, so it needs to be relatively
403 * fast when invoked more often than every 500ms.
408 current_time = timeval_ms();
409 if (current_time-last_time > 1000) {
410 extern int gdb_actual_connections;
412 if (gdb_actual_connections)
413 LOG_WARNING("keep_alive() was not invoked in the "
414 "1000ms timelimit. GDB alive packet not "
415 "sent! (%lld). Workaround: increase "
416 "\"set remotetimeout\" in GDB",
417 current_time-last_time);
419 LOG_DEBUG("keep_alive() was not invoked in the "
420 "1000ms timelimit (%lld). This may cause "
421 "trouble with GDB connections.",
422 current_time-last_time);
424 if (current_time-last_time > 500) {
425 /* this will keep the GDB connection alive */
426 LOG_USER_N("%s", "");
428 /* DANGER!!!! do not add code to invoke e.g. target event processing,
429 * jim timer processing, etc. it can cause infinite recursion +
430 * jim event callbacks need to happen at a well defined time,
431 * not anywhere keep_alive() is invoked.
433 * These functions should be invoked at a well defined spot in server.c
436 last_time = current_time;
440 /* reset keep alive timer without sending message */
443 current_time = timeval_ms();
444 last_time = current_time;
447 /* if we sleep for extended periods of time, we must invoke keep_alive() intermittantly */
448 void alive_sleep(uint64_t ms)
450 uint64_t napTime = 10;
451 for (uint64_t i = 0; i < ms; i += napTime) {
452 uint64_t sleep_a_bit = ms - i;
453 if (sleep_a_bit > napTime)
454 sleep_a_bit = napTime;
456 usleep(sleep_a_bit * 1000);
461 void busy_sleep(uint64_t ms)
463 uint64_t then = timeval_ms();
464 while (timeval_ms() - then < ms) {