dfilter: Split tostr() into debug and pretty print

This commit is contained in:
João Valverde 2021-10-11 12:45:13 +01:00 committed by Wireshark GitLab Utility
parent 5dd90e3b30
commit 07371d4557
9 changed files with 72 additions and 36 deletions

View File

@ -638,7 +638,7 @@ check_drange_sanity(dfwork_t *dfw, stnode_t *st)
check_drange_sanity(dfw, entity1);
} else if (entity1) {
dfilter_fail(dfw, "Range is not supported for entity %s of type %s",
stnode_tostr(entity1), stnode_type_name(entity1));
stnode_todisplay(entity1), stnode_type_name(entity1));
THROW(TypeError);
} else {
dfilter_fail(dfw, "Range is not supported, details: " G_STRLOC " entity: NULL");

View File

@ -51,7 +51,7 @@ function_dup(gconstpointer data)
}
static char *
function_tostr(const void *data)
function_tostr(const void *data, gboolean pretty)
{
const function_t *stfuncrec = (const function_t *)data;
const df_func_def_t *def = stfuncrec->funcdef;
@ -63,7 +63,7 @@ function_tostr(const void *data)
g_string_printf(repr, "%s(", def->name);
while (params != NULL) {
ws_assert(params->data);
g_string_append(repr, stnode_tostr(params->data));
g_string_append(repr, stnode_tostr(params->data, pretty));
params = params->next;
if (params != NULL) {
g_string_append(repr, ", ");

View File

@ -41,20 +41,23 @@ pcre_free(gpointer value)
}
static char *
fvalue_tostr(const void *data)
fvalue_tostr(const void *data, gboolean pretty)
{
fvalue_t *fvalue = (fvalue_t*)data;
char *s, *repr;
s = fvalue_to_string_repr(NULL, fvalue, FTREPR_DFILTER, BASE_NONE);
repr = g_strdup_printf("%s[%s]", fvalue_type_name(fvalue), s);
if (pretty)
repr = g_strdup(s);
else
repr = g_strdup_printf("%s[%s]", fvalue_type_name(fvalue), s);
g_free(s);
return repr;
}
static char *
field_tostr(const void *data)
field_tostr(const void *data, gboolean pretty _U_)
{
header_field_info *hfinfo = (header_field_info *)data;
@ -62,7 +65,7 @@ field_tostr(const void *data)
}
static char *
pcre_tostr(const void *data)
pcre_tostr(const void *data, gboolean pretty _U_)
{
const GRegex *pcre = (const GRegex *)data;

View File

@ -73,7 +73,7 @@ range_free(gpointer value)
}
static char *
range_tostr(const void *data)
range_tostr(const void *data, gboolean pretty)
{
range_t *range = (range_t*)data;
ws_assert_magic(range, RANGE_MAGIC);
@ -81,7 +81,7 @@ range_tostr(const void *data)
drange_str = drange_tostr(range->drange);
repr = g_strdup_printf("%s[%s]",
stnode_tostr(range->entity),
stnode_tostr(range->entity, pretty),
drange_str);
g_free(drange_str);

View File

@ -42,7 +42,7 @@ sttype_set_free(gpointer value)
}
static char *
sttype_set_tostr(const void *data)
sttype_set_tostr(const void *data, gboolean pretty)
{
GSList* nodelist = (GSList *)data;
stnode_t *lower, *upper;
@ -50,7 +50,7 @@ sttype_set_tostr(const void *data)
while (nodelist) {
lower = nodelist->data;
g_string_append(repr, stnode_tostr(lower));
g_string_append(repr, stnode_tostr(lower, pretty));
/* Set elements are always in pairs; upper may be null. */
nodelist = g_slist_next(nodelist);
@ -58,7 +58,7 @@ sttype_set_tostr(const void *data)
upper = nodelist->data;
if (upper != NULL) {
g_string_append(repr, "..");
g_string_append(repr, stnode_tostr(upper));
g_string_append(repr, stnode_tostr(upper, pretty));
}
nodelist = g_slist_next(nodelist);

View File

@ -28,7 +28,7 @@ string_free(gpointer value)
}
static char *
string_tostr(const void *data)
string_tostr(const void *data, gboolean pretty _U_)
{
return g_strdup(data);
}

View File

@ -65,7 +65,7 @@ test_free(gpointer value)
}
static char *
test_tostr(const void *value)
test_tostr(const void *value, gboolean pretty _U_)
{
const test_t *test = (const test_t *)value;
ws_assert_magic(test, TEST_MAGIC);

View File

@ -88,8 +88,10 @@ _node_clear(stnode_t *node)
node->type = NULL;
node->flags = 0;
node->data = NULL;
g_free(node->repr);
node->repr = NULL;
g_free(node->repr_display);
node->repr_display = NULL;
g_free(node->repr_debug);
node->repr_debug = NULL;
}
void
@ -109,7 +111,8 @@ _node_init(stnode_t *node, sttype_id_t type_id, gpointer data)
ws_assert(!node->type);
ws_assert(!node->data);
node->flags = 0;
node->repr = NULL;
node->repr_display = NULL;
node->repr_debug = NULL;
if (type_id == STTYPE_UNINITIALIZED) {
node->type = NULL;
@ -256,18 +259,38 @@ stnode_set_inside_parens(stnode_t *node, gboolean inside)
}
}
const char *
stnode_tostr(stnode_t *node)
static char *
_node_tostr(stnode_t *node, gboolean pretty)
{
if (node->repr != NULL)
return node->repr;
const char *s;
if (node->type->func_tostr == NULL)
node->repr = g_strdup("<FIXME>");
s = "FIXME";
else
node->repr = node->type->func_tostr(node->data);
s = node->type->func_tostr(node->data, pretty);
return node->repr;
if (pretty)
return g_strdup(s);
return g_strdup_printf("%s<%s>", stnode_type_name(node), s);
}
const char *
stnode_tostr(stnode_t *node, gboolean pretty)
{
if (pretty && node->repr_display != NULL)
return node->repr_display;
if (!pretty && node->repr_debug != NULL)
return node->repr_debug;
char *str = _node_tostr(node, pretty);
if (pretty)
node->repr_display = str;
else
node->repr_debug = str;
return str;
}
static char *
@ -276,13 +299,15 @@ sprint_node(stnode_t *node)
wmem_strbuf_t *buf = wmem_strbuf_new(NULL, NULL);
wmem_strbuf_append_printf(buf, "stnode <%p> = {\n", (void *)node);
wmem_strbuf_append_printf(buf, "\tmagic = %"PRIx32"\n", node->magic);
wmem_strbuf_append_printf(buf, "\ttype = %s\n", stnode_type_name(node));
wmem_strbuf_append_printf(buf,
"\tflags = %"PRIx16" (inside_parens = %s)\n",
node->flags, true_or_false(stnode_inside_parens(node)));
wmem_strbuf_append_printf(buf, "\tdata = %s<%s>\n", stnode_type_name(node), stnode_tostr(node));
wmem_strbuf_append_printf(buf, "}\n");
wmem_strbuf_append_printf(buf, "\tmagic = 0x%"PRIx32"\n", node->magic);
wmem_strbuf_append_printf(buf, "\ttype = <%p>\n", (void *)(node->type));
wmem_strbuf_append_printf(buf, "\tdata = %s\n", stnode_todebug(node));
wmem_strbuf_append_printf(buf, "\tflags (0x%04"PRIx16") = {\n", node->flags);
wmem_strbuf_append_printf(buf, "\t\tinside_parens = %s\n",
true_or_false(stnode_inside_parens(node)));
wmem_strbuf_append(buf, "\t}\n");
wmem_strbuf_append_printf(buf, "\ttoken_value = \"%s\"\n", stnode_token_value(node));
wmem_strbuf_append(buf, "}\n");
return wmem_strbuf_finalize(buf);
}
@ -314,7 +339,7 @@ visit_tree(wmem_strbuf_t *buf, stnode_t *node, int level)
stnode_t *left, *right;
if (stnode_type_id(node) == STTYPE_TEST) {
wmem_strbuf_append_printf(buf, "%s(", stnode_tostr(node));
wmem_strbuf_append_printf(buf, "%s(", stnode_todisplay(node));
sttype_test_get(node, NULL, &left, &right);
if (left && right) {
wmem_strbuf_append_c(buf, '\n');
@ -337,7 +362,7 @@ visit_tree(wmem_strbuf_t *buf, stnode_t *node, int level)
wmem_strbuf_append(buf, ")");
}
else {
wmem_strbuf_append_printf(buf, "%s<%s>", stnode_type_name(node), stnode_tostr(node));
wmem_strbuf_append(buf, stnode_todebug(node));
}
}

View File

@ -37,7 +37,7 @@ typedef enum {
typedef gpointer (*STTypeNewFunc)(gpointer);
typedef gpointer (*STTypeDupFunc)(gconstpointer);
typedef void (*STTypeFreeFunc)(gpointer);
typedef char* (*STTypeToStrFunc)(gconstpointer);
typedef char* (*STTypeToStrFunc)(gconstpointer, gboolean pretty);
/* Type information */
@ -59,7 +59,8 @@ typedef struct {
uint16_t flags;
gpointer data;
char *token_value;
char *repr;
char *repr_display;
char *repr_debug;
} stnode_t;
/* These are the sttype_t registration function prototypes. */
@ -114,7 +115,14 @@ const char *
stnode_token_value(stnode_t *node);
const char *
stnode_tostr(stnode_t *node);
stnode_tostr(stnode_t *node, gboolean pretty);
#define stnode_todisplay(node) stnode_tostr(node, TRUE)
#define stnode_todebug(node) stnode_tostr(node, FALSE)
const char *
stnode_repr(stnode_t *node);
gboolean
stnode_inside_parens(stnode_t *node);