Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugger filterx #356

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
4e837da
cfg-source: add num_lines argument to _print_underlined_source_block()
bazsi Oct 12, 2024
2480d7b
cfg-source: refactor source file printing to use line numbers
bazsi Oct 12, 2024
81cd8c9
cfg-source: add cfg_source_print_source_text() function
bazsi Oct 11, 2024
6881193
debugger: add source display using the cfg-source module
bazsi Oct 11, 2024
2c62279
debugger: extract _set_command()
bazsi Oct 12, 2024
87fe40d
logpipe: add PIF_BREAKPOINT flag
bazsi Oct 13, 2024
01bd755
debugger: add debugger mode
bazsi Oct 11, 2024
64de89e
debugger: filter out uninteresting stop events
bazsi Oct 13, 2024
14b4d28
debugger: add step and follow commands
bazsi Oct 13, 2024
ebfaa60
debugger: clean up trace command
bazsi Dec 10, 2024
bfd41fc
debugger: add welcome blurb on startup
bazsi Oct 13, 2024
393a07c
filterx: make FilterXExpr ref_cnt atomic
bazsi Oct 26, 2024
66594ce
filterx: return list of exprs from the grammar
bazsi Oct 26, 2024
c027897
filterx: don't require filterx expr parsing to run in the main thread
bazsi Oct 26, 2024
ef019fe
filterx: rename error formatting functions for consistency
bazsi Oct 26, 2024
b65048e
filterx: add filterx_error_format() function
bazsi Oct 27, 2024
6598d98
cfg-source: fix off-by-one in report_buffer_location()
bazsi Oct 27, 2024
186334d
debugger: add path_options argument to debugger stop/trace entry points
bazsi Oct 24, 2024
fb10503
debugger: move commands into their separate implementation file
bazsi Oct 26, 2024
bcd453e
debugger: implement printx command to evaluate filterx expressions
bazsi Oct 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/cfg-grammar-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "filter/filter-pipe.h"
#include "filterx/filterx-parser.h"
#include "filterx/filterx-expr.h"
#include "filterx/expr-compound.h"
#include "filterx/filterx-pipe.h"
#include "parser/parser-expr-parser.h"
#include "rewrite/rewrite-expr-parser.h"
Expand Down
12 changes: 8 additions & 4 deletions lib/cfg-grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -712,13 +712,17 @@ filter_content
;

filterx_content
: _filterx_context_push <ptr>{
FilterXExpr *filterx_block = NULL;
: '{' _filterx_context_push <ptr>{
GList *filterx_expr_list = NULL;

CHECK_ERROR_WITHOUT_MESSAGE(cfg_parser_parse(&filterx_parser, lexer, (gpointer *) &filterx_block, NULL), @$);
CHECK_ERROR_WITHOUT_MESSAGE(cfg_parser_parse(&filterx_parser, lexer, (gpointer *) &filterx_expr_list, NULL), @$);

FilterXExpr *filterx_block = filterx_compound_expr_new(FALSE);
filterx_compound_expr_add_list(filterx_block, filterx_expr_list);
filterx_expr_set_location_with_text(filterx_block, lexer, &@$, "{ ... }");

$$ = log_expr_node_new_pipe(log_filterx_pipe_new(filterx_block, configuration), &@$);
} _filterx_context_pop { $$ = $2; }
} _filterx_context_pop '}' { $$ = $3; }
;

parser_content
Expand Down
84 changes: 51 additions & 33 deletions lib/cfg-source.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,24 @@ _print_underline(const gchar *line, gint whitespace_before, gint number_of_caret
}

static void
_print_underlined_source_block(const CFG_LTYPE *yylloc, gchar **lines, gint error_index)
_print_underlined_source_block(const CFG_LTYPE *yylloc, gchar **lines, gsize num_lines, gint start_line)
{
gint line_ndx;
gchar line_prefix[12];
gint error_length = yylloc->last_line - yylloc->first_line + 1;

for (line_ndx = 0; lines[line_ndx]; line_ndx++)
for (line_ndx = 0; line_ndx < num_lines; line_ndx++)
{
gint lineno = yylloc->first_line + line_ndx - error_index;
gint lineno = start_line + line_ndx;
const gchar *line = lines[line_ndx];
gint line_len = strlen(line);
gboolean line_ends_with_newline = line_len > 0 && line[line_len - 1] == '\n';

_format_source_prefix(line_prefix, sizeof(line_prefix), lineno,
line_ndx >= error_index && line_ndx < error_index + error_length);
lineno >= yylloc->first_line && lineno <= yylloc->last_line);

fprintf(stderr, "%-8s%s%s", line_prefix, line, line_ends_with_newline ? "" : "\n");

if (line_ndx == error_index)
if (lineno == yylloc->first_line)
{
/* print the underline right below the source line we just printed */
fprintf(stderr, "%-8s", line_prefix);
Expand All @@ -84,78 +83,97 @@ _print_underlined_source_block(const CFG_LTYPE *yylloc, gchar **lines, gint erro
multi_line ? strlen(&line[yylloc->first_column]) + 1
: yylloc->last_column - yylloc->first_column);
}
else if (line_ndx >= error_index + CONTEXT)
break;
}
}

