a58509ee |
1 | /* This file is part of the Linux Trace Toolkit viewer |
2 | * Copyright (C) 2003-2004 Michel Dagenais |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License Version 2 as |
6 | * published by the Free Software Foundation; |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License |
14 | * along with this program; if not, write to the Free Software |
15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
16 | * MA 02111-1307, USA. |
17 | */ |
18 | |
91ad3f0a |
19 | /* |
20 | * The text filter facility will prompt for user filter option |
21 | * and transmit them to the lttv core |
22 | */ |
a58509ee |
23 | |
24 | #include <lttv/lttv.h> |
25 | #include <lttv/option.h> |
26 | #include <lttv/module.h> |
27 | #include <lttv/hook.h> |
28 | #include <lttv/attribute.h> |
29 | #include <lttv/iattribute.h> |
30 | #include <lttv/stats.h> |
31 | #include <lttv/filter.h> |
32 | #include <ltt/ltt.h> |
33 | #include <ltt/event.h> |
34 | #include <ltt/type.h> |
35 | #include <ltt/trace.h> |
36 | #include <ltt/facility.h> |
37 | #include <stdio.h> |
38 | |
39 | /* Insert the hooks before and after each trace and tracefile, and for each |
40 | event. Print a global header. */ |
41 | |
91ad3f0a |
42 | /* |
43 | * YET TO BE ANSWERED ! |
44 | * - why does this module need dependency with batchAnalysis ? |
45 | */ |
46 | |
1601b365 |
47 | /* |
48 | * TODO |
49 | * - specify wich hook function will be used to call the core filter |
50 | */ |
51 | |
91ad3f0a |
52 | static char |
a58509ee |
53 | *a_file_name = NULL, |
91ad3f0a |
54 | *a_string = NULL; |
55 | |
a58509ee |
56 | static LttvHooks |
57 | *before_traceset, |
58 | *event_hook; |
59 | |
a58509ee |
60 | /** |
91ad3f0a |
61 | * filters the file input from user |
a58509ee |
62 | * @param hook_data the hook data |
91ad3f0a |
63 | * @return success/failure of operation |
a58509ee |
64 | */ |
91ad3f0a |
65 | void filter_analyze_file(void *hook_data) { |
66 | |
4d9ff942 |
67 | // g_print("textFilter::filter_analyze_file\n"); |
91ad3f0a |
68 | |
73050a5f |
69 | LttvAttributeValue value; |
70 | |
71 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
72 | |
91ad3f0a |
73 | /* |
a58509ee |
74 | * User may specify filtering options through static file |
75 | * and/or command line string. From these sources, an |
76 | * option string is rebuilded and sent to the filter core |
77 | */ |
c684db06 |
78 | if(!g_file_test(a_file_name,G_FILE_TEST_EXISTS)) { |
79 | g_warning("file %s does not exist", a_file_name); |
80 | return; |
81 | } |
82 | |
83 | char* a_file_content = NULL; |
84 | |
85 | g_file_get_contents(a_file_name,&a_file_content,NULL,NULL); |
91ad3f0a |
86 | |
73050a5f |
87 | g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression", |
88 | LTTV_POINTER, &value)); |
89 | |
73050a5f |
90 | if(((GString*)*(value.v_pointer))->len != 0) g_string_append_c((GString*)*(value.v_pointer),'&'); |
c684db06 |
91 | g_string_append_c((GString*)*(value.v_pointer),'('); |
92 | g_string_append((GString*)*(value.v_pointer),a_file_content); |
93 | g_string_append_c((GString*)*(value.v_pointer),')'); |
56e29124 |
94 | |
91ad3f0a |
95 | } |
96 | |
97 | /** |
98 | * filters the string input from user |
99 | * @param hook_data the hook data |
100 | * @return success/failure of operation |
101 | */ |
102 | void filter_analyze_string(void *hook_data) { |
103 | |
4d9ff942 |
104 | // g_print("textFilter::filter_analyze_string\n"); |
18d1226f |
105 | |
73050a5f |
106 | LttvAttributeValue value; |
107 | |
108 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
109 | |
91ad3f0a |
110 | /* |
111 | * User may specify filtering options through static file |
112 | * and/or command line string. From these sources, an |
113 | * option string is rebuilded and sent to the filter core |
114 | */ |
73050a5f |
115 | g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression", |
116 | LTTV_POINTER, &value)); |
cec3d7b0 |
117 | |
73050a5f |
118 | if(((GString*)*(value.v_pointer))->len != 0) g_string_append_c((GString*)*(value.v_pointer),'&'); |
c684db06 |
119 | g_string_append_c((GString*)*(value.v_pointer),'('); |
73050a5f |
120 | g_string_append((GString*)*(value.v_pointer),a_string); |
c684db06 |
121 | g_string_append_c((GString*)*(value.v_pointer),')'); |
56e29124 |
122 | |
a58509ee |
123 | } |
124 | |
125 | /** |
126 | * initialize the new module |
127 | */ |
128 | static void init() { |
129 | |
4d9ff942 |
130 | // g_print("textFilter::init()\n"); /* debug */ |
91ad3f0a |
131 | |
a58509ee |
132 | LttvAttributeValue value; |
133 | |
134 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
135 | |
73050a5f |
136 | g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression", |
137 | LTTV_POINTER, &value)); |
138 | |
139 | *(value.v_pointer) = g_string_new(""); |
140 | |
a58509ee |
141 | g_info("Init textFilter.c"); |
cec3d7b0 |
142 | |
91ad3f0a |
143 | a_string = NULL; |
73050a5f |
144 | lttv_option_add("expression", 'e', |
a58509ee |
145 | "filters a string issued by the user on the command line", |
146 | "string", |
91ad3f0a |
147 | LTTV_OPT_STRING, &a_string, filter_analyze_string, NULL); |
1a7fa682 |
148 | // add function to call for option |
149 | |
a58509ee |
150 | a_file_name = NULL; |
151 | lttv_option_add("filename", 'f', |
152 | "browse the filter options contained in specified file", |
153 | "file name", |
91ad3f0a |
154 | LTTV_OPT_STRING, &a_file_name, filter_analyze_file, NULL); |
a58509ee |
155 | |
a58509ee |
156 | } |
157 | |
158 | /** |
159 | * Destroy the current module |
160 | */ |
161 | static void destroy() { |
162 | g_info("Destroy textFilter"); |
163 | |
73050a5f |
164 | lttv_option_remove("expression"); |
a58509ee |
165 | |
166 | lttv_option_remove("filename"); |
167 | |
73050a5f |
168 | LttvAttributeValue value; |
169 | |
170 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
a58509ee |
171 | |
73050a5f |
172 | g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression", |
173 | LTTV_POINTER, &value)); |
174 | |
175 | g_string_free((GString*)*(value.v_pointer),TRUE); |
a58509ee |
176 | |
177 | } |
178 | |
179 | |
180 | LTTV_MODULE("textFilter", "Filters traces", \ |
181 | "Filter the trace following commands issued by user input", \ |
73050a5f |
182 | init, destroy, "option") |
a58509ee |
183 | |