From: Bhagya Tholpady (bbantwal) Date: Thu, 2 Jul 2020 14:40:08 +0000 (+0000) Subject: Merge pull request #2297 in SNORT/snort3 from ~OKHOMIAK/snort3:trace_print_instance_i... X-Git-Tag: 3.0.2-1~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=07088aaab664b1ea0398d87ef91c68b4a6413d40;p=thirdparty%2Fsnort3.git Merge pull request #2297 in SNORT/snort3 from ~OKHOMIAK/snort3:trace_print_instance_id to master Squashed commit of the following: commit 83da91a0bf7dcc47eb2bcdde87860a240229f78b Author: Oleksii Khomiakovskyi Date: Tue Jun 30 18:43:12 2020 +0300 main: set thread type for main thread commit 00065f327ea2f5555fedc514f7ab2434e7e3086c Author: Oleksii Khomiakovskyi Date: Thu Jun 18 14:00:25 2020 +0300 trace: add thread type and thread instance id to each log message for stdout logger --- diff --git a/doc/trace.txt b/doc/trace.txt index 4efa1ff15..e5443d59d 100644 --- a/doc/trace.txt +++ b/doc/trace.txt @@ -190,6 +190,16 @@ Each tracing message has a standard format: ::: +The stdout logger also prints thread type and thread instance ID at the beginning +of each trace message in a colon-separated manner. + +The capital letter at the beginning of the trace message indicates the thread type. + +Possible thread types: +C – main (control) thread +P – packet thread +O – other thread + ==== Example - Debugging rules using detection trace The detection engine is responsible for rule evaluation. Turning on the diff --git a/src/main.cc b/src/main.cc index e9ac83040..dff06ac12 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1034,6 +1034,8 @@ int main(int argc, char* argv[]) if ( s ) prompt = s; + set_thread_type(STHREAD_TYPE_MAIN); + Snort::setup(argc, argv); if ( set_mode() ) diff --git a/src/main/thread_config.cc b/src/main/thread_config.cc index d44997ce1..e7859b140 100644 --- a/src/main/thread_config.cc +++ b/src/main/thread_config.cc @@ -319,7 +319,7 @@ TEST_CASE("Named thread affinity with type configured", "[ThreadConfig]") // Configure type affinity, but not the named thread affinity. hwloc_bitmap_singlify(type_cpuset->cpuset); - tc.set_thread_affinity(STHREAD_TYPE_OTHER, ThreadConfig::DEFAULT_THREAD_ID, type_cpuset); + tc.set_thread_affinity(STHREAD_TYPE_MAIN, ThreadConfig::DEFAULT_THREAD_ID, type_cpuset); // The named thread should inherit the type affinity. tc.implement_named_thread_affinity("not found, type other"); diff --git a/src/trace/trace_log.cc b/src/trace/trace_log.cc index 968d1b09c..6f52714a0 100644 --- a/src/trace/trace_log.cc +++ b/src/trace/trace_log.cc @@ -26,6 +26,8 @@ #include #include +#include "main/thread.h" + using namespace snort; //----------------------------------------------- @@ -44,16 +46,32 @@ public: private: FILE* file; + char thread_type; + unsigned instance_id; }; StdoutTraceLogger::StdoutTraceLogger() - : file(stdout) -{ } + : file(stdout), instance_id(get_instance_id()) +{ + auto t = get_thread_type(); + switch (t) + { + case STHREAD_TYPE_PACKET: + thread_type = 'P'; + break; + case STHREAD_TYPE_MAIN: + thread_type = 'C'; + break; + default: + thread_type = 'O'; + } +} void StdoutTraceLogger::log(const char* log_msg, const char* name, uint8_t log_level, const char* trace_option, const Packet*) { - fprintf(file, "%s:%s:%d: %s", name, trace_option, log_level, log_msg); + fprintf(file, "%c%u:%s:%s:%d: %s", thread_type, instance_id, name, + trace_option, log_level, log_msg); } // Syslog