db: export get_static_filter()
[smatch.git] / check_debug.c
blob98d464e71bd20065192d997df684eb6f0f745040
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;
24 char *trace_variable;
26 static void match_all_values(const char *fn, struct expression *expr, void *info)
28 struct stree *stree;
30 stree = get_all_states_stree(SMATCH_EXTRA);
31 __print_stree(stree);
32 free_stree(&stree);
35 static void match_cur_stree(const char *fn, struct expression *expr, void *info)
37 __print_cur_stree();
40 static void match_state(const char *fn, struct expression *expr, void *info)
42 struct expression *check_arg, *state_arg;
43 struct sm_state *sm;
44 int found = 0;
46 check_arg = get_argument_from_call_expr(expr->args, 0);
47 if (check_arg->type != EXPR_STRING) {
48 sm_msg("error: the check_name argument to %s is supposed to be a string literal", fn);
49 return;
51 state_arg = get_argument_from_call_expr(expr->args, 1);
52 if (!state_arg || state_arg->type != EXPR_STRING) {
53 sm_msg("error: the state_name argument to %s is supposed to be a string literal", fn);
54 return;
57 FOR_EACH_SM(__get_cur_stree(), sm) {
58 if (strcmp(check_name(sm->owner), check_arg->string->data) != 0)
59 continue;
60 if (strcmp(sm->name, state_arg->string->data) != 0)
61 continue;
62 sm_msg("'%s' = '%s'", sm->name, sm->state->name);
63 found = 1;
64 } END_FOR_EACH_SM(sm);
66 if (!found)
67 sm_msg("%s '%s' not found", check_arg->string->data, state_arg->string->data);
70 static void match_states(const char *fn, struct expression *expr, void *info)
72 struct expression *check_arg;
73 struct sm_state *sm;
74 int found = 0;
76 check_arg = get_argument_from_call_expr(expr->args, 0);
77 if (check_arg->type != EXPR_STRING) {
78 sm_msg("error: the check_name argument to %s is supposed to be a string literal", fn);
79 return;
82 FOR_EACH_SM(__get_cur_stree(), sm) {
83 if (strcmp(check_name(sm->owner), check_arg->string->data) != 0)
84 continue;
85 sm_msg("'%s' = '%s'", sm->name, sm->state->name);
86 found = 1;
87 } END_FOR_EACH_SM(sm);
89 if (!found)
90 sm_msg("%s: no states", check_arg->string->data);
93 static void match_print_value(const char *fn, struct expression *expr, void *info)
95 struct stree *stree;
96 struct sm_state *tmp;
97 struct expression *arg_expr;
99 arg_expr = get_argument_from_call_expr(expr->args, 0);
100 if (arg_expr->type != EXPR_STRING) {
101 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
102 return;
105 stree = __get_cur_stree();
106 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
107 if (!strcmp(tmp->name, arg_expr->string->data))
108 sm_msg("%s = %s", tmp->name, tmp->state->name);
109 } END_FOR_EACH_SM(tmp);
112 static void match_print_known(const char *fn, struct expression *expr, void *info)
114 struct expression *arg;
115 struct range_list *rl = NULL;
116 char *name;
117 int known = 0;
118 sval_t sval;
120 arg = get_argument_from_call_expr(expr->args, 0);
121 if (get_value(arg, &sval))
122 known = 1;
124 get_implied_rl(arg, &rl);
126 name = expr_to_str(arg);
127 sm_msg("known: '%s' = '%s'. implied = '%s'", name, known ? sval_to_str(sval) : "<unknown>", show_rl(rl));
128 free_string(name);
131 static void match_print_implied(const char *fn, struct expression *expr, void *info)
133 struct expression *arg;
134 struct range_list *rl = NULL;
135 char *name;
137 arg = get_argument_from_call_expr(expr->args, 0);
138 get_implied_rl(arg, &rl);
140 name = expr_to_str(arg);
141 sm_msg("implied: %s = '%s'", name, show_rl(rl));
142 free_string(name);
145 static void match_print_implied_min(const char *fn, struct expression *expr, void *info)
147 struct expression *arg;
148 sval_t sval;
149 char *name;
151 arg = get_argument_from_call_expr(expr->args, 0);
152 name = expr_to_str(arg);
154 if (get_implied_min(arg, &sval))
155 sm_msg("implied min: %s = %s", name, sval_to_str(sval));
156 else
157 sm_msg("implied min: %s = <unknown>", name);
159 free_string(name);
162 static void match_print_implied_max(const char *fn, struct expression *expr, void *info)
164 struct expression *arg;
165 sval_t sval;
166 char *name;
168 arg = get_argument_from_call_expr(expr->args, 0);
169 name = expr_to_str(arg);
171 if (get_implied_max(arg, &sval))
172 sm_msg("implied max: %s = %s", name, sval_to_str(sval));
173 else
174 sm_msg("implied max: %s = <unknown>", name);
176 free_string(name);
179 static void match_user_rl(const char *fn, struct expression *expr, void *info)
181 struct expression *arg;
182 struct range_list *rl;
183 char *name;
185 arg = get_argument_from_call_expr(expr->args, 0);
186 name = expr_to_str(arg);
188 get_user_rl(arg, &rl);
189 sm_msg("user rl: '%s' = '%s'", name, show_rl(rl));
191 free_string(name);
194 static void match_print_hard_max(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_hard_max(arg, &sval))
204 sm_msg("hard max: %s = %s", name, sval_to_str(sval));
205 else
206 sm_msg("hard max: %s = <unknown>", name);
208 free_string(name);
211 static void match_print_fuzzy_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 name = expr_to_str(arg);
220 if (get_fuzzy_max(arg, &sval))
221 sm_msg("fuzzy max: %s = %s", name, sval_to_str(sval));
222 else
223 sm_msg("fuzzy max: %s = <unknown>", name);
225 free_string(name);
228 static void match_print_absolute_min(const char *fn, struct expression *expr, void *info)
230 struct expression *arg;
231 sval_t sval;
232 char *name;
234 arg = get_argument_from_call_expr(expr->args, 0);
235 name = expr_to_str(arg);
237 if (get_absolute_min(arg, &sval))
238 sm_msg("absolute min: %s = %s", name, sval_to_str(sval));
239 else
240 sm_msg("absolute min: %s = <unknown>", name);
242 free_string(name);
245 static void match_print_absolute_max(const char *fn, struct expression *expr, void *info)
247 struct expression *arg;
248 sval_t sval;
249 char *name;
251 arg = get_argument_from_call_expr(expr->args, 0);
252 get_absolute_max(arg, &sval);
254 name = expr_to_str(arg);
255 sm_msg("absolute max: %s = %s", name, sval_to_str(sval));
256 free_string(name);
259 static void match_sval_info(const char *fn, struct expression *expr, void *info)
261 struct expression *arg;
262 sval_t sval;
263 char *name;
265 arg = get_argument_from_call_expr(expr->args, 0);
266 name = expr_to_str(arg);
268 if (!get_implied_value(arg, &sval)) {
269 sm_msg("no sval for '%s'", name);
270 goto free;
273 sm_msg("implied: %s %c%d ->value = %llx", name, sval_unsigned(sval) ? 'u' : 's', sval_bits(sval), sval.value);
274 free:
275 free_string(name);
278 static void match_member_name(const char *fn, struct expression *expr, void *info)
280 struct expression *arg;
281 char *name, *member_name;
283 arg = get_argument_from_call_expr(expr->args, 0);
284 name = expr_to_str(arg);
285 member_name = get_member_name(arg);
286 sm_msg("member name: '%s => %s'", name, member_name);
287 free_string(member_name);
288 free_string(name);
291 static void print_possible(struct sm_state *sm)
293 struct sm_state *tmp;
295 sm_msg("Possible values for %s", sm->name);
296 FOR_EACH_PTR(sm->possible, tmp) {
297 printf("%s\n", tmp->state->name);
298 } END_FOR_EACH_PTR(tmp);
299 sm_msg("===");
302 static void match_possible(const char *fn, struct expression *expr, void *info)
304 struct stree *stree;
305 struct sm_state *tmp;
306 struct expression *arg_expr;
308 arg_expr = get_argument_from_call_expr(expr->args, 0);
309 if (arg_expr->type != EXPR_STRING) {
310 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
311 return;
314 stree = __get_cur_stree();
315 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
316 if (!strcmp(tmp->name, arg_expr->string->data))
317 print_possible(tmp);
318 } END_FOR_EACH_SM(tmp);
321 static void match_strlen(const char *fn, struct expression *expr, void *info)
323 struct expression *arg;
324 struct range_list *rl;
325 char *name;
327 arg = get_argument_from_call_expr(expr->args, 0);
328 get_implied_strlen(arg, &rl);
330 name = expr_to_str(arg);
331 sm_msg("strlen: '%s' %s characters", name, show_rl(rl));
332 free_string(name);
335 static void match_buf_size(const char *fn, struct expression *expr, void *info)
337 struct expression *arg;
338 int elements, bytes;
339 char *name;
341 arg = get_argument_from_call_expr(expr->args, 0);
342 elements = get_array_size(arg);
343 bytes = get_array_size_bytes(arg);
345 name = expr_to_str(arg);
346 sm_msg("buf size: '%s' %d elements, %d bytes", name, elements, bytes);
347 free_string(name);
350 static void match_buf_size_rl(const char *fn, struct expression *expr, void *info)
352 struct expression *arg;
353 struct range_list *rl;
354 int elements, bytes;
355 char *name;
357 arg = get_argument_from_call_expr(expr->args, 0);
358 rl = get_array_size_bytes_rl(arg);
359 elements = get_array_size(arg);
360 bytes = get_array_size_bytes(arg);
362 name = expr_to_str(arg);
363 sm_msg("buf size: '%s' %s %d elements, %d bytes", name, show_rl(rl), elements, bytes);
364 free_string(name);
367 static void match_note(const char *fn, struct expression *expr, void *info)
369 struct expression *arg_expr;
371 arg_expr = get_argument_from_call_expr(expr->args, 0);
372 if (arg_expr->type != EXPR_STRING) {
373 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
374 return;
376 sm_msg("%s", arg_expr->string->data);
379 static void print_related(struct sm_state *sm)
381 struct relation *rel;
383 if (!estate_related(sm->state))
384 return;
386 sm_prefix();
387 sm_printf("%s: ", sm->name);
388 FOR_EACH_PTR(estate_related(sm->state), rel) {
389 sm_printf("%s ", rel->name);
390 } END_FOR_EACH_PTR(rel);
391 sm_printf("\n");
394 static void match_dump_related(const char *fn, struct expression *expr, void *info)
396 struct stree *stree;
397 struct sm_state *tmp;
399 stree = __get_cur_stree();
400 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
401 print_related(tmp);
402 } END_FOR_EACH_SM(tmp);
405 static void match_compare(const char *fn, struct expression *expr, void *info)
407 struct expression *one, *two;
408 char *one_name, *two_name;
409 int comparison;
410 char buf[16];
412 one = get_argument_from_call_expr(expr->args, 0);
413 two = get_argument_from_call_expr(expr->args, 1);
415 comparison = get_comparison(one, two);
416 if (!comparison)
417 snprintf(buf, sizeof(buf), "<none>");
418 else
419 snprintf(buf, sizeof(buf), "%s", show_special(comparison));
421 one_name = expr_to_str(one);
422 two_name = expr_to_str(two);
424 sm_msg("%s %s %s", one_name, buf, two_name);
426 free_string(one_name);
427 free_string(two_name);
430 static void match_debug_on(const char *fn, struct expression *expr, void *info)
432 option_debug = 1;
435 static void match_debug_check(const char *fn, struct expression *expr, void *info)
437 struct expression *arg;
439 arg = get_argument_from_call_expr(expr->args, 0);
440 if (!arg || arg->type != EXPR_STRING)
441 return;
442 option_debug_check = arg->string->data;
443 sm_msg("arg = '%s'", option_debug_check);
446 static void match_debug_off(const char *fn, struct expression *expr, void *info)
448 option_debug_check = (char *)"";
449 option_debug = 0;
452 static void match_local_debug_on(const char *fn, struct expression *expr, void *info)
454 local_debug = 1;
457 static void match_local_debug_off(const char *fn, struct expression *expr, void *info)
459 local_debug = 0;
462 static void match_debug_implied_on(const char *fn, struct expression *expr, void *info)
464 option_debug_implied = 1;
467 static void match_debug_implied_off(const char *fn, struct expression *expr, void *info)
469 option_debug_implied = 0;
472 static void match_about(const char *fn, struct expression *expr, void *info)
474 struct expression *arg;
475 struct sm_state *sm;
476 char *name;
478 sm_msg("---- about ----");
479 match_print_implied(fn, expr, NULL);
480 match_buf_size(fn, expr, NULL);
481 match_strlen(fn, expr, NULL);
483 arg = get_argument_from_call_expr(expr->args, 0);
484 name = expr_to_str(arg);
485 if (!name) {
486 sm_msg("info: not a straight forward variable.");
487 return;
490 FOR_EACH_SM(__get_cur_stree(), sm) {
491 if (strcmp(sm->name, name) != 0)
492 continue;
493 sm_msg("%s '%s' = '%s'", check_name(sm->owner), sm->name, sm->state->name);
494 } END_FOR_EACH_SM(sm);
497 static void match_intersection(const char *fn, struct expression *expr, void *info)
499 struct expression *one, *two;
500 struct range_list *one_rl, *two_rl;
501 struct range_list *res;
503 one = get_argument_from_call_expr(expr->args, 0);
504 two = get_argument_from_call_expr(expr->args, 1);
506 get_absolute_rl(one, &one_rl);
507 get_absolute_rl(two, &two_rl);
509 res = rl_intersection(one_rl, two_rl);
510 sm_msg("'%s' intersect '%s' is '%s'", show_rl(one_rl), show_rl(two_rl), show_rl(res));
513 static void match_type(const char *fn, struct expression *expr, void *info)
515 struct expression *one;
516 struct symbol *type;
517 char *name;
519 one = get_argument_from_call_expr(expr->args, 0);
520 type = get_type(one);
521 name = expr_to_str(one);
522 sm_msg("type of '%s' is: '%s'", name, type_to_str(type));
523 free_string(name);
526 static struct stree *old_stree;
527 static void trace_var(struct statement *stmt)
529 struct sm_state *sm, *old;
531 if (!trace_variable)
532 return;
533 if (__inline_fn)
534 return;
536 FOR_EACH_SM(__get_cur_stree(), sm) {
537 if (strcmp(sm->name, trace_variable) != 0)
538 continue;
539 old = get_sm_state_stree(old_stree, sm->owner, sm->name, sm->sym);
540 if (old && old->state == sm->state)
541 continue;
542 sm_msg("[%d] %s '%s': '%s' => '%s'", stmt->type,
543 check_name(sm->owner),
544 sm->name, old ? old->state->name : "<none>", sm->state->name);
545 } END_FOR_EACH_SM(sm);
547 free_stree(&old_stree);
548 old_stree = clone_stree(__get_cur_stree());
551 static void free_old_stree(struct symbol *sym)
553 free_stree(&old_stree);
556 void check_debug(int id)
558 my_id = id;
559 add_function_hook("__smatch_about", &match_about, NULL);
560 add_function_hook("__smatch_all_values", &match_all_values, NULL);
561 add_function_hook("__smatch_state", &match_state, NULL);
562 add_function_hook("__smatch_states", &match_states, NULL);
563 add_function_hook("__smatch_value", &match_print_value, NULL);
564 add_function_hook("__smatch_known", &match_print_known, NULL);
565 add_function_hook("__smatch_implied", &match_print_implied, NULL);
566 add_function_hook("__smatch_implied_min", &match_print_implied_min, NULL);
567 add_function_hook("__smatch_implied_max", &match_print_implied_max, NULL);
568 add_function_hook("__smatch_user_rl", &match_user_rl, NULL);
569 add_function_hook("__smatch_hard_max", &match_print_hard_max, NULL);
570 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max, NULL);
571 add_function_hook("__smatch_absolute_min", &match_print_absolute_min, NULL);
572 add_function_hook("__smatch_absolute_max", &match_print_absolute_max, NULL);
573 add_function_hook("__smatch_sval_info", &match_sval_info, NULL);
574 add_function_hook("__smatch_member_name", &match_member_name, NULL);
575 add_function_hook("__smatch_possible", &match_possible, NULL);
576 add_function_hook("__smatch_cur_stree", &match_cur_stree, NULL);
577 add_function_hook("__smatch_strlen", &match_strlen, NULL);
578 add_function_hook("__smatch_buf_size", &match_buf_size, NULL);
579 add_function_hook("__smatch_buf_size_rl", &match_buf_size_rl, NULL);
580 add_function_hook("__smatch_note", &match_note, NULL);
581 add_function_hook("__smatch_dump_related", &match_dump_related, NULL);
582 add_function_hook("__smatch_compare", &match_compare, NULL);
583 add_function_hook("__smatch_debug_on", &match_debug_on, NULL);
584 add_function_hook("__smatch_debug_check", &match_debug_check, NULL);
585 add_function_hook("__smatch_debug_off", &match_debug_off, NULL);
586 add_function_hook("__smatch_local_debug_on", &match_local_debug_on, NULL);
587 add_function_hook("__smatch_local_debug_off", &match_local_debug_off, NULL);
588 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on, NULL);
589 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off, NULL);
590 add_function_hook("__smatch_intersection", &match_intersection, NULL);
591 add_function_hook("__smatch_type", &match_type, NULL);
593 add_hook(free_old_stree, END_FUNC_HOOK);
594 add_hook(trace_var, STMT_HOOK_AFTER);