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