static void
_report_file_location(const gchar *filename, const CFG_LTYPE *yylloc)
_report_file_location(const gchar *filename, const CFG_LTYPE *yylloc, gint start_line)
{
FILE *f;
gint lineno = 0;
gsize buflen = 65520;
gchar *buf = g_malloc(buflen);
GPtrArray *context = g_ptr_array_new();
gint error_index = 0;
gint end_line = start_line + 2*CONTEXT;

if (start_line <= 0)
{
start_line = yylloc->first_line > CONTEXT ? yylloc->first_line - CONTEXT : 1;
end_line = yylloc->first_line + CONTEXT;
}

f = fopen(filename, "r");
if (f)
{
while (fgets(buf, buflen, f))
{
lineno++;
if (lineno > (gint) yylloc->first_line + CONTEXT)
if (lineno > end_line)
break;
else if (lineno < (gint) yylloc->first_line - CONTEXT)
else if (lineno < start_line)
continue;
else if (lineno == yylloc->first_line)
error_index = context->len;
g_ptr_array_add(context, g_strdup(buf));
}
/* NOTE: do we have the appropriate number of lines? */
if (lineno <= yylloc->first_line)
goto exit;
g_ptr_array_add(context, NULL);
fclose(f);
}
if (context->len > 0)
_print_underlined_source_block(yylloc, (gchar **) context->pdata, error_index);
_print_underlined_source_block(yylloc, (gchar **) context->pdata, context->len, start_line);

exit:
g_free(buf);
g_ptr_array_foreach(context, (GFunc) g_free, NULL);
g_ptr_array_free(context, TRUE);
}

/* this will report source content from the buffer, but use the line numbers
* of the file where the block was defined.
*
* buffer_* => tracks buffer related information
* file_* => tracks file related information
*/
static void
_report_buffer_location(const gchar *buffer_content, const CFG_LTYPE *file_lloc, const CFG_LTYPE *buf_lloc)
{
gchar **lines = g_strsplit(buffer_content, "\n", buf_lloc->first_line + CONTEXT + 1);
gint num_lines = g_strv_length(lines);
gchar **buffer_lines = g_strsplit(buffer_content, "\n", buf_lloc->first_line + CONTEXT + 1);
gint buffer_num_lines = g_strv_length(buffer_lines);

if (num_lines <= buf_lloc->first_line)
if (buffer_num_lines < buf_lloc->first_line)
goto exit;

gint start = buf_lloc->first_line - 1 - CONTEXT;
gint error_index = CONTEXT;
if (start < 0)
{
error_index += start;
start = 0;
}
_print_underlined_source_block(file_lloc, &lines[start], error_index);
/* the line number in the file, which we report in the source dump, 1 based */
gint range_backwards = CONTEXT;
if (file_lloc->first_line <= range_backwards)
range_backwards = file_lloc->first_line - 1;

/* the index of the line in the buffer where we start printing 0-based */
gint buffer_start_index = buf_lloc->first_line - 1 - range_backwards;
if (buffer_start_index < 0)
buffer_start_index = 0;

_print_underlined_source_block(file_lloc, &buffer_lines[buffer_start_index], buffer_num_lines - buffer_start_index,
file_lloc->first_line - range_backwards);

exit:
g_strfreev(lines);
g_strfreev(buffer_lines);
}

gboolean
cfg_source_print_source_text(const gchar *filename, gint line, gint column, gint start_line)
{
CFG_LTYPE yylloc = {0};

yylloc.name = filename;
yylloc.first_line = yylloc.last_line = line;
yylloc.first_column = yylloc.last_column = column;
_report_file_location(yylloc.name, &yylloc, start_line);
return TRUE;
}

