35 #ifdef _DEBUG_FREE_SPACE_ 39 #error "malloc.h is required to use --enable-malloc-logging" 65 static void log_forward(
const char *file,
unsigned line,
const char *
function,
const char *
string)
72 cb->
fn(cb->
priv, file, line,
function,
string);
97 fputs(
string, stderr);
109 f = strrchr(file,
'/');
113 if (strlen(
string) > 0) {
117 #ifdef _DEBUG_FREE_SPACE_ 118 struct mallinfo info;
121 fprintf(
log_output,
"%s%d %" PRId64
" %s:%d %s()" 122 #ifdef _DEBUG_FREE_SPACE_
126 #ifdef _DEBUG_FREE_SPACE_
151 const char *
function,
162 va_start(ap, format);
165 if (
string !=
NULL) {
166 log_puts(level, file, line,
function,
string);
174 const char *
function,
const char *format, va_list args)
193 log_puts(level, file, line,
function, tmp);
200 const char *
function,
206 va_start(ap, format);
241 FILE *file = fopen(
CMD_ARGV[0],
"w");
260 .
name =
"log_output",
261 .handler = handle_log_output_command,
263 .help =
"redirect logging to a file (default: stderr)",
264 .usage =
"[file_name | \"default\"]",
267 .name =
"debug_level",
268 .handler = handle_debug_level_command,
270 .help =
"Sets the verbosity level of debugging output. " 271 "0 shows errors only; 1 adds warnings; " 272 "2 (default) adds other info; 3 adds debugging; " 273 "4 adds extra verbose debugging.",
288 char *debug_env = getenv(
"OPENOCD_DEBUG_LEVEL");
289 if (
NULL != debug_env) {
291 int retval = parse_int(debug_env, &value);
316 for (cb = log_callbacks; cb; cb = cb->
next) {
317 if (cb->
fn == fn && cb->
priv == priv)
340 for (p = &log_callbacks; (cb = *p); p = &(*p)->
next) {
341 if (cb->
fn == fn && cb->
priv == priv) {
360 va_copy(ap_copy, ap);
361 len = vsnprintf(
NULL, 0, fmt, ap_copy);
368 string = malloc(len + 2);
373 vsnprintf(
string, len + 1, fmt, ap);
382 va_start(ap, format);
409 #define KEEP_ALIVE_KICK_TIME_MS 500 410 #define KEEP_ALIVE_TIMEOUT_MS 1000 416 if (gdb_actual_connections)
418 "%d ms timelimit. GDB alive packet not " 419 "sent! (%" PRId64
" ms). Workaround: increase " 420 "\"set remotetimeout\" in GDB",
424 LOG_DEBUG(
"keep_alive() was not invoked in the " 425 "%d ms timelimit (%" PRId64
" ms). This may cause " 426 "trouble with GDB connections.",
475 uint64_t napTime = 10;
476 for (uint64_t i = 0; i < ms; i += napTime) {
477 uint64_t sleep_a_bit = ms - i;
478 if (sleep_a_bit > napTime)
479 sleep_a_bit = napTime;
481 usleep(sleep_a_bit * 1000);
497 #define MAX_SOCKET_ERR_MSG_LENGTH 256 505 error_code = WSAGetLastError();
507 error_message[0] =
'\0';
508 DWORD retval = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
NULL, error_code, 0,
511 const bool have_message = (retval != 0) && (error_message[0] !=
'\0');
512 LOG_ERROR(
"Error on socket '%s': WSAGetLastError==%d%s%s.", socket_desc, error_code,
513 (have_message ?
", message: " :
""),
514 (have_message ? error_message :
""));
517 LOG_ERROR(
"Error on socket '%s': errno==%d, message: %s.", socket_desc, error_code, strerror(error_code));
static void log_puts(enum log_levels level, const char *file, int line, const char *function, const char *string)
#define ERROR_BUF_TOO_SMALL
void log_printf_lf(enum log_levels level, const char *file, unsigned line, const char *function, const char *format,...)
int register_commands(struct command_context *cmd_ctx, struct command *parent, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends...
void log_printf(enum log_levels level, const char *file, unsigned line, const char *function, const char *format,...)
void log_init(void)
Initialize logging module.
char * alloc_printf(const char *format,...)
int log_remove_callback(log_callback_fn fn, void *priv)
#define MAX_SOCKET_ERR_MSG_LENGTH
COMMAND_HANDLER(handle_debug_level_command)
int log_add_callback(log_callback_fn fn, void *priv)
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
int gdb_actual_connections
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly...
static const char *const log_strings[6]
#define KEEP_ALIVE_TIMEOUT_MS
#define KEEP_ALIVE_KICK_TIME_MS
void command_print(struct command_invocation *cmd, const char *format,...)
static const struct command_registration log_command_handlers[]
static struct log_callback * log_callbacks
#define LOG_ERROR(expr ...)
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
char * alloc_vprintf(const char *fmt, va_list ap)
static void gdb_timeout_warning(int64_t delta_time)
#define LOG_USER_N(expr ...)
void log_vprintf_lf(enum log_levels level, const char *file, unsigned line, const char *function, const char *format, va_list args)
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define ERROR_COMMAND_SYNTAX_ERROR
#define LOG_WARNING(expr ...)
int log_register_commands(struct command_context *cmd_ctx)
void busy_sleep(uint64_t ms)
struct log_callback * next
static void log_forward(const char *file, unsigned line, const char *function, const char *string)
void(* log_callback_fn)(void *priv, const char *file, unsigned line, const char *function, const char *string)
void alive_sleep(uint64_t ms)
int set_log_output(struct command_context *cmd_ctx, FILE *output)
#define LOG_DEBUG(expr ...)
static int64_t current_time
void log_socket_error(const char *socket_desc)