dftest: Add debug command-line options

This commit is contained in:
João Valverde 2022-12-30 04:00:22 +00:00
parent 55ffdb08bb
commit d3d06c2552
4 changed files with 73 additions and 22 deletions

View File

@ -41,6 +41,18 @@
static void dftest_cmdarg_err(const char *fmt, va_list ap);
static void dftest_cmdarg_err_cont(const char *fmt, va_list ap);
static int debug_noisy = 0;
static int debug_flex = 0;
static int debug_lemon = 0;
static GOptionEntry entries[] =
{
{ "debug", 'd', 0, G_OPTION_ARG_NONE, &debug_noisy, "Enable verbose debug logs", NULL },
{ "debug-flex", 'f', 0, G_OPTION_ARG_NONE, &debug_flex, "Enable debugging for Flex", NULL },
{ "debug-lemon", 'l', 0, G_OPTION_ARG_NONE, &debug_lemon, "Enable debugging for Lemon", NULL },
{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, "", NULL }
};
static void
putloc(FILE *fp, df_loc_t loc)
{
@ -76,10 +88,13 @@ main(int argc, char **argv)
dfilter_t *df = NULL;
gchar *err_msg = NULL;
df_error_t *df_err = NULL;
unsigned df_flags;
GTimer *timer = NULL;
gdouble elapsed_expand, elapsed_compile;
gboolean ok;
int exit_status = 0;
GError *error = NULL;
GOptionContext *context;
cmdarg_err_init(dftest_cmdarg_err, dftest_cmdarg_err_cont);
@ -91,6 +106,27 @@ main(int argc, char **argv)
ws_noisy("Finished log init and parsing command line log arguments");
/*
* Set the C-language locale to the native environment and set the
* code page to UTF-8 on Windows.
*/
#ifdef _WIN32
setlocale(LC_ALL, ".UTF-8");
#else
setlocale(LC_ALL, "");
#endif
context = g_option_context_new("EXPR");
g_option_context_add_main_entries(context, entries, NULL);
if (!g_option_context_parse(context, &argc, &argv, &error)) {
printf("Error parsing arguments: %s\n", error->message);
g_error_free(error);
exit(1);
}
if (debug_noisy)
ws_log_set_noisy_filter("DFilter");
/*
* Get credential information for later use.
*/
@ -126,16 +162,6 @@ main(int argc, char **argv)
if (!epan_init(NULL, NULL, FALSE))
return 2;
/*
* Set the C-language locale to the native environment and set the
* code page to UTF-8 on Windows.
*/
#ifdef _WIN32
setlocale(LC_ALL, ".UTF-8");
#else
setlocale(LC_ALL, "");
#endif
/* Load libwireshark settings from the current profile. */
epan_load_settings();
@ -146,8 +172,10 @@ main(int argc, char **argv)
/* Check for filter on command line */
if (argc <= 1) {
fprintf(stderr, "Usage: dftest <filter>\n");
exit(1);
char *help = g_option_context_get_help(context, TRUE, NULL);
fprintf(stderr, "%s", help);
g_free(help);
goto out;
}
timer = g_timer_new();
@ -170,9 +198,13 @@ main(int argc, char **argv)
printf("Filter: %s\n", expanded_text);
/* Compile it */
df_flags = DF_SAVE_TREE;
if (debug_flex)
df_flags |= DF_DEBUG_FLEX;
if (debug_lemon)
df_flags |= DF_DEBUG_LEMON;
g_timer_start(timer);
ok = dfilter_compile_real(expanded_text, &df, &df_err,
DF_SAVE_TREE, "dftest");
ok = dfilter_compile_real(expanded_text, &df, &df_err, df_flags, "dftest");
g_timer_stop(timer);
elapsed_compile = g_timer_elapsed(timer, NULL);
if (!ok) {
@ -212,6 +244,7 @@ out:
g_free(expanded_text);
if (timer != NULL)
g_timer_destroy(timer);
g_option_context_free(context);
exit(exit_status);
}

View File

@ -121,14 +121,6 @@ dfilter_init(void)
/* Allocate an instance of our Lemon-based parser */
ParserObj = DfilterAlloc(g_malloc);
/* Enable parser tracing by defining AM_CFLAGS
* so that it contains "-DDFTRACE".
*/
#ifdef DFTRACE
/* Trace parser */
DfilterTrace(stdout, "lemon> ");
#endif
/* Initialize the syntax-tree sub-sub-system */
sttype_init();
@ -433,6 +425,18 @@ dfilter_compile_real(const gchar *text, dfilter_t **dfp,
df_set_extra(&state, scanner);
/* Enable/disable debugging for Flex. */
df_set_debug(flags & DF_DEBUG_FLEX, scanner);
#ifndef NDEBUG
/* Enable/disable debugging for Lemon. */
DfilterTrace(flags & DF_DEBUG_LEMON ? stderr : NULL, "lemon> ");
#else
if (flags & DF_DEBUG_LEMON) {
ws_message("Compile Wireshark without NDEBUG to enable Lemon debug traces");
}
#endif
while (1) {
df_lval = stnode_new_empty(STTYPE_UNINITIALIZED);
token = df_lex(scanner);

View File

@ -73,6 +73,10 @@ dfilter_error_free(df_error_t *);
#define DF_EXPAND_MACROS (1U << 1)
/* Do an optimization pass on the compiled filter. */
#define DF_OPTIMIZE (1U << 2)
/* Enable debug trace for flex. */
#define DF_DEBUG_FLEX (1U << 3)
/* Enable debug trace for lemon. */
#define DF_DEBUG_LEMON (1U << 4)
WS_DLL_PUBLIC
gboolean

View File

@ -15,6 +15,16 @@
#include "dfunctions.h"
}
/*
* Always generate warnings.
*/
%option warn
/*
* Generate debug code (output is off by default for reentrant scanners).
*/
%option debug
/*
* We want a reentrant scanner.
*/