slist: a pointer is equal to itself in cmp_possible_sm()
[smatch.git] / check_debug.c
blob8c8bc4c1777cf6596ed26bc0376feff5c9e6e628
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_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_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_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", show_sm(sm));
86 found = 1;
87 } END_FOR_EACH_SM(sm);
89 if (found)
90 return;
92 if (!id_from_name(check_arg->string->data))
93 sm_msg("invalid check name '%s'", check_arg->string->data);
94 else
95 sm_msg("%s: no states", check_arg->string->data);
98 static void match_print_value(const char *fn, struct expression *expr, void *info)
100 struct stree *stree;
101 struct sm_state *tmp;
102 struct expression *arg_expr;
104 arg_expr = get_argument_from_call_expr(expr->args, 0);
105 if (arg_expr->type != EXPR_STRING) {
106 sm_error("the argument to %s is supposed to be a string literal", fn);
107 return;
110 stree = __get_cur_stree();
111 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
112 if (!strcmp(tmp->name, arg_expr->string->data))
113 sm_msg("%s = %s", tmp->name, tmp->state->name);
114 } END_FOR_EACH_SM(tmp);
117 static void match_print_known(const char *fn, struct expression *expr, void *info)
119 struct expression *arg;
120 struct range_list *rl = NULL;
121 char *name;
122 int known = 0;
123 sval_t sval;
125 arg = get_argument_from_call_expr(expr->args, 0);
126 if (get_value(arg, &sval))
127 known = 1;
129 get_implied_rl(arg, &rl);
131 name = expr_to_str(arg);
132 sm_msg("known: '%s' = '%s'. implied = '%s'", name, known ? sval_to_str(sval) : "<unknown>", show_rl(rl));
133 free_string(name);
136 static void match_print_implied(const char *fn, struct expression *expr, void *info)
138 struct expression *arg;
139 struct range_list *rl = NULL;
140 char *name;
142 arg = get_argument_from_call_expr(expr->args, 0);
143 get_implied_rl(arg, &rl);
145 name = expr_to_str(arg);
146 sm_msg("implied: %s = '%s'", name, show_rl(rl));
147 free_string(name);
150 static void match_real_absolute(const char *fn, struct expression *expr, void *info)
152 struct expression *arg;
153 struct range_list *rl = NULL;
154 char *name;
156 arg = get_argument_from_call_expr(expr->args, 0);
157 get_real_absolute_rl(arg, &rl);
159 name = expr_to_str(arg);
160 sm_msg("real absolute: %s = '%s'", name, show_rl(rl));
161 free_string(name);
164 static void match_print_implied_min(const char *fn, struct expression *expr, void *info)
166 struct expression *arg;
167 sval_t sval;
168 char *name;
170 arg = get_argument_from_call_expr(expr->args, 0);
171 name = expr_to_str(arg);
173 if (get_implied_min(arg, &sval))
174 sm_msg("implied min: %s = %s", name, sval_to_str(sval));
175 else
176 sm_msg("implied min: %s = <unknown>", name);
178 free_string(name);
181 static void match_print_implied_max(const char *fn, struct expression *expr, void *info)
183 struct expression *arg;
184 sval_t sval;
185 char *name;
187 arg = get_argument_from_call_expr(expr->args, 0);
188 name = expr_to_str(arg);
190 if (get_implied_max(arg, &sval))
191 sm_msg("implied max: %s = %s", name, sval_to_str(sval));
192 else
193 sm_msg("implied max: %s = <unknown>", name);
195 free_string(name);
198 static void match_user_rl(const char *fn, struct expression *expr, void *info)
200 struct expression *arg;
201 struct range_list *rl;
202 char *name;
204 arg = get_argument_from_call_expr(expr->args, 0);
205 name = expr_to_str(arg);
207 get_user_rl(arg, &rl);
208 sm_msg("user rl: '%s' = '%s'", name, show_rl(rl));
210 free_string(name);
213 static void match_capped(const char *fn, struct expression *expr, void *info)
215 struct expression *arg;
216 char *name;
218 arg = get_argument_from_call_expr(expr->args, 0);
219 name = expr_to_str(arg);
220 sm_msg("'%s' = '%s'", name, is_capped(arg) ? "capped" : "not capped");
221 free_string(name);
224 static void match_print_hard_max(const char *fn, struct expression *expr, void *info)
226 struct expression *arg;
227 sval_t sval;
228 char *name;
230 arg = get_argument_from_call_expr(expr->args, 0);
231 name = expr_to_str(arg);
233 if (get_hard_max(arg, &sval))
234 sm_msg("hard max: %s = %s", name, sval_to_str(sval));
235 else
236 sm_msg("hard max: %s = <unknown>", name);
238 free_string(name);
241 static void match_print_fuzzy_max(const char *fn, struct expression *expr, void *info)
243 struct expression *arg;
244 sval_t sval;
245 char *name;
247 arg = get_argument_from_call_expr(expr->args, 0);
248 name = expr_to_str(arg);
250 if (get_fuzzy_max(arg, &sval))
251 sm_msg("fuzzy max: %s = %s", name, sval_to_str(sval));
252 else
253 sm_msg("fuzzy max: %s = <unknown>", name);
255 free_string(name);
258 static void match_print_absolute(const char *fn, struct expression *expr, void *info)
260 struct expression *arg;
261 struct range_list *rl;
262 char *name;
264 arg = get_argument_from_call_expr(expr->args, 0);
265 name = expr_to_str(arg);
267 get_absolute_rl(arg, &rl);
268 sm_msg("absolute: %s = %s", name, show_rl(rl));
270 free_string(name);
273 static void match_print_absolute_min(const char *fn, struct expression *expr, void *info)
275 struct expression *arg;
276 sval_t sval;
277 char *name;
279 arg = get_argument_from_call_expr(expr->args, 0);
280 name = expr_to_str(arg);
282 if (get_absolute_min(arg, &sval))
283 sm_msg("absolute min: %s = %s", name, sval_to_str(sval));
284 else
285 sm_msg("absolute min: %s = <unknown>", name);
287 free_string(name);
290 static void match_print_absolute_max(const char *fn, struct expression *expr, void *info)
292 struct expression *arg;
293 sval_t sval;
294 char *name;
296 arg = get_argument_from_call_expr(expr->args, 0);
297 get_absolute_max(arg, &sval);
299 name = expr_to_str(arg);
300 sm_msg("absolute max: %s = %s", name, sval_to_str(sval));
301 free_string(name);
304 static void match_sval_info(const char *fn, struct expression *expr, void *info)
306 struct expression *arg;
307 sval_t sval;
308 char *name;
310 arg = get_argument_from_call_expr(expr->args, 0);
311 name = expr_to_str(arg);
313 if (!get_implied_value(arg, &sval)) {
314 sm_msg("no sval for '%s'", name);
315 goto free;
318 sm_msg("implied: %s %c%d ->value = %llx", name, sval_unsigned(sval) ? 'u' : 's', sval_bits(sval), sval.value);
319 free:
320 free_string(name);
323 static void match_member_name(const char *fn, struct expression *expr, void *info)
325 struct expression *arg;
326 char *name, *member_name;
328 arg = get_argument_from_call_expr(expr->args, 0);
329 name = expr_to_str(arg);
330 member_name = get_member_name(arg);
331 sm_msg("member name: '%s => %s'", name, member_name);
332 free_string(member_name);
333 free_string(name);
336 static void print_possible(struct sm_state *sm)
338 struct sm_state *tmp;
340 sm_msg("Possible values for %s", sm->name);
341 FOR_EACH_PTR(sm->possible, tmp) {
342 printf("%s\n", tmp->state->name);
343 } END_FOR_EACH_PTR(tmp);
344 sm_msg("===");
347 static void match_possible(const char *fn, struct expression *expr, void *info)
349 struct stree *stree;
350 struct sm_state *tmp;
351 struct expression *arg_expr;
353 arg_expr = get_argument_from_call_expr(expr->args, 0);
354 if (arg_expr->type != EXPR_STRING) {
355 sm_error("the argument to %s is supposed to be a string literal", fn);
356 return;
359 stree = __get_cur_stree();
360 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
361 if (!strcmp(tmp->name, arg_expr->string->data))
362 print_possible(tmp);
363 } END_FOR_EACH_SM(tmp);
366 static void match_strlen(const char *fn, struct expression *expr, void *info)
368 struct expression *arg;
369 struct range_list *rl;
370 char *name;
372 arg = get_argument_from_call_expr(expr->args, 0);
373 get_implied_strlen(arg, &rl);
375 name = expr_to_str(arg);
376 sm_msg("strlen: '%s' %s characters", name, show_rl(rl));
377 free_string(name);
380 static void match_buf_size(const char *fn, struct expression *expr, void *info)
382 struct expression *arg, *comp;
383 struct range_list *rl;
384 int elements, bytes;
385 char *name;
386 char buf[256] = "";
387 int limit_type;
388 int n;
389 sval_t sval;
391 arg = get_argument_from_call_expr(expr->args, 0);
393 elements = get_array_size(arg);
394 bytes = get_array_size_bytes_max(arg);
395 rl = get_array_size_bytes_rl(arg);
396 comp = get_size_variable(arg, &limit_type);
398 name = expr_to_str(arg);
399 n = snprintf(buf, sizeof(buf), "buf size: '%s' %d elements, %d bytes", name, elements, bytes);
400 free_string(name);
402 if (!rl_to_sval(rl, &sval))
403 n += snprintf(buf + n, sizeof(buf) - n, " (rl = %s)", show_rl(rl));
405 if (comp) {
406 name = expr_to_str(comp);
407 snprintf(buf + n, sizeof(buf) - n, "[size_var=%s %s]", limit_type_str(limit_type), name);
408 free_string(name);
410 sm_msg("%s", buf);
413 static void match_note(const char *fn, struct expression *expr, void *info)
415 struct expression *arg_expr;
417 arg_expr = get_argument_from_call_expr(expr->args, 0);
418 if (arg_expr->type != EXPR_STRING) {
419 sm_error("the argument to %s is supposed to be a string literal", fn);
420 return;
422 sm_msg("%s", arg_expr->string->data);
425 static void print_related(struct sm_state *sm)
427 struct relation *rel;
429 if (!estate_related(sm->state))
430 return;
432 sm_prefix();
433 sm_printf("%s: ", sm->name);
434 FOR_EACH_PTR(estate_related(sm->state), rel) {
435 sm_printf("%s ", rel->name);
436 } END_FOR_EACH_PTR(rel);
437 sm_printf("\n");
440 static void match_dump_related(const char *fn, struct expression *expr, void *info)
442 struct stree *stree;
443 struct sm_state *tmp;
445 stree = __get_cur_stree();
446 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
447 print_related(tmp);
448 } END_FOR_EACH_SM(tmp);
451 static void match_compare(const char *fn, struct expression *expr, void *info)
453 struct expression *one, *two;
454 char *one_name, *two_name;
455 int comparison;
456 char buf[16];
458 one = get_argument_from_call_expr(expr->args, 0);
459 two = get_argument_from_call_expr(expr->args, 1);
461 comparison = get_comparison(one, two);
462 if (!comparison)
463 snprintf(buf, sizeof(buf), "<none>");
464 else
465 snprintf(buf, sizeof(buf), "%s", show_special(comparison));
467 one_name = expr_to_str(one);
468 two_name = expr_to_str(two);
470 sm_msg("%s %s %s", one_name, buf, two_name);
472 free_string(one_name);
473 free_string(two_name);
476 static void match_debug_on(const char *fn, struct expression *expr, void *info)
478 option_debug = 1;
481 static void match_debug_check(const char *fn, struct expression *expr, void *info)
483 struct expression *arg;
485 arg = get_argument_from_call_expr(expr->args, 0);
486 if (!arg || arg->type != EXPR_STRING)
487 return;
488 option_debug_check = arg->string->data;
489 sm_msg("arg = '%s'", option_debug_check);
492 static void match_debug_off(const char *fn, struct expression *expr, void *info)
494 option_debug_check = (char *)"";
495 option_debug = 0;
498 static void match_local_debug_on(const char *fn, struct expression *expr, void *info)
500 local_debug = 1;
503 static void match_local_debug_off(const char *fn, struct expression *expr, void *info)
505 local_debug = 0;
508 static void match_about(const char *fn, struct expression *expr, void *info)
510 struct expression *arg;
511 struct sm_state *sm;
512 char *name;
514 sm_msg("---- about ----");
515 match_print_implied(fn, expr, NULL);
516 match_buf_size(fn, expr, NULL);
517 match_strlen(fn, expr, NULL);
518 match_real_absolute(fn, expr, NULL);
520 arg = get_argument_from_call_expr(expr->args, 0);
521 name = expr_to_str(arg);
522 if (!name) {
523 sm_msg("info: not a straight forward variable.");
524 return;
527 FOR_EACH_SM(__get_cur_stree(), sm) {
528 if (strcmp(sm->name, name) != 0)
529 continue;
530 sm_msg("%s", show_sm(sm));
531 } END_FOR_EACH_SM(sm);
534 static void match_intersection(const char *fn, struct expression *expr, void *info)
536 struct expression *one, *two;
537 struct range_list *one_rl, *two_rl;
538 struct range_list *res;
540 one = get_argument_from_call_expr(expr->args, 0);
541 two = get_argument_from_call_expr(expr->args, 1);
543 get_absolute_rl(one, &one_rl);
544 get_absolute_rl(two, &two_rl);
546 res = rl_intersection(one_rl, two_rl);
547 sm_msg("'%s' intersect '%s' is '%s'", show_rl(one_rl), show_rl(two_rl), show_rl(res));
550 static void match_type(const char *fn, struct expression *expr, void *info)
552 struct expression *one;
553 struct symbol *type;
554 char *name;
556 one = get_argument_from_call_expr(expr->args, 0);
557 type = get_type(one);
558 name = expr_to_str(one);
559 sm_msg("type of '%s' is: '%s'", name, type_to_str(type));
560 free_string(name);
563 static int match_type_rl_return(struct expression *call, void *unused, struct range_list **rl)
565 struct expression *one, *two;
566 struct symbol *type;
568 one = get_argument_from_call_expr(call->args, 0);
569 type = get_type(one);
571 two = get_argument_from_call_expr(call->args, 1);
572 if (!two || two->type != EXPR_STRING) {
573 sm_msg("expected: __smatch_type_rl(type, \"string\")");
574 return 0;
576 call_results_to_rl(call, type, two->string->data, rl);
577 return 1;
580 static void print_left_right(struct sm_state *sm)
582 if (!sm)
583 return;
584 if (!sm->left && !sm->right)
585 return;
587 sm_printf("[ ");
588 if (sm->left)
589 sm_printf("(%d: %s->'%s')", get_stree_id(sm->left->pool), sm->left->name, sm->left->state->name);
590 else
591 sm_printf(" - ");
594 print_left_right(sm->left);
596 if (sm->right)
597 sm_printf("(%d: %s->'%s')", get_stree_id(sm->right->pool), sm->right->name, sm->right->state->name);
598 else
599 sm_printf(" - ");
601 print_left_right(sm->right);
604 static void match_print_merge_tree(const char *fn, struct expression *expr, void *info)
606 struct sm_state *sm;
607 struct expression *arg;
608 char *name;
610 arg = get_argument_from_call_expr(expr->args, 0);
611 name = expr_to_str(arg);
613 sm = get_sm_state_expr(SMATCH_EXTRA, arg);
614 if (!sm) {
615 sm_msg("no sm state for '%s'", name);
616 goto free;
619 sm_prefix();
620 sm_printf("merge tree: %s -> %s", name, sm->state->name);
621 print_left_right(sm);
622 sm_printf("\n");
624 free:
625 free_string(name);
628 static void match_print_stree_id(const char *fn, struct expression *expr, void *info)
630 sm_msg("stree_id %d", __stree_id);
633 static void match_mtag(const char *fn, struct expression *expr, void *info)
635 struct expression *arg;
636 char *name;
637 mtag_t tag = 0;
639 arg = get_argument_from_call_expr(expr->args, 0);
640 name = expr_to_str(arg);
641 get_mtag(arg, &tag);
642 sm_msg("mtag: '%s' => tag: %lld", name, tag);
643 free_string(name);
646 static void match_mtag_data_offset(const char *fn, struct expression *expr, void *info)
648 struct expression *arg;
649 char *name;
650 mtag_t tag = 0;
651 int offset = -1;
653 arg = get_argument_from_call_expr(expr->args, 0);
654 name = expr_to_str(arg);
655 expr_to_mtag_offset(arg, &tag, &offset);
656 sm_msg("mtag: '%s' => tag: %lld, offset: %d", name, tag, offset);
657 free_string(name);
660 static void match_state_count(const char *fn, struct expression *expr, void *info)
662 sm_msg("state_count = %d\n", sm_state_counter);
665 static void match_mem(const char *fn, struct expression *expr, void *info)
667 show_sname_alloc();
668 show_ptrlist_alloc();
669 sm_msg("%lu pools", get_pool_count());
670 sm_msg("%d strees", unfree_stree);
671 show_smatch_state_alloc();
672 show_sm_state_alloc();
675 static void match_exit(const char *fn, struct expression *expr, void *info)
677 exit(0);
680 static struct stree *old_stree;
681 static void trace_var(struct statement *stmt)
683 struct sm_state *sm, *old;
684 int printed = 0;
686 if (!trace_variable)
687 return;
688 if (__inline_fn)
689 return;
691 FOR_EACH_SM(__get_cur_stree(), sm) {
692 if (strcmp(sm->name, trace_variable) != 0)
693 continue;
694 old = get_sm_state_stree(old_stree, sm->owner, sm->name, sm->sym);
695 if (old && old->state == sm->state)
696 continue;
697 sm_msg("[%d] %s '%s': '%s' => '%s'", stmt->type,
698 check_name(sm->owner),
699 sm->name, old ? old->state->name : "<none>", sm->state->name);
700 printed = 1;
701 } END_FOR_EACH_SM(sm);
703 if (printed) {
704 free_stree(&old_stree);
705 old_stree = clone_stree(__get_cur_stree());
709 static void free_old_stree(struct symbol *sym)
711 free_stree(&old_stree);
714 void check_debug(int id)
716 my_id = id;
717 add_function_hook("__smatch_about", &match_about, NULL);
718 add_function_hook("__smatch_all_values", &match_all_values, NULL);
719 add_function_hook("__smatch_state", &match_state, NULL);
720 add_function_hook("__smatch_states", &match_states, NULL);
721 add_function_hook("__smatch_value", &match_print_value, NULL);
722 add_function_hook("__smatch_known", &match_print_known, NULL);
723 add_function_hook("__smatch_implied", &match_print_implied, NULL);
724 add_function_hook("__smatch_implied_min", &match_print_implied_min, NULL);
725 add_function_hook("__smatch_implied_max", &match_print_implied_max, NULL);
726 add_function_hook("__smatch_user_rl", &match_user_rl, NULL);
727 add_function_hook("__smatch_capped", &match_capped, NULL);
728 add_function_hook("__smatch_hard_max", &match_print_hard_max, NULL);
729 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max, NULL);
730 add_function_hook("__smatch_absolute", &match_print_absolute, NULL);
731 add_function_hook("__smatch_absolute_min", &match_print_absolute_min, NULL);
732 add_function_hook("__smatch_absolute_max", &match_print_absolute_max, NULL);
733 add_function_hook("__smatch_real_absolute", &match_real_absolute, NULL);
734 add_function_hook("__smatch_sval_info", &match_sval_info, NULL);
735 add_function_hook("__smatch_member_name", &match_member_name, NULL);
736 add_function_hook("__smatch_possible", &match_possible, NULL);
737 add_function_hook("__smatch_cur_stree", &match_cur_stree, NULL);
738 add_function_hook("__smatch_strlen", &match_strlen, NULL);
739 add_function_hook("__smatch_buf_size", &match_buf_size, NULL);
740 add_function_hook("__smatch_note", &match_note, NULL);
741 add_function_hook("__smatch_dump_related", &match_dump_related, NULL);
742 add_function_hook("__smatch_compare", &match_compare, NULL);
743 add_function_hook("__smatch_debug_on", &match_debug_on, NULL);
744 add_function_hook("__smatch_debug_check", &match_debug_check, NULL);
745 add_function_hook("__smatch_debug_off", &match_debug_off, NULL);
746 add_function_hook("__smatch_local_debug_on", &match_local_debug_on, NULL);
747 add_function_hook("__smatch_local_debug_off", &match_local_debug_off, NULL);
748 add_function_hook("__smatch_intersection", &match_intersection, NULL);
749 add_function_hook("__smatch_type", &match_type, NULL);
750 add_implied_return_hook("__smatch_type_rl_helper", &match_type_rl_return, NULL);
751 add_function_hook("__smatch_merge_tree", &match_print_merge_tree, NULL);
752 add_function_hook("__smatch_stree_id", &match_print_stree_id, NULL);
753 add_function_hook("__smatch_mtag", &match_mtag, NULL);
754 add_function_hook("__smatch_mtag_data", &match_mtag_data_offset, NULL);
755 add_function_hook("__smatch_state_count", &match_state_count, NULL);
756 add_function_hook("__smatch_mem", &match_mem, NULL);
757 add_function_hook("__smatch_exit", &match_exit, NULL);
759 add_hook(free_old_stree, AFTER_FUNC_HOOK);
760 add_hook(trace_var, STMT_HOOK_AFTER);