db: member_info callback should pass the sm_state instead of the state
[smatch.git] / check_debug.c
blob2779a83c48bed0c10af2b87635525e2287f57da1
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_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 stree *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_cur_stree();
281 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
282 if (!strcmp(tmp->name, arg_expr->string->data))
283 print_possible(tmp);
284 } END_FOR_EACH_SM(tmp);
287 static void match_strlen(const char *fn, struct expression *expr, void *info)
289 struct expression *arg;
290 struct range_list *rl;
291 char *name;
293 arg = get_argument_from_call_expr(expr->args, 0);
294 get_implied_strlen(arg, &rl);
296 name = expr_to_str(arg);
297 sm_msg("strlen: '%s' %s characters", name, show_rl(rl));
298 free_string(name);
301 static void match_buf_size(const char *fn, struct expression *expr, void *info)
303 struct expression *arg;
304 int elements, bytes;
305 char *name;
307 arg = get_argument_from_call_expr(expr->args, 0);
308 elements = get_array_size(arg);
309 bytes = get_array_size_bytes(arg);
311 name = expr_to_str(arg);
312 sm_msg("buf size: '%s' %d elements, %d bytes", name, elements, bytes);
313 free_string(name);
316 static void match_buf_size_rl(const char *fn, struct expression *expr, void *info)
318 struct expression *arg;
319 struct range_list *rl;
320 int elements, bytes;
321 char *name;
323 arg = get_argument_from_call_expr(expr->args, 0);
324 rl = get_array_size_bytes_rl(arg);
325 elements = get_array_size(arg);
326 bytes = get_array_size_bytes(arg);
328 name = expr_to_str(arg);
329 sm_msg("buf size: '%s' %s %d elements, %d bytes", name, show_rl(rl), elements, bytes);
330 free_string(name);
333 static void match_note(const char *fn, struct expression *expr, void *info)
335 struct expression *arg_expr;
337 arg_expr = get_argument_from_call_expr(expr->args, 0);
338 if (arg_expr->type != EXPR_STRING) {
339 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
340 return;
342 sm_msg("%s", arg_expr->string->data);
345 static void print_related(struct sm_state *sm)
347 struct relation *rel;
349 if (!estate_related(sm->state))
350 return;
352 sm_prefix();
353 sm_printf("%s: ", sm->name);
354 FOR_EACH_PTR(estate_related(sm->state), rel) {
355 sm_printf("%s ", rel->name);
356 } END_FOR_EACH_PTR(rel);
357 sm_printf("\n");
360 static void match_dump_related(const char *fn, struct expression *expr, void *info)
362 struct stree *stree;
363 struct sm_state *tmp;
365 stree = __get_cur_stree();
366 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
367 print_related(tmp);
368 } END_FOR_EACH_SM(tmp);
371 static void match_compare(const char *fn, struct expression *expr, void *info)
373 struct expression *one, *two;
374 char *one_name, *two_name;
375 int comparison;
376 char buf[16];
378 one = get_argument_from_call_expr(expr->args, 0);
379 two = get_argument_from_call_expr(expr->args, 1);
381 comparison = get_comparison(one, two);
382 if (!comparison)
383 snprintf(buf, sizeof(buf), "<none>");
384 else
385 snprintf(buf, sizeof(buf), "%s", show_special(comparison));
387 one_name = expr_to_str(one);
388 two_name = expr_to_str(two);
390 sm_msg("%s %s %s", one_name, buf, two_name);
392 free_string(one_name);
393 free_string(two_name);
396 static void match_debug_on(const char *fn, struct expression *expr, void *info)
398 option_debug = 1;
401 static void match_debug_check(const char *fn, struct expression *expr, void *info)
403 struct expression *arg;
405 arg = get_argument_from_call_expr(expr->args, 0);
406 if (!arg || arg->type != EXPR_STRING)
407 return;
408 option_debug_check = arg->string->data;
409 sm_msg("arg = '%s'", option_debug_check);
412 static void match_debug_off(const char *fn, struct expression *expr, void *info)
414 option_debug_check = (char *)"";
415 option_debug = 0;
418 static void match_local_debug_on(const char *fn, struct expression *expr, void *info)
420 local_debug = 1;
423 static void match_local_debug_off(const char *fn, struct expression *expr, void *info)
425 local_debug = 0;
428 static void match_debug_implied_on(const char *fn, struct expression *expr, void *info)
430 option_debug_implied = 1;
433 static void match_debug_implied_off(const char *fn, struct expression *expr, void *info)
435 option_debug_implied = 0;
438 static void match_about(const char *fn, struct expression *expr, void *info)
440 struct expression *arg;
441 struct sm_state *sm;
442 char *name;
444 sm_msg("---- about ----");
445 match_print_implied(fn, expr, NULL);
446 match_buf_size(fn, expr, NULL);
447 match_strlen(fn, expr, NULL);
449 arg = get_argument_from_call_expr(expr->args, 0);
450 name = expr_to_str(arg);
451 if (!name) {
452 sm_msg("info: not a straight forward variable.");
453 return;
456 FOR_EACH_SM(__get_cur_stree(), sm) {
457 if (strcmp(sm->name, name) != 0)
458 continue;
459 sm_msg("%s '%s' = '%s'", check_name(sm->owner), sm->name, sm->state->name);
460 } END_FOR_EACH_SM(sm);
463 static void match_intersection(const char *fn, struct expression *expr, void *info)
465 struct expression *one, *two;
466 struct range_list *one_rl, *two_rl;
467 struct range_list *res;
469 one = get_argument_from_call_expr(expr->args, 0);
470 two = get_argument_from_call_expr(expr->args, 1);
472 get_absolute_rl(one, &one_rl);
473 get_absolute_rl(two, &two_rl);
475 res = rl_intersection(one_rl, two_rl);
476 sm_msg("'%s' intersect '%s' is '%s'", show_rl(one_rl), show_rl(two_rl), show_rl(res));
479 static void match_type(const char *fn, struct expression *expr, void *info)
481 struct expression *one;
482 struct symbol *type;
483 char *name;
485 one = get_argument_from_call_expr(expr->args, 0);
486 type = get_type(one);
487 name = expr_to_str(one);
488 sm_msg("type of '%s' is: '%s'", name, type_to_str(type));
489 free_string(name);
492 static struct stree *old_stree;
493 static void trace_var(struct statement *stmt)
495 struct sm_state *sm, *old;
497 if (!trace_variable)
498 return;
499 if (__inline_fn)
500 return;
502 FOR_EACH_SM(__get_cur_stree(), sm) {
503 if (strcmp(sm->name, trace_variable) != 0)
504 continue;
505 old = get_sm_state_stree(old_stree, sm->owner, sm->name, sm->sym);
506 if (old && old->state == sm->state)
507 continue;
508 sm_msg("[%d] %s '%s': '%s' => '%s'", stmt->type,
509 check_name(sm->owner),
510 sm->name, old ? old->state->name : "<none>", sm->state->name);
511 } END_FOR_EACH_SM(sm);
513 free_stree(&old_stree);
514 old_stree = clone_stree(__get_cur_stree());
517 static void free_old_stree(struct symbol *sym)
519 free_stree(&old_stree);
522 void check_debug(int id)
524 my_id = id;
525 add_function_hook("__smatch_about", &match_about, NULL);
526 add_function_hook("__smatch_all_values", &match_all_values, NULL);
527 add_function_hook("__smatch_state", &match_state, NULL);
528 add_function_hook("__smatch_states", &match_states, NULL);
529 add_function_hook("__smatch_value", &match_print_value, NULL);
530 add_function_hook("__smatch_implied", &match_print_implied, NULL);
531 add_function_hook("__smatch_implied_min", &match_print_implied_min, NULL);
532 add_function_hook("__smatch_implied_max", &match_print_implied_max, NULL);
533 add_function_hook("__smatch_hard_max", &match_print_hard_max, NULL);
534 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max, NULL);
535 add_function_hook("__smatch_absolute_min", &match_print_absolute_min, NULL);
536 add_function_hook("__smatch_absolute_max", &match_print_absolute_max, NULL);
537 add_function_hook("__smatch_sval_info", &match_sval_info, NULL);
538 add_function_hook("__smatch_member_name", &match_member_name, NULL);
539 add_function_hook("__smatch_possible", &match_possible, NULL);
540 add_function_hook("__smatch_cur_stree", &match_cur_stree, NULL);
541 add_function_hook("__smatch_strlen", &match_strlen, NULL);
542 add_function_hook("__smatch_buf_size", &match_buf_size, NULL);
543 add_function_hook("__smatch_buf_size_rl", &match_buf_size_rl, NULL);
544 add_function_hook("__smatch_note", &match_note, NULL);
545 add_function_hook("__smatch_dump_related", &match_dump_related, NULL);
546 add_function_hook("__smatch_compare", &match_compare, NULL);
547 add_function_hook("__smatch_debug_on", &match_debug_on, NULL);
548 add_function_hook("__smatch_debug_check", &match_debug_check, NULL);
549 add_function_hook("__smatch_debug_off", &match_debug_off, NULL);
550 add_function_hook("__smatch_local_debug_on", &match_local_debug_on, NULL);
551 add_function_hook("__smatch_local_debug_off", &match_local_debug_off, NULL);
552 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on, NULL);
553 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off, NULL);
554 add_function_hook("__smatch_intersection", &match_intersection, NULL);
555 add_function_hook("__smatch_type", &match_type, NULL);
557 add_hook(free_old_stree, END_FUNC_HOOK);
558 add_hook(trace_var, STMT_HOOK_AFTER);