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 = SCCalloc(1, sizeof(SCLogFDFilterThreadList))) == NULL) {
583  SCMutexUnlock(&sc_log_fd_filters_tl_m);
584  return 0;
585  }
586 
587  thread_list_temp->t = self;
588  thread_list_temp->entered++;
589 
590  sc_log_fd_filters_tl = thread_list_temp;
591 
592  SCMutexUnlock(&sc_log_fd_filters_tl_m);
593 
594  return 1;
595 }
596 
597 /**
598  * \brief Updates a FD filter, based on whether the function that calls this
599  * function, is registered as a FD filter or not. This is called by
600  * a function only before its exit.
601  *
602  * \param function Function_name from where the log_message originated
603  *
604  */
605 void SCLogCheckFDFilterExit(const char *function)
606 {
607  SCLogFDFilter *curr = NULL;
608 
609  SCLogFDFilterThreadList *thread_list = NULL;
610 
611  //pid_t self = syscall(SYS_gettid);
612  pthread_t self = pthread_self();
613 
614  if (sc_log_module_initialized != 1) {
615  printf("Logging module not initialized. Call SCLogInitLogModule() "
616  "first before using the debug API\n");
617  return;
618  }
619 
620  SCMutexLock(&sc_log_fd_filters_m);
621 
622  curr = sc_log_fd_filters;
623 
624  while (curr != NULL) {
625  if (strcmp(function, curr->func) == 0)
626  break;
627 
628  curr = curr->next;
629  }
630 
631  if (curr == NULL) {
632  SCMutexUnlock(&sc_log_fd_filters_m);
633  return;
634  }
635 
636  SCMutexUnlock(&sc_log_fd_filters_m);
637 
638  SCMutexLock(&sc_log_fd_filters_tl_m);
639 
640  thread_list = sc_log_fd_filters_tl;
641  while (thread_list != NULL) {
642  if (pthread_equal(self, thread_list->t))
643  break;
644 
645  thread_list = thread_list->next;
646  }
647 
648  SCMutexUnlock(&sc_log_fd_filters_tl_m);
649 
650  if (thread_list != NULL)
651  thread_list->entered--;
652 
653  return;
654 }
655 
656 /**
657  * \brief Adds a Function-Dependent(FD) filter
658  *
659  * \param Name of the function for which a FD filter has to be registered
660  *
661  * \retval 0 on success
662  * \retval -1 on failure
663  */
664 int SCLogAddFDFilter(const char *function)
665 {
666  SCLogFDFilter *curr = NULL;
667  SCLogFDFilter *prev = NULL;
668  SCLogFDFilter *temp = NULL;
669 
670  if (sc_log_module_initialized != 1) {
671  printf("Logging module not initialized. Call SCLogInitLogModule() "
672  "first before using the debug API\n");
673  return -1;
674  }
675 
676  if (function == NULL) {
677  printf("Invalid argument supplied to SCLogAddFDFilter\n");
678  return -1;
679  }
680 
681  SCMutexLock(&sc_log_fd_filters_m);
682 
683  curr = sc_log_fd_filters;
684  while (curr != NULL) {
685  prev = curr;
686 
687  if (strcmp(function, curr->func) == 0) {
688 
689  SCMutexUnlock(&sc_log_fd_filters_m);
690  return 0;
691  }
692 
693  curr = curr->next;
694  }
695 
696  if ((temp = SCCalloc(1, sizeof(SCLogFDFilter))) == NULL) {
697  printf("Error Allocating memory (SCCalloc)\n");
698  exit(EXIT_FAILURE);
699  }
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 = SCCalloc(1, sizeof(SCLogFGFilterFile))) == NULL) {
866  FatalError("Fatal error encountered in SCLogAddToFGFFileList. Exiting...");
867  }
868 
869  if ( file != NULL && (fgf_file_temp->file = SCStrdup(file)) == NULL) {
870  printf("Error Allocating memory\n");
871  exit(EXIT_FAILURE);
872  }
873 
874  if ((fgf_func_temp = SCCalloc(1, sizeof(SCLogFGFilterFunc))) == NULL) {
875  FatalError("Fatal error encountered in SCLogAddToFGFFileList. Exiting...");
876  }
877 
878  if ( function != NULL && (fgf_func_temp->func = SCStrdup(function)) == NULL) {
879  printf("Error Allocating memory\n");
880  exit(EXIT_FAILURE);
881  }
882 
883  if ((fgf_line_temp = SCCalloc(1, sizeof(SCLogFGFilterLine))) == NULL) {
884  FatalError("Fatal error encountered in SCLogAddToFGFFileList. Exiting...");
885  }
886 
887  fgf_line_temp->line = line;
888 
889  /* add to the lists */
890  fgf_func_temp->line = fgf_line_temp;
891 
892  fgf_file_temp->func = fgf_func_temp;
893 
894  if (fgf_file == NULL)
895  sc_log_fg_filters[listtype] = fgf_file_temp;
896  else
897  fgf_file->next = fgf_file_temp;
898 
899  return;
900 }
901 
902 /**
903  * \brief Helper function used internally to add a FG filter. This function is
904  * called when the file component of the incoming filter has an entry
905  * in the filter list, but the function component doesn't have an entry
906  * for the corresponding file component
907  *
908  * \param fgf_file The file component from the filter list to which the new
909  * filter has to be added
910  * \param fgf_func The function component(basically the position in the list),
911  * from the filter list, after which the new filter has to be
912  * added
913  * \param function Function_name of the filter
914  * \param line Line number of the filter
915  */
917  SCLogFGFilterFunc *fgf_func,
918  const char *function, int line)
919 {
920  SCLogFGFilterFunc *fgf_func_temp = NULL;
921  SCLogFGFilterLine *fgf_line_temp = NULL;
922 
923  if ((fgf_func_temp = SCCalloc(1, sizeof(SCLogFGFilterFunc))) == NULL) {
924  FatalError("Fatal error encountered in SCLogAddToFGFFuncList. Exiting...");
925  }
926 
927  if ( function != NULL && (fgf_func_temp->func = SCStrdup(function)) == NULL) {
928  printf("Error Allocating memory\n");
929  exit(EXIT_FAILURE);
930  }
931 
932  if ((fgf_line_temp = SCCalloc(1, sizeof(SCLogFGFilterLine))) == NULL) {
933  FatalError("Fatal error encountered in SCLogAddToFGFFuncList. Exiting...");
934  }
935 
936  fgf_line_temp->line = line;
937 
938  /* add to the lists */
939  fgf_func_temp->line = fgf_line_temp;
940 
941  if (fgf_func == NULL)
942  fgf_file->func = fgf_func_temp;
943  else
944  fgf_func->next = fgf_func_temp;
945 
946  return;
947 }
948 
949 /**
950  * \brief Helper function used internally to add a FG filter. This function is
951  * called when the file and function components of the incoming filter
952  * have an entry in the filter list, but the line component doesn't have
953  * an entry for the corresponding function component
954  *
955  * \param fgf_func The function component from the filter list to which the new
956  * filter has to be added
957  * \param fgf_line The function component(basically the position in the list),
958  * from the filter list, after which the new filter has to be
959  * added
960  * \param line Line number of the filter
961  */
963  SCLogFGFilterLine *fgf_line,
964  int line)
965 {
966  SCLogFGFilterLine *fgf_line_temp = NULL;
967 
968  if ((fgf_line_temp = SCCalloc(1, sizeof(SCLogFGFilterLine))) == NULL) {
969  FatalError("Fatal error encountered in SCLogAddToFGFLineList. Exiting...");
970  }
971 
972  fgf_line_temp->line = line;
973 
974  /* add to the lists */
975  if (fgf_line == NULL)
976  fgf_func->line = fgf_line_temp;
977  else
978  fgf_line->next = fgf_line_temp;
979 
980  return;
981 }
982 
983 /**
984  * \brief Releases the memory alloted to a FD filter
985  *
986  * \param Pointer to the FD filter that has to be freed
987  */
989 {
990  if (fdf != NULL) {
991  if (fdf->func != NULL)
992  SCFree(fdf->func);
993  SCFree(fdf);
994  }
995 
996  return;
997 }
998 
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:916
SCLogReleaseFDFilter
void SCLogReleaseFDFilter(SCLogFDFilter *fdf)
Releases the memory alloted to a FD filter.
Definition: util-debug-filters.c:988
sc_log_fg_filters_present
int sc_log_fg_filters_present
Definition: util-debug-filters.c:36
SC_LOG_FILTER_BL
@ SC_LOG_FILTER_BL
Definition: util-debug-filters.h:34
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:962
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
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:723
SC_LOG_FILTER_MAX
@ SC_LOG_FILTER_MAX
Definition: util-debug-filters.h:36
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:605
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:856
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:812
suricata-common.h
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
SC_LOG_FILTER_WL
@ SC_LOG_FILTER_WL
Definition: util-debug-filters.h:35
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
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:752
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:664
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
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
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
SCLogFGFilterFile_::file
char * file
Definition: util-debug-filters.h:63