]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #3477: Refactor: move trace related files from main to trace folder
authorMike Stepanek (mstepane) <mstepane@cisco.com>
Mon, 27 Jun 2022 10:47:40 +0000 (10:47 +0000)
committerMike Stepanek (mstepane) <mstepane@cisco.com>
Mon, 27 Jun 2022 10:47:40 +0000 (10:47 +0000)
Merge in SNORT/snort3 from ~ASERBENI/snort3:trace_refactor to master

Squashed commit of the following:

commit 2e074bcbe3732bdc06c3cc6d2cbfb4c1a80e84a7
Author: Andrii Serbeniuk <aserbeni@cisco.com>
Date:   Wed Jun 15 13:34:38 2022 +0300

    main: move trace related code to trace folder

47 files changed:
src/actions/act_react.cc
src/decompress/file_olefile.h
src/detection/context_switcher.cc
src/detection/detection_engine.cc
src/detection/detection_options.h
src/detection/fp_detect.cc
src/detection/tag.cc
src/file_api/file_flows.cc
src/file_api/file_module.h
src/flow/flow_cache.cc
src/flow/test/flow_cache_test.cc
src/latency/packet_latency.cc
src/main.cc
src/main/CMakeLists.txt
src/main/snort_debug.cc [deleted file]
src/main/snort_debug.h [deleted file]
src/main/snort_module.cc
src/managers/inspector_manager.cc
src/mime/file_mime_paf.cc
src/network_inspectors/appid/detector_plugins/detector_pattern.cc
src/network_inspectors/appid/lua_detector_api.cc
src/network_inspectors/appid/tp_appid_utils.cc
src/network_inspectors/appid/tp_lib_handler.cc
src/network_inspectors/rna/rna_module.h
src/ports/port_table.cc
src/protocols/packet_manager.cc
src/service_inspectors/dce_rpc/dce_smb_common.h
src/service_inspectors/dce_rpc/dce_smb_transaction.cc
src/service_inspectors/dce_rpc/dce_smb_utils.cc
src/service_inspectors/dce_rpc/dce_udp_processing.cc
src/service_inspectors/dce_rpc/smb_message.cc
src/service_inspectors/gtp/gtp.cc
src/service_inspectors/gtp/gtp_parser.cc
src/service_inspectors/http_inspect/http_flow_data.cc
src/service_inspectors/http_inspect/http_js_norm.cc
src/service_inspectors/iec104/iec104_decode.cc
src/service_inspectors/mms/mms_decode.cc
src/service_inspectors/sip/sip_config.cc
src/service_inspectors/ssl/ssl_inspector.cc
src/service_inspectors/wizard/wizard.cc
src/stream/ip/ip_defrag.cc
src/stream/stream.cc
src/stream/tcp/tcp_trace.cc
src/stream/user/user_session.cc
src/trace/trace_api.cc
src/trace/trace_api.h
src/utils/js_tokenizer.h

index 0c205c9e791a71e69f16bc680b0d10dfba52d701..4ea94e633220484fb59b0548e6a0690321d48784 100644 (file)
 #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"
 
index 10aecdcf297dc5862e4c4122360108dffd3a0b9d..75171787dc69b6e005ea0f5227036790b8441115 100644 (file)
@@ -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"
 
index f3c2256ba89f6d7098a51bcfa3fb83703a7f3e0b..0839d6e2631c6e73140b51550ab6b6a8e9786127 100644 (file)
@@ -26,8 +26,8 @@
 
 #include <cassert>
 
-#include "main/snort_debug.h"
 #include "packet_io/active.h"
+#include "trace/trace_api.h"
 #include "utils/stats.h"
 
 #include "detect_trace.h"
