a58509ee |
1 | /* This file is part of the Linux Trace Toolkit viewer |
852f16bb |
2 | * Copyright (C) 2005 Simon Bouvier-Zappa |
a58509ee |
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 | |
7e7af7f2 |
19 | /*! \file lttv/modules/text/textFilter.c |
20 | * \brief Textual prompt for user filtering expression. |
21 | * |
22 | * The text filter facility will prompt for user filter option |
23 | * and transmit them to the lttv core. User can either specify |
24 | * a filtering string with the command line or/and specify a |
25 | * file containing filtering expressions. |
91ad3f0a |
26 | */ |
a58509ee |
27 | |
28 | #include <lttv/lttv.h> |
29 | #include <lttv/option.h> |
30 | #include <lttv/module.h> |
31 | #include <lttv/hook.h> |
32 | #include <lttv/attribute.h> |
33 | #include <lttv/iattribute.h> |
34 | #include <lttv/stats.h> |
35 | #include <lttv/filter.h> |
36 | #include <ltt/ltt.h> |
37 | #include <ltt/event.h> |
38 | #include <ltt/type.h> |
39 | #include <ltt/trace.h> |
40 | #include <ltt/facility.h> |
41 | #include <stdio.h> |
42 | |
43 | /* Insert the hooks before and after each trace and tracefile, and for each |
44 | event. Print a global header. */ |
45 | |
91ad3f0a |
46 | /* |
47 | * YET TO BE ANSWERED ! |
48 | * - why does this module need dependency with batchAnalysis ? |
49 | */ |
50 | |
1601b365 |
51 | /* |
52 | * TODO |
53 | * - specify wich hook function will be used to call the core filter |
54 | */ |
55 | |
91ad3f0a |
56 | static char |
a58509ee |
57 | *a_file_name = NULL, |
91ad3f0a |
58 | *a_string = NULL; |
59 | |
a58509ee |
60 | static LttvHooks |
61 | *before_traceset, |
62 | *event_hook; |
63 | |
a58509ee |
64 | /** |
91ad3f0a |
65 | * filters the file input from user |
bd523c6f |
66 | * @param hook_data the hook data, unused |
a58509ee |
67 | */ |
91ad3f0a |
68 | void filter_analyze_file(void *hook_data) { |
69 | |
73050a5f |
70 | LttvAttributeValue value; |
71 | |
72 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
73 | |
91ad3f0a |
74 | /* |
a58509ee |
75 | * User may specify filtering options through static file |
76 | * and/or command line string. From these sources, an |
77 | * option string is rebuilded and sent to the filter core |
78 | */ |
c684db06 |
79 | if(!g_file_test(a_file_name,G_FILE_TEST_EXISTS)) { |
80 | g_warning("file %s does not exist", a_file_name); |
81 | return; |
82 | } |
83 | |
84 | char* a_file_content = NULL; |
85 | |
86 | g_file_get_contents(a_file_name,&a_file_content,NULL,NULL); |
91ad3f0a |
87 | |
73050a5f |
88 | g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression", |
89 | LTTV_POINTER, &value)); |
90 | |
73050a5f |
91 | if(((GString*)*(value.v_pointer))->len != 0) g_string_append_c((GString*)*(value.v_pointer),'&'); |
c684db06 |
92 | g_string_append_c((GString*)*(value.v_pointer),'('); |
93 | g_string_append((GString*)*(value.v_pointer),a_file_content); |
94 | g_string_append_c((GString*)*(value.v_pointer),')'); |
56e29124 |
95 | |
91ad3f0a |
96 | } |
97 | |
98 | /** |
99 | * filters the string input from user |
bd523c6f |
100 | * @param hook_data the hook data, unused |
91ad3f0a |
101 | */ |
102 | void filter_analyze_string(void *hook_data) { |
103 | |
73050a5f |
104 | LttvAttributeValue value; |
105 | |
106 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
107 | |
91ad3f0a |
108 | /* |
109 | * User may specify filtering options through static file |
110 | * and/or command line string. From these sources, an |
111 | * option string is rebuilded and sent to the filter core |
112 | */ |
73050a5f |
113 | g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression", |
114 | LTTV_POINTER, &value)); |
cec3d7b0 |
115 | |
73050a5f |
116 | if(((GString*)*(value.v_pointer))->len != 0) g_string_append_c((GString*)*(value.v_pointer),'&'); |
c684db06 |
117 | g_string_append_c((GString*)*(value.v_pointer),'('); |
73050a5f |
118 | g_string_append((GString*)*(value.v_pointer),a_string); |
c684db06 |
119 | g_string_append_c((GString*)*(value.v_pointer),')'); |
56e29124 |
120 | |
a58509ee |
121 | } |
122 | |
bd523c6f |
123 | /** |
124 | * Output all filter commands on console |
125 | * @param hook_data the hook data |
126 | */ |
127 | void filter_list_commands(void *hook_data) { |
128 | |
129 | g_print("[field] [op] [value]\n\n"); |
130 | |
131 | g_print("*** Possible fields ***\n"); |
132 | g_print("event.name (string)\n"); |
133 | g_print("event.category (string)\n"); |
134 | g_print("event.time (double)\n"); |
135 | g_print("event.tsc (integer)\n"); |
136 | g_print("tracefile.name (string)\n"); |
137 | g_print("trace.name (string)\n"); |
138 | g_print("state.pid (integer)\n"); |
139 | g_print("state.ppid (integer)\n"); |
140 | g_print("state.creation_time (double)\n"); |
141 | g_print("trace.insertion_time (double)\n"); |
142 | g_print("trace.process_name (string)\n"); |
143 | g_print("trace.execution_mode (string)\n"); |
144 | g_print("trace.execution_submode (string)\n"); |
145 | g_print("trace.process_status (string)\n"); |
146 | g_print("trace.cpu (string)\n\n"); |
147 | |
148 | g_print("*** Possible operators ***\n"); |
149 | g_print("equal '='\n"); |
150 | g_print("not equal '!='\n"); |
151 | g_print("greater '>'\n"); |
152 | g_print("greater or equal '>='\n"); |
153 | g_print("lower '<'\n"); |
154 | g_print("lower or equal '<='\n"); |
155 | |
156 | g_print("*** Possible values ***\n"); |
157 | g_print("string, integer, double"); |
158 | } |
159 | |
a58509ee |
160 | /** |
161 | * initialize the new module |
162 | */ |
163 | static void init() { |
164 | |
a58509ee |
165 | LttvAttributeValue value; |
166 | |
167 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
168 | |
73050a5f |
169 | g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression", |
170 | LTTV_POINTER, &value)); |
171 | |
172 | *(value.v_pointer) = g_string_new(""); |
173 | |
a58509ee |
174 | g_info("Init textFilter.c"); |
cec3d7b0 |
175 | |
91ad3f0a |
176 | a_string = NULL; |
73050a5f |
177 | lttv_option_add("expression", 'e', |
a58509ee |
178 | "filters a string issued by the user on the command line", |
179 | "string", |
91ad3f0a |
180 | LTTV_OPT_STRING, &a_string, filter_analyze_string, NULL); |
1a7fa682 |
181 | // add function to call for option |
182 | |
a58509ee |
183 | a_file_name = NULL; |
184 | lttv_option_add("filename", 'f', |
185 | "browse the filter options contained in specified file", |
186 | "file name", |
91ad3f0a |
187 | LTTV_OPT_STRING, &a_file_name, filter_analyze_file, NULL); |
a58509ee |
188 | |
bd523c6f |
189 | lttv_option_add("list", 'l', |
190 | "list all possible filter commands for module", |
191 | "list commands", |
192 | LTTV_OPT_NONE, NULL, filter_list_commands, NULL); |
193 | |
a58509ee |
194 | } |
195 | |
196 | /** |
197 | * Destroy the current module |
198 | */ |
199 | static void destroy() { |
200 | g_info("Destroy textFilter"); |
201 | |
73050a5f |
202 | lttv_option_remove("expression"); |
a58509ee |
203 | |
204 | lttv_option_remove("filename"); |
205 | |
bd523c6f |
206 | lttv_option_remove("list"); |
207 | |
73050a5f |
208 | LttvAttributeValue value; |
209 | |
210 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
a58509ee |
211 | |
73050a5f |
212 | g_assert(lttv_iattribute_find_by_path(attributes, "filter/expression", |
213 | LTTV_POINTER, &value)); |
214 | |
215 | g_string_free((GString*)*(value.v_pointer),TRUE); |
a58509ee |
216 | |
217 | } |
218 | |
219 | |
220 | LTTV_MODULE("textFilter", "Filters traces", \ |
221 | "Filter the trace following commands issued by user input", \ |
73050a5f |
222 | init, destroy, "option") |
a58509ee |
223 | |