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");
149 line = strtok_r(buf, "\n", &tmp1);
151 char *tmp2, *tmp_path, *fstype;
152 /* "<dev> <mountpoint> <fs type> ..." */
153 strtok_r(line, " ", &tmp2);
154 tmp_path = strtok_r(NULL, " ", &tmp2);
155 fstype = strtok_r(NULL, " ", &tmp2);
156 if (fstype && strcmp(fstype, "debugfs") == 0) {
161 line = strtok_r(NULL, "\n", &tmp1);
165 printf("debugfs mountpoint not found\n");
169 snprintf(buf, sizeof(buf) - 1, "%s/tracing/trace_marker", path);
171 trace_fd = open(buf, O_WRONLY);
173 printf("failed to open trace_marker file\n");
176 wpa_debug_tracing_file = fdopen(trace_fd, "w");
177 if (wpa_debug_tracing_file == NULL) {
179 printf("failed to fdopen()\n");
187 void wpa_debug_close_linux_tracing(void)
189 if (wpa_debug_tracing_file == NULL)
191 fclose(wpa_debug_tracing_file);
192 wpa_debug_tracing_file = NULL;
195 #endif /* CONFIG_DEBUG_LINUX_TRACING */
199 * wpa_printf - conditional printf
200 * @level: priority level (MSG_*) of the message
201 * @fmt: printf format string, followed by optional arguments
203 * This function is used to print conditional debugging and error messages. The
204 * output may be directed to stdout, stderr, and/or syslog based on
207 * Note: New line '\n' is added to the end of the text when printing to stdout.
209 void wpa_printf(int level, const char *fmt, ...)
214 if (level >= wpa_debug_level) {
215 #ifdef CONFIG_ANDROID_LOG
216 __android_log_vprint(wpa_to_android_level(level),
217 ANDROID_LOG_NAME, fmt, ap);
218 #else /* CONFIG_ANDROID_LOG */
219 #ifdef CONFIG_DEBUG_SYSLOG
220 if (wpa_debug_syslog) {
221 vsyslog(syslog_priority(level), fmt, ap);
223 #endif /* CONFIG_DEBUG_SYSLOG */
224 wpa_debug_print_timestamp();
225 #ifdef CONFIG_DEBUG_FILE
227 vfprintf(out_file, fmt, ap);
228 fprintf(out_file, "\n");
230 #endif /* CONFIG_DEBUG_FILE */
233 #ifdef CONFIG_DEBUG_FILE
235 #endif /* CONFIG_DEBUG_FILE */
236 #ifdef CONFIG_DEBUG_SYSLOG
238 #endif /* CONFIG_DEBUG_SYSLOG */
239 #endif /* CONFIG_ANDROID_LOG */
243 #ifdef CONFIG_DEBUG_LINUX_TRACING
244 if (wpa_debug_tracing_file != NULL) {
246 fprintf(wpa_debug_tracing_file, WPAS_TRACE_PFX, level);
247 vfprintf(wpa_debug_tracing_file, fmt, ap);
248 fprintf(wpa_debug_tracing_file, "\n");
249 fflush(wpa_debug_tracing_file);
252 #endif /* CONFIG_DEBUG_LINUX_TRACING */
256 static void _wpa_hexdump(int level, const char *title, const u8 *buf,
257 size_t len, int show)
261 #ifdef CONFIG_DEBUG_LINUX_TRACING
262 if (wpa_debug_tracing_file != NULL) {
263 fprintf(wpa_debug_tracing_file,
264 WPAS_TRACE_PFX "%s - hexdump(len=%lu):",
265 level, title, (unsigned long) len);
267 fprintf(wpa_debug_tracing_file, " [NULL]\n");
269 fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
271 for (i = 0; i < len; i++)
272 fprintf(wpa_debug_tracing_file,
275 fflush(wpa_debug_tracing_file);
277 #endif /* CONFIG_DEBUG_LINUX_TRACING */
279 if (level < wpa_debug_level)
281 #ifdef CONFIG_ANDROID_LOG
288 } else if (len == 0) {
290 } else if (show && len) {
291 /* Limit debug message length for Android log */
294 strbuf = os_malloc(1 + 3 * slen);
295 if (strbuf == NULL) {
296 wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
297 "allocate message buffer");
301 for (i = 0; i < slen; i++)
302 os_snprintf(&strbuf[i * 3], 4, " %02x",
307 display = " [REMOVED]";
310 __android_log_print(wpa_to_android_level(level),
312 "%s - hexdump(len=%lu):%s%s",
313 title, (long unsigned int) len, display,
314 len > slen ? " ..." : "");
315 bin_clear_free(strbuf, 1 + 3 * slen);
318 #else /* CONFIG_ANDROID_LOG */
319 #ifdef CONFIG_DEBUG_SYSLOG
320 if (wpa_debug_syslog) {
326 } else if (len == 0) {
328 } else if (show && len) {
329 strbuf = os_malloc(1 + 3 * len);
330 if (strbuf == NULL) {
331 wpa_printf(MSG_ERROR, "wpa_hexdump: Failed to "
332 "allocate message buffer");
336 for (i = 0; i < len; i++)
337 os_snprintf(&strbuf[i * 3], 4, " %02x",
342 display = " [REMOVED]";
345 syslog(syslog_priority(level), "%s - hexdump(len=%lu):%s",
346 title, (unsigned long) len, display);
347 bin_clear_free(strbuf, 1 + 3 * len);
350 #endif /* CONFIG_DEBUG_SYSLOG */
351 wpa_debug_print_timestamp();
352 #ifdef CONFIG_DEBUG_FILE
354 fprintf(out_file, "%s - hexdump(len=%lu):",
355 title, (unsigned long) len);
357 fprintf(out_file, " [NULL]");
359 for (i = 0; i < len; i++)
360 fprintf(out_file, " %02x", buf[i]);
362 fprintf(out_file, " [REMOVED]");
364 fprintf(out_file, "\n");
366 #endif /* CONFIG_DEBUG_FILE */
367 printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
371 for (i = 0; i < len; i++)
372 printf(" %02x", buf[i]);
374 printf(" [REMOVED]");
377 #ifdef CONFIG_DEBUG_FILE
379 #endif /* CONFIG_DEBUG_FILE */
380 #endif /* CONFIG_ANDROID_LOG */
383 void wpa_hexdump(int level, const char *title, const void *buf, size_t len)
385 _wpa_hexdump(level, title, buf, len, 1);
389 void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len)
391 _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys);
395 static void _wpa_hexdump_ascii(int level, const char *title, const void *buf,
396 size_t len, int show)
400 const size_t line_len = 16;
402 #ifdef CONFIG_DEBUG_LINUX_TRACING
403 if (wpa_debug_tracing_file != NULL) {
404 fprintf(wpa_debug_tracing_file,
405 WPAS_TRACE_PFX "%s - hexdump_ascii(len=%lu):",
406 level, title, (unsigned long) len);
408 fprintf(wpa_debug_tracing_file, " [NULL]\n");
410 fprintf(wpa_debug_tracing_file, " [REMOVED]\n");
412 /* can do ascii processing in userspace */
413 for (i = 0; i < len; i++)
414 fprintf(wpa_debug_tracing_file,
417 fflush(wpa_debug_tracing_file);
419 #endif /* CONFIG_DEBUG_LINUX_TRACING */
421 if (level < wpa_debug_level)
423 #ifdef CONFIG_ANDROID_LOG
424 _wpa_hexdump(level, title, buf, len, show);
425 #else /* CONFIG_ANDROID_LOG */
426 #ifdef CONFIG_DEBUG_SYSLOG
427 if (wpa_debug_syslog) {
428 _wpa_hexdump(level, title, buf, len, show);
431 #endif /* CONFIG_DEBUG_SYSLOG */
432 wpa_debug_print_timestamp();
433 #ifdef CONFIG_DEBUG_FILE
437 "%s - hexdump_ascii(len=%lu): [REMOVED]\n",
438 title, (unsigned long) len);
443 "%s - hexdump_ascii(len=%lu): [NULL]\n",
444 title, (unsigned long) len);
447 fprintf(out_file, "%s - hexdump_ascii(len=%lu):\n",
448 title, (unsigned long) len);
450 llen = len > line_len ? line_len : len;
451 fprintf(out_file, " ");
452 for (i = 0; i < llen; i++)
453 fprintf(out_file, " %02x", pos[i]);
454 for (i = llen; i < line_len; i++)
455 fprintf(out_file, " ");
456 fprintf(out_file, " ");
457 for (i = 0; i < llen; i++) {
459 fprintf(out_file, "%c", pos[i]);
461 fprintf(out_file, "_");
463 for (i = llen; i < line_len; i++)
464 fprintf(out_file, " ");
465 fprintf(out_file, "\n");
470 #endif /* CONFIG_DEBUG_FILE */
472 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
473 title, (unsigned long) len);
477 printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
478 title, (unsigned long) len);
481 printf("%s - hexdump_ascii(len=%lu):\n", title, (unsigned long) len);
483 llen = len > line_len ? line_len : len;
485 for (i = 0; i < llen; i++)
486 printf(" %02x", pos[i]);
487 for (i = llen; i < line_len; i++)
490 for (i = 0; i < llen; i++) {
492 printf("%c", pos[i]);
496 for (i = llen; i < line_len; i++)
502 #ifdef CONFIG_DEBUG_FILE
504 #endif /* CONFIG_DEBUG_FILE */
505 #endif /* CONFIG_ANDROID_LOG */
509 void wpa_hexdump_ascii(int level, const char *title, const void *buf,
512 _wpa_hexdump_ascii(level, title, buf, len, 1);
516 void wpa_hexdump_ascii_key(int level, const char *title, const void *buf,
519 _wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
523 #ifdef CONFIG_DEBUG_FILE
524 static char *last_path = NULL;
525 #endif /* CONFIG_DEBUG_FILE */
527 int wpa_debug_reopen_file(void)
529 #ifdef CONFIG_DEBUG_FILE
534 return 0; /* logfile not used */
536 tmp = os_strdup(last_path);
540 wpa_debug_close_file();
541 rv = wpa_debug_open_file(tmp);
544 #else /* CONFIG_DEBUG_FILE */
546 #endif /* CONFIG_DEBUG_FILE */
550 int wpa_debug_open_file(const char *path)
552 #ifdef CONFIG_DEBUG_FILE
558 if (last_path == NULL || os_strcmp(last_path, path) != 0) {
559 /* Save our path to enable re-open */
561 last_path = os_strdup(path);
564 out_fd = open(path, O_CREAT | O_APPEND | O_WRONLY,
565 S_IRUSR | S_IWUSR | S_IRGRP);
567 wpa_printf(MSG_ERROR,
568 "%s: Failed to open output file descriptor, using standard output",
574 if (fcntl(out_fd, F_SETFD, FD_CLOEXEC) < 0) {
575 wpa_printf(MSG_DEBUG,
576 "%s: Failed to set FD_CLOEXEC - continue without: %s",
577 __func__, strerror(errno));
579 #endif /* __linux__ */
581 out_file = fdopen(out_fd, "a");
582 if (out_file == NULL) {
583 wpa_printf(MSG_ERROR, "wpa_debug_open_file: Failed to open "
584 "output file, using standard output");
589 setvbuf(out_file, NULL, _IOLBF, 0);
591 #else /* CONFIG_DEBUG_FILE */
593 #endif /* CONFIG_DEBUG_FILE */
598 void wpa_debug_close_file(void)
600 #ifdef CONFIG_DEBUG_FILE
607 #endif /* CONFIG_DEBUG_FILE */
611 void wpa_debug_setup_stdout(void)
614 setvbuf(stdout, NULL, _IOLBF, 0);
618 #endif /* CONFIG_NO_STDOUT_DEBUG */
621 #ifndef CONFIG_NO_WPA_MSG
622 static wpa_msg_cb_func wpa_msg_cb = NULL;
624 void wpa_msg_register_cb(wpa_msg_cb_func func)
630 static wpa_msg_get_ifname_func wpa_msg_ifname_cb = NULL;
632 void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func)
634 wpa_msg_ifname_cb = func;
638 void wpa_msg(void *ctx, int level, const char *fmt, ...)
647 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
650 buf = os_malloc(buflen);
652 wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
658 if (wpa_msg_ifname_cb) {
659 const char *ifname = wpa_msg_ifname_cb(ctx);
661 int res = os_snprintf(prefix, sizeof(prefix), "%s: ",
663 if (os_snprintf_error(sizeof(prefix), res))
667 len = vsnprintf(buf, buflen, fmt, ap);
669 wpa_printf(level, "%s%s", prefix, buf);
671 wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
672 bin_clear_free(buf, buflen);
676 void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
687 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
690 buf = os_malloc(buflen);
692 wpa_printf(MSG_ERROR, "wpa_msg_ctrl: Failed to allocate "
697 len = vsnprintf(buf, buflen, fmt, ap);
699 wpa_msg_cb(ctx, level, WPA_MSG_PER_INTERFACE, buf, len);
700 bin_clear_free(buf, buflen);
704 void wpa_msg_global(void *ctx, int level, const char *fmt, ...)
712 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
715 buf = os_malloc(buflen);
717 wpa_printf(MSG_ERROR, "wpa_msg_global: Failed to allocate "
722 len = vsnprintf(buf, buflen, fmt, ap);
724 wpa_printf(level, "%s", buf);
726 wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
727 bin_clear_free(buf, buflen);
731 void wpa_msg_global_ctrl(void *ctx, int level, const char *fmt, ...)
742 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
745 buf = os_malloc(buflen);
747 wpa_printf(MSG_ERROR,
748 "wpa_msg_global_ctrl: Failed to allocate message buffer");
752 len = vsnprintf(buf, buflen, fmt, ap);
754 wpa_msg_cb(ctx, level, WPA_MSG_GLOBAL, buf, len);
755 bin_clear_free(buf, buflen);
759 void wpa_msg_no_global(void *ctx, int level, const char *fmt, ...)
767 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
770 buf = os_malloc(buflen);
772 wpa_printf(MSG_ERROR, "wpa_msg_no_global: Failed to allocate "
777 len = vsnprintf(buf, buflen, fmt, ap);
779 wpa_printf(level, "%s", buf);
781 wpa_msg_cb(ctx, level, WPA_MSG_NO_GLOBAL, buf, len);
782 bin_clear_free(buf, buflen);
786 void wpa_msg_global_only(void *ctx, int level, const char *fmt, ...)
794 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
797 buf = os_malloc(buflen);
799 wpa_printf(MSG_ERROR, "%s: Failed to allocate message buffer",
804 len = vsnprintf(buf, buflen, fmt, ap);
806 wpa_printf(level, "%s", buf);
808 wpa_msg_cb(ctx, level, WPA_MSG_ONLY_GLOBAL, buf, len);
812 #endif /* CONFIG_NO_WPA_MSG */
815 #ifndef CONFIG_NO_HOSTAPD_LOGGER
816 static hostapd_logger_cb_func hostapd_logger_cb = NULL;
818 void hostapd_logger_register_cb(hostapd_logger_cb_func func)
820 hostapd_logger_cb = func;
824 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
825 const char *fmt, ...)
833 buflen = vsnprintf(NULL, 0, fmt, ap) + 1;
836 buf = os_malloc(buflen);
838 wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
843 len = vsnprintf(buf, buflen, fmt, ap);
845 if (hostapd_logger_cb)
846 hostapd_logger_cb(ctx, addr, module, level, buf, len);
848 wpa_printf(MSG_DEBUG, "hostapd_logger: STA " MACSTR " - %s",
851 wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
852 bin_clear_free(buf, buflen);
854 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
857 const char * debug_level_str(int level)
878 int str_to_debug_level(const char *s)
880 if (os_strcasecmp(s, "EXCESSIVE") == 0)
881 return MSG_EXCESSIVE;
882 if (os_strcasecmp(s, "MSGDUMP") == 0)
884 if (os_strcasecmp(s, "DEBUG") == 0)
886 if (os_strcasecmp(s, "INFO") == 0)
888 if (os_strcasecmp(s, "WARNING") == 0)
890 if (os_strcasecmp(s, "ERROR") == 0)