From: Mike Stepanek (mstepane) Date: Mon, 27 Jun 2022 10:47:40 +0000 (+0000) Subject: Pull request #3477: Refactor: move trace related files from main to trace folder X-Git-Tag: 3.1.33.0~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1a1e882220def5f203e4ed813bcaaff441d35951;p=thirdparty%2Fsnort3.git Pull request #3477: Refactor: move trace related files from main to trace folder Merge in SNORT/snort3 from ~ASERBENI/snort3:trace_refactor to master Squashed commit of the following: commit 2e074bcbe3732bdc06c3cc6d2cbfb4c1a80e84a7 Author: Andrii Serbeniuk Date: Wed Jun 15 13:34:38 2022 +0300 main: move trace related code to trace folder --- diff --git a/src/actions/act_react.cc b/src/actions/act_react.cc index 0c205c9e7..4ea94e633 100644 --- a/src/actions/act_react.cc +++ b/src/actions/act_react.cc @@ -52,12 +52,12 @@ #include "framework/module.h" #include "log/messages.h" #include "main/snort_config.h" -#include "main/snort_debug.h" #include "packet_io/active.h" #include "payload_injector/payload_injector.h" #include "profiler/profiler.h" #include "protocols/packet.h" #include "service_inspectors/http2_inspect/http2_flow_data.h" +#include "trace/trace_api.h" #include "utils/util.h" #include "utils/util_cstring.h" diff --git a/src/decompress/file_olefile.h b/src/decompress/file_olefile.h index 10aecdcf2..75171787d 100644 --- a/src/decompress/file_olefile.h +++ b/src/decompress/file_olefile.h @@ -29,7 +29,7 @@ #include "detection/detection_engine.h" #include "helpers/literal_search.h" #include "ips_options/ips_vba_data.h" -#include "main/snort_debug.h" +#include "trace/trace_api.h" #include "utils/util.h" #include "utils/util_utf.h" diff --git a/src/detection/context_switcher.cc b/src/detection/context_switcher.cc index f3c2256ba..0839d6e26 100644 --- a/src/detection/context_switcher.cc +++ b/src/detection/context_switcher.cc @@ -26,8 +26,8 @@ #include -#include "main/snort_debug.h" #include "packet_io/active.h" +#include "trace/trace_api.h" #include "utils/stats.h" #include "detect_trace.h" diff --git a/src/detection/detection_engine.cc b/src/detection/detection_engine.cc index 469b26837..e81bc97c7 100644 --- a/src/detection/detection_engine.cc +++ b/src/detection/detection_engine.cc @@ -31,7 +31,6 @@ #include "latency/packet_latency.h" #include "main/analyzer.h" #include "main/snort_config.h" -#include "main/snort_debug.h" #include "main/thread.h" #include "managers/inspector_manager.h" #include "managers/mpse_manager.h" @@ -42,6 +41,7 @@ #include "protocols/packet.h" #include "stream/stream.h" #include "time/packet_time.h" +#include "trace/trace_api.h" #include "utils/stats.h" #include "context_switcher.h" diff --git a/src/detection/detection_options.h b/src/detection/detection_options.h index 4582a577d..aaf9f760b 100644 --- a/src/detection/detection_options.h +++ b/src/detection/detection_options.h @@ -35,7 +35,7 @@ #include "detection/rule_option_types.h" #include "time/clock_defs.h" -#include "main/snort_debug.h" +#include "trace/trace_api.h" namespace snort { diff --git a/src/detection/fp_detect.cc b/src/detection/fp_detect.cc index e356ee8a5..67040823e 100644 --- a/src/detection/fp_detect.cc +++ b/src/detection/fp_detect.cc @@ -52,7 +52,6 @@ #include "log/messages.h" #include "main/snort.h" #include "main/snort_config.h" -#include "main/snort_debug.h" #include "managers/action_manager.h" #include "packet_io/active.h" #include "packet_tracer/packet_tracer.h" @@ -63,6 +62,7 @@ #include "protocols/udp.h" #include "search_engines/pat_stats.h" #include "stream/stream.h" +#include "trace/trace_api.h" #include "utils/stats.h" #include "utils/util.h" diff --git a/src/detection/tag.cc b/src/detection/tag.cc index 7592d210f..89abec907 100644 --- a/src/detection/tag.cc +++ b/src/detection/tag.cc @@ -31,10 +31,10 @@ #include "hash/xhash.h" #include "log/messages.h" #include "main/snort_config.h" -#include "main/snort_debug.h" #include "parser/parser.h" #include "protocols/packet.h" #include "sfip/sf_ip.h" +#include "trace/trace_api.h" #include "utils/cpp_macros.h" #include "utils/util.h" diff --git a/src/file_api/file_flows.cc b/src/file_api/file_flows.cc index 1d84a212c..068b50f03 100644 --- a/src/file_api/file_flows.cc +++ b/src/file_api/file_flows.cc @@ -35,7 +35,7 @@ #include "managers/inspector_manager.h" #include "packet_tracer/packet_tracer.h" #include "protocols/packet.h" -#include "main/snort_debug.h" +#include "trace/trace_api.h" #include "file_cache.h" #include "file_config.h" diff --git a/src/file_api/file_module.h b/src/file_api/file_module.h index 923b568d8..97d120083 100644 --- a/src/file_api/file_module.h +++ b/src/file_api/file_module.h @@ -26,7 +26,7 @@ #include "file_config.h" #include "file_identifier.h" -#include "main/snort_debug.h" +#include "trace/trace_api.h" #include "utils/util.h" //------------------------------------------------------------------------- // file_id module diff --git a/src/flow/flow_cache.cc b/src/flow/flow_cache.cc index 8630ece92..75677b378 100644 --- a/src/flow/flow_cache.cc +++ b/src/flow/flow_cache.cc @@ -28,12 +28,12 @@ #include "hash/hash_defs.h" #include "hash/zhash.h" #include "helpers/flag_context.h" -#include "main/snort_debug.h" #include "memory/memory_cap.h" #include "packet_io/active.h" #include "packet_tracer/packet_tracer.h" #include "stream/base/stream_module.h" #include "time/packet_time.h" +#include "trace/trace_api.h" #include "utils/stats.h" #include "flow.h" diff --git a/src/flow/test/flow_cache_test.cc b/src/flow/test/flow_cache_test.cc index 2be521139..1e0e40cfe 100644 --- a/src/flow/test/flow_cache_test.cc +++ b/src/flow/test/flow_cache_test.cc @@ -29,7 +29,6 @@ #include "detection/detection_engine.h" #include "main/policy.h" #include "main/snort_config.h" -#include "main/snort_debug.h" #include "managers/inspector_manager.h" #include "packet_io/active.h" #include "packet_tracer/packet_tracer.h" @@ -44,6 +43,7 @@ #include "flow/flow_cache.h" #include "flow/ha.h" #include "flow/session.h" +#include "trace/trace_api.h" #include #include diff --git a/src/latency/packet_latency.cc b/src/latency/packet_latency.cc index 733a9ad89..836567a8b 100644 --- a/src/latency/packet_latency.cc +++ b/src/latency/packet_latency.cc @@ -27,8 +27,8 @@ #include "detection/detection_engine.h" #include "log/messages.h" #include "main/snort_config.h" -#include "main/snort_debug.h" #include "protocols/packet.h" +#include "trace/trace_api.h" #include "utils/stats.h" #include "latency_config.h" diff --git a/src/main.cc b/src/main.cc index 05577699d..02eafc3ea 100644 --- a/src/main.cc +++ b/src/main.cc @@ -38,7 +38,6 @@ #include "main/shell.h" #include "main/snort.h" #include "main/snort_config.h" -#include "main/snort_debug.h" #include "main/snort_module.h" #include "main/swapper.h" #include "main/thread_config.h" diff --git a/src/main/CMakeLists.txt b/src/main/CMakeLists.txt index da6580c96..1c7d59edb 100644 --- a/src/main/CMakeLists.txt +++ b/src/main/CMakeLists.txt @@ -7,7 +7,6 @@ set (INCLUDES reload_tuner.h snort.h snort_config.h - snort_debug.h snort_types.h swapper.h thread.h @@ -43,7 +42,6 @@ add_library (main OBJECT snort.cc snort.h snort_config.cc - snort_debug.cc snort_module.h snort_module.cc swapper.cc diff --git a/src/main/snort_debug.cc b/src/main/snort_debug.cc deleted file mode 100644 index 7b53c43ab..000000000 --- a/src/main/snort_debug.cc +++ /dev/null @@ -1,342 +0,0 @@ -//-------------------------------------------------------------------------- -// Copyright (C) 2014-2022 Cisco and/or its affiliates. All rights reserved. -// Copyright (C) 2002-2013 Sourcefire, Inc. -// Copyright (C) 1998-2002 Martin Roesch -// -// This program is free software; you can redistribute it and/or modify it -// under the terms of the GNU General Public License Version 2 as published -// by the Free Software Foundation. You may not use, modify or distribute -// this program under any other version of the GNU General Public License. -// -// This program 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 -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License along -// with this program; if not, write to the Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -//-------------------------------------------------------------------------- - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include "snort_debug.h" - -#include - -#include "trace/trace_api.h" -#include "utils/safec.h" - -#define BUF_SIZE_MIN (1 << 10) // guaranteed size, this one will be allocated on stack -#define BUF_SIZE_MAX (1 << 20) // this prevents unlimited memory allocation - -namespace snort -{ -template -void trace_vprintf(const char* name, TraceLevel log_level, - const char* trace_option, const Packet* p, const char* fmt, va_list ap) -{ - char buf[BUF_SIZE_MIN]; - int n; - - va_list dup_ap; - va_copy(dup_ap, ap); - - n = vsnprintf(buf, sizeof(buf), fmt, ap); - - if (n < 0) - { - std::string err_msg = "trace_vprintf: printf error: "; - err_msg += strerror(errno); - err_msg += "\n"; - log_func(err_msg.c_str(), name, log_level, trace_option, p); - } - else if (n < (int)sizeof(buf)) - { - log_func(buf, name, log_level, trace_option, p); - } - else if (n < BUF_SIZE_MAX) - { - char* d_buf = new char[n + 1]; - - if (d_buf) - { - vsnprintf(d_buf, n + 1, fmt, dup_ap); - log_func(d_buf, name, log_level, trace_option, p); - } - else - log_func("trace_vprintf: out of memory\n", name, log_level, trace_option, p); - - delete[] d_buf; - } - else - { - n = BUF_SIZE_MAX; - char* d_buf = new char[n]; - - if (d_buf) - { - log_func("trace_vprintf: next message will be truncated\n", name, log_level, trace_option, p); - - vsnprintf(d_buf, n, fmt, dup_ap); - d_buf[n - 2] = '\n'; - d_buf[n - 1] = '\0'; - log_func(d_buf, name, log_level, trace_option, p); - } - else - log_func("trace_vprintf: out of memory\n", name, log_level, trace_option, p); - - delete[] d_buf; - } - - va_end(dup_ap); -} - -void trace_vprintf(const char* name, TraceLevel log_level, - const char* trace_option, const Packet* p, const char* fmt, va_list ap) -{ - trace_vprintf(name, log_level, trace_option, p, fmt, ap); -} -} - -#ifdef UNIT_TEST -#include - -#ifdef DEBUG_MSGS - -#include "framework/module.h" - -using namespace snort; - -//stringify the expansion of the macro -#define sx(code) sm(code) - -//stringify the macro -#define sm(code) #code - -struct TestCase -{ - const char* test; - const char* expected; -}; - -static char testing_dump[BUF_SIZE_MAX * 2]; - -static void test_log(const char* log_msg, const char* name, - uint8_t log_level, const char* trace_option, const snort::Packet*) -{ - snprintf(testing_dump, sizeof(testing_dump), "%s:%s:%d: %s", - name, trace_option, log_level, log_msg); -} - -TEST_CASE("macros", "[trace]") -{ - TestCase cases[] = - { - { - sx(debug_log(1, test_trace, "my message")), - "trace_print(1, test_trace, \"my message\")" - }, - { - sx(debug_log(test_trace, my_flags, "my message")), - "trace_print(test_trace, my_flags, \"my message\")" - }, - { - sx(debug_logf(1, test_trace, "%s %s", "my", "message")), - "trace_printf(1, test_trace, \"%s %s\", \"my\", \"message\")" - }, - { - sx(debug_logf(test_trace, my_flags, "%s %s", "my", "message")), - "trace_printf(test_trace, my_flags, \"%s %s\", \"my\", \"message\")" - } - }; - - CHECK( !strcmp(cases[0].expected, cases[0].test) ); - CHECK( !strcmp(cases[1].expected, cases[1].test) ); - CHECK( !strcmp(cases[2].expected, cases[2].test) ); - CHECK( !strcmp(cases[3].expected, cases[3].test) ); -} - -#undef trace_print -#undef trace_printf - -//These templates expand to replace the default expansion of trace_vprintf. -//This custom expansion replaces log_func (expands to TraceApi::log()) -//with test_log for capturing what would be passed to the console. -#define trace_print trace_print> -#define trace_printf trace_printf> - -class TraceTestModule : public Module -{ -public: - TraceTestModule(const char* name, const TraceOption* trace_options) : - Module(name, "trace_test_help"), test_trace_options(trace_options) - { } - - const TraceOption* get_trace_options() const override - { return test_trace_options; } - -private: - const TraceOption* test_trace_options; -}; - -TEST_CASE("debug_log, debug_logf", "[trace]") -{ - enum - { - TEST_TRACE_OPTION1 = 0, - TEST_TRACE_OPTION2, - TEST_TRACE_OPTION3, - TEST_TRACE_OPTION4, - TEST_TRACE_OPTION5, - }; - - const TraceOption test_trace_values[] = - { - { "option1", TEST_TRACE_OPTION1, "help_option1" }, - { "option2", TEST_TRACE_OPTION2, "help_option2" }, - { "option3", TEST_TRACE_OPTION3, "help_option3" }, - { "option4", TEST_TRACE_OPTION4, "help_option4" }, - { "option5", TEST_TRACE_OPTION5, "help_option5" }, - - { nullptr, 0, nullptr }, - }; - - TraceOption test_trace_options(nullptr, 0, nullptr); - TraceTestModule trace_test_module("test_module", &test_trace_options); - Trace test_trace(trace_test_module); - - TraceTestModule trace_test_module_opt("test_opt_module", test_trace_values); - Trace test_opt_trace(trace_test_module_opt); - - test_trace.set("all", 0); - - testing_dump[0] = '\0'; - debug_log(&test_trace, nullptr, "my message"); - CHECK( testing_dump[0] == '\0' ); - - test_trace.set("all", 1); - test_opt_trace.set("option1", 1); - test_opt_trace.set("option2", 2); - test_opt_trace.set("option3", 3); - test_opt_trace.set("option4", 2); - test_opt_trace.set("option5", 2); - - char message[BUF_SIZE_MIN + 1]; - for( int i = 0; i < BUF_SIZE_MIN; i++ ) - message[i] = 'A'; - message[BUF_SIZE_MIN] = '\0'; - - testing_dump[0] = '\0'; - debug_log(6, &test_opt_trace, TEST_TRACE_OPTION3, nullptr, message); - CHECK( (strlen(testing_dump) == 0) ); - - testing_dump[0] = '\0'; - debug_log(&test_trace, nullptr, "my message"); - CHECK( !strcmp(testing_dump, "test_module:all:1: my message") ); - - testing_dump[0] = '\0'; - debug_logf(&test_trace, nullptr, "%s %s", "my", "message"); - CHECK( !strcmp(testing_dump, "test_module:all:1: my message") ); - - testing_dump[0] = '\0'; - debug_log(&test_opt_trace, TEST_TRACE_OPTION1, nullptr, "log option1 message"); - CHECK( !strcmp(testing_dump, "test_opt_module:option1:1: log option1 message") ); - - testing_dump[0] = '\0'; - debug_logf(&test_opt_trace, TEST_TRACE_OPTION1, nullptr, "%s %s %s", "log", "option1", "message"); - CHECK( !strcmp(testing_dump, "test_opt_module:option1:1: log option1 message") ); - - testing_dump[0] = '\0'; - debug_log(3, &test_opt_trace, TEST_TRACE_OPTION2, nullptr, "log option2 message"); - CHECK( testing_dump[0] == '\0' ); - - testing_dump[0] = '\0'; - debug_log(&test_opt_trace, TEST_TRACE_OPTION2, nullptr, "log option2 message"); - CHECK( !strcmp(testing_dump, "test_opt_module:option2:1: log option2 message") ); - - testing_dump[0] = '\0'; - debug_logf(&test_opt_trace, TEST_TRACE_OPTION2, nullptr, "%s %s %s", "log", "option2", "message"); - CHECK( !strcmp(testing_dump, "test_opt_module:option2:1: log option2 message") ); - - testing_dump[0] = '\0'; - debug_log(6, &test_opt_trace, TEST_TRACE_OPTION3, nullptr, "log option3 message"); - CHECK( testing_dump[0] == '\0' ); - - testing_dump[0] = '\0'; - debug_log(3, &test_opt_trace, TEST_TRACE_OPTION3, nullptr, "log option3 message"); - CHECK( !strcmp(testing_dump, "test_opt_module:option3:3: log option3 message") ); - - testing_dump[0] = '\0'; - debug_logf(3, &test_opt_trace, TEST_TRACE_OPTION3, nullptr, "%s %s %s", "log", "option3", "message"); - CHECK( !strcmp(testing_dump, "test_opt_module:option3:3: log option3 message") ); - - testing_dump[0] = '\0'; - debug_log(2, &test_opt_trace, TEST_TRACE_OPTION4, nullptr, "log option4 message"); - CHECK( !strcmp(testing_dump, "test_opt_module:option4:2: log option4 message") ); - - testing_dump[0] = '\0'; - debug_logf(2, &test_opt_trace, TEST_TRACE_OPTION4, nullptr, "%s %s %s", "log", "option4", "message"); - CHECK( !strcmp(testing_dump, "test_opt_module:option4:2: log option4 message") ); - - testing_dump[0] = '\0'; - debug_log(4, &test_opt_trace, TEST_TRACE_OPTION5, nullptr, "log option5 message"); - CHECK( testing_dump[0] == '\0' ); -} - -TEST_CASE("trace big message", "[trace]") -{ - TraceOption test_trace_options(nullptr, 0, nullptr); - TraceTestModule trace_test_module("test_module", &test_trace_options); - Trace test_trace(trace_test_module); - - const int hdr_size = strlen("test_module:all:1: "); - const char exp_1[] = "test_module:all:1: 1111111111111111111111111111"; - const char exp_2[] = "test_module:all:1: 2222222222222222222222222222"; - const char exp_3[] = "test_module:all:1: 3333333333333333333333333333"; - const char exp_4[] = "test_module:all:1: 4444444444444444444444444444"; - - - char msg_1[BUF_SIZE_MIN * 1]; - char msg_2[BUF_SIZE_MIN * 2]; - char msg_3[BUF_SIZE_MAX * 1]; - char msg_4[BUF_SIZE_MAX * 2]; - - test_trace.set("all", 1); - - memset(msg_1, '1', sizeof(msg_1)); - memset(msg_2, '2', sizeof(msg_2)); - memset(msg_3, '3', sizeof(msg_3)); - memset(msg_4, '4', sizeof(msg_4)); - - msg_1[sizeof(msg_1) - 1] = '\0'; - msg_2[sizeof(msg_2) - 1] = '\0'; - msg_3[sizeof(msg_3) - 1] = '\0'; - msg_4[sizeof(msg_4) - 1] = '\0'; - - memset(testing_dump, '\0', sizeof(testing_dump)); - debug_log(&test_trace, nullptr, msg_1); - CHECK( strlen(testing_dump) == hdr_size + strlen(msg_1) ); - CHECK( !strncmp(testing_dump, exp_1, strlen(exp_1)) ); - - memset(testing_dump, '\0', sizeof(testing_dump)); - debug_log(&test_trace, nullptr, msg_2); - CHECK( strlen(testing_dump) == hdr_size + strlen(msg_2) ); - CHECK( !strncmp(testing_dump, exp_2, strlen(exp_2)) ); - - memset(testing_dump, '\0', sizeof(testing_dump)); - debug_log(&test_trace, nullptr, msg_3); - CHECK( strlen(testing_dump) == hdr_size + strlen(msg_3) ); - CHECK( !strncmp(testing_dump, exp_3, strlen(exp_3)) ); - - memset(testing_dump, '\0', sizeof(testing_dump)); - debug_log(&test_trace, nullptr, msg_4); - CHECK( strlen(testing_dump) == hdr_size + BUF_SIZE_MAX - 1 ); - CHECK( !strncmp(testing_dump, exp_4, strlen(exp_4)) ); -} - -#endif // DEBUG_MSGS - -#endif // UNIT_TEST diff --git a/src/main/snort_debug.h b/src/main/snort_debug.h deleted file mode 100644 index cce96afa5..000000000 --- a/src/main/snort_debug.h +++ /dev/null @@ -1,214 +0,0 @@ -//-------------------------------------------------------------------------- -// Copyright (C) 2014-2022 Cisco and/or its affiliates. All rights reserved. -// Copyright (C) 2002-2013 Sourcefire, Inc. -// Copyright (C) 1998-2002 Martin Roesch -// -// This program is free software; you can redistribute it and/or modify it -// under the terms of the GNU General Public License Version 2 as published -// by the Free Software Foundation. You may not use, modify or distribute -// this program under any other version of the GNU General Public License. -// -// This program 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 -// General Public License for more details. -// -// You should have received a copy of the GNU General Public License along -// with this program; if not, write to the Free Software Foundation, Inc., -// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -//-------------------------------------------------------------------------- - -#ifndef SNORT_DEBUG_H -#define SNORT_DEBUG_H - -// this provides a module trace capability that can be set by config to -// turn on the output of specific debug messages. -// - -#include - -#include "protocols/packet.h" -#include "trace/trace.h" -#include "trace/trace_api.h" - -static inline bool trace_enabled(const snort::Trace* trace, - TraceOptionID trace_option_id, - TraceLevel log_level = DEFAULT_TRACE_LOG_LEVEL, - const snort::Packet* p = nullptr) -{ - if ( !trace or !trace->enabled(trace_option_id, log_level) ) - return false; - - if ( !p ) - return true; - - const auto gid = snort::TraceApi::get_constraints_generation(); - if ( !p->filtering_state.was_checked(gid) ) - snort::TraceApi::filter(*p); - - return p->filtering_state.matched; -} - -namespace snort -{ -SO_PUBLIC void trace_vprintf(const char* name, TraceLevel log_level, - const char* trace_option, const snort::Packet* p, const char* fmt, va_list); -} - -using trace_func = void(const char*, TraceLevel, const char*, const snort::Packet*, const char*, va_list); - -template -static inline void trace_uprintf(const snort::Trace* trace, - TraceOptionID trace_option_id, const snort::Packet* p, const char* fmt, ...) __attribute__((format (printf, 4, 5))); - -template -static inline void trace_uprintf(const snort::Trace* trace, - TraceOptionID trace_option_id, const snort::Packet* p, const char* fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - - const char* trace_option_name = trace->option_name(trace_option_id); - trace_vprintf(trace->module_name(), DEFAULT_TRACE_LOG_LEVEL, trace_option_name, p, fmt, ap); - - va_end(ap); -} - -template -static inline void trace_printf(TraceLevel log_level, - const snort::Trace* trace, TraceOptionID trace_option_id, - const snort::Packet* p, const char* fmt, ...) - __attribute__((format (printf, 5, 6))); - -template -static inline void trace_printf(TraceLevel log_level, - const snort::Trace* trace, TraceOptionID trace_option_id, - const snort::Packet* p, const char* fmt, ...) -{ - if ( !trace_enabled(trace, trace_option_id, log_level, p) ) - return; - - va_list ap; - va_start(ap, fmt); - - const char* trace_option_name = trace->option_name(trace_option_id); - trace_vprintf(trace->module_name(), log_level, trace_option_name, p, - fmt, ap); - - va_end(ap); -} - -template -static inline void trace_printf(TraceLevel log_level, - const snort::Trace* trace, const snort::Packet* p, - const char* fmt, ...) __attribute__((format (printf, 4, 5))); - -template -static inline void trace_printf(TraceLevel log_level, - const snort::Trace* trace, const snort::Packet* p, - const char* fmt, ...) -{ - if ( !trace_enabled(trace, DEFAULT_TRACE_OPTION_ID, log_level, p) ) - return; - - va_list ap; - va_start(ap, fmt); - - const char* trace_option_name = trace->option_name(DEFAULT_TRACE_OPTION_ID); - trace_vprintf(trace->module_name(), log_level, trace_option_name, p, - fmt, ap); - - va_end(ap); -} - -template -static inline void trace_printf(const snort::Trace* trace, - TraceOptionID trace_option_id, const snort::Packet* p, - const char* fmt, ...) __attribute__((format (printf, 4, 5))); - -template -static inline void trace_printf(const snort::Trace* trace, - TraceOptionID trace_option_id, const snort::Packet* p, const char* fmt, ...) -{ - if ( !trace_enabled(trace, trace_option_id, DEFAULT_TRACE_LOG_LEVEL, p) ) - return; - - va_list ap; - va_start(ap, fmt); - - const char* trace_option_name = trace->option_name(trace_option_id); - trace_vprintf(trace->module_name(), DEFAULT_TRACE_LOG_LEVEL, - trace_option_name, p, fmt, ap); - - va_end(ap); -} - -template -static inline void trace_printf(const snort::Trace* trace, - const snort::Packet* p, const char* fmt, ...) - __attribute__((format (printf, 3, 4))); - -template -static inline void trace_printf(const snort::Trace* trace, - const snort::Packet* p, const char* fmt, ...) -{ - if ( !trace_enabled(trace, DEFAULT_TRACE_OPTION_ID, DEFAULT_TRACE_LOG_LEVEL, p) ) - return; - - va_list ap; - va_start(ap, fmt); - - const char* trace_option_name = trace->option_name(DEFAULT_TRACE_OPTION_ID); - trace_vprintf(trace->module_name(), DEFAULT_TRACE_LOG_LEVEL, - trace_option_name, p, fmt, ap); - - va_end(ap); -} - -template -static inline void trace_print(TraceLevel log_level, - const snort::Trace* trace, TraceOptionID trace_option_id, - const snort::Packet* p, const char* msg) -{ - trace_printf(log_level, trace, trace_option_id, p, - "%s", msg); -} - -template -static inline void trace_print(const snort::Trace* trace, - TraceOptionID trace_option_id, const snort::Packet* p, const char* msg) -{ - trace_printf(trace, trace_option_id, p, "%s", msg); -} - -template -static inline void trace_print(TraceLevel log_level, - const snort::Trace* trace, const snort::Packet* p, const char* msg) -{ - trace_printf(log_level, trace, p, "%s", msg); -} - -template -static inline void trace_print(const snort::Trace* trace, const snort::Packet* p, - const char* msg) -{ - trace_printf(trace, p, "%s", msg); -} - -#define trace_print trace_print -#define trace_printf trace_printf -#define trace_uprintf trace_uprintf - -#define trace_log(...) trace_print(__VA_ARGS__) -#define trace_logf(...) trace_printf(__VA_ARGS__) -#define trace_ulogf(...) trace_uprintf(__VA_ARGS__) - -#ifdef DEBUG_MSGS -#define debug_log trace_log -#define debug_logf trace_logf -#else -#define debug_log(...) -#define debug_logf(...) -#endif - -#endif diff --git a/src/main/snort_module.cc b/src/main/snort_module.cc index 7cb81cd2c..24b46eca1 100644 --- a/src/main/snort_module.cc +++ b/src/main/snort_module.cc @@ -33,7 +33,6 @@ #include "framework/parameter.h" #include "log/messages.h" #include "main.h" -#include "main/snort_debug.h" #include "managers/codec_manager.h" #include "packet_io/sfdaq_config.h" #include "packet_io/trough.h" @@ -41,6 +40,7 @@ #include "parser/parser.h" #include "parser/parse_utils.h" #include "parser/vars.h" +#include "trace/trace_api.h" #include "trace/trace_config.h" #if defined(UNIT_TEST) || defined(BENCHMARK_TEST) diff --git a/src/managers/inspector_manager.cc b/src/managers/inspector_manager.cc index 39d15dd49..cc926718e 100644 --- a/src/managers/inspector_manager.cc +++ b/src/managers/inspector_manager.cc @@ -37,7 +37,6 @@ #include "main/shell.h" #include "main/snort.h" #include "main/snort_config.h" -#include "main/snort_debug.h" #include "main/snort_module.h" #include "main/thread_config.h" #include "protocols/packet.h" @@ -45,6 +44,7 @@ #include "target_based/snort_protocols.h" #include "time/clock_defs.h" #include "time/stopwatch.h" +#include "trace/trace_api.h" #include "module_manager.h" diff --git a/src/mime/file_mime_paf.cc b/src/mime/file_mime_paf.cc index d040cd87b..630828d23 100644 --- a/src/mime/file_mime_paf.cc +++ b/src/mime/file_mime_paf.cc @@ -28,7 +28,7 @@ #include -#include "main/snort_debug.h" +#include "trace/trace_api.h" using namespace snort; diff --git a/src/network_inspectors/appid/detector_plugins/detector_pattern.cc b/src/network_inspectors/appid/detector_plugins/detector_pattern.cc index dff7e0e24..7a3a4bed5 100644 --- a/src/network_inspectors/appid/detector_plugins/detector_pattern.cc +++ b/src/network_inspectors/appid/detector_plugins/detector_pattern.cc @@ -26,9 +26,9 @@ #include "detector_pattern.h" #include "log/messages.h" -#include "main/snort_debug.h" #include "protocols/packet.h" #include "search_engines/search_tool.h" +#include "trace/trace_api.h" #include "app_info_table.h" #include "appid_inspector.h" diff --git a/src/network_inspectors/appid/lua_detector_api.cc b/src/network_inspectors/appid/lua_detector_api.cc index d8dd5d685..6c28d3dcb 100644 --- a/src/network_inspectors/appid/lua_detector_api.cc +++ b/src/network_inspectors/appid/lua_detector_api.cc @@ -31,10 +31,10 @@ #include "host_tracker/cache_allocator.cc" #include "host_tracker/host_cache.h" #include "log/messages.h" -#include "main/snort_debug.h" #include "main/snort_types.h" #include "profiler/profiler.h" #include "protocols/packet.h" +#include "trace/trace_api.h" #include "app_info_table.h" #include "appid_debug.h" diff --git a/src/network_inspectors/appid/tp_appid_utils.cc b/src/network_inspectors/appid/tp_appid_utils.cc index 35b36879b..b75558180 100644 --- a/src/network_inspectors/appid/tp_appid_utils.cc +++ b/src/network_inspectors/appid/tp_appid_utils.cc @@ -27,10 +27,10 @@ #include #include "log/messages.h" -#include "main/snort_debug.h" #include "profiler/profiler.h" #include "protocols/packet.h" #include "stream/stream.h" +#include "trace/trace_api.h" #include "app_info_table.h" #include "appid_config.h" diff --git a/src/network_inspectors/appid/tp_lib_handler.cc b/src/network_inspectors/appid/tp_lib_handler.cc index 2f0658bc7..09313ce22 100644 --- a/src/network_inspectors/appid/tp_lib_handler.cc +++ b/src/network_inspectors/appid/tp_lib_handler.cc @@ -26,8 +26,8 @@ #include "appid_config.h" -#include "main/snort_debug.h" #include "log/messages.h" +#include "trace/trace_api.h" #include "tp_lib_handler.h" diff --git a/src/network_inspectors/rna/rna_module.h b/src/network_inspectors/rna/rna_module.h index 8d327711d..b7882c82e 100644 --- a/src/network_inspectors/rna/rna_module.h +++ b/src/network_inspectors/rna/rna_module.h @@ -23,8 +23,8 @@ #include "framework/module.h" #include "main/reload_tuner.h" -#include "main/snort_debug.h" #include "profiler/profiler.h" +#include "trace/trace_api.h" #include "rna_config.h" #include "rna_fingerprint.h" diff --git a/src/ports/port_table.cc b/src/ports/port_table.cc index 5fad5a930..c6abfcaff 100644 --- a/src/ports/port_table.cc +++ b/src/ports/port_table.cc @@ -29,7 +29,7 @@ #include "hash/hash_defs.h" #include "hash/hash_key_operations.h" #include "log/messages.h" -#include "main/snort_debug.h" +#include "trace/trace_api.h" #include "utils/util.h" #include "utils/util_cstring.h" diff --git a/src/protocols/packet_manager.cc b/src/protocols/packet_manager.cc index 3a629a6b3..60a8bef7e 100644 --- a/src/protocols/packet_manager.cc +++ b/src/protocols/packet_manager.cc @@ -31,11 +31,11 @@ #include "detection/detection_engine.h" #include "log/text_log.h" #include "main/snort_config.h" -#include "main/snort_debug.h" #include "packet_io/active.h" #include "packet_io/sfdaq.h" #include "profiler/profiler_defs.h" #include "stream/stream.h" +#include "trace/trace_api.h" #include "eth.h" #include "icmp4.h" diff --git a/src/service_inspectors/dce_rpc/dce_smb_common.h b/src/service_inspectors/dce_rpc/dce_smb_common.h index 3cbb25959..1d579318d 100644 --- a/src/service_inspectors/dce_rpc/dce_smb_common.h +++ b/src/service_inspectors/dce_rpc/dce_smb_common.h @@ -25,9 +25,9 @@ // Also provides common functions used by both versions #include "file_api/file_api.h" -#include "main/snort_debug.h" #include "protocols/packet.h" #include "profiler/profiler_defs.h" +#include "trace/trace_api.h" #include "dce_common.h" #include "dce_smb_module.h" diff --git a/src/service_inspectors/dce_rpc/dce_smb_transaction.cc b/src/service_inspectors/dce_rpc/dce_smb_transaction.cc index dd5c03fd0..0dbb5d6d4 100644 --- a/src/service_inspectors/dce_rpc/dce_smb_transaction.cc +++ b/src/service_inspectors/dce_rpc/dce_smb_transaction.cc @@ -30,7 +30,7 @@ #include "dce_smb_module.h" #include "dce_smb_transaction_utils.h" -#include "main/snort_debug.h" +#include "trace/trace_api.h" using namespace snort; diff --git a/src/service_inspectors/dce_rpc/dce_smb_utils.cc b/src/service_inspectors/dce_rpc/dce_smb_utils.cc index c08e52f03..6d84ed20e 100644 --- a/src/service_inspectors/dce_rpc/dce_smb_utils.cc +++ b/src/service_inspectors/dce_rpc/dce_smb_utils.cc @@ -30,9 +30,9 @@ #include "file_api/file_api.h" #include "hash/hash_key_operations.h" #include "main/snort.h" -#include "main/snort_debug.h" #include "network_inspectors/packet_tracer/packet_tracer.h" #include "packet_io/active.h" +#include "trace/trace_api.h" #include "utils/util.h" #include "dce_smb_module.h" diff --git a/src/service_inspectors/dce_rpc/dce_udp_processing.cc b/src/service_inspectors/dce_rpc/dce_udp_processing.cc index 68277da07..dc4be569d 100644 --- a/src/service_inspectors/dce_rpc/dce_udp_processing.cc +++ b/src/service_inspectors/dce_rpc/dce_udp_processing.cc @@ -33,7 +33,7 @@ #include "dce_udp.h" #include "flow/session.h" -#include "main/snort_debug.h" +#include "trace/trace_api.h" #include "utils/safec.h" #include "utils/util.h" diff --git a/src/service_inspectors/dce_rpc/smb_message.cc b/src/service_inspectors/dce_rpc/smb_message.cc index b7082d25c..07f872a3f 100644 --- a/src/service_inspectors/dce_rpc/smb_message.cc +++ b/src/service_inspectors/dce_rpc/smb_message.cc @@ -30,10 +30,10 @@ #include "dce_smb_transaction.h" #include "detection/detect.h" #include "file_api/file_service.h" -#include "main/snort_debug.h" #include "memory/memory_cap.h" #include "packet_io/active.h" #include "protocols/packet.h" +#include "trace/trace_api.h" #include "utils/util.h" using namespace snort; diff --git a/src/service_inspectors/gtp/gtp.cc b/src/service_inspectors/gtp/gtp.cc index f6656c916..d16866c4b 100644 --- a/src/service_inspectors/gtp/gtp.cc +++ b/src/service_inspectors/gtp/gtp.cc @@ -26,8 +26,8 @@ #include "gtp.h" -#include "main/snort_debug.h" #include "protocols/packet.h" +#include "trace/trace_api.h" #include "gtp_inspect.h" #include "gtp_module.h" diff --git a/src/service_inspectors/gtp/gtp_parser.cc b/src/service_inspectors/gtp/gtp_parser.cc index c56422365..96440e01f 100644 --- a/src/service_inspectors/gtp/gtp_parser.cc +++ b/src/service_inspectors/gtp/gtp_parser.cc @@ -31,7 +31,7 @@ #include "detection/detection_engine.h" #include "events/event_queue.h" #include "log/messages.h" -#include "main/snort_debug.h" +#include "trace/trace_api.h" #include "utils/util_cstring.h" #include "gtp.h" diff --git a/src/service_inspectors/http_inspect/http_flow_data.cc b/src/service_inspectors/http_inspect/http_flow_data.cc index 2c74979a1..e44ab62d4 100644 --- a/src/service_inspectors/http_inspect/http_flow_data.cc +++ b/src/service_inspectors/http_inspect/http_flow_data.cc @@ -24,9 +24,9 @@ #include "http_flow_data.h" #include "decompress/file_decomp.h" -#include "main/snort_debug.h" #include "mime/file_mime_process.h" #include "service_inspectors/http2_inspect/http2_flow_data.h" +#include "trace/trace_api.h" #include "utils/js_identifier_ctx.h" #include "utils/js_normalizer.h" diff --git a/src/service_inspectors/http_inspect/http_js_norm.cc b/src/service_inspectors/http_inspect/http_js_norm.cc index 8423fc5ac..196220ddb 100644 --- a/src/service_inspectors/http_inspect/http_js_norm.cc +++ b/src/service_inspectors/http_inspect/http_js_norm.cc @@ -23,7 +23,7 @@ #include "http_js_norm.h" -#include "main/snort_debug.h" +#include "trace/trace_api.h" #include "utils/js_normalizer.h" #include "utils/safec.h" #include "utils/util_jsnorm.h" diff --git a/src/service_inspectors/iec104/iec104_decode.cc b/src/service_inspectors/iec104/iec104_decode.cc index 0f91d4f92..9309a02fa 100644 --- a/src/service_inspectors/iec104/iec104_decode.cc +++ b/src/service_inspectors/iec104/iec104_decode.cc @@ -29,8 +29,8 @@ #include "detection/detection_engine.h" #include "events/event_queue.h" #include "log/messages.h" -#include "main/snort_debug.h" #include "protocols/packet.h" +#include "trace/trace_api.h" #include "iec104.h" #include "iec104_module.h" diff --git a/src/service_inspectors/mms/mms_decode.cc b/src/service_inspectors/mms/mms_decode.cc index 1756b3916..9fcb5951e 100644 --- a/src/service_inspectors/mms/mms_decode.cc +++ b/src/service_inspectors/mms/mms_decode.cc @@ -29,9 +29,9 @@ #include "detection/detection_engine.h" #include "events/event_queue.h" #include "log/messages.h" -#include "main/snort_debug.h" #include "managers/plugin_manager.h" #include "protocols/packet.h" +#include "trace/trace_api.h" #include "utils/util_ber.h" #include "mms.h" diff --git a/src/service_inspectors/sip/sip_config.cc b/src/service_inspectors/sip/sip_config.cc index 61478b848..6414c4d9d 100644 --- a/src/service_inspectors/sip/sip_config.cc +++ b/src/service_inspectors/sip/sip_config.cc @@ -28,7 +28,7 @@ #include #include "log/messages.h" -#include "main/snort_debug.h" +#include "trace/trace_api.h" #include "utils/util.h" using namespace snort; diff --git a/src/service_inspectors/ssl/ssl_inspector.cc b/src/service_inspectors/ssl/ssl_inspector.cc index d11b05413..f958edac9 100644 --- a/src/service_inspectors/ssl/ssl_inspector.cc +++ b/src/service_inspectors/ssl/ssl_inspector.cc @@ -32,7 +32,6 @@ #include "detection/detection_engine.h" #include "events/event_queue.h" #include "log/messages.h" -#include "main/snort_debug.h" #include "profiler/profiler.h" #include "protocols/packet.h" #include "protocols/ssl.h" @@ -40,6 +39,7 @@ #include "pub_sub/opportunistic_tls_event.h" #include "stream/stream.h" #include "stream/stream_splitter.h" +#include "trace/trace_api.h" #include "ssl_module.h" #include "ssl_splitter.h" diff --git a/src/service_inspectors/wizard/wizard.cc b/src/service_inspectors/wizard/wizard.cc index 68e650591..12dc1f5c5 100644 --- a/src/service_inspectors/wizard/wizard.cc +++ b/src/service_inspectors/wizard/wizard.cc @@ -23,10 +23,10 @@ #include "flow/flow.h" #include "log/messages.h" -#include "main/snort_debug.h" #include "profiler/profiler.h" #include "protocols/packet.h" #include "stream/stream_splitter.h" +#include "trace/trace_api.h" #include "curses.h" #include "magic.h" diff --git a/src/stream/ip/ip_defrag.cc b/src/stream/ip/ip_defrag.cc index bb7901da3..c725e9d25 100644 --- a/src/stream/ip/ip_defrag.cc +++ b/src/stream/ip/ip_defrag.cc @@ -75,13 +75,13 @@ #include "log/messages.h" #include "main/analyzer.h" #include "main/snort_config.h" -#include "main/snort_debug.h" #include "memory/memory_cap.h" #include "packet_io/active.h" #include "packet_io/sfdaq_config.h" #include "profiler/profiler_defs.h" #include "protocols/ipv4_options.h" #include "time/timersub.h" +#include "trace/trace_api.h" #include "utils/safec.h" #include "utils/stats.h" #include "utils/util.h" diff --git a/src/stream/stream.cc b/src/stream/stream.cc index 8cc3f4fc5..d62febd46 100644 --- a/src/stream/stream.cc +++ b/src/stream/stream.cc @@ -34,13 +34,13 @@ #include "flow/prune_stats.h" #include "main/snort.h" #include "main/snort_config.h" -#include "main/snort_debug.h" #include "network_inspectors/packet_tracer/packet_tracer.h" #include "packet_io/active.h" #include "protocols/vlan.h" #include "stream/base/stream_module.h" #include "target_based/host_attributes.h" #include "target_based/snort_protocols.h" +#include "trace/trace_api.h" #include "utils/util.h" #include "tcp/tcp_session.h" diff --git a/src/stream/tcp/tcp_trace.cc b/src/stream/tcp/tcp_trace.cc index 7dda66aa8..e3b882bde 100644 --- a/src/stream/tcp/tcp_trace.cc +++ b/src/stream/tcp/tcp_trace.cc @@ -25,7 +25,7 @@ #include "tcp_trace.h" -#include "main/snort_debug.h" +#include "trace/trace_api.h" #include "utils/stats.h" #include "tcp_module.h" diff --git a/src/stream/user/user_session.cc b/src/stream/user/user_session.cc index b08f0697a..dcaa5b25c 100644 --- a/src/stream/user/user_session.cc +++ b/src/stream/user/user_session.cc @@ -26,10 +26,10 @@ #include "detection/detection_engine.h" #include "detection/rules.h" #include "main/analyzer.h" -#include "main/snort_debug.h" #include "memory/memory_cap.h" #include "profiler/profiler_defs.h" #include "protocols/packet.h" +#include "trace/trace_api.h" #include "utils/util.h" #include "stream_user.h" diff --git a/src/trace/trace_api.cc b/src/trace/trace_api.cc index 6b432d3c1..51770a917 100644 --- a/src/trace/trace_api.cc +++ b/src/trace/trace_api.cc @@ -23,11 +23,14 @@ #include "trace_api.h" +#include + #include "framework/packet_constraints.h" #include "main/snort.h" #include "main/snort_config.h" #include "main/thread.h" #include "protocols/packet.h" +#include "utils/safec.h" #include "trace_config.h" #include "trace_logger.h" @@ -137,3 +140,314 @@ uint8_t TraceApi::get_constraints_generation() return g_constraints_generation; } +#define BUF_SIZE_MIN (1 << 10) // guaranteed size, this one will be allocated on stack +#define BUF_SIZE_MAX (1 << 20) // this prevents unlimited memory allocation + +namespace snort +{ +template +void trace_vprintf(const char* name, TraceLevel log_level, + const char* trace_option, const Packet* p, const char* fmt, va_list ap) +{ + char buf[BUF_SIZE_MIN]; + int n; + + va_list dup_ap; + va_copy(dup_ap, ap); + + n = vsnprintf(buf, sizeof(buf), fmt, ap); + + if (n < 0) + { + std::string err_msg = "trace_vprintf: printf error: "; + err_msg += strerror(errno); + err_msg += "\n"; + log_func(err_msg.c_str(), name, log_level, trace_option, p); + } + else if (n < (int)sizeof(buf)) + { + log_func(buf, name, log_level, trace_option, p); + } + else if (n < BUF_SIZE_MAX) + { + char* d_buf = new char[n + 1]; + + if (d_buf) + { + vsnprintf(d_buf, n + 1, fmt, dup_ap); + log_func(d_buf, name, log_level, trace_option, p); + } + else + log_func("trace_vprintf: out of memory\n", name, log_level, trace_option, p); + + delete[] d_buf; + } + else + { + n = BUF_SIZE_MAX; + char* d_buf = new char[n]; + + if (d_buf) + { + log_func("trace_vprintf: next message will be truncated\n", name, log_level, trace_option, p); + + vsnprintf(d_buf, n, fmt, dup_ap); + d_buf[n - 2] = '\n'; + d_buf[n - 1] = '\0'; + log_func(d_buf, name, log_level, trace_option, p); + } + else + log_func("trace_vprintf: out of memory\n", name, log_level, trace_option, p); + + delete[] d_buf; + } + + va_end(dup_ap); +} + +void trace_vprintf(const char* name, TraceLevel log_level, + const char* trace_option, const Packet* p, const char* fmt, va_list ap) +{ + trace_vprintf(name, log_level, trace_option, p, fmt, ap); +} +} + +#ifdef UNIT_TEST +#include + +#ifdef DEBUG_MSGS + +#include "framework/module.h" + +using namespace snort; + +//stringify the expansion of the macro +#define sx(code) sm(code) + +//stringify the macro +#define sm(code) #code + +struct TestCase +{ + const char* test; + const char* expected; +}; + +static char testing_dump[BUF_SIZE_MAX * 2]; + +static void test_log(const char* log_msg, const char* name, + uint8_t log_level, const char* trace_option, const snort::Packet*) +{ + snprintf(testing_dump, sizeof(testing_dump), "%s:%s:%d: %s", + name, trace_option, log_level, log_msg); +} + +TEST_CASE("macros", "[trace]") +{ + TestCase cases[] = + { + { + sx(debug_log(1, test_trace, "my message")), + "trace_print(1, test_trace, \"my message\")" + }, + { + sx(debug_log(test_trace, my_flags, "my message")), + "trace_print(test_trace, my_flags, \"my message\")" + }, + { + sx(debug_logf(1, test_trace, "%s %s", "my", "message")), + "trace_printf(1, test_trace, \"%s %s\", \"my\", \"message\")" + }, + { + sx(debug_logf(test_trace, my_flags, "%s %s", "my", "message")), + "trace_printf(test_trace, my_flags, \"%s %s\", \"my\", \"message\")" + } + }; + + CHECK( !strcmp(cases[0].expected, cases[0].test) ); + CHECK( !strcmp(cases[1].expected, cases[1].test) ); + CHECK( !strcmp(cases[2].expected, cases[2].test) ); + CHECK( !strcmp(cases[3].expected, cases[3].test) ); +} + +#undef trace_print +#undef trace_printf + +//These templates expand to replace the default expansion of trace_vprintf. +//This custom expansion replaces log_func (expands to TraceApi::log()) +//with test_log for capturing what would be passed to the console. +#define trace_print trace_print> +#define trace_printf trace_printf> + +class TraceTestModule : public Module +{ +public: + TraceTestModule(const char* name, const TraceOption* trace_options) : + Module(name, "trace_test_help"), test_trace_options(trace_options) + { } + + const TraceOption* get_trace_options() const override + { return test_trace_options; } + +private: + const TraceOption* test_trace_options; +}; + +TEST_CASE("debug_log, debug_logf", "[trace]") +{ + enum + { + TEST_TRACE_OPTION1 = 0, + TEST_TRACE_OPTION2, + TEST_TRACE_OPTION3, + TEST_TRACE_OPTION4, + TEST_TRACE_OPTION5, + }; + + const TraceOption test_trace_values[] = + { + { "option1", TEST_TRACE_OPTION1, "help_option1" }, + { "option2", TEST_TRACE_OPTION2, "help_option2" }, + { "option3", TEST_TRACE_OPTION3, "help_option3" }, + { "option4", TEST_TRACE_OPTION4, "help_option4" }, + { "option5", TEST_TRACE_OPTION5, "help_option5" }, + + { nullptr, 0, nullptr }, + }; + + TraceOption test_trace_options(nullptr, 0, nullptr); + TraceTestModule trace_test_module("test_module", &test_trace_options); + Trace test_trace(trace_test_module); + + TraceTestModule trace_test_module_opt("test_opt_module", test_trace_values); + Trace test_opt_trace(trace_test_module_opt); + + test_trace.set("all", 0); + + testing_dump[0] = '\0'; + debug_log(&test_trace, nullptr, "my message"); + CHECK( testing_dump[0] == '\0' ); + + test_trace.set("all", 1); + test_opt_trace.set("option1", 1); + test_opt_trace.set("option2", 2); + test_opt_trace.set("option3", 3); + test_opt_trace.set("option4", 2); + test_opt_trace.set("option5", 2); + + char message[BUF_SIZE_MIN + 1]; + for( int i = 0; i < BUF_SIZE_MIN; i++ ) + message[i] = 'A'; + message[BUF_SIZE_MIN] = '\0'; + + testing_dump[0] = '\0'; + debug_log(6, &test_opt_trace, TEST_TRACE_OPTION3, nullptr, message); + CHECK( (strlen(testing_dump) == 0) ); + + testing_dump[0] = '\0'; + debug_log(&test_trace, nullptr, "my message"); + CHECK( !strcmp(testing_dump, "test_module:all:1: my message") ); + + testing_dump[0] = '\0'; + debug_logf(&test_trace, nullptr, "%s %s", "my", "message"); + CHECK( !strcmp(testing_dump, "test_module:all:1: my message") ); + + testing_dump[0] = '\0'; + debug_log(&test_opt_trace, TEST_TRACE_OPTION1, nullptr, "log option1 message"); + CHECK( !strcmp(testing_dump, "test_opt_module:option1:1: log option1 message") ); + + testing_dump[0] = '\0'; + debug_logf(&test_opt_trace, TEST_TRACE_OPTION1, nullptr, "%s %s %s", "log", "option1", "message"); + CHECK( !strcmp(testing_dump, "test_opt_module:option1:1: log option1 message") ); + + testing_dump[0] = '\0'; + debug_log(3, &test_opt_trace, TEST_TRACE_OPTION2, nullptr, "log option2 message"); + CHECK( testing_dump[0] == '\0' ); + + testing_dump[0] = '\0'; + debug_log(&test_opt_trace, TEST_TRACE_OPTION2, nullptr, "log option2 message"); + CHECK( !strcmp(testing_dump, "test_opt_module:option2:1: log option2 message") ); + + testing_dump[0] = '\0'; + debug_logf(&test_opt_trace, TEST_TRACE_OPTION2, nullptr, "%s %s %s", "log", "option2", "message"); + CHECK( !strcmp(testing_dump, "test_opt_module:option2:1: log option2 message") ); + + testing_dump[0] = '\0'; + debug_log(6, &test_opt_trace, TEST_TRACE_OPTION3, nullptr, "log option3 message"); + CHECK( testing_dump[0] == '\0' ); + + testing_dump[0] = '\0'; + debug_log(3, &test_opt_trace, TEST_TRACE_OPTION3, nullptr, "log option3 message"); + CHECK( !strcmp(testing_dump, "test_opt_module:option3:3: log option3 message") ); + + testing_dump[0] = '\0'; + debug_logf(3, &test_opt_trace, TEST_TRACE_OPTION3, nullptr, "%s %s %s", "log", "option3", "message"); + CHECK( !strcmp(testing_dump, "test_opt_module:option3:3: log option3 message") ); + + testing_dump[0] = '\0'; + debug_log(2, &test_opt_trace, TEST_TRACE_OPTION4, nullptr, "log option4 message"); + CHECK( !strcmp(testing_dump, "test_opt_module:option4:2: log option4 message") ); + + testing_dump[0] = '\0'; + debug_logf(2, &test_opt_trace, TEST_TRACE_OPTION4, nullptr, "%s %s %s", "log", "option4", "message"); + CHECK( !strcmp(testing_dump, "test_opt_module:option4:2: log option4 message") ); + + testing_dump[0] = '\0'; + debug_log(4, &test_opt_trace, TEST_TRACE_OPTION5, nullptr, "log option5 message"); + CHECK( testing_dump[0] == '\0' ); +} + +TEST_CASE("trace big message", "[trace]") +{ + TraceOption test_trace_options(nullptr, 0, nullptr); + TraceTestModule trace_test_module("test_module", &test_trace_options); + Trace test_trace(trace_test_module); + + const int hdr_size = strlen("test_module:all:1: "); + const char exp_1[] = "test_module:all:1: 1111111111111111111111111111"; + const char exp_2[] = "test_module:all:1: 2222222222222222222222222222"; + const char exp_3[] = "test_module:all:1: 3333333333333333333333333333"; + const char exp_4[] = "test_module:all:1: 4444444444444444444444444444"; + + + char msg_1[BUF_SIZE_MIN * 1]; + char msg_2[BUF_SIZE_MIN * 2]; + char msg_3[BUF_SIZE_MAX * 1]; + char msg_4[BUF_SIZE_MAX * 2]; + + test_trace.set("all", 1); + + memset(msg_1, '1', sizeof(msg_1)); + memset(msg_2, '2', sizeof(msg_2)); + memset(msg_3, '3', sizeof(msg_3)); + memset(msg_4, '4', sizeof(msg_4)); + + msg_1[sizeof(msg_1) - 1] = '\0'; + msg_2[sizeof(msg_2) - 1] = '\0'; + msg_3[sizeof(msg_3) - 1] = '\0'; + msg_4[sizeof(msg_4) - 1] = '\0'; + + memset(testing_dump, '\0', sizeof(testing_dump)); + debug_log(&test_trace, nullptr, msg_1); + CHECK( strlen(testing_dump) == hdr_size + strlen(msg_1) ); + CHECK( !strncmp(testing_dump, exp_1, strlen(exp_1)) ); + + memset(testing_dump, '\0', sizeof(testing_dump)); + debug_log(&test_trace, nullptr, msg_2); + CHECK( strlen(testing_dump) == hdr_size + strlen(msg_2) ); + CHECK( !strncmp(testing_dump, exp_2, strlen(exp_2)) ); + + memset(testing_dump, '\0', sizeof(testing_dump)); + debug_log(&test_trace, nullptr, msg_3); + CHECK( strlen(testing_dump) == hdr_size + strlen(msg_3) ); + CHECK( !strncmp(testing_dump, exp_3, strlen(exp_3)) ); + + memset(testing_dump, '\0', sizeof(testing_dump)); + debug_log(&test_trace, nullptr, msg_4); + CHECK( strlen(testing_dump) == hdr_size + BUF_SIZE_MAX - 1 ); + CHECK( !strncmp(testing_dump, exp_4, strlen(exp_4)) ); +} + +#endif // DEBUG_MSGS + +#endif // UNIT_TEST diff --git a/src/trace/trace_api.h b/src/trace/trace_api.h index 2b8e98fe7..9d4b307ac 100644 --- a/src/trace/trace_api.h +++ b/src/trace/trace_api.h @@ -20,9 +20,12 @@ #ifndef TRACE_API_H #define TRACE_API_H +#include #include #include "main/snort_types.h" +#include "protocols/packet.h" +#include "trace.h" class TraceConfig; @@ -51,5 +54,185 @@ public: }; } +static inline bool trace_enabled(const snort::Trace* trace, + TraceOptionID trace_option_id, + TraceLevel log_level = DEFAULT_TRACE_LOG_LEVEL, + const snort::Packet* p = nullptr) +{ + if ( !trace or !trace->enabled(trace_option_id, log_level) ) + return false; + + if ( !p ) + return true; + + const auto gid = snort::TraceApi::get_constraints_generation(); + if ( !p->filtering_state.was_checked(gid) ) + snort::TraceApi::filter(*p); + + return p->filtering_state.matched; +} + +namespace snort +{ +SO_PUBLIC void trace_vprintf(const char* name, TraceLevel log_level, + const char* trace_option, const snort::Packet* p, const char* fmt, va_list); +} + +using trace_func = void(const char*, TraceLevel, const char*, const snort::Packet*, const char*, va_list); + +template +static inline void trace_uprintf(const snort::Trace* trace, + TraceOptionID trace_option_id, const snort::Packet* p, const char* fmt, ...) __attribute__((format (printf, 4, 5))); + +template +static inline void trace_uprintf(const snort::Trace* trace, + TraceOptionID trace_option_id, const snort::Packet* p, const char* fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + + const char* trace_option_name = trace->option_name(trace_option_id); + trace_vprintf(trace->module_name(), DEFAULT_TRACE_LOG_LEVEL, trace_option_name, p, fmt, ap); + + va_end(ap); +} + +template +static inline void trace_printf(TraceLevel log_level, + const snort::Trace* trace, TraceOptionID trace_option_id, + const snort::Packet* p, const char* fmt, ...) + __attribute__((format (printf, 5, 6))); + +template +static inline void trace_printf(TraceLevel log_level, + const snort::Trace* trace, TraceOptionID trace_option_id, + const snort::Packet* p, const char* fmt, ...) +{ + if ( !trace_enabled(trace, trace_option_id, log_level, p) ) + return; + + va_list ap; + va_start(ap, fmt); + + const char* trace_option_name = trace->option_name(trace_option_id); + trace_vprintf(trace->module_name(), log_level, trace_option_name, p, + fmt, ap); + + va_end(ap); +} + +template +static inline void trace_printf(TraceLevel log_level, + const snort::Trace* trace, const snort::Packet* p, + const char* fmt, ...) __attribute__((format (printf, 4, 5))); + +template +static inline void trace_printf(TraceLevel log_level, + const snort::Trace* trace, const snort::Packet* p, + const char* fmt, ...) +{ + if ( !trace_enabled(trace, DEFAULT_TRACE_OPTION_ID, log_level, p) ) + return; + + va_list ap; + va_start(ap, fmt); + + const char* trace_option_name = trace->option_name(DEFAULT_TRACE_OPTION_ID); + trace_vprintf(trace->module_name(), log_level, trace_option_name, p, + fmt, ap); + + va_end(ap); +} + +template +static inline void trace_printf(const snort::Trace* trace, + TraceOptionID trace_option_id, const snort::Packet* p, + const char* fmt, ...) __attribute__((format (printf, 4, 5))); + +template +static inline void trace_printf(const snort::Trace* trace, + TraceOptionID trace_option_id, const snort::Packet* p, const char* fmt, ...) +{ + if ( !trace_enabled(trace, trace_option_id, DEFAULT_TRACE_LOG_LEVEL, p) ) + return; + + va_list ap; + va_start(ap, fmt); + + const char* trace_option_name = trace->option_name(trace_option_id); + trace_vprintf(trace->module_name(), DEFAULT_TRACE_LOG_LEVEL, + trace_option_name, p, fmt, ap); + + va_end(ap); +} + +template +static inline void trace_printf(const snort::Trace* trace, + const snort::Packet* p, const char* fmt, ...) + __attribute__((format (printf, 3, 4))); + +template +static inline void trace_printf(const snort::Trace* trace, + const snort::Packet* p, const char* fmt, ...) +{ + if ( !trace_enabled(trace, DEFAULT_TRACE_OPTION_ID, DEFAULT_TRACE_LOG_LEVEL, p) ) + return; + + va_list ap; + va_start(ap, fmt); + + const char* trace_option_name = trace->option_name(DEFAULT_TRACE_OPTION_ID); + trace_vprintf(trace->module_name(), DEFAULT_TRACE_LOG_LEVEL, + trace_option_name, p, fmt, ap); + + va_end(ap); +} + +template +static inline void trace_print(TraceLevel log_level, + const snort::Trace* trace, TraceOptionID trace_option_id, + const snort::Packet* p, const char* msg) +{ + trace_printf(log_level, trace, trace_option_id, p, + "%s", msg); +} + +template +static inline void trace_print(const snort::Trace* trace, + TraceOptionID trace_option_id, const snort::Packet* p, const char* msg) +{ + trace_printf(trace, trace_option_id, p, "%s", msg); +} + +template +static inline void trace_print(TraceLevel log_level, + const snort::Trace* trace, const snort::Packet* p, const char* msg) +{ + trace_printf(log_level, trace, p, "%s", msg); +} + +template +static inline void trace_print(const snort::Trace* trace, const snort::Packet* p, + const char* msg) +{ + trace_printf(trace, p, "%s", msg); +} + +#define trace_print trace_print +#define trace_printf trace_printf +#define trace_uprintf trace_uprintf + +#define trace_log(...) trace_print(__VA_ARGS__) +#define trace_logf(...) trace_printf(__VA_ARGS__) +#define trace_ulogf(...) trace_uprintf(__VA_ARGS__) + +#ifdef DEBUG_MSGS +#define debug_log trace_log +#define debug_logf trace_logf +#else +#define debug_log(...) +#define debug_logf(...) +#endif + #endif // TRACE_API_H diff --git a/src/utils/js_tokenizer.h b/src/utils/js_tokenizer.h index d697887ab..f0a16027a 100644 --- a/src/utils/js_tokenizer.h +++ b/src/utils/js_tokenizer.h @@ -26,8 +26,8 @@ #include #include "log/messages.h" -#include "main/snort_debug.h" #include "service_inspectors/http_inspect/http_enum.h" +#include "trace/trace_api.h" extern THREAD_LOCAL const snort::Trace* http_trace;