gboolean
cfg_source_print_source_context(CfgLexer *lexer, CfgIncludeLevel *level, const CFG_LTYPE *yylloc)
{
if (level->include_type == CFGI_FILE)
{
_report_file_location(yylloc->name, yylloc);
_report_file_location(yylloc->name, yylloc, -1);
}
else if (level->include_type == CFGI_BUFFER)
{
Expand Down
2 changes: 2 additions & 0 deletions lib/cfg-source.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include "cfg-lexer.h"

gboolean cfg_source_print_source_text(const gchar *filename, gint line, gint column, gint offset);

/* These functions are only available during parsing */
gboolean cfg_source_print_source_context(CfgLexer *lexer, CfgIncludeLevel *level, const CFG_LTYPE *yylloc);
gboolean cfg_source_extract_source_text(CfgLexer *lexer, const CFG_LTYPE *yylloc, GString *result);
Expand Down
6 changes: 6 additions & 0 deletions lib/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,12 @@ cfg_run_parser(GlobalConfig *self, CfgLexer *lexer, CfgParser *parser, gpointer
return res;
}

gboolean
cfg_parsing_in_progress(void)
{
return configuration != NULL;
}

gboolean
cfg_run_parser_with_main_context(GlobalConfig *self, CfgLexer *lexer, CfgParser *parser, gpointer *result, gpointer arg,
const gchar *desc)
Expand Down
1 change: 1 addition & 0 deletions lib/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ GlobalConfig *cfg_new_subordinate(GlobalConfig *master);
gboolean cfg_run_parser(GlobalConfig *self, CfgLexer *lexer, CfgParser *parser, gpointer *result, gpointer arg);
gboolean cfg_run_parser_with_main_context(GlobalConfig *self, CfgLexer *lexer, CfgParser *parser, gpointer *result,
gpointer arg, const gchar *desc);
gboolean cfg_parsing_in_progress(void);
gboolean cfg_read_config(GlobalConfig *cfg, const gchar *fname, gchar *preprocess_into);
void cfg_shutdown(GlobalConfig *self);
gboolean cfg_is_shutting_down(GlobalConfig *cfg);
Expand Down
14 changes: 13 additions & 1 deletion lib/debugger/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
debuggerincludedir = ${pkgincludedir}/debugger

EXTRA_DIST += lib/debugger/CMakeLists.txt
EXTRA_DIST += lib/debugger/CMakeLists.txt \
cmd-continue.c \
cmd-display.c \
cmd-drop.c \
cmd-follow.c \
cmd-help.c \
cmd-info.c \
cmd-list.c \
cmd-print.c \
cmd-printx.c \
cmd-quit.c \
cmd-step.c \
cmd-trace.c

debuggerinclude_HEADERS = \
lib/debugger/debugger.h \
Expand Down
32 changes: 32 additions & 0 deletions lib/debugger/cmd-continue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2015 Balabit
* Copyright (c) 2015 Balázs Scheidler
* Copyright (c) 2024 Balázs Scheidler <[email protected]>
* Copyright (c) 2024 Axoflow
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

static gboolean
_cmd_continue(Debugger *self, gint argc, gchar *argv[])
{
_set_mode(self, DBG_WAITING_FOR_BREAKPOINT, FALSE);
return FALSE;
}
42 changes: 42 additions & 0 deletions lib/debugger/cmd-display.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2015 Balabit
* Copyright (c) 2015 Balázs Scheidler
* Copyright (c) 2024 Balázs Scheidler <[email protected]>
* Copyright (c) 2024 Axoflow
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

static gboolean
_cmd_display(Debugger *self, gint argc, gchar *argv[])
{
if (argc == 2)
{
GError *error = NULL;
if (!log_template_compile(self->display_template, argv[1], &error))
{
printf("display: Error compiling template: %s\n", error->message);
g_clear_error(&error);
return TRUE;
}
}
printf("display: The template is set to: \"%s\"\n", self->display_template->template_str);
return TRUE;
}
32 changes: 32 additions & 0 deletions lib/debugger/cmd-drop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2015 Balabit
* Copyright (c) 2015 Balázs Scheidler
* Copyright (c) 2024 Balázs Scheidler <[email protected]>
* Copyright (c) 2024 Axoflow
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

static gboolean
_cmd_drop(Debugger *self, gint argc, gchar *argv[])
{
self->breakpoint_site->drop = TRUE;
return FALSE;
}
32 changes: 32 additions & 0 deletions lib/debugger/cmd-follow.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2015 Balabit
* Copyright (c) 2015 Balázs Scheidler
* Copyright (c) 2024 Balázs Scheidler <[email protected]>
* Copyright (c) 2024 Axoflow
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

static gboolean
_cmd_follow(Debugger *self, gint argc, gchar *argv[])
{
_set_mode(self, DBG_FOLLOW_AND_BREAK, TRUE);
return FALSE;
}
Loading
Loading