index 469b26837066525fd59cc498eb5b9318ba4ef2a0..e81bc97c7b4c78008ba8295bfcc7074ddced497e 100644 (file)
@@ -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"
index 4582a577d832b8807aa6397898e03e36e5f6604b..aaf9f760b3388a351e6f6431a0db979a67e0d0d9 100644 (file)
@@ -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
 {
index e356ee8a5b0d35cf826c1ea23dc0240fb6fcaa26..67040823e159c64fbbba46635486781551cc9d8f 100644 (file)
@@ -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"
 
index 7592d210f93cd696a0d5533c68f238aaad52130d..89abec90713178530be64e34ac5c214e23f732a7 100644 (file)
 #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"
 
index 1d84a212cc8f9701bd7f243ccac0043e3a9b1961..068b50f03d160039e385eecfe00cf6a19304870b 100644 (file)
@@ -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"
index 923b568d838b4a5104fd3acf8a20e9751a5bcf37..97d120083cfc7b13b510af664be372a71c874800 100644 (file)
@@ -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
index 8630ece926ebc69fa8d8064953193ccecec4b089..75677b3788b06a03694e537022fca3a10c82f486 100644 (file)
 #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"
index 2be52113936d6b20de13b007ff75ee1e386a8bff..1e0e40cfe3742384ffc390b65dea5d6758aec164 100644 (file)
@@ -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 <CppUTest/CommandLineTestRunner.h>
 #include <CppUTest/TestHarness.h>
index 733a9ad89b1b7878116310f5a2854f933ecba01c..836567a8b739deed428b9c3aaf3f9e46120c50b4 100644 (file)
@@ -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"
index 05577699d23487ac5d1150c00f2286493fefcabe..02eafc3eabb5ce665ff455200520561adf962369 100644 (file)
@@ -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"
index da6580c96c861ea2c5d3cd2dd37d865948cc32e9..1c7d59edb37aa2641b55592a4ad427c3d6bbfe54 100644 (file)
@@ -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 (file)
index 7b53c43..0000000
+++ /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 <roesch@sourcefire.com>
-//
-// 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 <cstring>
-
-#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 (log_func)(const char*, const char*, uint8_t, const char*, const Packet*)>
-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<TraceApi::log>(name, log_level, trace_option, p, fmt, ap);
-}
-}
-
-#ifdef UNIT_TEST
-#include <catch/snort_catch.h>
-
-#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<snort::trace_vprintf>(1, test_trace, \"my message\")"
-        },
-        {
-            sx(debug_log(test_trace, my_flags, "my message")),
-            "trace_print<snort::trace_vprintf>(test_trace, my_flags, \"my message\")"
-        },
-        {
-            sx(debug_logf(1, test_trace, "%s %s", "my", "message")),
-            "trace_printf<snort::trace_vprintf>(1, test_trace, \"%s %s\", \"my\", \"message\")"
-        },
-        {
-            sx(debug_logf(test_trace, my_flags, "%s %s", "my", "message")),
-            "trace_printf<snort::trace_vprintf>(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<trace_vprintf<test_log>>
-#define trace_printf trace_printf<trace_vprintf<test_log>>
-
-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 (file)
index cce96af..0000000
+++ /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 <roesch@sourcefire.com>
-//
-// 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 <cstdarg>
-
-#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 <trace_func>
-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 <trace_func trace_vprintf = snort::trace_vprintf>
-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 <trace_func>
-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 <trace_func trace_vprintf = snort::trace_vprintf>
-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 <trace_func>
-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 <trace_func trace_vprintf = snort::trace_vprintf>
-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 <trace_func>
-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 <trace_func trace_vprintf = snort::trace_vprintf>
-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 <trace_func>
-static inline void trace_printf(const snort::Trace* trace,
-    const snort::Packet* p, const char* fmt, ...)
-    __attribute__((format (printf, 3, 4)));
-
-template <trace_func trace_vprintf = snort::trace_vprintf>
-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 <trace_func trace_vprintf = snort::trace_vprintf>
-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<trace_vprintf>(log_level, trace, trace_option_id, p,
-        "%s", msg);
-}
-
-template <trace_func trace_vprintf = snort::trace_vprintf>
-static inline void trace_print(const snort::Trace* trace,
-    TraceOptionID trace_option_id, const snort::Packet* p, const char* msg)
-{
-    trace_printf<trace_vprintf>(trace, trace_option_id, p, "%s", msg);
-}
-
-template <trace_func trace_vprintf = snort::trace_vprintf>
-static inline void trace_print(TraceLevel log_level,
-    const snort::Trace* trace, const snort::Packet* p, const char* msg)
-{
-    trace_printf<trace_vprintf>(log_level, trace, p, "%s", msg);
-}
-
-template <trace_func trace_vprintf = snort::trace_vprintf>
-static inline void trace_print(const snort::Trace* trace, const snort::Packet* p,
-    const char* msg)
-{
-    trace_printf<trace_vprintf>(trace, p, "%s", msg);
-}
-
-#define trace_print trace_print<snort::trace_vprintf>
-#define trace_printf trace_printf<snort::trace_vprintf>
-#define trace_uprintf trace_uprintf<snort::trace_vprintf>
-
-#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
index 7cb81cd2c2e8d003e2b687c34b4cfcf7d11e85e5..24b46eca1f44a20c8e9ec45cd63ee64c3c870776 100644 (file)
@@ -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)
index 39d15dd49bbcd1ed3c8243dac29e4edd90fc5cfe..cc926718e3f525ca3f429e8627bea45f882724d8 100644 (file)
@@ -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"
 
