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, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
31 #include "replacements.h"
32 #include "time_support.h"
36 #ifdef _DEBUG_FREE_SPACE_
40 #error "malloc.h is required to use --enable-malloc-logging"
44 int debug_level
= LOG_LVL_INFO
;
46 static FILE *log_output
;
47 static struct log_callback
*log_callbacks
;
49 static int64_t last_time
;
50 static int64_t current_time
;
54 static const char * const log_strings
[6] = {
57 "Warn : ", /* want a space after each colon, all same width, colons aligned */
65 /* forward the log to the listeners */
66 static void log_forward(const char *file
, unsigned line
, const char *function
, const char *string
)
68 struct log_callback
*cb
, *next
;
70 /* DANGER!!!! the log callback can remove itself!!!! */
73 cb
->fn(cb
->priv
, file
, line
, function
, string
);
78 /* The log_puts() serves two somewhat different goals:
81 * - feeding low-level info to the user in GDB or Telnet
83 * The latter dictates that strings without newline are not logged, lest there
84 * will be *MANY log lines when sending one char at the time(e.g.
88 static void log_puts(enum log_levels level
,
97 /* log_init() not called yet; print on stderr */
98 fputs(string
, stderr
);
103 if (level
== LOG_LVL_OUTPUT
) {
104 /* do not prepend any headers, just print out what we were given and return */
105 fputs(string
, log_output
);
110 f
= strrchr(file
, '/');
114 if (strlen(string
) > 0) {
115 if (debug_level
>= LOG_LVL_DEBUG
) {
116 /* print with count and time information */
117 int64_t t
= timeval_ms() - start
;
118 #ifdef _DEBUG_FREE_SPACE_
119 struct mallinfo info
;
122 fprintf(log_output
, "%s%d %" PRId64
" %s:%d %s()"
123 #ifdef _DEBUG_FREE_SPACE_
126 ": %s", log_strings
[level
+ 1], count
, t
, file
, line
, function
,
127 #ifdef _DEBUG_FREE_SPACE_
132 /* if we are using gdb through pipes then we do not want any output
133 * to the pipe otherwise we get repeated strings */
134 fprintf(log_output
, "%s%s",
135 (level
> LOG_LVL_USER
) ? log_strings
[level
+ 1] : "", string
);
138 /* Empty strings are sent to log callbacks to keep e.g. gdbserver alive, here we do
144 /* Never forward LOG_LVL_DEBUG, too verbose and they can be found in the log if need be */
145 if (level
<= LOG_LVL_INFO
)
146 log_forward(file
, line
, function
, string
);
149 void log_printf(enum log_levels level
,
152 const char *function
,
160 if (level
> debug_level
)
163 va_start(ap
, format
);
165 string
= alloc_vprintf(format
, ap
);
167 log_puts(level
, file
, line
, function
, string
);
174 void log_vprintf_lf(enum log_levels level
, const char *file
, unsigned line
,
175 const char *function
, const char *format
, va_list args
)
181 if (level
> debug_level
)
184 tmp
= alloc_vprintf(format
, args
);
190 * Note: alloc_vprintf() guarantees that the buffer is at least one
194 log_puts(level
, file
, line
, function
, tmp
);
198 void log_printf_lf(enum log_levels level
,
201 const char *function
,
207 va_start(ap
, format
);
208 log_vprintf_lf(level
, file
, line
, function
, format
, ap
);
212 COMMAND_HANDLER(handle_debug_level_command
)
216 COMMAND_PARSE_NUMBER(int, CMD_ARGV
[0], new_level
);
217 if ((new_level
> LOG_LVL_DEBUG_IO
) || (new_level
< LOG_LVL_SILENT
)) {
218 LOG_ERROR("level must be between %d and %d", LOG_LVL_SILENT
, LOG_LVL_DEBUG_IO
);
219 return ERROR_COMMAND_SYNTAX_ERROR
;
221 debug_level
= new_level
;
222 } else if (CMD_ARGC
> 1)
223 return ERROR_COMMAND_SYNTAX_ERROR
;
225 command_print(CMD
, "debug_level: %i", debug_level
);
230 COMMAND_HANDLER(handle_log_output_command
)
232 if (CMD_ARGC
== 0 || (CMD_ARGC
== 1 && strcmp(CMD_ARGV
[0], "default") == 0)) {
233 if (log_output
!= stderr
&& log_output
) {
234 /* Close previous log file, if it was open and wasn't stderr. */
238 LOG_DEBUG("set log_output to default");
242 FILE *file
= fopen(CMD_ARGV
[0], "w");
244 LOG_ERROR("failed to open output log '%s'", CMD_ARGV
[0]);
247 if (log_output
!= stderr
&& log_output
) {
248 /* Close previous log file, if it was open and wasn't stderr. */
252 LOG_DEBUG("set log_output to \"%s\"", CMD_ARGV
[0]);
256 return ERROR_COMMAND_SYNTAX_ERROR
;
259 static const struct command_registration log_command_handlers
[] = {
261 .name
= "log_output",
262 .handler
= handle_log_output_command
,
264 .help
= "redirect logging to a file (default: stderr)",
265 .usage
= "[file_name | \"default\"]",
268 .name
= "debug_level",
269 .handler
= handle_debug_level_command
,
271 .help
= "Sets the verbosity level of debugging output. "
272 "0 shows errors only; 1 adds warnings; "
273 "2 (default) adds other info; 3 adds debugging; "
274 "4 adds extra verbose debugging.",
277 COMMAND_REGISTRATION_DONE
280 int log_register_commands(struct command_context
*cmd_ctx
)
282 return register_commands(cmd_ctx
, NULL
, log_command_handlers
);
287 /* set defaults for daemon configuration,
288 * if not set by cmdline or cfgfile */
289 char *debug_env
= getenv("OPENOCD_DEBUG_LEVEL");
292 int retval
= parse_int(debug_env
, &value
);
293 if (retval
== ERROR_OK
&&
294 debug_level
>= LOG_LVL_SILENT
&&
295 debug_level
<= LOG_LVL_DEBUG_IO
)
302 start
= last_time
= timeval_ms();
305 int set_log_output(struct command_context
*cmd_ctx
, FILE *output
)
311 /* add/remove log callback handler */
312 int log_add_callback(log_callback_fn fn
, void *priv
)
314 struct log_callback
*cb
;
316 /* prevent the same callback to be registered more than once, just for sure */
317 for (cb
= log_callbacks
; cb
; cb
= cb
->next
) {
318 if (cb
->fn
== fn
&& cb
->priv
== priv
)
319 return ERROR_COMMAND_SYNTAX_ERROR
;
322 /* alloc memory, it is safe just to return in case of an error, no need for the caller to
324 cb
= malloc(sizeof(struct log_callback
));
326 return ERROR_BUF_TOO_SMALL
;
328 /* add item to the beginning of the linked list */
331 cb
->next
= log_callbacks
;
337 int log_remove_callback(log_callback_fn fn
, void *priv
)
339 struct log_callback
*cb
, **p
;
341 for (p
= &log_callbacks
; (cb
= *p
); p
= &(*p
)->next
) {
342 if (cb
->fn
== fn
&& cb
->priv
== priv
) {
350 return ERROR_COMMAND_SYNTAX_ERROR
;
353 /* return allocated string w/printf() result */
354 char *alloc_vprintf(const char *fmt
, va_list ap
)
360 /* determine the length of the buffer needed */
361 va_copy(ap_copy
, ap
);
362 len
= vsnprintf(NULL
, 0, fmt
, ap_copy
);
365 /* allocate and make room for terminating zero. */
366 /* FIXME: The old version always allocated at least one byte extra and
367 * other code depend on that. They should be probably be fixed, but for
368 * now reserve the extra byte. */
369 string
= malloc(len
+ 2);
373 /* do the real work */
374 vsnprintf(string
, len
+ 1, fmt
, ap
);
379 char *alloc_printf(const char *format
, ...)
383 va_start(ap
, format
);
384 string
= alloc_vprintf(format
, ap
);
389 /* Code must return to the server loop before 1000ms has returned or invoke
392 * The GDB connection will time out if it spends >2000ms and you'll get nasty
393 * error messages from GDB:
395 * Ignoring packet error, continuing...
396 * Reply contains invalid hex digit 116
398 * While it is possible use "set remotetimeout" to more than the default 2000ms
399 * in GDB, OpenOCD guarantees that it sends keep-alive packages on the
400 * GDB protocol and it is a bug in OpenOCD not to either return to the server
401 * loop or invoke keep_alive() every 1000ms.
403 * This function will send a keep alive packet if >500ms has passed since last time
406 * Note that this function can be invoked often, so it needs to be relatively
407 * fast when invoked more often than every 500ms.
410 #define KEEP_ALIVE_KICK_TIME_MS 500
411 #define KEEP_ALIVE_TIMEOUT_MS 1000
413 static void gdb_timeout_warning(int64_t delta_time
)
415 extern int gdb_actual_connections
;
417 if (gdb_actual_connections
)
418 LOG_WARNING("keep_alive() was not invoked in the "
419 "%d ms timelimit. GDB alive packet not "
420 "sent! (%" PRId64
" ms). Workaround: increase "
421 "\"set remotetimeout\" in GDB",
422 KEEP_ALIVE_TIMEOUT_MS
,
425 LOG_DEBUG("keep_alive() was not invoked in the "
426 "%d ms timelimit (%" PRId64
" ms). This may cause "
427 "trouble with GDB connections.",
428 KEEP_ALIVE_TIMEOUT_MS
,
432 void keep_alive(void)
434 current_time
= timeval_ms();
436 int64_t delta_time
= current_time
- last_time
;
438 if (delta_time
> KEEP_ALIVE_TIMEOUT_MS
) {
439 last_time
= current_time
;
441 gdb_timeout_warning(delta_time
);
444 if (delta_time
> KEEP_ALIVE_KICK_TIME_MS
) {
445 last_time
= current_time
;
447 /* this will keep the GDB connection alive */
448 LOG_USER_N("%s", "");
450 /* DANGER!!!! do not add code to invoke e.g. target event processing,
451 * jim timer processing, etc. it can cause infinite recursion +
452 * jim event callbacks need to happen at a well defined time,
453 * not anywhere keep_alive() is invoked.
455 * These functions should be invoked at a well defined spot in server.c
460 /* reset keep alive timer without sending message */
461 void kept_alive(void)
463 current_time
= timeval_ms();
465 int64_t delta_time
= current_time
- last_time
;
467 last_time
= current_time
;
469 if (delta_time
> KEEP_ALIVE_TIMEOUT_MS
)
470 gdb_timeout_warning(delta_time
);
473 /* if we sleep for extended periods of time, we must invoke keep_alive() intermittently */
474 void alive_sleep(uint64_t ms
)
476 uint64_t nap_time
= 10;
477 for (uint64_t i
= 0; i
< ms
; i
+= nap_time
) {
478 uint64_t sleep_a_bit
= ms
- i
;
479 if (sleep_a_bit
> nap_time
)
480 sleep_a_bit
= nap_time
;
482 usleep(sleep_a_bit
* 1000);
487 void busy_sleep(uint64_t ms
)
489 uint64_t then
= timeval_ms();
490 while (timeval_ms() - then
< ms
) {
497 /* Maximum size of socket error message retrieved from operation system */
498 #define MAX_SOCKET_ERR_MSG_LENGTH 256
500 /* Provide log message for the last socket error.
501 Uses errno on *nix and WSAGetLastError() on Windows */
502 void log_socket_error(const char *socket_desc
)
506 error_code
= WSAGetLastError();
507 char error_message
[MAX_SOCKET_ERR_MSG_LENGTH
];
508 error_message
[0] = '\0';
509 DWORD retval
= FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error_code
, 0,
510 error_message
, MAX_SOCKET_ERR_MSG_LENGTH
, NULL
);
511 error_message
[MAX_SOCKET_ERR_MSG_LENGTH
- 1] = '\0';
512 const bool have_message
= (retval
!= 0) && (error_message
[0] != '\0');
513 LOG_ERROR("Error on socket '%s': WSAGetLastError==%d%s%s.", socket_desc
, error_code
,
514 (have_message
? ", message: " : ""),
515 (have_message
? error_message
: ""));
518 LOG_ERROR("Error on socket '%s': errno==%d, message: %s.", socket_desc
, error_code
, strerror(error_code
));
523 * Find the first non-printable character in the char buffer, return a pointer to it.
524 * If no such character exists, return NULL.
526 char *find_nonprint_char(char *buf
, unsigned buf_len
)
528 for (unsigned int i
= 0; i
< buf_len
; i
++) {
529 if (!isprint(buf
[i
]))
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)