suricata
util-debug-filters.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2013 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
22  *
23  * Debug filter utility functions
24  */
25 
26 #include "suricata-common.h"
27 #include "util-debug-filters.h"
28 #include "threads.h"
29 #include "util-debug.h"
30 
31 /* both of these are defined in util-debug.c */
32 extern int sc_log_module_initialized;
33 extern int sc_log_module_cleaned;
34 
35 /* used to indicate if any FG filters are registered */
37 
38 /* used to indicate if any FD filters are registered */
40 
41 /**
42  * \brief Holds the fine-grained filters
43  */
45 
46 /**
47  * \brief Mutex for accessing the fine-grained filters sc_log_fg_filters
48  */
49 static SCMutex sc_log_fg_filters_m[SC_LOG_FILTER_MAX] = { SCMUTEX_INITIALIZER,
51 
52 /**
53  * \brief Holds the function-dependent filters
54  */
55 static SCLogFDFilter *sc_log_fd_filters = NULL;
56 
57 /**
58  * \brief Mutex for accessing the function-dependent filters sc_log_fd_filters
59  */
60 static SCMutex sc_log_fd_filters_m = SCMUTEX_INITIALIZER;
61 
62 /**
63  * \brief Holds the thread_list required by function-dependent filters
64  */
65 static SCLogFDFilterThreadList *sc_log_fd_filters_tl = NULL;
66 
67 /**
68  * \brief Mutex for accessing the FD thread_list sc_log_fd_filters_tl
69  */
70 static SCMutex sc_log_fd_filters_tl_m = SCMUTEX_INITIALIZER;
71 
72 /**
73  * \brief Helper function used internally to add a FG filter
74  *
75  * \param file File_name of the filter
76  * \param function Function_name of the filter
77  * \param line Line number of the filter
78  * \param listtype The filter listtype. Can be either a blacklist or whitelist
79  * filter listtype(SC_LOG_FILTER_BL or SC_LOG_FILTER_WL)
80  *
81  * \retval 0 on successfully adding the filter;
82  * \retval -1 on failure
83  */
84 static int SCLogAddFGFilter(const char *file, const char *function,
85  int line, int listtype)
86 {
87  SCLogFGFilterFile *fgf_file = NULL;
88  SCLogFGFilterFile *prev_fgf_file = NULL;
89 
90  SCLogFGFilterFunc *fgf_func = NULL;
91  SCLogFGFilterFunc *prev_fgf_func = NULL;
92 
93  SCLogFGFilterLine *fgf_line = NULL;
94  SCLogFGFilterLine *prev_fgf_line = NULL;
95 
96  int found = 0;
97 
98  if (sc_log_module_initialized != 1) {
99  printf("Logging module not initialized. Call SCLogInitLogModule() "
100  "first before using the debug API\n");
101  return -1 ;
102  }
103 
104  if (file == NULL && function == NULL && line < 0) {
105  printf("Error: Invalid arguments supplied to SCLogAddFGFilter\n");
106  return -1;
107  }
108 
109  SCMutex *m = &sc_log_fg_filters_m[listtype];
110 
111  SCMutexLock(m);
112 
113  fgf_file = sc_log_fg_filters[listtype];
114 
115  prev_fgf_file = fgf_file;
116  while (fgf_file != NULL) {
117  prev_fgf_file = fgf_file;
118  if (file == NULL && fgf_file->file == NULL)
119  found = 1;
120  else if (file != NULL && fgf_file->file != NULL)
121  found = (strcmp(file, fgf_file->file) == 0);
122  else
123  found = 0;
124 
125  if (found == 1)
126  break;
127 
128  fgf_file = fgf_file->next;
129  }
130 
131  if (found == 0) {
132  SCLogAddToFGFFileList(prev_fgf_file, file, function, line, listtype);
133  goto done;
134  }
135 
136  found = 0;
137  fgf_func = fgf_file->func;
138  prev_fgf_func = fgf_func;
139  while (fgf_func != NULL) {
140  prev_fgf_func = fgf_func;
141  if (function == NULL && fgf_func->func == NULL)
142  found = 1;
143  else if (function != NULL && fgf_func->func != NULL)
144  found = (strcmp(function, fgf_func->func) == 0);
145  else
146  found = 0;
147 
148  if (found == 1)
149  break;
150 
151  fgf_func = fgf_func->next;
152  }
153 
154  if (found == 0) {
155  SCLogAddToFGFFuncList(fgf_file, prev_fgf_func, function, line);
156  goto done;
157  }
158 
159  found = 0;
160  fgf_line = fgf_func->line;
161  prev_fgf_line = fgf_line;
162  while(fgf_line != NULL) {
163  prev_fgf_line = fgf_line;
164  if (line == fgf_line->line) {
165  found = 1;
166  break;
167  }
168 
169  fgf_line = fgf_line->next;
170  }
171 
172  if (found == 0) {
173  SCLogAddToFGFLineList(fgf_func, prev_fgf_line, line);
174  goto done;
175  }
176 
177  done:
178  SCMutexUnlock(&sc_log_fg_filters_m[listtype]);
180 
181  return 0;
182 }
183 
184 /**
185  * \brief Internal function used to check for matches against registered FG
186  * filters. Checks if there is a match for the incoming log_message with
187  * any of the FG filters. Based on whether the filter type is whitelist
188  * or blacklist, the function allows the message to be logged or not.
189  *
190  * \param file File_name from where the log_message originated
191  * \param function Function_name from where the log_message originated
192  * \param line Line number from where the log_message originated
193  * \param listtype The filter listtype. Can be either a blacklist or whitelist
194  * filter listtype(SC_LOG_FILTER_BL or SC_LOG_FILTER_WL)
195  *
196  * \retval 1 if there is a match
197  * \retval 0 on no match
198  * \retval -1 on failure
199  */
200 static int SCLogMatchFGFilter(const char *file, const char *function, int line,
201  int listtype)
202 {
203  SCLogFGFilterFile *fgf_file = NULL;
204  SCLogFGFilterFunc *fgf_func = NULL;
205  SCLogFGFilterLine *fgf_line = NULL;
206  int match = 1;
207 
208  if (sc_log_module_initialized != 1) {
209  printf("Logging module not initialized. Call SCLogInitLogModule() "
210  "first before using the debug API\n");
211  return -1;
212  }
213 
214  SCMutexLock(&sc_log_fg_filters_m[listtype]);
215 
216  fgf_file = sc_log_fg_filters[listtype];
217 
218  if (fgf_file == NULL) {
219  SCMutexUnlock(&sc_log_fg_filters_m[listtype]);
220  return 1;
221  }
222 
223  while(fgf_file != NULL) {
224  match = 1;
225 
226  match &= (fgf_file->file != NULL)? !strcmp(file, fgf_file->file): 1;
227 
228  if (match == 0) {
229  fgf_file = fgf_file->next;
230  continue;
231  }
232 
233  fgf_func = fgf_file->func;
234  while (fgf_func != NULL) {
235  match = 1;
236 
237  match &= (fgf_func->func != NULL)? !strcmp(function, fgf_func->func): 1;
238 
239  if (match == 0) {
240  fgf_func = fgf_func->next;
241  continue;
242  }
243 
244  fgf_line = fgf_func->line;
245  while (fgf_line != NULL) {
246  match = 1;
247 
248  match &= (fgf_line->line != -1)? (line == fgf_line->line): 1;
249 
250  if (match == 1)
251  break;
252 
253  fgf_line = fgf_line->next;
254  }
255 
256  if (match == 1)
257  break;
258 
259  fgf_func = fgf_func->next;
260  }
261 
262  if (match == 1) {
263  SCMutexUnlock(&sc_log_fg_filters_m[listtype]);
264  if (listtype == SC_LOG_FILTER_WL)
265  return 1;
266  else
267  return 0;
268  }
269 
270  fgf_file = fgf_file->next;
271  }
272 
273  SCMutexUnlock(&sc_log_fg_filters_m[listtype]);
274 
275  if (listtype == SC_LOG_FILTER_WL)
276  return 0;
277  else
278  return 1;
279 }
280 
281 /**
282  * \brief Checks if there is a match for the incoming log_message with any
283  * of the FG filters. If there is a match, it allows the message
284  * to be logged, else it rejects that message.
285  *
286  * \param file File_name from where the log_message originated
287  * \param function Function_name from where the log_message originated
288  * \param line Line number from where the log_message originated
289  *
290  * \retval 1 if there is a match
291  * \retval 0 on no match
292  * \retval -1 on failure
293  */
294 int SCLogMatchFGFilterWL(const char *file, const char *function, int line)
295 {
296  return SCLogMatchFGFilter(file, function, line, SC_LOG_FILTER_WL);
297 }
298 
299 /**
300  * \brief Checks if there is a match for the incoming log_message with any
301  * of the FG filters. If there is a match it rejects the logging
302  * for that messages, else it allows that message to be logged
303  *
304  * \param file File_name from where the log_message originated
305  * \param function Function_name from where the log_message originated
306  * \param line Line number from where the log_message originated
307  *
308  * \retval 1 if there is a match
309  * \retval 0 on no match
310  * \retval -1 on failure
311  */
312 int SCLogMatchFGFilterBL(const char *file, const char *function, int line)
313 {
314  return SCLogMatchFGFilter(file, function, line, SC_LOG_FILTER_BL);
315 }
316 
317 /**
318  * \brief Adds a Whitelist(WL) fine-grained(FG) filter. A FG filter WL filter
319  * allows messages that match this filter, to be logged, while the filter
320  * is defined using a file_name, function_name and line_number.
321  *
322  * If a particular parameter in the fg-filter(file, function and line),
323  * shouldn't be considered while logging the message, one can supply
324  * NULL for the file_name or function_name and a negative line_no.
325  *
326  * \param file File_name of the filter
327  * \param function Function_name of the filter
328  * \param line Line number of the filter
329  *
330  * \retval 0 on successfully adding the filter;
331  * \retval -1 on failure
332  */
333 int SCLogAddFGFilterWL(const char *file, const char *function, int line)
334 {
335  return SCLogAddFGFilter(file, function, line, SC_LOG_FILTER_WL);
336 }
337 
338 /**
339  * \brief Adds a Blacklist(BL) fine-grained(FG) filter. A FG filter BL filter
340  * allows messages that don't match this filter, to be logged, while the
341  * filter is defined using a file_name, function_name and line_number
342  *
343  * If a particular parameter in the fg-filter(file, function and line),
344  * shouldn't be considered while logging the message, one can supply
345  * NULL for the file_name or function_name and a negative line_no.
346  *
347  * \param file File_name of the filter
348  * \param function Function_name of the filter
349  * \param line Line number of the filter
350  *
351  * \retval 0 on successfully adding the filter
352  * \retval -1 on failure
353  */
354 int SCLogAddFGFilterBL(const char *file, const char *function, int line)
355 {
356  return SCLogAddFGFilter(file, function, line, SC_LOG_FILTER_BL);
357 }
358 
360 {
361  SCLogFGFilterFile *fgf_file = NULL;
362  SCLogFGFilterFunc *fgf_func = NULL;
363  SCLogFGFilterLine *fgf_line = NULL;
364 
365  void *temp = NULL;
366 
367  int i = 0;
368 
369  for (i = 0; i < SC_LOG_FILTER_MAX; i++) {
370  SCMutexLock(&sc_log_fg_filters_m[i]);
371 
372  fgf_file = sc_log_fg_filters[i];
373  while (fgf_file != NULL) {
374 
375  fgf_func = fgf_file->func;
376  while (fgf_func != NULL) {
377 
378  fgf_line = fgf_func->line;
379  while(fgf_line != NULL) {
380  temp = fgf_line;
381  fgf_line = fgf_line->next;
382  SCFree(temp);
383  }
384 
385  if (fgf_func->func != NULL)
386  SCFree(fgf_func->func);
387  temp = fgf_func;
388  fgf_func = fgf_func->next;
389  SCFree(temp);
390  }
391 
392  if (fgf_file->file != NULL)
393  SCFree(fgf_file->file);
394  temp = fgf_file;
395  fgf_file = fgf_file->next;
396  SCFree(temp);
397  }
398 
399  SCMutexUnlock(&sc_log_fg_filters_m[i]);
400  sc_log_fg_filters[i] = NULL;
401  }
402 
403  return;
404 }
405 
406 /**
407  * \brief Prints the FG filters(both WL and BL). Used for debugging purposes.
408  *
409  * \retval count The no of FG filters
410  */
412 {
413  SCLogFGFilterFile *fgf_file = NULL;
414  SCLogFGFilterFunc *fgf_func = NULL;
415  SCLogFGFilterLine *fgf_line = NULL;
416 
417  int count = 0;
418  int i = 0;
419 
420  if (sc_log_module_initialized != 1) {
421  printf("Logging module not initialized. Call SCLogInitLogModule() "
422  "first before using the debug API\n");
423  return 0;
424  }
425 
426 #ifdef DEBUG
427  printf("Fine grained filters:\n");
428 #endif
429 
430  for (i = 0; i < SC_LOG_FILTER_MAX; i++) {
431  SCMutexLock(&sc_log_fg_filters_m[i]);
432 
433  fgf_file = sc_log_fg_filters[i];
434  while (fgf_file != NULL) {
435 
436  fgf_func = fgf_file->func;
437  while (fgf_func != NULL) {
438 
439  fgf_line = fgf_func->line;
440  while(fgf_line != NULL) {
441 #ifdef DEBUG
442  printf("%s - ", fgf_file->file);
443  printf("%s - ", fgf_func->func);
444  printf("%d\n", fgf_line->line);
445 #endif
446 
447  count++;
448 
449  fgf_line = fgf_line->next;
450  }
451 
452  fgf_func = fgf_func->next;
453  }
454 
455  fgf_file = fgf_file->next;
456  }
457  SCMutexUnlock(&sc_log_fg_filters_m[i]);
458  }
459 
460  return count;
461 }
462 
463 
464 
465 /* --------------------------------------------------|--------------------------
466  * -------------------------- Code for the FD Filter |--------------------------
467  * --------------------------------------------------V--------------------------
468  */
469 
470 /**
471  * \brief Checks if there is a match for the incoming log_message with any
472  * of the FD filters
473  *
474  * \param function Function_name from where the log_message originated
475  *
476  * \retval 1 if there is a match
477  * \retval 0 on no match;
478  */
479 int SCLogMatchFDFilter(const char *function)
480 {
481 #ifndef DEBUG
482  return 1;
483 #else
484  SCLogFDFilterThreadList *thread_list = NULL;
485 
486  pthread_t self = pthread_self();
487 
488  if (sc_log_module_initialized != 1) {
489  printf("Logging module not initialized. Call SCLogInitLogModule() "
490  "first before using the debug API\n");
491  return 0;
492  }
493 
494  SCMutexLock(&sc_log_fd_filters_tl_m);
495 
496  if (sc_log_fd_filters_tl == NULL) {
497  SCMutexUnlock(&sc_log_fd_filters_tl_m);
498  if (sc_log_fd_filters != NULL)
499  return 0;
500  return 1;
501  }
502 
503  thread_list = sc_log_fd_filters_tl;
504  while (thread_list != NULL) {
505  if (pthread_equal(self, thread_list->t)) {
506  if (thread_list->entered > 0) {
507  SCMutexUnlock(&sc_log_fd_filters_tl_m);
508  return 1;
509  }
510  SCMutexUnlock(&sc_log_fd_filters_tl_m);
511  return 0;
512  }
513 
514  thread_list = thread_list->next;
515  }
516 
517  SCMutexUnlock(&sc_log_fd_filters_tl_m);
518 
519  return 0;
520 #endif
521 }
522 
523 /**
524  * \brief Updates a FD filter, based on whether the function that calls this
525  * function, is registered as a FD filter or not. This is called by
526  * a function only on its entry
527  *
528  * \param function Function_name from where the log_message originated
529  *
530  * \retval 1 Since it is a hack to get things working inside the macros
531  */
532 int SCLogCheckFDFilterEntry(const char *function)
533 {
534  SCLogFDFilter *curr = NULL;
535 
536  SCLogFDFilterThreadList *thread_list = NULL;
537  SCLogFDFilterThreadList *thread_list_temp = NULL;
538 
539  //pid_t self = syscall(SYS_gettid);
540  pthread_t self = pthread_self();
541 
542  if (sc_log_module_initialized != 1) {
543  printf("Logging module not initialized. Call SCLogInitLogModule() "
544  "first before using the debug API\n");
545  return 0;
546  }
547 
548  SCMutexLock(&sc_log_fd_filters_m);
549 
550  curr = sc_log_fd_filters;
551 
552  while (curr != NULL) {
553  if (strcmp(function, curr->func) == 0)
554  break;
555 
556  curr = curr->next;
557  }
558 
559  if (curr == NULL) {
560  SCMutexUnlock(&sc_log_fd_filters_m);
561  return 1;
562  }
563 
564  SCMutexUnlock(&sc_log_fd_filters_m);
565 
566  SCMutexLock(&sc_log_fd_filters_tl_m);
567 
568  thread_list = sc_log_fd_filters_tl;
569  while (thread_list != NULL) {
570  if (pthread_equal(self, thread_list->t))
571  break;
572 
573  thread_list = thread_list->next;
574  }
575 
576  if (thread_list != NULL) {
577  thread_list->entered++;
578  SCMutexUnlock(&sc_log_fd_filters_tl_m);
579  return 1;
580  }
581 
582  if ( (thread_list_temp = SCMalloc(sizeof(SCLogFDFilterThreadList))) == NULL) {
583  SCMutexUnlock(&sc_log_fd_filters_tl_m);
584  return 0;
585  }
586  memset(thread_list_temp, 0, sizeof(SCLogFDFilterThreadList));
587 
588  thread_list_temp->t = self;
589  thread_list_temp->entered++;
590 
591  sc_log_fd_filters_tl = thread_list_temp;
592 
593  SCMutexUnlock(&sc_log_fd_filters_tl_m);
594 
595  return 1;
596 }
597 
598 /**
599  * \brief Updates a FD filter, based on whether the function that calls this
600  * function, is registered as a FD filter or not. This is called by
601  * a function only before its exit.
602  *
603  * \param function Function_name from where the log_message originated
604  *
605  */
606 void SCLogCheckFDFilterExit(const char *function)
607 {
608  SCLogFDFilter *curr = NULL;
609 
610  SCLogFDFilterThreadList *thread_list = NULL;
611 
612  //pid_t self = syscall(SYS_gettid);
613  pthread_t self = pthread_self();
614 
615  if (sc_log_module_initialized != 1) {
616  printf("Logging module not initialized. Call SCLogInitLogModule() "
617  "first before using the debug API\n");
618  return;
619  }
620 
621  SCMutexLock(&sc_log_fd_filters_m);
622 
623  curr = sc_log_fd_filters;
624 
625  while (curr != NULL) {
626  if (strcmp(function, curr->func) == 0)
627  break;
628 
629  curr = curr->next;
630  }
631 
632  if (curr == NULL) {
633  SCMutexUnlock(&sc_log_fd_filters_m);
634  return;
635  }
636 
637  SCMutexUnlock(&sc_log_fd_filters_m);
638 
639  SCMutexLock(&sc_log_fd_filters_tl_m);
640 
641  thread_list = sc_log_fd_filters_tl;
642  while (thread_list != NULL) {
643  if (pthread_equal(self, thread_list->t))
644  break;
645 
646  thread_list = thread_list->next;
647  }
648 
649  SCMutexUnlock(&sc_log_fd_filters_tl_m);
650 
651  if (thread_list != NULL)
652  thread_list->entered--;
653 
654  return;
655 }
656 
657 /**
658  * \brief Adds a Function-Dependent(FD) filter
659  *
660  * \param Name of the function for which a FD filter has to be registered
661  *
662  * \retval 0 on success
663  * \retval -1 on failure
664  */
665 int SCLogAddFDFilter(const char *function)
666 {
667  SCLogFDFilter *curr = NULL;
668  SCLogFDFilter *prev = NULL;
669  SCLogFDFilter *temp = NULL;
670 
671  if (sc_log_module_initialized != 1) {
672  printf("Logging module not initialized. Call SCLogInitLogModule() "
673  "first before using the debug API\n");
674  return -1;
675  }
676 
677  if (function == NULL) {
678  printf("Invalid argument supplied to SCLogAddFDFilter\n");
679  return -1;
680  }
681 
682  SCMutexLock(&sc_log_fd_filters_m);
683 
684  curr = sc_log_fd_filters;
685  while (curr != NULL) {
686  prev = curr;
687 
688  if (strcmp(function, curr->func) == 0) {
689 
690  SCMutexUnlock(&sc_log_fd_filters_m);
691  return 0;
692  }
693 
694  curr = curr->next;
695  }
696 
697  if ( (temp = SCMalloc(sizeof(SCLogFDFilter))) == NULL) {
698  printf("Error Allocating memory (SCMalloc)\n");
699  exit(EXIT_FAILURE);
700  }
701  memset(temp, 0, sizeof(SCLogFDFilter));
702 
703  if ( (temp->func = SCStrdup(function)) == NULL) {
704  printf("Error Allocating memory (SCStrdup)\n");
705  exit(EXIT_FAILURE);
706  }
707 
708  if (sc_log_fd_filters == NULL)
709  sc_log_fd_filters = temp;
710  /* clang thinks prev can be NULL, but it can't be unless
711  * sc_log_fd_filters is also NULL which is handled here.
712  * Doing this "fix" to shut clang up. */
713  else if (prev != NULL)
714  prev->next = temp;
715 
716  SCMutexUnlock(&sc_log_fd_filters_m);
718 
719  return 0;
720 }
721 
722 /**
723  * \brief Releases all the FD filters added to the logging module
724  */
726 {
727  SCLogFDFilter *fdf = NULL;
728  SCLogFDFilter *temp = NULL;
729 
730  SCMutexLock(&sc_log_fd_filters_m);
731 
732  fdf = sc_log_fd_filters;
733  while (fdf != NULL) {
734  temp = fdf;
735  fdf = fdf->next;
736  SCLogReleaseFDFilter(temp);
737  }
738 
739  sc_log_fd_filters = NULL;
740 
741  SCMutexUnlock( &sc_log_fd_filters_m );
742 
743  return;
744 }
745 
746 /**
747  * \brief Removes a Function-Dependent(FD) filter
748  *
749  * \param Name of the function for which a FD filter has to be unregistered
750  *
751  * \retval 0 on success(the filter was removed or the filter was not present)
752  * \retval -1 on failure/error
753  */
754 int SCLogRemoveFDFilter(const char *function)
755 {
756  SCLogFDFilter *curr = NULL;
757  SCLogFDFilter *prev = NULL;
758 
759  if (sc_log_module_initialized != 1) {
760  printf("Logging module not initialized. Call SCLogInitLogModule() "
761  "first before using the debug API\n");
762  return -1 ;
763  }
764 
765  if (function == NULL) {
766  printf("Invalid argument(s) supplied to SCLogRemoveFDFilter\n");
767  return -1;
768  }
769 
770  SCMutexLock(&sc_log_fd_filters_m);
771 
772  if (sc_log_fd_filters == NULL) {
773  SCMutexUnlock(&sc_log_fd_filters_m);
774  return 0;
775  }
776 
777  curr = sc_log_fd_filters;
778  prev = curr;
779  while (curr != NULL) {
780  if (strcmp(function, curr->func) == 0)
781  break;
782 
783  prev = curr;
784  curr = curr->next;
785  }
786 
787  if (curr == NULL) {
788 
789  SCMutexUnlock(&sc_log_fd_filters_m);
790 
791  return 0;
792  }
793 
794  if (sc_log_fd_filters == curr)
795  sc_log_fd_filters = curr->next;
796  else
797  prev->next = curr->next;
798 
799  SCLogReleaseFDFilter(curr);
800 
801  SCMutexUnlock(&sc_log_fd_filters_m);
802 
803  if (sc_log_fd_filters == NULL)
805 
806  return 0;
807 }
808 
809 /**
810  * \brief Prints the FG filters(both WL and BL). Used for debugging purposes.
811  *
812  * \retval count The no of FG filters
813  */
815 {
816  SCLogFDFilter *fdf = NULL;
817  int count = 0;
818 
819  if (sc_log_module_initialized != 1) {
820  printf("Logging module not initialized. Call SCLogInitLogModule() "
821  "first before using the debug API\n");
822  return 0;
823  }
824 
825 #ifdef DEBUG
826  printf("FD filters:\n");
827 #endif
828 
829  SCMutexLock(&sc_log_fd_filters_m);
830 
831  fdf = sc_log_fd_filters;
832  while (fdf != NULL) {
833 #ifdef DEBUG
834  printf("%s \n", fdf->func);
835 #endif
836  fdf = fdf->next;
837  count++;
838  }
839 
840  SCMutexUnlock(&sc_log_fd_filters_m);
841 
842  return count;
843 }
844 
845 /**
846  * \brief Helper function used internally to add a FG filter. This function is
847  * called when the file component of the incoming filter has no entry
848  * in the filter list.
849  *
850  * \param fgf_file The file component(basically the position in the list) from
851  * the filter list, after which the new filter has to be added
852  * \param file File_name of the filter
853  * \param function Function_name of the filter
854  * \param line Line number of the filter
855  * \param listtype The filter listtype. Can be either a blacklist or whitelist
856  * filter listtype(SC_LOG_FILTER_BL or SC_LOG_FILTER_WL)
857  */
859  const char *file,
860  const char *function, int line,
861  int listtype)
862 {
863  SCLogFGFilterFile *fgf_file_temp = NULL;
864  SCLogFGFilterFunc *fgf_func_temp = NULL;
865  SCLogFGFilterLine *fgf_line_temp = NULL;
866 
867  if ( (fgf_file_temp = SCMalloc(sizeof(SCLogFGFilterFile))) == NULL) {
868  FatalError("Fatal error encountered in SCLogAddToFGFFileList. Exiting...");
869  }
870  memset(fgf_file_temp, 0, sizeof(SCLogFGFilterFile));
871 
872  if ( file != NULL && (fgf_file_temp->file = SCStrdup(file)) == NULL) {
873  printf("Error Allocating memory\n");
874  exit(EXIT_FAILURE);
875  }
876 
877  if ( (fgf_func_temp = SCMalloc(sizeof(SCLogFGFilterFunc))) == NULL) {
878  FatalError("Fatal error encountered in SCLogAddToFGFFileList. Exiting...");
879  }
880  memset(fgf_func_temp, 0, sizeof(SCLogFGFilterFunc));
881 
882  if ( function != NULL && (fgf_func_temp->func = SCStrdup(function)) == NULL) {
883  printf("Error Allocating memory\n");
884  exit(EXIT_FAILURE);
885  }
886 
887  if ( (fgf_line_temp = SCMalloc(sizeof(SCLogFGFilterLine))) == NULL) {
888  FatalError("Fatal error encountered in SCLogAddToFGFFileList. Exiting...");
889  }
890  memset(fgf_line_temp, 0, sizeof(SCLogFGFilterLine));
891 
892  fgf_line_temp->line = line;
893 
894  /* add to the lists */
895  fgf_func_temp->line = fgf_line_temp;
896 
897  fgf_file_temp->func = fgf_func_temp;
898 
899  if (fgf_file == NULL)
900  sc_log_fg_filters[listtype] = fgf_file_temp;
901  else
902  fgf_file->next = fgf_file_temp;
903 
904  return;
905 }
906 
907 /**
908  * \brief Helper function used internally to add a FG filter. This function is
909  * called when the file component of the incoming filter has an entry
910  * in the filter list, but the function component doesn't have an entry
911  * for the corresponding file component
912  *
913  * \param fgf_file The file component from the filter list to which the new
914  * filter has to be added
915  * \param fgf_func The function component(basically the position in the list),
916  * from the filter list, after which the new filter has to be
917  * added
918  * \param function Function_name of the filter
919  * \param line Line number of the filter
920  */
922  SCLogFGFilterFunc *fgf_func,
923  const char *function, int line)
924 {
925  SCLogFGFilterFunc *fgf_func_temp = NULL;
926  SCLogFGFilterLine *fgf_line_temp = NULL;
927 
928  if ( (fgf_func_temp = SCMalloc(sizeof(SCLogFGFilterFunc))) == NULL) {
929  FatalError("Fatal error encountered in SCLogAddToFGFFuncList. Exiting...");
930  }
931  memset(fgf_func_temp, 0, sizeof(SCLogFGFilterFunc));
932 
933  if ( function != NULL && (fgf_func_temp->func = SCStrdup(function)) == NULL) {
934  printf("Error Allocating memory\n");
935  exit(EXIT_FAILURE);
936  }
937 
938  if ( (fgf_line_temp = SCMalloc(sizeof(SCLogFGFilterLine))) == NULL) {
939  FatalError("Fatal error encountered in SCLogAddToFGFFuncList. Exiting...");
940  }
941  memset(fgf_line_temp, 0, sizeof(SCLogFGFilterLine));
942 
943  fgf_line_temp->line = line;
944 
945  /* add to the lists */
946  fgf_func_temp->line = fgf_line_temp;
947 
948  if (fgf_func == NULL)
949  fgf_file->func = fgf_func_temp;
950  else
951  fgf_func->next = fgf_func_temp;
952 
953  return;
954 }
955 
956 /**
957  * \brief Helper function used internally to add a FG filter. This function is
958  * called when the file and function components of the incoming filter
959  * have an entry in the filter list, but the line component doesn't have
960  * an entry for the corresponding function component
961  *
962  * \param fgf_func The function component from the filter list to which the new
963  * filter has to be added
964  * \param fgf_line The function component(basically the position in the list),
965  * from the filter list, after which the new filter has to be
966  * added
967  * \param line Line number of the filter
968  */
970  SCLogFGFilterLine *fgf_line,
971  int line)
972 {
973  SCLogFGFilterLine *fgf_line_temp = NULL;
974 
975  if ( (fgf_line_temp = SCMalloc(sizeof(SCLogFGFilterLine))) == NULL) {
976  FatalError("Fatal error encountered in SCLogAddToFGFLineList. Exiting...");
977  }
978  memset(fgf_line_temp, 0, sizeof(SCLogFGFilterLine));
979 
980  fgf_line_temp->line = line;
981 
982  /* add to the lists */
983  if (fgf_line == NULL)
984  fgf_func->line = fgf_line_temp;
985  else
986  fgf_line->next = fgf_line_temp;
987 
988  return;
989 }
990 
991 /**
992  * \brief Releases the memory alloted to a FD filter
993  *
994  * \param Pointer to the FD filter that has to be freed
995  */
997 {
998  if (fdf != NULL) {
999  if (fdf->func != NULL)
1000  SCFree(fdf->func);
1001  SCFree(fdf);
1002  }
1003 
1004  return;
1005 }
1006 
SCLogMatchFGFilterBL
int SCLogMatchFGFilterBL(const char *file, const char *function, int line)
Checks if there is a match for the incoming log_message with any of the FG filters....
Definition: util-debug-filters.c:312
SCLogFDFilterThreadList_
Structure used to hold the thread_list used by FD filters.
Definition: util-debug-filters.h:72
SCLogCheckFDFilterEntry
int SCLogCheckFDFilterEntry(const char *function)
Updates a FD filter, based on whether the function that calls this function, is registered as a FD fi...
Definition: util-debug-filters.c:532
SCLogAddToFGFFuncList
void SCLogAddToFGFFuncList(SCLogFGFilterFile *fgf_file, SCLogFGFilterFunc *fgf_func, const char *function, int line)
Helper function used internally to add a FG filter. This function is called when the file component o...
Definition: util-debug-filters.c:921
SCLogReleaseFDFilter
void SCLogReleaseFDFilter(SCLogFDFilter *fdf)
Releases the memory alloted to a FD filter.
Definition: util-debug-filters.c:996
sc_log_fg_filters_present
int sc_log_fg_filters_present
Definition: util-debug-filters.c:36
threads.h
SCLogFDFilterThreadList_::t
pthread_t t
Definition: util-debug-filters.h:74
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
SCLogFGFilterLine_::line
int line
Definition: util-debug-filters.h:43
SCMUTEX_INITIALIZER
#define SCMUTEX_INITIALIZER
Definition: threads-debug.h:121
SCLogFGFilterLine_::next
struct SCLogFGFilterLine_ * next
Definition: util-debug-filters.h:45
SCLogAddToFGFLineList
void SCLogAddToFGFLineList(SCLogFGFilterFunc *fgf_func, SCLogFGFilterLine *fgf_line, int line)
Helper function used internally to add a FG filter. This function is called when the file and functio...
Definition: util-debug-filters.c:969
m
SCMutex m
Definition: flow-hash.h:6
SCLogMatchFDFilter
int SCLogMatchFDFilter(const char *function)
Checks if there is a match for the incoming log_message with any of the FD filters.
Definition: util-debug-filters.c:479
SC_LOG_FILTER_BL
@ SC_LOG_FILTER_BL
Definition: util-debug-filters.h:34
SCLogFGFilterFile_::next
struct SCLogFGFilterFile_ * next
Definition: util-debug-filters.h:66
SCLogReleaseFGFilters
void SCLogReleaseFGFilters(void)
Definition: util-debug-filters.c:359
SCLogAddFGFilterWL
int SCLogAddFGFilterWL(const char *file, const char *function, int line)
Adds a Whitelist(WL) fine-grained(FG) filter. A FG filter WL filter allows messages that match this f...
Definition: util-debug-filters.c:333
util-debug.h
SCLogReleaseFDFilters
void SCLogReleaseFDFilters(void)
Releases all the FD filters added to the logging module.
Definition: util-debug-filters.c:725
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:119
SCLogFGFilterFunc_::line
SCLogFGFilterLine * line
Definition: util-debug-filters.h:53
SCLogCheckFDFilterExit
void SCLogCheckFDFilterExit(const char *function)
Updates a FD filter, based on whether the function that calls this function, is registered as a FD fi...
Definition: util-debug-filters.c:606
SCLogFDFilter_::next
struct SCLogFDFilter_ * next
Definition: util-debug-filters.h:86
SCLogFDFilterThreadList_::next
struct SCLogFDFilterThreadList_ * next
Definition: util-debug-filters.h:77
SCLogMatchFGFilterWL
int SCLogMatchFGFilterWL(const char *file, const char *function, int line)
Checks if there is a match for the incoming log_message with any of the FG filters....
Definition: util-debug-filters.c:294
SCLogFGFilterLine_
Structure used to hold the line_no details of a FG filter.
Definition: util-debug-filters.h:42
SCLogAddToFGFFileList
void SCLogAddToFGFFileList(SCLogFGFilterFile *fgf_file, const char *file, const char *function, int line, int listtype)
Helper function used internally to add a FG filter. This function is called when the file component o...
Definition: util-debug-filters.c:858
SCLogFDFilter_
Structure that holds the FD filters.
Definition: util-debug-filters.h:83
SCLogPrintFDFilters
int SCLogPrintFDFilters(void)
Prints the FG filters(both WL and BL). Used for debugging purposes.
Definition: util-debug-filters.c:814
suricata-common.h
SC_LOG_FILTER_WL
@ SC_LOG_FILTER_WL
Definition: util-debug-filters.h:35
sc_log_fd_filters_present
int sc_log_fd_filters_present
Definition: util-debug-filters.c:39
SCLogFGFilterFunc_::func
char * func
Definition: util-debug-filters.h:52
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
FatalError
#define FatalError(...)
Definition: util-debug.h:502
SCLogFDFilter_::func
char * func
Definition: util-debug-filters.h:84
sc_log_module_cleaned
int sc_log_module_cleaned
Used to indicate whether the logging module has been cleaned or not.
Definition: util-debug.c:111
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCLogFGFilterFunc_
structure used to hold the function details of a FG filter
Definition: util-debug-filters.h:51
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SCLogRemoveFDFilter
int SCLogRemoveFDFilter(const char *function)
Removes a Function-Dependent(FD) filter.
Definition: util-debug-filters.c:754
SCLogPrintFGFilters
int SCLogPrintFGFilters(void)
Prints the FG filters(both WL and BL). Used for debugging purposes.
Definition: util-debug-filters.c:411
SCLogAddFGFilterBL
int SCLogAddFGFilterBL(const char *file, const char *function, int line)
Adds a Blacklist(BL) fine-grained(FG) filter. A FG filter BL filter allows messages that don't match ...
Definition: util-debug-filters.c:354
sc_log_module_initialized
int sc_log_module_initialized
Used to indicate whether the logging module has been init or not.
Definition: util-debug.c:106
SCLogFGFilterFile_::func
SCLogFGFilterFunc * func
Definition: util-debug-filters.h:64
util-debug-filters.h
SCLogAddFDFilter
int SCLogAddFDFilter(const char *function)
Adds a Function-Dependent(FD) filter.
Definition: util-debug-filters.c:665
SCLogFGFilterFile_
Structure used to hold FG filters. Encapsulates filename details, func details, which inturn encapsul...
Definition: util-debug-filters.h:62
SCLogFGFilterFunc_::next
struct SCLogFGFilterFunc_ * next
Definition: util-debug-filters.h:55
sc_log_fg_filters
SCLogFGFilterFile * sc_log_fg_filters[SC_LOG_FILTER_MAX]
Holds the fine-grained filters.
Definition: util-debug-filters.c:44
SCMutex
#define SCMutex
Definition: threads-debug.h:114
SCLogFDFilterThreadList_::entered
int entered
Definition: util-debug-filters.h:73
SC_LOG_FILTER_MAX
@ SC_LOG_FILTER_MAX
Definition: util-debug-filters.h:36
SCLogFGFilterFile_::file
char * file
Definition: util-debug-filters.h:63