index d040cd87be1579474fafc2b0c52a469e003f2a03..630828d23ef776e3f0a484ea99eecf509505b3c8 100644 (file)
@@ -28,7 +28,7 @@
 
 #include <cctype>
 
-#include "main/snort_debug.h"
+#include "trace/trace_api.h"
 
 using namespace snort;
 
index dff7e0e240317a2810867b95f29bcf8ab05f8b27..7a3a4bed54818b7688d53862b6233d16316e61f0 100644 (file)
@@ -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"
index d8dd5d6859767cbd794ce223b402e5c272c36b39..6c28d3dcbdaf7573fda2e7d29293b463d062b512 100644 (file)
 #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"
index 35b36879b03e37a989d6d5e5733737b1745bec4f..b755581803bed1518591a39b39a1f0f2171f33ff 100644 (file)
 #include <dlfcn.h>
 
 #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"
index 2f0658bc72aaf5faea4cea656c031f5061178c78..09313ce22a811b1a0ee78d4bc2679972c0a5decc 100644 (file)
@@ -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"
 
index 8d327711dc6e40630495bbb06c32cb6d0c9af219..b7882c82e50d3c5e04ded30a77704dd68491c233 100644 (file)
@@ -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"
index 5fad5a930c44998cac99ae06715b8c13ac5a5e77..c6abfcaff0fcacb4bc44029f1129ed280888722d 100644 (file)
@@ -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"
 
index 3a629a6b3397e236ff0445d3275a026eab5bf65d..60a8bef7eaac76f5d6900a4dc998c4567437881e 100644 (file)
 #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"
index 3cbb25959b4ecb8c58ea5bb100e901d94eaee1e7..1d579318d088b3b2abcd3c77a72a7e0d57efbb74 100644 (file)
@@ -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"
index dd5c03fd0c209c0b1e39957095e0f9e1a921cf23..0dbb5d6d46daeeab4eeaa5e4c51046651fead0c0 100644 (file)
@@ -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;
 
index c08e52f03d774e8bd48ed56706f3c8772e7720da..6d84ed20ebc6e24dc9098fbd38f6229b956440fe 100644 (file)
@@ -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"
index 68277da07955f9deab25f796698b8d0050e1cfcc..dc4be569d6bebc7b9bd6fb82a38cf3adb54c16a2 100644 (file)
@@ -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"
 
index b7082d25c5e7aacd06808f21cc1624eeaca0b0fb..07f872a3fc4624ab352340847be358a95980d457 100644 (file)
 #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;
index f6656c916b56e9438f244adc6a07332d1daad7c6..d16866c4b74b574f4e99a9b36a7d68dfd1afe67a 100644 (file)
@@ -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"
index c56422365ec50de11f928d0e9b3f99338fe3003a..96440e01f1057d8c7c31b4f6fbb8722c6e9aac67 100644 (file)
@@ -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"
index 2c74979a14c1ec31a53b00b257c6de50e4d3ed13..e44ab62d46e53a70599ba417682fc4cebf263faa 100644 (file)
@@ -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"
 
index 8423fc5ac35919458496a5a6d349d4bdbbfdf988..196220ddbddbc63c6f54b5ee4bfb9fa497bc4596 100644 (file)
@@ -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"
index 0f91d4f92269e83dd4245ee9632b4bac0b32a460..9309a02fa0abadf1bbb743d6a77eb30f83b0cd84 100644 (file)
@@ -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"
index 1756b391675ce0f48e47498cf086408b2f25f009..9fcb5951e479456277e7eaca857800dad51195b2 100644 (file)
@@ -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"
index 61478b8487a116b4e6e961d028b31efa940241f8..6414c4d9d90d14678b4bfdbff942d2cf5a9641ea 100644 (file)
@@ -28,7 +28,7 @@
 #include <cassert>
 
 #include "log/messages.h"
-#include "main/snort_debug.h"
+#include "trace/trace_api.h"
 #include "utils/util.h"
 
 using namespace snort;
index d11b0541361b7e170f704a3c1b9f4bb33c335a76..f958edac9e5491920e2aaff62877d0ba38e9402d 100644 (file)
@@ -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"
index 68e6505919bbdbf0137587afa17ce6bcf5812bf1..12dc1f5c514eec2c2c9a95a084ee19e909c12019 100644 (file)
 
 #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"
