param_filter: update to stree
[smatch.git] / check_debug.c
blob442f890e22ea6fd5f8dddf2dd2a472f71ae89cb9
1 /*
2 * Copyright (C) 2009 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
18 #include "smatch.h"
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
22 int local_debug;
23 static int my_id;
25 static void match_all_values(const char *fn, struct expression *expr, void *info)
27 struct AVL *stree;
29 stree = get_all_states_stree(SMATCH_EXTRA);
30 __print_stree(stree);
31 free_stree(&stree);
34 static void match_cur_slist(const char *fn, struct expression *expr, void *info)
36 __print_cur_slist();
39 static void match_state(const char *fn, struct expression *expr, void *info)
41 struct expression *check_arg, *state_arg;
42 struct sm_state *sm;
43 int found = 0;
45 check_arg = get_argument_from_call_expr(expr->args, 0);
46 if (check_arg->type != EXPR_STRING) {
47 sm_msg("error: the check_name argument to %s is supposed to be a string literal", fn);
48 return;
50 state_arg = get_argument_from_call_expr(expr->args, 1);
51 if (!state_arg || state_arg->type != EXPR_STRING) {
52 sm_msg("error: the state_name argument to %s is supposed to be a string literal", fn);
53 return;
56 FOR_EACH_PTR(__get_cur_slist(), sm) {
57 if (strcmp(check_name(sm->owner), check_arg->string->data) != 0)
58 continue;
59 if (strcmp(sm->name, state_arg->string->data) != 0)
60 continue;
61 sm_msg("'%s' = '%s'", sm->name, sm->state->name);
62 found = 1;
63 } END_FOR_EACH_PTR(sm);
65 if (!found)
66 sm_msg("%s '%s' not found", check_arg->string->data, state_arg->string->data);
69 static void match_states(const char *fn, struct expression *expr, void *info)
71 struct expression *check_arg;
72 struct sm_state *sm;
73 int found = 0;
75 check_arg = get_argument_from_call_expr(expr->args, 0);
76 if (check_arg->type != EXPR_STRING) {
77 sm_msg("error: the check_name argument to %s is supposed to be a string literal", fn);
78 return;
81 FOR_EACH_PTR(__get_cur_slist(), sm) {
82 if (strcmp(check_name(sm->owner), check_arg->string->data) != 0)
83 continue;
84 sm_msg("'%s' = '%s'", sm->name, sm->state->name);
85 found = 1;
86 } END_FOR_EACH_PTR(sm);
88 if (!found)
89 sm_msg("%s: no states", check_arg->string->data);
92 static void match_print_value(const char *fn, struct expression *expr, void *info)
94 struct AVL *stree;
95 struct sm_state *tmp;
96 struct expression *arg_expr;
98 arg_expr = get_argument_from_call_expr(expr->args, 0);
99 if (arg_expr->type != EXPR_STRING) {
100 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
101 return;
104 stree = get_all_states_stree(SMATCH_EXTRA);
105 FOR_EACH_SM(stree, tmp) {
106 if (!strcmp(tmp->name, arg_expr->string->data))
107 sm_msg("%s = %s", tmp->name, tmp->state->name);
108 } END_FOR_EACH_SM(tmp);
109 free_stree(&stree);
112 static void match_print_implied(const char *fn, struct expression *expr, void *info)
114 struct expression *arg;
115 struct range_list *rl = NULL;
116 char *name;
118 arg = get_argument_from_call_expr(expr->args, 0);
119 get_implied_rl(arg, &rl);
121 name = expr_to_str(arg);
122 sm_msg("implied: %s = '%s'", name, show_rl(rl));
123 free_string(name);
126 static void match_print_implied_min(const char *fn, struct expression *expr, void *info)
128 struct expression *arg;
129 sval_t sval;
130 char *name;
132 arg = get_argument_from_call_expr(expr->args, 0);
133 name = expr_to_str(arg);
135 if (get_implied_min(arg, &sval))
136 sm_msg("implied min: %s = %s", name, sval_to_str(sval));
137 else
138 sm_msg("implied min: %s = <unknown>", name);
140 free_string(name);
143 static void match_print_implied_max(const char *fn, struct expression *expr, void *info)
145 struct expression *arg;
146 sval_t sval;
147 char *name;
149 arg = get_argument_from_call_expr(expr->args, 0);
150 name = expr_to_str(arg);
152 if (get_implied_max(arg, &sval))
153 sm_msg("implied max: %s = %s", name, sval_to_str(sval));
154 else
155 sm_msg("implied max: %s = <unknown>", name);
157 free_string(name);
160 static void match_print_hard_max(const char *fn, struct expression *expr, void *info)
162 struct expression *arg;
163 sval_t sval;
164 char *name;
166 arg = get_argument_from_call_expr(expr->args, 0);
167 name = expr_to_str(arg);
169 if (get_hard_max(arg, &sval))
170 sm_msg("hard max: %s = %s", name, sval_to_str(sval));
171 else
172 sm_msg("hard max: %s = <unknown>", name);
174 free_string(name);
177 static void match_print_fuzzy_max(const char *fn, struct expression *expr, void *info)
179 struct expression *arg;
180 sval_t sval;
181 char *name;
183 arg = get_argument_from_call_expr(expr->args, 0);
184 name = expr_to_str(arg);
186 if (get_fuzzy_max(arg, &sval))
187 sm_msg("fuzzy max: %s = %s", name, sval_to_str(sval));
188 else
189 sm_msg("fuzzy max: %s = <unknown>", name);
191 free_string(name);
194 static void match_print_absolute_min(const char *fn, struct expression *expr, void *info)
196 struct expression *arg;
197 sval_t sval;
198 char *name;
200 arg = get_argument_from_call_expr(expr->args, 0);
201 name = expr_to_str(arg);
203 if (get_absolute_min(arg, &sval))
204 sm_msg("absolute min: %s = %s", name, sval_to_str(sval));
205 else
206 sm_msg("absolute min: %s = <unknown>", name);
208 free_string(name);
211 static void match_print_absolute_max(const char *fn, struct expression *expr, void *info)
213 struct expression *arg;
214 sval_t sval;
215 char *name;
217 arg = get_argument_from_call_expr(expr->args, 0);
218 get_absolute_max(arg, &sval);
220 name = expr_to_str(arg);
221 sm_msg("absolute max: %s = %s", name, sval_to_str(sval));
222 free_string(name);
225 static void match_sval_info(const char *fn, struct expression *expr, void *info)
227 struct expression *arg;
228 sval_t sval;
229 char *name;
231 arg = get_argument_from_call_expr(expr->args, 0);
232 name = expr_to_str(arg);
234 if (!get_implied_value(arg, &sval)) {
235 sm_msg("no sval for '%s'", name);
236 goto free;
239 sm_msg("implied: %s %c%d ->value = %llx", name, sval_unsigned(sval) ? 'u' : 's', sval_bits(sval), sval.value);
240 free:
241 free_string(name);
244 static void match_member_name(const char *fn, struct expression *expr, void *info)
246 struct expression *arg;
247 char *name, *member_name;
249 arg = get_argument_from_call_expr(expr->args, 0);
250 name = expr_to_str(arg);
251 member_name = get_member_name(arg);
252 sm_msg("member name: '%s => %s'", name, member_name);
253 free_string(member_name);
254 free_string(name);
257 static void print_possible(struct sm_state *sm)
259 struct sm_state *tmp;
261 sm_msg("Possible values for %s", sm->name);
262 FOR_EACH_PTR(sm->possible, tmp) {
263 printf("%s\n", tmp->state->name);
264 } END_FOR_EACH_PTR(tmp);
265 sm_msg("===");
268 static void match_possible(const char *fn, struct expression *expr, void *info)
270 struct AVL *stree;
271 struct sm_state *tmp;
272 struct expression *arg_expr;
274 arg_expr = get_argument_from_call_expr(expr->args, 0);
275 if (arg_expr->type != EXPR_STRING) {
276 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
277 return;
280 stree = get_all_states_stree(SMATCH_EXTRA);
281 FOR_EACH_SM(stree, tmp) {
282 if (!strcmp(tmp->name, arg_expr->string->data))
283 print_possible(tmp);
284 } END_FOR_EACH_SM(tmp);
285 free_stree(&stree);
288 static void match_strlen(const char *fn, struct expression *expr, void *info)
290 struct expression *arg;
291 struct range_list *rl;
292 char *name;
294 arg = get_argument_from_call_expr(expr->args, 0);
295 get_implied_strlen(arg, &rl);
297 name = expr_to_str(arg);
298 sm_msg("strlen: '%s' %s characters", name, show_rl(rl));
299 free_string(name);
302 static void match_buf_size(const char *fn, struct expression *expr, void *info)
304 struct expression *arg;
305 int elements, bytes;
306 char *name;
308 arg = get_argument_from_call_expr(expr->args, 0);
309 elements = get_array_size(arg);
310 bytes = get_array_size_bytes(arg);
312 name = expr_to_str(arg);
313 sm_msg("buf size: '%s' %d elements, %d bytes", name, elements, bytes);
314 free_string(name);
317 static void match_buf_size_rl(const char *fn, struct expression *expr, void *info)
319 struct expression *arg;
320 struct range_list *rl;
321 int elements, bytes;
322 char *name;
324 arg = get_argument_from_call_expr(expr->args, 0);
325 rl = get_array_size_bytes_rl(arg);
326 elements = get_array_size(arg);
327 bytes = get_array_size_bytes(arg);
329 name = expr_to_str(arg);
330 sm_msg("buf size: '%s' %s %d elements, %d bytes", name, show_rl(rl), elements, bytes);
331 free_string(name);
334 static void match_note(const char *fn, struct expression *expr, void *info)
336 struct expression *arg_expr;
338 arg_expr = get_argument_from_call_expr(expr->args, 0);
339 if (arg_expr->type != EXPR_STRING) {
340 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
341 return;
343 sm_msg("%s", arg_expr->string->data);
346 static void print_related(struct sm_state *sm)
348 struct relation *rel;
350 if (!estate_related(sm->state))
351 return;
353 sm_prefix();
354 sm_printf("%s: ", sm->name);
355 FOR_EACH_PTR(estate_related(sm->state), rel) {
356 sm_printf("%s ", rel->name);
357 } END_FOR_EACH_PTR(rel);
358 sm_printf("\n");
361 static void match_dump_related(const char *fn, struct expression *expr, void *info)
363 struct AVL *stree;
364 struct sm_state *tmp;
366 stree = get_all_states_stree(SMATCH_EXTRA);
367 FOR_EACH_SM(stree, tmp) {
368 print_related(tmp);
369 } END_FOR_EACH_SM(tmp);
370 free_stree(&stree);
373 static void match_compare(const char *fn, struct expression *expr, void *info)
375 struct expression *one, *two;
376 char *one_name, *two_name;
377 int comparison;
378 char buf[16];
380 one = get_argument_from_call_expr(expr->args, 0);
381 two = get_argument_from_call_expr(expr->args, 1);
383 comparison = get_comparison(one, two);
384 if (!comparison)
385 snprintf(buf, sizeof(buf), "<none>");
386 else
387 snprintf(buf, sizeof(buf), "%s", show_special(comparison));
389 one_name = expr_to_str(one);
390 two_name = expr_to_str(two);
392 sm_msg("%s %s %s", one_name, buf, two_name);
394 free_string(one_name);
395 free_string(two_name);
398 static void match_debug_on(const char *fn, struct expression *expr, void *info)
400 option_debug = 1;
403 static void match_debug_off(const char *fn, struct expression *expr, void *info)
405 option_debug = 0;
408 static void match_local_debug_on(const char *fn, struct expression *expr, void *info)
410 local_debug = 1;
413 static void match_local_debug_off(const char *fn, struct expression *expr, void *info)
415 local_debug = 0;
418 static void match_debug_implied_on(const char *fn, struct expression *expr, void *info)
420 option_debug_implied = 1;
423 static void match_debug_implied_off(const char *fn, struct expression *expr, void *info)
425 option_debug_implied = 0;
428 static void match_about(const char *fn, struct expression *expr, void *info)
430 struct expression *arg;
431 struct sm_state *sm;
432 char *name;
434 sm_msg("---- about ----");
435 match_print_implied(fn, expr, NULL);
436 match_buf_size(fn, expr, NULL);
437 match_strlen(fn, expr, NULL);
439 arg = get_argument_from_call_expr(expr->args, 0);
440 name = expr_to_str(arg);
441 if (!name) {
442 sm_msg("info: not a straight forward variable.");
443 return;
446 FOR_EACH_PTR(__get_cur_slist(), sm) {
447 if (strcmp(sm->name, name) != 0)
448 continue;
449 sm_msg("%s '%s' = '%s'", check_name(sm->owner), sm->name, sm->state->name);
450 } END_FOR_EACH_PTR(sm);
453 void check_debug(int id)
455 my_id = id;
456 add_function_hook("__smatch_about", &match_about, NULL);
457 add_function_hook("__smatch_all_values", &match_all_values, NULL);
458 add_function_hook("__smatch_state", &match_state, NULL);
459 add_function_hook("__smatch_states", &match_states, NULL);
460 add_function_hook("__smatch_value", &match_print_value, NULL);
461 add_function_hook("__smatch_implied", &match_print_implied, NULL);
462 add_function_hook("__smatch_implied_min", &match_print_implied_min, NULL);
463 add_function_hook("__smatch_implied_max", &match_print_implied_max, NULL);
464 add_function_hook("__smatch_hard_max", &match_print_hard_max, NULL);
465 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max, NULL);
466 add_function_hook("__smatch_absolute_min", &match_print_absolute_min, NULL);
467 add_function_hook("__smatch_absolute_max", &match_print_absolute_max, NULL);
468 add_function_hook("__smatch_sval_info", &match_sval_info, NULL);
469 add_function_hook("__smatch_member_name", &match_member_name, NULL);
470 add_function_hook("__smatch_possible", &match_possible, NULL);
471 add_function_hook("__smatch_cur_slist", &match_cur_slist, NULL);
472 add_function_hook("__smatch_strlen", &match_strlen, NULL);
473 add_function_hook("__smatch_buf_size", &match_buf_size, NULL);
474 add_function_hook("__smatch_buf_size_rl", &match_buf_size_rl, NULL);
475 add_function_hook("__smatch_note", &match_note, NULL);
476 add_function_hook("__smatch_dump_related", &match_dump_related, NULL);
477 add_function_hook("__smatch_compare", &match_compare, NULL);
478 add_function_hook("__smatch_debug_on", &match_debug_on, NULL);
479 add_function_hook("__smatch_debug_off", &match_debug_off, NULL);
480 add_function_hook("__smatch_local_debug_on", &match_local_debug_on, NULL);
481 add_function_hook("__smatch_local_debug_off", &match_local_debug_off, NULL);
482 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on, NULL);
483 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off, NULL);