2 * wpa_supplicant/hostapd / Debug prints
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #ifdef CONFIG_DEBUG_SYSLOG
16 int wpa_debug_syslog = 0;
17 #endif /* CONFIG_DEBUG_SYSLOG */
19 #ifdef CONFIG_DEBUG_LINUX_TRACING
20 #include <sys/types.h>
26 static FILE *wpa_debug_tracing_file = NULL;
28 #define WPAS_TRACE_PFX "wpas <%d>: "
29 #endif /* CONFIG_DEBUG_LINUX_TRACING */
32 int wpa_debug_level = MSG_INFO;
33 int wpa_debug_show_keys = 0;
34 int wpa_debug_timestamp = 0;
37 #ifdef CONFIG_ANDROID_LOG
39 #include <android/log.h>
41 #ifndef ANDROID_LOG_NAME
42 #define ANDROID_LOG_NAME "wpa_supplicant"
43 #endif /* ANDROID_LOG_NAME */
45 static int wpa_to_android_level(int level)
47 if (level == MSG_ERROR)
48 return ANDROID_LOG_ERROR;
49 if (level == MSG_WARNING)
50 return ANDROID_LOG_WARN;
51 if (level == MSG_INFO)
52 return ANDROID_LOG_INFO;
53 return ANDROID_LOG_DEBUG;
56 #endif /* CONFIG_ANDROID_LOG */
58 #ifndef CONFIG_NO_STDOUT_DEBUG
60 #ifdef CONFIG_DEBUG_FILE
61 #include <sys/types.h>
65 static FILE *out_file = NULL;
66 #endif /* CONFIG_DEBUG_FILE */
69 void wpa_debug_print_timestamp(void)
71 #ifndef CONFIG_ANDROID_LOG
74 if (!wpa_debug_timestamp)
78 #ifdef CONFIG_DEBUG_FILE
80 fprintf(out_file, "%ld.%06u: ", (long) tv.sec,
81 (unsigned int) tv.usec);
83 #endif /* CONFIG_DEBUG_FILE */
84 printf("%ld.%06u: ", (long) tv.sec, (unsigned int) tv.usec);
85 #endif /* CONFIG_ANDROID_LOG */
89 #ifdef CONFIG_DEBUG_SYSLOG
91 #define LOG_HOSTAPD LOG_DAEMON
92 #endif /* LOG_HOSTAPD */
94 void wpa_debug_open_syslog(void)
96 openlog("wpa_supplicant", LOG_PID | LOG_NDELAY, LOG_HOSTAPD);
101 void wpa_debug_close_syslog(void)
103 if (wpa_debug_syslog)
108 static int syslog_priority(int level)
123 #endif /* CONFIG_DEBUG_SYSLOG */
126 #ifdef CONFIG_DEBUG_LINUX_TRACING
128 int wpa_debug_open_linux_tracing(void)
130 int mounts, trace_fd;
133 char *line, *tmp1, *path = NULL;
135 mounts = open("/proc/mounts", O_RDONLY);
137 printf("no /proc/mounts\n");
141 buflen = read(mounts, buf, sizeof(buf) - 1);
144 printf("failed to read /proc/mounts\n");
148 line = strtok_r(buf, "\n", &tmp1);
150 char *tmp2, *tmp_path, *fstype;
151 /* "<dev> <mountpoint> <fs type> ..." */
152 strtok_r(line, " ", &tmp2);
153 tmp_path = strtok_r(NULL, " ", &tmp2);
154 fstype = strtok_r(NULL, " ", &tmp2);
155 if (fstype && strcmp(fstype, "debugfs") == 0) {
160 line = strtok_r(NULL, "\n", &tmp1);
164 printf("debugfs mountpoint not found\n");
168 snprintf(buf, sizeof(buf) - 1, "%s/tracing/trace_marker", path);
170 trace_fd = open(buf, O_WRONLY);
172 printf("failed to open trace_marker file\n");
175 wpa_debug_tracing_file = fdopen(trace_fd, "w");
176 if (wpa_debug_tracing_file == NULL) {
178 printf("failed to fdopen()\n");
186 void wpa_debug_close_linux_tracing(void)
188 if (wpa_debug_tracing_file == NULL)
190 fclose(wpa_debug_tracing_file);
191 wpa_debug_tracing_file = NULL;
194 #endif /* CONFIG_DEBUG_LINUX_TRACING */
198 * wpa_printf - conditional printf
199 * @level: priority level (MSG_*) of the message
200 * @fmt: printf format string, followed by optional arguments
202 * This function is used to print conditional debugging and error messages. The
203 * output may be directed to stdout, stderr, and/or syslog based on
206 * Note: New line '\n' is added to the end of the text when printing to stdout.
208 void wpa_printf(int level, const char *fmt, ...)
213 if (level >= wpa_debug_level) {
214 #ifdef CONFIG_ANDROID_LOG
215 __android_log_vprint(wpa_to_android_level(level),
216 ANDROID_LOG_NAME, fmt, ap);
217 #else /* CONFIG_ANDROID_LOG */
218 #ifdef CONFIG_DEBUG_SYSLOG
219 if (wpa_debug_syslog) {
220 vsyslog(syslog_priority(level), fmt, ap);
222 #endif /* CONFIG_DEBUG_SYSLOG */
223 wpa_debug_print_timestamp();
224 #ifdef CONFIG_DEBUG_FILE
226 vfprintf(out_file, fmt, ap);
227 fprintf(out_file, "\n");
229 #endif /* CONFIG_DEBUG_FILE */
232 #ifdef CONFIG_DEBUG_FILE
234 #endif /* CONFIG_DEBUG_FILE */
235 #ifdef CONFIG_DEBUG_SYSLOG
237 #endif /* CONFIG_DEBUG_SYSLOG */
238 #endif /* CONFIG_ANDROID_LOG */
242 #ifdef CONFIG_DEBUG_LINUX_TRACING
243 if (wpa_debug_tracing_file != NULL) {
245 fprintf(wpa_debug_tracing_file, WPAS_TRACE_PFX, level);
246 vfprintf(wpa_debug_tracing_file, fmt, ap);
247 fprintf(wpa_debug_tracing_file, "\n");
248 fflush(wpa_debug_tracing_file);
251 #endif /* CONFIG_DEBUG_LINUX_TRACING */
255 static void _wpa_hexdump(int level, const char *title, const u8 *buf,
256 size_t len, int show)
260 #ifdef CONFIG_DEBUG_LINUX_TRACING
261 if (wpa_debug_tracing_file != NULL) {
262 fprintf(wpa_debug_tracing_file,
263 WPAS_TRACE_PFX "%s - hexdump(len=%lu):",
264 level, title, (unsigned long) len);
266 fprintf(wpa_debug_tracing_file, " [NULL]\n");
268 fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
270 for (i = 0; i < len; i++)
271 fprintf(wpa_debug_tracing_file,
274 fflush(wpa_debug_tracing_file);
276 #endif /* CONFIG_DEBUG_LINUX_TRACING */
278 if (level < wpa_debug_level)
280 #ifdef CONFIG_ANDROID_LOG
287 } else if (len == 0) {
289 } else if (show && len) {
290 /* Limit debug message length for Android log */
293 strbuf = os_malloc(1 + 3 * slen);
294 if (strbuf == NULL) {
295 wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
296 "allocate message buffer");
300 for (i = 0; i < slen; i++)
301 os_snprintf(&strbuf[i * 3], 4, " %02x",
306 display = " [REMOVED]";
309 __android_log_print(wpa_to_android_level(level),
311 "%s - hexdump(len=%lu):%s%s",
312 title, (long unsigned int) len, display,
313 len > slen ? " ..." : "");
314 bin_clear_free(strbuf, 1 + 3 * slen);
317 #else /* CONFIG_ANDROID_LOG */
318 #ifdef CONFIG_DEBUG_SYSLOG
319 if (wpa_debug_syslog) {
325 } else if (len == 0) {
327 } else if (show && len) {
328 strbuf = os_malloc(1 + 3 * len);
329 if (strbuf == NULL) {
330 wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
331 "allocate message buffer");
335 for (i = 0; i < len; i++)
336 os_snprintf(&strbuf[i * 3], 4, " %02x",
341 display = " [REMOVED]";
344 syslog(syslog_priority(level), "%s - hexdump(len=%lu):%s",
345 title, (unsigned long) len, display);
346 bin_clear_free(strbuf, 1 + 3 * len);
349 #endif /* CONFIG_DEBUG_SYSLOG */
350 wpa_debug_print_timestamp();
351 #ifdef CONFIG_DEBUG_FILE
353 fprintf(out_file, "%s - hexdump(len=%lu):",
354 title, (unsigned long) len);
356 fprintf(out_file, " [NULL]");
358 for (i = 0; i < len; i++)
359 fprintf(out_file, " %02x", buf[i]);
361 fprintf(out_file, " [REMOVED]");
363 fprintf(out_file, "\n");
365 #endif /* CONFIG_DEBUG_FILE */
366 printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
370 for (i = 0; i < len; i++)
371 printf(" %02x", buf[i]);
373 printf(" [REMOVED]");
376 #ifdef CONFIG_DEBUG_FILE
378 #endif /* CONFIG_DEBUG_FILE */
379 #endif /* CONFIG_ANDROID_LOG */
382 void wpa_hexdump(int level, const char *title, const void *buf, size_t len)
384 _wpa_hexdump(level, title, buf, len, 1);
388 void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len)
390 _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys);
394 static void _wpa_hexdump_ascii(int level, const char *title, const void *buf,
395 size_t len, int show)
399 const size_t line_len = 16;
401 #ifdef CONFIG_DEBUG_LINUX_TRACING
402 if (wpa_debug_tracing_file != NULL) {
403 fprintf(wpa_debug_tracing_file,
404 WPAS_TRACE_PFX "%s - hexdump_ascii(len=%lu):",
405 level, title, (unsigned long) len);
407 fprintf(wpa_debug_tracing_file, " [NULL]\n");
409 fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
411 /* can do ascii processing in userspace */
412 for (i = 0; i < len; i++)
413 fprintf(wpa_debug_tracing_file,
416 fflush(wpa_debug_tracing_file);
418 #endif /* CONFIG_DEBUG_LINUX_TRACING */
420 if (level < wpa_debug_level)
422 #ifdef CONFIG_ANDROID_LOG
423 _wpa_hexdump(level, title, buf, len, show);
424 #else /* CONFIG_ANDROID_LOG */
425 #ifdef CONFIG_DEBUG_SYSLOG
426 if (wpa_debug_syslog) {
427 _wpa_hexdump(level, title, buf, len, show);
430 #endif /* CONFIG_DEBUG_SYSLOG */
431 wpa_debug_print_timestamp();
432 #ifdef CONFIG_DEBUG_FILE
436 "%s - hexdump_ascii(len=%lu): [REMOVED]\n",
437 title, (unsigned long) len);
442 "%s - hexdump_ascii(len=%lu): [NULL]\n",
443 title, (unsigned long) len);
446 fprintf(out_file, "%s - hexdump_ascii(len=%lu):\n",
447 title, (unsigned long) len);
449 llen = len > line_len ? line_len : len;
450 fprintf(out_file, " ");
451 for (i = 0; i < llen; i++)
452 fprintf(out_file, " %02x", pos[i]);
453 for (i = llen; i < line_len; i++)
454 fprintf(out_file, " ");
455 fprintf(out_file, " ");
456 for (i = 0; i < llen; i++) {
458 fprintf(out_file, "%c", pos[i]);
460 fprintf(out_file, "_");
462 for (i = llen; i < line_len; i++)
463 fprintf(out_file, " ");
464 fprintf(out_file, "\n");
469 #endif /* CONFIG_DEBUG_FILE */
471 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
472 title, (unsigned long) len);
476 printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
477 title, (unsigned long) len);
480 printf("%s - hexdump_ascii(len=%lu):\n", title, (unsigned long) len);
482 llen = len > line_len ? line_len : len;
484 for (i = 0; i < llen; i++)
485 printf(" %02x", pos[i]);
486 for (i = llen; i < line_len; i++)
489 for (i = 0; i < llen; i++) {
491 printf("%c", pos[i]);
495 for (i = llen; i < line_len; i++)
501 #ifdef CONFIG_DEBUG_FILE
503 #endif /* CONFIG_DEBUG_FILE */
504 #endif /* CONFIG_ANDROID_LOG */
508 void wpa_hexdump_ascii(int level, const char *title, const void *buf,
511 _wpa_hexdump_ascii(level, title, buf, len, 1);
515 void wpa_hexdump_ascii_key(int level, const char *title, const void *buf,
518 _wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
522 #ifdef CONFIG_DEBUG_FILE
523 static char *last_path = NULL;
524 #endif /* CONFIG_DEBUG_FILE */
526 int wpa_debug_reopen_file(void)
528 #ifdef CONFIG_DEBUG_FILE
533 return 0; /* logfile not used */
535 tmp = os_strdup(last_path);
539 wpa_debug_close_file();
540 rv = wpa_debug_open_file(tmp);
543 #else /* CONFIG_DEBUG_FILE */
545 #endif /* CONFIG_DEBUG_FILE */
549 int wpa_debug_open_file(const char *path)
551 #ifdef CONFIG_DEBUG_FILE
557 if (last_path == NULL || os_strcmp(last_path, path) != 0) {
558 /* Save our path to enable re-open */
560 last_path = os_strdup(path);
563 out_fd = open(path, O_CREAT | O_APPEND | O_WRONLY,
564 S_IRUSR | S_IWUSR | S_IRGRP);
566 wpa_printf(MSG_ERROR,
567 "%s: Failed to open output file descriptor, using standard output",
573 if (fcntl(out_fd, F_SETFD, FD_CLOEXEC) < 0) {
574 wpa_printf(MSG_DEBUG,
575 "%s: Failed to set FD_CLOEXEC - continue without: %s",
576 __func__, strerror(errno));
578 #endif /* __linux__ */
580 out_file = fdopen(out_fd, "a");
581 if (out_file == NULL) {
582 wpa_printf(MSG_ERROR, "wpa_debug_open_file: Failed to open "
583 "output file, using standard output");
588 setvbuf(out_file, NULL, _IOLBF, 0);
590 #else /* CONFIG_DEBUG_FILE */
592 #endif /* CONFIG_DEBUG_FILE */
597 void wpa_debug_close_file(void)
599 #ifdef CONFIG_DEBUG_FILE
606 #endif /* CONFIG_DEBUG_FILE */
610 void wpa_debug_setup_stdout(void)
613 setvbuf(stdout, NULL, _IOLBF, 0);
617 #endif /* CONFIG_NO_STDOUT_DEBUG */
620 #ifndef CONFIG_NO_WPA_MSG
621 static wpa_msg_cb_func wpa_msg_cb = NULL;
623 void wpa_msg_register_cb(wpa_msg_cb_func func)
629 static wpa_msg_get_ifname_func wpa_msg_ifname_cb = NULL;
631 void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)
633 wpa_msg_ifname_cb = func;
637 void wpa_msg(void *ctx, int level, const char *fmt, ...)
646 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
649 buf = os_malloc(buflen);
651 wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
657 if (wpa_msg_ifname_cb) {
658 const char *ifname = wpa_msg_ifname_cb(ctx);
660 int res = os_snprintf(prefix, sizeof(prefix), "%s: ",
662 if (os_snprintf_error(sizeof(prefix), res))
666 len = vsnprintf(buf, buflen, fmt, ap);
668 wpa_printf(level, "%s%s", prefix, buf);
670 wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
671 bin_clear_free(buf, buflen);
675 void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
686 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
689 buf = os_malloc(buflen);
691 wpa_printf(MSG_ERROR, "wpa_msg_ctrl: Failed to allocate "
696 len = vsnprintf(buf, buflen, fmt, ap);
698 wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
699 bin_clear_free(buf, buflen);
703 void wpa_msg_global(void *ctx, int level, const char *fmt, ...)
711 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
714 buf = os_malloc(buflen);
716 wpa_printf(MSG_ERROR, "wpa_msg_global: Failed to allocate "
721 len = vsnprintf(buf, buflen, fmt, ap);
723 wpa_printf(level, "%s", buf);
725 wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
726 bin_clear_free(buf, buflen);
730 void wpa_msg_global_ctrl(void *ctx, int level, const char *fmt, ...)
741 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
744 buf = os_malloc(buflen);
746 wpa_printf(MSG_ERROR,
747 "wpa_msg_global_ctrl: Failed to allocate message buffer");
751 len = vsnprintf(buf, buflen, fmt, ap);
753 wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
754 bin_clear_free(buf, buflen);
758 void wpa_msg_no_global(void *ctx, int level, const char *fmt, ...)
766 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
769 buf = os_malloc(buflen);
771 wpa_printf(MSG_ERROR, "wpa_msg_no_global: Failed to allocate "
776 len = vsnprintf(buf, buflen, fmt, ap);
778 wpa_printf(level, "%s", buf);
780 wpa_msg_cb(ctx, level, WPA_MSG_NO_GLOBAL, buf, len);
781 bin_clear_free(buf, buflen);
785 void wpa_msg_global_only(void *ctx, int level, const char *fmt, ...)
793 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
796 buf = os_malloc(buflen);
798 wpa_printf(MSG_ERROR, "%s: Failed to allocate message buffer",
803 len = vsnprintf(buf, buflen, fmt, ap);
805 wpa_printf(level, "%s", buf);
807 wpa_msg_cb(ctx, level, WPA_MSG_ONLY_GLOBAL, buf, len);
811 #endif /* CONFIG_NO_WPA_MSG */
814 #ifndef CONFIG_NO_HOSTAPD_LOGGER
815 static hostapd_logger_cb_func hostapd_logger_cb = NULL;
817 void hostapd_logger_register_cb(hostapd_logger_cb_func func)
819 hostapd_logger_cb = func;
823 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
824 const char *fmt, ...)
832 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
835 buf = os_malloc(buflen);
837 wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
842 len = vsnprintf(buf, buflen, fmt, ap);
844 if (hostapd_logger_cb)
845 hostapd_logger_cb(ctx, addr, module, level, buf, len);
847 wpa_printf(MSG_DEBUG, "hostapd_logger: STA " MACSTR " - %s",
850 wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
851 bin_clear_free(buf, buflen);
853 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
856 const char * debug_level_str(int level)
877 int str_to_debug_level(const char *s)
879 if (os_strcasecmp(s, "EXCESSIVE") == 0)
880 return MSG_EXCESSIVE;
881 if (os_strcasecmp(s, "MSGDUMP") == 0)
883 if (os_strcasecmp(s, "DEBUG") == 0)
885 if (os_strcasecmp(s, "INFO") == 0)
887 if (os_strcasecmp(s, "WARNING") == 0)
889 if (os_strcasecmp(s, "ERROR") == 0)