From f978465fb36cc8ea799ba5553f5c6722c7cf241a Mon Sep 17 00:00:00 2001 From: Dario Lombardo Date: Sun, 23 Jul 2017 22:54:31 +0200 Subject: extcap: add debug log file feature to extcap-base. Change-Id: I3bfc9b2fd7b73dcfef52c5101360384d205c0d12 Reviewed-on: https://code.wireshark.org/review/22835 Petri-Dish: Dario Lombardo Tested-by: Petri Dish Buildbot Reviewed-by: Roland Knall --- extcap/androiddump.c | 61 +++++++++++++++++++++++++++++----------------------- extcap/ciscodump.c | 2 ++ extcap/extcap-base.c | 42 +++++++++++++++++++++++++++++++++++- extcap/extcap-base.h | 8 +++++-- extcap/randpktdump.c | 2 ++ extcap/sshdump.c | 2 ++ extcap/udpdump.c | 2 ++ 7 files changed, 89 insertions(+), 30 deletions(-) (limited to 'extcap') diff --git a/extcap/androiddump.c b/extcap/androiddump.c index fa612e41fb..5dbebd8a5b 100644 --- a/extcap/androiddump.c +++ b/extcap/androiddump.c @@ -1239,46 +1239,53 @@ static int register_interfaces(extcap_parameters * extcap_conf, const char *adb_ } static int list_config(char *interface) { + int ret = EXIT_CODE_INVALID_INTERFACE; + unsigned inc = 0; + if (!interface) { g_warning("No interface specified."); return EXIT_CODE_NO_INTERFACE_SPECIFIED; } if (is_specified_interface(interface, INTERFACE_ANDROID_BLUETOOTH_EXTERNAL_PARSER)) { - printf("arg {number=0}{call=--adb-server-ip}{display=ADB Server IP Address}{type=string}{default=127.0.0.1}\n" - "arg {number=1}{call=--adb-server-tcp-port}{display=ADB Server TCP Port}{type=integer}{range=0,65535}{default=5037}\n" - "arg {number=2}{call=--bt-server-tcp-port}{display=Bluetooth Server TCP Port}{type=integer}{range=0,65535}{default=4330}\n" - "arg {number=3}{call=--bt-forward-socket}{display=Forward Bluetooth Socket}{type=boolean}{default=false}\n" - "arg {number=4}{call=--bt-local-ip}{display=Bluetooth Local IP Address}{type=string}{default=127.0.0.1}\n" - "arg {number=5}{call=--bt-local-tcp-port}{display=Bluetooth Local TCP Port}{type=integer}{range=0,65535}{default=4330}{tooltip=Used to do \"adb forward tcp:LOCAL_TCP_PORT tcp:SERVER_TCP_PORT\"}\n" - "arg {number=6}{call=--verbose}{display=Verbose/Debug output on console}{type=boolean}{default=false}\n"); - return EXIT_CODE_SUCCESS; + printf("arg {number=%u}{call=--adb-server-ip}{display=ADB Server IP Address}{type=string}{default=127.0.0.1}\n", inc++); + printf("arg {number=%u}{call=--adb-server-tcp-port}{display=ADB Server TCP Port}{type=integer}{range=0,65535}{default=5037}\n", inc++); + printf("arg {number=%u}{call=--bt-server-tcp-port}{display=Bluetooth Server TCP Port}{type=integer}{range=0,65535}{default=4330}\n", inc++); + printf("arg {number=%u}{call=--bt-forward-socket}{display=Forward Bluetooth Socket}{type=boolean}{default=false}\n", inc++); + printf("arg {number=%u}{call=--bt-local-ip}{display=Bluetooth Local IP Address}{type=string}{default=127.0.0.1}\n", inc++); + printf("arg {number=%u}{call=--bt-local-tcp-port}{display=Bluetooth Local TCP Port}{type=integer}{range=0,65535}{default=4330}{tooltip=Used to do \"adb forward tcp:LOCAL_TCP_PORT tcp:SERVER_TCP_PORT\"}\n", inc++); + printf("arg {number=%u}{call=--verbose}{display=Verbose/Debug output on console}{type=boolean}{default=false}\n", inc++); + ret = EXIT_CODE_SUCCESS; } else if (is_specified_interface(interface, INTERFACE_ANDROID_BLUETOOTH_HCIDUMP) || is_specified_interface(interface, INTERFACE_ANDROID_BLUETOOTH_BTSNOOP_NET) || is_specified_interface(interface, INTERFACE_ANDROID_TCPDUMP)) { - printf("arg {number=0}{call=--adb-server-ip}{display=ADB Server IP Address}{type=string}{default=127.0.0.1}\n" - "arg {number=1}{call=--adb-server-tcp-port}{display=ADB Server TCP Port}{type=integer}{range=0,65535}{default=5037}\n" - "arg {number=2}{call=--verbose}{display=Verbose/Debug output on console}{type=boolean}{default=false}\n"); - return EXIT_CODE_SUCCESS; + printf("arg {number=%u}{call=--adb-server-ip}{display=ADB Server IP Address}{type=string}{default=127.0.0.1}\n", inc++); + printf("arg {number=%u}{call=--adb-server-tcp-port}{display=ADB Server TCP Port}{type=integer}{range=0,65535}{default=5037}\n", inc++); + printf("arg {number=%u}{call=--verbose}{display=Verbose/Debug output on console}{type=boolean}{default=false}\n", inc++); + ret = EXIT_CODE_SUCCESS; } else if (is_logcat_interface(interface)) { - printf("arg {number=0}{call=--adb-server-ip}{display=ADB Server IP Address}{type=string}{default=127.0.0.1}\n" - "arg {number=1}{call=--adb-server-tcp-port}{display=ADB Server TCP Port}{type=integer}{range=0,65535}{default=5037}\n" - "arg {number=2}{call=--logcat-text}{display=Use text logcat}{type=boolean}{default=false}\n" - "arg {number=3}{call=--logcat-ignore-log-buffer}{display=Ignore log buffer}{type=boolean}{default=false}\n" - "arg {number=4}{call=--logcat-custom-options}{display=Custom logcat parameters}{type=string}\n" - "arg {number=5}{call=--verbose}{display=Verbose/Debug output on console}{type=boolean}{default=false}\n"); - return EXIT_CODE_SUCCESS; + printf("arg {number=%u}{call=--adb-server-ip}{display=ADB Server IP Address}{type=string}{default=127.0.0.1}\n", inc++); + printf("arg {number=%u}{call=--adb-server-tcp-port}{display=ADB Server TCP Port}{type=integer}{range=0,65535}{default=5037}\n", inc++); + printf("arg {number=%u}{call=--logcat-text}{display=Use text logcat}{type=boolean}{default=false}\n", inc++); + printf("arg {number=%u}{call=--logcat-ignore-log-buffer}{display=Ignore log buffer}{type=boolean}{default=false}\n", inc++); + printf("arg {number=%u}{call=--logcat-custom-options}{display=Custom logcat parameters}{type=string}\n", inc++); + printf("arg {number=%u}{call=--verbose}{display=Verbose/Debug output on console}{type=boolean}{default=false}\n", inc++); + ret = EXIT_CODE_SUCCESS; } else if (is_logcat_text_interface(interface)) { - printf("arg {number=0}{call=--adb-server-ip}{display=ADB Server IP Address}{type=string}{default=127.0.0.1}\n" - "arg {number=1}{call=--adb-server-tcp-port}{display=ADB Server TCP Port}{type=integer}{range=0,65535}{default=5037}\n" - "arg {number=2}{call=--logcat-ignore-log-buffer}{display=Ignore log buffer}{type=boolean}{default=false}\n" - "arg {number=3}{call=--logcat-custom-options}{display=Custom logcat parameters}{type=string}\n" - "arg {number=4}{call=--verbose}{display=Verbose/Debug output on console}{type=boolean}{default=false}\n"); - return EXIT_CODE_SUCCESS; + printf("arg {number=%u}{call=--adb-server-ip}{display=ADB Server IP Address}{type=string}{default=127.0.0.1}\n", inc++); + printf("arg {number=%u}{call=--adb-server-tcp-port}{display=ADB Server TCP Port}{type=integer}{range=0,65535}{default=5037}\n", inc++); + printf("arg {number=%u}{call=--logcat-ignore-log-buffer}{display=Ignore log buffer}{type=boolean}{default=false}\n", inc++); + printf("arg {number=%u}{call=--logcat-custom-options}{display=Custom logcat parameters}{type=string}\n", inc++); + printf("arg {number=%u}{call=--verbose}{display=Verbose/Debug output on console}{type=boolean}{default=false}\n", inc++); + ret = EXIT_CODE_SUCCESS; } - g_warning("Invalid interface: <%s>", interface); - return EXIT_CODE_INVALID_INTERFACE; + if (ret != EXIT_CODE_SUCCESS) + g_warning("Invalid interface: <%s>", interface); + else + extcap_config_debug(&inc); + + return ret; } /*----------------------------------------------------------------------------*/ diff --git a/extcap/ciscodump.c b/extcap/ciscodump.c index 595ec82b46..224c30356b 100644 --- a/extcap/ciscodump.c +++ b/extcap/ciscodump.c @@ -512,6 +512,8 @@ static int list_config(char *interface, unsigned int remote_port) "{type=unsigned}{required=true}{tooltip=The number of remote packets to capture.}\n", inc++); + extcap_config_debug(&inc); + g_free(ipfilter); return EXIT_SUCCESS; diff --git a/extcap/extcap-base.c b/extcap/extcap-base.c index 6c1193d0ad..0e854d7c0c 100644 --- a/extcap/extcap-base.c +++ b/extcap/extcap-base.c @@ -29,6 +29,7 @@ #include #include #include +#include #ifdef HAVE_GETOPT_H #include @@ -57,6 +58,8 @@ typedef struct _extcap_option { char * optdesc; } extcap_option_t; +FILE* custom_log = NULL; + #ifdef _WIN32 BOOLEAN IsHandleRedirected(DWORD handle) { @@ -146,7 +149,22 @@ void extcap_base_set_util_info(extcap_parameters * extcap, const char * exename, extcap->helppage = g_strdup(helppage); } -uint8_t extcap_base_parse_options(extcap_parameters * extcap, int result, char * optargument ) +static void extcap_custom_log(const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data) +{ + if (log_level & G_LOG_LEVEL_DEBUG) { + if (!custom_log) + return; + fprintf(custom_log, "%s\n", message); + fflush(custom_log); + } else { + g_log_default_handler(log_domain, log_level, message, user_data); + } +} + +uint8_t extcap_base_parse_options(extcap_parameters * extcap, int result, char * optargument) { switch (result) { case EXTCAP_OPT_DEBUG: @@ -156,6 +174,10 @@ uint8_t extcap_base_parse_options(extcap_parameters * extcap, int result, char * setenv("G_MESSAGES_DEBUG", "all", 1); #endif break; + case EXTCAP_OPT_DEBUG_FILE: + extcap_init_custom_log(optargument); + g_log_set_default_handler(extcap_custom_log, NULL); + break; case EXTCAP_OPT_LIST_INTERFACES: extcap->do_list_interfaces = 1; break; @@ -330,6 +352,24 @@ void extcap_help_add_header(extcap_parameters * extcap, char * help_header) extcap_help_add_option(extcap, "--fifo ", "dump data to file or fifo"); extcap_help_add_option(extcap, "--extcap-version", "print tool version"); extcap_help_add_option(extcap, "--debug", "print additional messages"); + extcap_help_add_option(extcap, "--debug-file", "print debug messages to file"); +} + +void extcap_init_custom_log(const char* filename) +{ + custom_log = fopen(filename, "w"); + if (!custom_log) + g_error("Can't open custom log file: %s (%s)", filename, strerror(errno)); +} + +void extcap_config_debug(unsigned* count) +{ + printf("arg {number=%u}{call=--debug}{display=Run in debug mode}" + "{type=boolean}{default=false}{tooltip=Print debug messages}\n", + (*count)++); + printf("arg {number=%u}{call=--debug-file}{display=Use a file for debug}" + "{type=string}{tooltip=Set a file where the debug messages are written}\n", + (*count)++); } void extcap_cmdline_debug(char** ar, const unsigned n) diff --git a/extcap/extcap-base.h b/extcap/extcap-base.h index 6e7f751d58..7255efbc7a 100644 --- a/extcap/extcap-base.h +++ b/extcap/extcap-base.h @@ -54,7 +54,8 @@ EXTCAP_OPT_CAPTURE, \ EXTCAP_OPT_CAPTURE_FILTER, \ EXTCAP_OPT_FIFO, \ - EXTCAP_OPT_DEBUG + EXTCAP_OPT_DEBUG, \ + EXTCAP_OPT_DEBUG_FILE #define EXTCAP_BASE_OPTIONS \ @@ -66,7 +67,8 @@ { "capture", no_argument, NULL, EXTCAP_OPT_CAPTURE}, \ { "extcap-capture-filter", required_argument, NULL, EXTCAP_OPT_CAPTURE_FILTER}, \ { "fifo", required_argument, NULL, EXTCAP_OPT_FIFO}, \ - { "debug", optional_argument, NULL, EXTCAP_OPT_DEBUG} \ + { "debug", required_argument, NULL, EXTCAP_OPT_DEBUG}, \ + { "debug-file", required_argument, NULL, EXTCAP_OPT_DEBUG_FILE} #if defined(_WIN32) BOOLEAN IsHandleRedirected(DWORD handle); @@ -106,6 +108,8 @@ void extcap_help_add_header(extcap_parameters * extcap, char * help_header); void extcap_help_add_option(extcap_parameters * extcap, const char * help_option_name, const char * help_optionn_desc); void extcap_help_print(extcap_parameters * extcap); void extcap_cmdline_debug(char** ar, const unsigned n); +void extcap_init_custom_log(const char* filename); +void extcap_config_debug(unsigned* count); void extcap_base_help(void); #endif diff --git a/extcap/randpktdump.c b/extcap/randpktdump.c index 894c0c0c45..82cc7d9f45 100644 --- a/extcap/randpktdump.c +++ b/extcap/randpktdump.c @@ -121,6 +121,8 @@ static int list_config(char *interface) g_strfreev(longname_list); inc++; + extcap_config_debug(&inc); + return EXIT_SUCCESS; } diff --git a/extcap/sshdump.c b/extcap/sshdump.c index f7f8b28446..575ae49273 100644 --- a/extcap/sshdump.c +++ b/extcap/sshdump.c @@ -307,6 +307,8 @@ static int list_config(char *interface, unsigned int remote_port) "{type=unsigned}{default=0}{tooltip=The number of remote packets to capture. (Default: inf)}\n", inc++); + extcap_config_debug(&inc); + g_free(ipfilter); return EXIT_SUCCESS; diff --git a/extcap/udpdump.c b/extcap/udpdump.c index 90166d4505..f26a5ff4ae 100644 --- a/extcap/udpdump.c +++ b/extcap/udpdump.c @@ -113,6 +113,8 @@ static int list_config(char *interface) "{type=string}{default=data}{tooltip=The type used to describe the payload in the exported pdu format}\n", inc++); + extcap_config_debug(&inc); + return EXIT_SUCCESS; } -- cgit v1.2.3