index bb7901da327e493e3e3d59bf8d8242c382c1ed82..c725e9d258f2283850e1c0776f77e8c530ded54c 100644 (file)
 #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"
index 8cc3f4fc5684fc3012760306dddfd4f7133cf7cc..d62febd4696c036a982e49a4b2fcee3acba2fc65 100644 (file)
 #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"
index 7dda66aa8f739508adfe42a09ddef5caba54ecad..e3b882bdeedaca06308177dd4fb693be38805c7b 100644 (file)
@@ -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"
index b08f0697ac16c9986164166f025355b0dd4ed0f8..dcaa5b25caab9f67c8890716dc6793598ef7b7f2 100644 (file)
 #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"
index 6b432d3c1d0b7d152a02cc7152c5e0f91d575444..51770a9172cebff1c3b7317a453b6a0528f39e22 100644 (file)
 
 #include "trace_api.h"
 
+#include <cstring>
+
 #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 (log_func)(const char*, const char*, uint8_t, const char*, const Packet*)>
+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<TraceApi::log>(name, log_level, trace_option, p, fmt, ap);
+}
+}
+
+#ifdef UNIT_TEST
+#include <catch/snort_catch.h>
+
+#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<snort::trace_vprintf>(1, test_trace, \"my message\")"
+        },
+        {
+            sx(debug_log(test_trace, my_flags, "my message")),
+            "trace_print<snort::trace_vprintf>(test_trace, my_flags, \"my message\")"
+        },
+        {
+            sx(debug_logf(1, test_trace, "%s %s", "my", "message")),
+            "trace_printf<snort::trace_vprintf>(1, test_trace, \"%s %s\", \"my\", \"message\")"
+        },
+        {
+            sx(debug_logf(test_trace, my_flags, "%s %s", "my", "message")),
+            "trace_printf<snort::trace_vprintf>(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<trace_vprintf<test_log>>
+#define trace_printf trace_printf<trace_vprintf<test_log>>
+
+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
index 2b8e98fe7f6a7d8f707852bc15cb6ac9bb43f646..9d4b307ac6c77c784962c433213d626ebd604f69 100644 (file)
 #ifndef TRACE_API_H
 #define TRACE_API_H
 
+#include <cstdarg>
 #include <cstdint>
 
 #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 <trace_func>
+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 <trace_func trace_vprintf = snort::trace_vprintf>
+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 <trace_func>
+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 <trace_func trace_vprintf = snort::trace_vprintf>
+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 <trace_func>
+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 <trace_func trace_vprintf = snort::trace_vprintf>
+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 <trace_func>
+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 <trace_func trace_vprintf = snort::trace_vprintf>
+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 <trace_func>
+static inline void trace_printf(const snort::Trace* trace,
+    const snort::Packet* p, const char* fmt, ...)
+    __attribute__((format (printf, 3, 4)));
+
+template <trace_func trace_vprintf = snort::trace_vprintf>
+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 <trace_func trace_vprintf = snort::trace_vprintf>
+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<trace_vprintf>(log_level, trace, trace_option_id, p,
+        "%s", msg);
+}
+
+template <trace_func trace_vprintf = snort::trace_vprintf>
+static inline void trace_print(const snort::Trace* trace,
+    TraceOptionID trace_option_id, const snort::Packet* p, const char* msg)
+{
+    trace_printf<trace_vprintf>(trace, trace_option_id, p, "%s", msg);
+}
+
+template <trace_func trace_vprintf = snort::trace_vprintf>
+static inline void trace_print(TraceLevel log_level,
+    const snort::Trace* trace, const snort::Packet* p, const char* msg)
+{
+    trace_printf<trace_vprintf>(log_level, trace, p, "%s", msg);
+}
+
+template <trace_func trace_vprintf = snort::trace_vprintf>
+static inline void trace_print(const snort::Trace* trace, const snort::Packet* p,
+    const char* msg)
+{
+    trace_printf<trace_vprintf>(trace, p, "%s", msg);
+}
+
+#define trace_print trace_print<snort::trace_vprintf>
+#define trace_printf trace_printf<snort::trace_vprintf>
+#define trace_uprintf trace_uprintf<snort::trace_vprintf>
+
+#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
 
index d697887ab00998c3e0ce45b9181ce1c4d5e5530f..f0a16027ad528b56892c1039394f52366fc62cee 100644 (file)
@@ -26,8 +26,8 @@
 #include <vector>
 
 #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;