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 | |
1da1525d |
56 | GString |
a58509ee |
57 | *a_filter_string = NULL; |
58 | |
59 | static LttvHooks |
60 | *before_traceset, |
61 | *event_hook; |
62 | |
63 | static FILE *a_file; |
64 | |
65 | /** |
91ad3f0a |
66 | * filters the file input from user |
a58509ee |
67 | * @param hook_data the hook data |
91ad3f0a |
68 | * @return success/failure of operation |
a58509ee |
69 | */ |
91ad3f0a |
70 | void filter_analyze_file(void *hook_data) { |
71 | |
72 | g_print("textFilter::filter_analyze_file\n"); |
73 | |
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 | */ |
91ad3f0a |
79 | a_file = fopen(a_file_name, "r"); |
80 | if(a_file == NULL) { |
81 | g_warning("file %s does not exist", a_file_name); |
82 | return; |
83 | } |
84 | |
1601b365 |
85 | char* line = NULL; |
86 | size_t len = 0; |
91ad3f0a |
87 | |
1601b365 |
88 | if(a_filter_string == NULL) { |
89 | a_filter_string = g_string_new(""); |
91ad3f0a |
90 | } |
91 | else { |
92 | g_string_append(a_filter_string,"&"); /*conjonction between expression*/ |
1601b365 |
93 | } |
cec3d7b0 |
94 | |
1601b365 |
95 | while(!feof(a_file)) { |
96 | getline(&line,&len,a_file); |
97 | g_string_append(a_filter_string,line); |
98 | line = NULL; |
91ad3f0a |
99 | } |
cec3d7b0 |
100 | |
101 | // lttv_filter_append_expression(lttvfilter_t,a_filter_string->str); |
91ad3f0a |
102 | |
103 | fclose(a_file); |
104 | } |
105 | |
106 | /** |
107 | * filters the string input from user |
108 | * @param hook_data the hook data |
109 | * @return success/failure of operation |
110 | */ |
111 | void filter_analyze_string(void *hook_data) { |
112 | |
113 | g_print("textFilter::filter_analyze_string\n"); |
18d1226f |
114 | |
91ad3f0a |
115 | /* |
116 | * User may specify filtering options through static file |
117 | * and/or command line string. From these sources, an |
118 | * option string is rebuilded and sent to the filter core |
119 | */ |
1601b365 |
120 | if(a_filter_string==NULL) { |
121 | a_filter_string = g_string_new(""); |
91ad3f0a |
122 | g_string_append(a_filter_string,a_string); |
1601b365 |
123 | } |
124 | else { |
cec3d7b0 |
125 | g_string_append(a_filter_string,"&"); |
1601b365 |
126 | g_string_append(a_filter_string,a_string); |
127 | } |
91ad3f0a |
128 | |
cec3d7b0 |
129 | // lttv_filter_append_expression(lttvfilter_t,a_string); |
130 | |
a58509ee |
131 | } |
132 | |
133 | /** |
134 | * filter to current event depending on the |
135 | * filter options tree |
136 | * @param hook_data the hook data |
137 | * @param call_data the call data |
138 | * @return success/error of operation |
139 | */ |
140 | static gboolean filter_event_content(void *hook_data, void *call_data) { |
141 | |
142 | g_print("textFilter::filter_event_content\n"); /* debug */ |
143 | } |
144 | |
145 | /** |
146 | * initialize the new module |
147 | */ |
148 | static void init() { |
149 | |
91ad3f0a |
150 | g_print("textFilter::init()\n"); /* debug */ |
151 | |
a58509ee |
152 | LttvAttributeValue value; |
153 | |
154 | LttvIAttribute *attributes = LTTV_IATTRIBUTE(lttv_global_attributes()); |
155 | |
156 | g_info("Init textFilter.c"); |
cec3d7b0 |
157 | |
91ad3f0a |
158 | a_string = NULL; |
a58509ee |
159 | lttv_option_add("string", 's', |
160 | "filters a string issued by the user on the command line", |
161 | "string", |
91ad3f0a |
162 | LTTV_OPT_STRING, &a_string, filter_analyze_string, NULL); |
1a7fa682 |
163 | // add function to call for option |
164 | |
a58509ee |
165 | a_file_name = NULL; |
166 | lttv_option_add("filename", 'f', |
167 | "browse the filter options contained in specified file", |
168 | "file name", |
91ad3f0a |
169 | LTTV_OPT_STRING, &a_file_name, filter_analyze_file, NULL); |
a58509ee |
170 | |
171 | /* |
172 | * Note to myself ! |
173 | * LttvAttributeValue::v_pointer is a gpointer* --> void** |
174 | * see union LttvAttributeValue for more info |
175 | */ |
176 | g_assert(lttv_iattribute_find_by_path(attributes, "hooks/event", |
177 | LTTV_POINTER, &value)); |
178 | g_assert((event_hook = *(value.v_pointer)) != NULL); |
179 | lttv_hooks_add(event_hook, filter_event_content, NULL, LTTV_PRIO_DEFAULT); |
180 | |
91ad3f0a |
181 | // g_assert(lttv_iattribute_find_by_path(attributes,"hooks/trace/before", |
182 | // LTTV_POINTER, &value)); |
183 | // g_assert((before_traceset = *(value.v_pointer)) != NULL); |
184 | // lttv_hooks_add(before_traceset, parse_filter_options, NULL, LTTV_PRIO_DEFAULT); |
a58509ee |
185 | |
186 | } |
187 | |
188 | /** |
189 | * Destroy the current module |
190 | */ |
191 | static void destroy() { |
192 | g_info("Destroy textFilter"); |
193 | |
194 | lttv_option_remove("string"); |
195 | |
196 | lttv_option_remove("filename"); |
197 | |
198 | lttv_hooks_remove_data(event_hook, filter_event_content, NULL); |
199 | |
91ad3f0a |
200 | // lttv_hooks_remove_data(before_traceset, parse_filter_options, NULL); |
a58509ee |
201 | |
202 | } |
203 | |
204 | |
205 | LTTV_MODULE("textFilter", "Filters traces", \ |
206 | "Filter the trace following commands issued by user input", \ |
91ad3f0a |
207 | init, destroy, "batchAnalysis", "option") |
a58509ee |
208 | |