kernel.return_fixes: add mipi_dsi_device_transfer(), timer_delete() and get_device()
[smatch.git] / check_debug.c
blob8e8316db154c5bd3a84c21a597a90ab36de99979
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 <string.h>
20 #include "smatch.h"
21 #include "smatch_slist.h"
22 #include "smatch_extra.h"
24 void show_sname_alloc(void);
25 void show_data_range_alloc(void);
26 void show_ptrlist_alloc(void);
27 void show_rl_ptrlist_alloc(void);
28 void show_sm_state_alloc(void);
30 int local_debug;
31 static int my_id;
32 char *trace_variable;
34 static void match_all_values(const char *fn, struct expression *expr, void *info)
36 struct stree *stree;
38 stree = get_all_states_stree(SMATCH_EXTRA);
39 __print_stree(stree);
40 free_stree(&stree);
43 static void match_cur_stree(const char *fn, struct expression *expr, void *info)
45 __print_cur_stree();
48 static void match_state(const char *fn, struct expression *expr, void *info)
50 struct expression *check_arg, *state_arg;
51 struct sm_state *sm;
52 int found = 0;
54 check_arg = get_argument_from_call_expr(expr->args, 0);
55 if (check_arg->type != EXPR_STRING) {
56 sm_error("the check_name argument to %s is supposed to be a string literal", fn);
57 return;
59 state_arg = get_argument_from_call_expr(expr->args, 1);
60 if (!state_arg || state_arg->type != EXPR_STRING) {
61 sm_error("the state_name argument to %s is supposed to be a string literal", fn);
62 return;
65 FOR_EACH_SM(__get_cur_stree(), sm) {
66 if (strcmp(check_name(sm->owner), check_arg->string->data) != 0)
67 continue;
68 if (strcmp(sm->name, state_arg->string->data) != 0)
69 continue;
70 sm_msg("'%s' = '%s'", sm->name, sm->state->name);
71 found = 1;
72 } END_FOR_EACH_SM(sm);
74 if (!found)
75 sm_msg("%s '%s' not found", check_arg->string->data, state_arg->string->data);
78 static void match_states(const char *fn, struct expression *expr, void *info)
80 struct expression *check_arg;
82 check_arg = get_argument_from_call_expr(expr->args, 0);
83 if (check_arg->type != EXPR_STRING) {
84 sm_error("the check_name argument to %s is supposed to be a string literal", fn);
85 return;
88 if (__print_states(check_arg->string->data))
89 return;
91 if (!id_from_name(check_arg->string->data))
92 sm_msg("invalid check name '%s'", check_arg->string->data);
93 else
94 sm_msg("%s: no states", check_arg->string->data);
97 static void match_print_value(const char *fn, struct expression *expr, void *info)
99 struct stree *stree;
100 struct sm_state *tmp;
101 struct expression *arg_expr;
103 arg_expr = get_argument_from_call_expr(expr->args, 0);
104 if (arg_expr->type != EXPR_STRING) {
105 sm_error("the argument to %s is supposed to be a string literal", fn);
106 return;
109 stree = __get_cur_stree();
110 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
111 if (!strcmp(tmp->name, arg_expr->string->data))
112 sm_msg("%s = %s", tmp->name, tmp->state->name);
113 } END_FOR_EACH_SM(tmp);
116 static void match_print_known(const char *fn, struct expression *expr, void *info)
118 struct expression *arg;
119 struct range_list *rl = NULL;
120 char *name;
121 int known = 0;
122 sval_t sval;
124 arg = get_argument_from_call_expr(expr->args, 0);
125 if (get_value(arg, &sval))
126 known = 1;
128 get_implied_rl(arg, &rl);
130 name = expr_to_str(arg);
131 sm_msg("known: '%s' = '%s'. implied = '%s'", name, known ? sval_to_str(sval) : "<unknown>", show_rl(rl));
132 free_string(name);
135 static void match_print_implied(const char *fn, struct expression *expr, void *info)
137 struct expression *arg;
138 struct range_list *rl = NULL;
139 char *name;
141 arg = get_argument_from_call_expr(expr->args, 0);
142 get_implied_rl(arg, &rl);
144 name = expr_to_str(arg);
145 sm_msg("implied: %s = '%s'", name, show_rl(rl));
146 free_string(name);
149 static void match_real_absolute(const char *fn, struct expression *expr, void *info)
151 struct expression *arg;
152 struct range_list *rl = NULL;
153 char *name;
155 arg = get_argument_from_call_expr(expr->args, 0);
156 get_real_absolute_rl(arg, &rl);
158 name = expr_to_str(arg);
159 sm_msg("real absolute: %s = '%s'", name, show_rl(rl));
160 free_string(name);
163 static void match_print_implied_min(const char *fn, struct expression *expr, void *info)
165 struct expression *arg;
166 sval_t sval;
167 char *name;
169 arg = get_argument_from_call_expr(expr->args, 0);
170 name = expr_to_str(arg);
172 if (get_implied_min(arg, &sval))
173 sm_msg("implied min: %s = %s", name, sval_to_str(sval));
174 else
175 sm_msg("implied min: %s = <unknown>", name);
177 free_string(name);
180 static void match_print_implied_max(const char *fn, struct expression *expr, void *info)
182 struct expression *arg;
183 sval_t sval;
184 char *name;
186 arg = get_argument_from_call_expr(expr->args, 0);
187 name = expr_to_str(arg);
189 if (get_implied_max(arg, &sval))
190 sm_msg("implied max: %s = %s", name, sval_to_str(sval));
191 else
192 sm_msg("implied max: %s = <unknown>", name);
194 free_string(name);
197 static void match_user_rl(const char *fn, struct expression *expr, void *info)
199 struct expression *arg;
200 struct range_list *rl = NULL;
201 bool capped = false;
202 char *name;
204 if (option_project != PROJ_KERNEL)
205 sm_msg("no user data for project = '%s'", option_project_str);
207 arg = get_argument_from_call_expr(expr->args, 0);
208 name = expr_to_str(arg);
210 get_user_rl(arg, &rl);
211 if (rl)
212 capped = user_rl_capped(arg);
213 sm_msg("user rl: '%s' = '%s'%s", name, show_rl(rl), capped ? " (capped)" : "");
215 free_string(name);
218 static void match_host_rl(const char *fn, struct expression *expr, void *info)
220 struct expression *arg;
221 struct range_list *rl = NULL;
222 struct sm_state *sm;
223 bool capped = false;
224 bool new = false;
225 int host_id;
226 char *name;
228 host_id = id_from_name("register_kernel_host_data");
229 if (!host_id) {
230 sm_msg("no host id");
231 return;
234 arg = get_argument_from_call_expr(expr->args, 0);
235 name = expr_to_str(arg);
237 get_host_rl(arg, &rl);
238 if (rl)
239 capped = host_rl_capped(arg);
240 sm = get_sm_state_expr(host_id, arg);
241 if (sm && estate_new(sm->state))
242 new = true;
244 sm_msg("host rl: '%s' = '%s'%s %s sm='%s'", name, show_rl(rl),
245 capped ? " (capped)" : "",
246 new ? "(new)" : "(old)",
247 show_sm(sm));
249 free_string(name);
252 static void match_capped(const char *fn, struct expression *expr, void *info)
254 struct expression *arg;
255 char *name;
257 arg = get_argument_from_call_expr(expr->args, 0);
258 name = expr_to_str(arg);
259 sm_msg("'%s' = '%s'", name, is_capped(arg) ? "capped" : "not capped");
260 free_string(name);
263 static void match_print_hard_max(const char *fn, struct expression *expr, void *info)
265 struct expression *arg;
266 sval_t sval;
267 char *name;
269 arg = get_argument_from_call_expr(expr->args, 0);
270 name = expr_to_str(arg);
272 if (get_hard_max(arg, &sval))
273 sm_msg("hard max: %s = %s", name, sval_to_str(sval));
274 else
275 sm_msg("hard max: %s = <unknown>", name);
277 free_string(name);
280 static void match_print_fuzzy_max(const char *fn, struct expression *expr, void *info)
282 struct expression *arg;
283 sval_t sval;
284 char *name;
286 arg = get_argument_from_call_expr(expr->args, 0);
287 name = expr_to_str(arg);
289 if (get_fuzzy_max(arg, &sval))
290 sm_msg("fuzzy max: %s = %s", name, sval_to_str(sval));
291 else
292 sm_msg("fuzzy max: %s = <unknown>", name);
294 free_string(name);
297 static void match_print_absolute(const char *fn, struct expression *expr, void *info)
299 struct expression *arg;
300 struct range_list *rl;
301 char *name;
303 arg = get_argument_from_call_expr(expr->args, 0);
304 name = expr_to_str(arg);
306 get_absolute_rl(arg, &rl);
307 sm_msg("absolute: %s = %s", name, show_rl(rl));
309 free_string(name);
312 static void match_print_absolute_min(const char *fn, struct expression *expr, void *info)
314 struct expression *arg;
315 sval_t sval;
316 char *name;
318 arg = get_argument_from_call_expr(expr->args, 0);
319 name = expr_to_str(arg);
321 if (get_absolute_min(arg, &sval))
322 sm_msg("absolute min: %s = %s", name, sval_to_str(sval));
323 else
324 sm_msg("absolute min: %s = <unknown>", name);
326 free_string(name);
329 static void match_print_absolute_max(const char *fn, struct expression *expr, void *info)
331 struct expression *arg;
332 sval_t sval;
333 char *name;
335 arg = get_argument_from_call_expr(expr->args, 0);
336 get_absolute_max(arg, &sval);
338 name = expr_to_str(arg);
339 sm_msg("absolute max: %s = %s", name, sval_to_str(sval));
340 free_string(name);
343 static void match_sval_info(const char *fn, struct expression *expr, void *info)
345 struct expression *arg;
346 sval_t sval;
347 char *name;
349 arg = get_argument_from_call_expr(expr->args, 0);
350 name = expr_to_str(arg);
352 if (!get_implied_value(arg, &sval)) {
353 sm_msg("no sval for '%s'", name);
354 goto free;
357 sm_msg("implied: %s %c%d ->value = %llx", name, sval_unsigned(sval) ? 'u' : 's', sval_bits(sval), sval.value);
358 free:
359 free_string(name);
362 static void match_member_name(const char *fn, struct expression *expr, void *info)
364 struct expression *arg;
365 char *name, *member_name;
367 arg = get_argument_from_call_expr(expr->args, 0);
368 name = expr_to_str(arg);
369 member_name = get_member_name(arg);
370 sm_msg("member name: '%s => %s'", name, member_name);
371 free_string(name);
374 static void print_possible(struct sm_state *sm)
376 struct sm_state *tmp;
378 sm_msg("Possible values for %s", sm->name);
379 FOR_EACH_PTR(sm->possible, tmp) {
380 printf("%s\n", tmp->state->name);
381 } END_FOR_EACH_PTR(tmp);
382 sm_msg("===");
385 static void match_possible(const char *fn, struct expression *expr, void *info)
387 struct stree *stree;
388 struct sm_state *tmp;
389 struct expression *arg_expr;
391 arg_expr = get_argument_from_call_expr(expr->args, 0);
392 if (arg_expr->type != EXPR_STRING) {
393 sm_error("the argument to %s is supposed to be a string literal", fn);
394 return;
397 stree = __get_cur_stree();
398 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
399 if (!strcmp(tmp->name, arg_expr->string->data))
400 print_possible(tmp);
401 } END_FOR_EACH_SM(tmp);
404 static void match_strlen(const char *fn, struct expression *expr, void *info)
406 struct expression *arg;
407 struct range_list *rl = NULL;
408 char *name;
410 arg = get_argument_from_call_expr(expr->args, 0);
411 get_implied_strlen(arg, &rl);
413 name = expr_to_str(arg);
414 sm_msg("strlen: '%s' %s characters", name, show_rl(rl));
415 free_string(name);
418 static void match_buf_size(const char *fn, struct expression *expr, void *info)
420 struct expression *arg, *comp;
421 struct range_list *rl;
422 int elements, bytes;
423 char *name;
424 char buf[256] = "";
425 int limit_type;
426 int n;
427 sval_t sval;
429 arg = get_argument_from_call_expr(expr->args, 0);
431 elements = get_array_size(arg);
432 bytes = get_array_size_bytes_max(arg);
433 rl = get_array_size_bytes_rl(arg);
434 comp = get_size_variable(arg, &limit_type);
436 name = expr_to_str(arg);
437 n = snprintf(buf, sizeof(buf), "buf size: '%s' %d elements, %d bytes", name, elements, bytes);
438 free_string(name);
440 if (!rl_to_sval(rl, &sval))
441 n += snprintf(buf + n, sizeof(buf) - n, " (rl = %s)", show_rl(rl));
443 if (comp) {
444 name = expr_to_str(comp);
445 snprintf(buf + n, sizeof(buf) - n, "[size_var=%s %s]", limit_type_str(limit_type), name);
446 free_string(name);
448 sm_msg("%s", buf);
451 static void match_note(const char *fn, struct expression *expr, void *info)
453 struct expression *arg_expr;
455 arg_expr = get_argument_from_call_expr(expr->args, 0);
456 if (arg_expr->type != EXPR_STRING) {
457 sm_error("the argument to %s is supposed to be a string literal", fn);
458 return;
460 sm_msg("%s", arg_expr->string->data);
463 static void print_related(struct sm_state *sm)
465 struct relation *rel;
467 if (!estate_related(sm->state))
468 return;
470 sm_prefix();
471 sm_printf("%s: ", sm->name);
472 FOR_EACH_PTR(estate_related(sm->state), rel) {
473 sm_printf("%s ", rel->name);
474 } END_FOR_EACH_PTR(rel);
475 sm_printf("\n");
478 static void match_dump_related(const char *fn, struct expression *expr, void *info)
480 struct stree *stree;
481 struct sm_state *tmp;
483 stree = __get_cur_stree();
484 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
485 print_related(tmp);
486 } END_FOR_EACH_SM(tmp);
489 static void match_compare(const char *fn, struct expression *expr, void *info)
491 struct expression *one, *two;
492 char *one_name, *two_name;
493 int comparison;
494 char buf[16];
496 one = get_argument_from_call_expr(expr->args, 0);
497 two = get_argument_from_call_expr(expr->args, 1);
499 comparison = get_comparison(one, two);
500 if (!comparison)
501 snprintf(buf, sizeof(buf), "<none>");
502 else
503 snprintf(buf, sizeof(buf), "%s", show_special(comparison));
505 one_name = expr_to_str(one);
506 two_name = expr_to_str(two);
508 sm_msg("%s %s %s", one_name, buf, two_name);
510 free_string(one_name);
511 free_string(two_name);
514 static void match_debug_on(const char *fn, struct expression *expr, void *info)
516 option_debug = 1;
519 static void match_debug_check(const char *fn, struct expression *expr, void *info)
521 struct expression *arg;
523 arg = get_argument_from_call_expr(expr->args, 0);
524 if (!arg || arg->type != EXPR_STRING)
525 return;
526 option_debug_check = arg->string->data;
527 sm_msg("arg = '%s'", option_debug_check);
530 static void match_debug_var(const char *fn, struct expression *expr, void *info)
532 struct expression *arg;
534 arg = get_argument_from_call_expr(expr->args, 0);
535 if (!arg || arg->type != EXPR_STRING)
536 return;
537 option_debug_var = arg->string->data;
538 sm_msg("debug var = '%s'", option_debug_var);
541 static void match_debug_off(const char *fn, struct expression *expr, void *info)
543 option_debug_check = NULL;
544 option_debug_var = NULL;
545 option_debug = 0;
548 static void match_start_skip(const char *fn, struct expression *expr, void *info)
550 __debug_skip = true;
553 static void match_local_debug_on(const char *fn, struct expression *expr, void *info)
555 local_debug = 1;
556 option_print_names++;
559 static void match_local_debug_off(const char *fn, struct expression *expr, void *info)
561 option_print_names--;
562 local_debug = 0;
565 static void match_debug_db_on(const char *fn, struct expression *expr, void *info)
567 debug_db = 1;
570 static void match_debug_db_off(const char *fn, struct expression *expr, void *info)
572 debug_db = 0;
575 static void match_debug_implied_on(const char *fn, struct expression *expr, void *info)
577 implied_debug = true;
580 static void match_debug_implied_off(const char *fn, struct expression *expr, void *info)
582 implied_debug = false;
585 static void mtag_info(struct expression *expr)
587 mtag_t tag = 0;
588 int offset = 0;
589 struct range_list *rl = NULL;
591 expr_to_mtag_offset(expr, &tag, &offset);
592 get_mtag_rl(expr, &rl);
593 sm_msg("mtag = %llu offset = %d rl = '%s'", tag, offset, show_rl(rl));
596 static void match_about(const char *fn, struct expression *expr, void *info)
598 struct expression *arg;
599 struct range_list *rl;
600 struct sm_state *sm;
601 char *name;
602 int len;
604 sm_msg("---- about ----");
605 match_print_implied(fn, expr, NULL);
606 match_buf_size(fn, expr, NULL);
607 match_strlen(fn, expr, NULL);
608 match_real_absolute(fn, expr, NULL);
609 mtag_info(expr);
611 arg = get_argument_from_call_expr(expr->args, 0);
612 name = expr_to_str(arg);
613 if (!name) {
614 sm_msg("info: not a straight forward variable.");
615 return;
618 if (get_user_rl(arg, &rl))
619 sm_msg("user_rl = '%s'", show_rl(rl));
620 if (points_to_user_data(arg))
621 sm_msg("points to user data");
623 len = strlen(name);
624 FOR_EACH_SM(__get_cur_stree(), sm) {
625 if (strcmp(sm->name, name) == 0)
626 goto print;
627 if (strncmp(sm->name, name, len) == 0 &&
628 sm->name[len] == ':')
629 goto print;
630 continue;
631 print:
632 sm_msg("%s", show_sm(sm));
633 } END_FOR_EACH_SM(sm);
636 static void match_intersection(const char *fn, struct expression *expr, void *info)
638 struct expression *one, *two;
639 struct range_list *one_rl, *two_rl;
640 struct range_list *res;
642 one = get_argument_from_call_expr(expr->args, 0);
643 two = get_argument_from_call_expr(expr->args, 1);
645 get_absolute_rl(one, &one_rl);
646 get_absolute_rl(two, &two_rl);
648 res = rl_intersection(one_rl, two_rl);
649 sm_msg("'%s' intersect '%s' is '%s'", show_rl(one_rl), show_rl(two_rl), show_rl(res));
652 static void match_type(const char *fn, struct expression *expr, void *info)
654 struct expression *one;
655 struct symbol *type;
656 char *name;
658 one = get_argument_from_call_expr(expr->args, 0);
659 type = get_type(one);
660 name = expr_to_str(one);
661 sm_msg("type of '%s' is: '%s'", name, type_to_str(type));
662 free_string(name);
665 static int match_type_rl_return(struct expression *call, void *unused, struct range_list **rl)
667 struct expression *one, *two;
668 struct symbol *type;
670 one = get_argument_from_call_expr(call->args, 0);
671 type = get_type(one);
673 two = get_argument_from_call_expr(call->args, 1);
674 if (!two || two->type != EXPR_STRING) {
675 sm_msg("expected: __smatch_type_rl(type, \"string\")");
676 return 0;
678 call_results_to_rl(call, type, two->string->data, rl);
679 return 1;
682 static void print_left_right(struct sm_state *sm)
684 if (!sm)
685 return;
686 if (!sm->left && !sm->right)
687 return;
689 sm_printf("[ ");
690 if (sm->left)
691 sm_printf("(%d: %s->'%s')", get_stree_id(sm->left->pool), sm->left->name, sm->left->state->name);
692 else
693 sm_printf(" - ");
696 print_left_right(sm->left);
698 if (sm->right)
699 sm_printf("(%d: %s->'%s')", get_stree_id(sm->right->pool), sm->right->name, sm->right->state->name);
700 else
701 sm_printf(" - ");
703 print_left_right(sm->right);
706 static void match_print_merge_tree(const char *fn, struct expression *expr, void *info)
708 struct sm_state *sm;
709 struct expression *arg;
710 char *name;
712 arg = get_argument_from_call_expr(expr->args, 0);
713 name = expr_to_str(arg);
715 sm = get_sm_state_expr(SMATCH_EXTRA, arg);
716 if (!sm) {
717 sm_msg("no sm state for '%s'", name);
718 goto free;
721 sm_prefix();
722 sm_printf("merge tree: %s -> %s", name, sm->state->name);
723 print_left_right(sm);
724 sm_printf("\n");
726 free:
727 free_string(name);
730 static void match_print_stree_id(const char *fn, struct expression *expr, void *info)
732 sm_msg("stree_id %d", __stree_id);
735 static void match_bits(const char *fn, struct expression *expr, void *_unused)
737 struct expression *arg;
738 struct bit_info *info;
739 char *name;
741 arg = get_argument_from_call_expr(expr->args, 0);
742 name = expr_to_str(arg);
744 info = get_bit_info(arg);
746 sm_msg("bit info '%s': definitely set 0x%llx. possibly set 0x%llx.",
747 name, info->set, info->possible);
750 static void match_units(const char *fn, struct expression *expr, void *info)
752 static int units_id;
753 struct expression *arg;
754 struct sm_state *sm;
755 char *name;
757 if (!units_id)
758 units_id = id_from_name("register_units");
760 arg = get_argument_from_call_expr(expr->args, 0);
761 sm = get_sm_state_expr(units_id, arg);
762 name = expr_to_str(arg);
764 sm_msg("units: '%s' '%s'", name, sm ? show_sm(sm) : get_unit_str(arg));
766 free_string(name);
769 static void match_mtag(const char *fn, struct expression *expr, void *info)
771 struct expression *arg;
772 char *name;
773 mtag_t tag = 0;
774 int offset = 0;
776 arg = get_argument_from_call_expr(expr->args, 0);
777 name = expr_to_str(arg);
778 expr_to_mtag_offset(arg, &tag, &offset);
779 sm_msg("mtag: '%s' => tag: %llu %d", name, tag, offset);
780 free_string(name);
783 static void match_mtag_data_offset(const char *fn, struct expression *expr, void *info)
785 struct expression *arg;
786 char *name;
787 mtag_t tag = 0;
788 int offset = -1;
790 arg = get_argument_from_call_expr(expr->args, 0);
791 name = expr_to_str(arg);
792 expr_to_mtag_offset(arg, &tag, &offset);
793 sm_msg("mtag: '%s' => tag: %lld, offset: %d", name, tag, offset);
794 free_string(name);
797 static void match_container(const char *fn, struct expression *expr, void *info)
799 struct expression *container, *x;
800 char *cont, *name, *str;
802 container = get_argument_from_call_expr(expr->args, 0);
803 x = get_argument_from_call_expr(expr->args, 1);
805 str = get_container_name(container, x);
806 cont = expr_to_str(container);
807 name = expr_to_str(x);
808 sm_msg("container: '%s' vs '%s' --> '%s'", cont, name, str);
809 free_string(cont);
810 free_string(name);
813 static void match_param_key(const char *fn, struct expression *expr, void *info)
815 struct expression *arg;
816 const char *key = NULL;
817 char *name;
818 int param;
820 arg = get_argument_from_call_expr(expr->args, 0);
822 param = get_param_key_from_expr(arg, NULL, &key);
824 name = expr_to_str(expr);
825 sm_msg("expr='%s' param=%d key='%s'", name, param, key);
826 free_string(name);
829 static struct timeval debug_timer;
830 static void match_timer_start(const char *fn, struct expression *expr, void *info)
832 gettimeofday(&debug_timer, NULL);
835 static void match_timer_stop(const char *fn, struct expression *expr, void *info)
837 struct timeval stop;
838 long sec, usec, msec;
840 gettimeofday(&stop, NULL);
842 sec = stop.tv_sec - debug_timer.tv_sec;
843 usec = stop.tv_usec - debug_timer.tv_usec;
844 msec = sec * 1000 + usec / 1000;
846 sm_msg("timer: %ld msec", msec);
847 gettimeofday(&debug_timer, NULL);
850 static void match_expr(const char *fn, struct expression *expr, void *info)
852 struct expression *arg, *str, *new;
853 char *name, *new_name;
855 str = get_argument_from_call_expr(expr->args, 0);
856 arg = get_argument_from_call_expr(expr->args, 1);
857 if (!arg || !str)
858 return;
860 if (str->type != EXPR_STRING)
861 return;
863 new = gen_expression_from_key(arg, str->string->data);
864 name = expr_to_str(arg);
865 new_name = expr_to_str(new);
867 sm_msg("str = '%s', arg = '%s' expr = '%s'", str->string->data, name, new_name);
869 free_string(new_name);
870 free_string(name);
872 static void match_state_count(const char *fn, struct expression *expr, void *info)
874 sm_msg("state_count = %d\n", sm_state_counter);
877 static void match_mem(const char *fn, struct expression *expr, void *info)
879 show_sname_alloc();
880 show_data_range_alloc();
881 show_rl_ptrlist_alloc();
882 show_ptrlist_alloc();
883 sm_msg("%lu pools", get_pool_count());
884 sm_msg("%d strees", unfree_stree);
885 show_smatch_state_alloc();
886 show_sm_state_alloc();
889 static void match_exit(const char *fn, struct expression *expr, void *info)
891 exit(0);
894 static struct stree *old_stree;
895 static void trace_var(struct statement *stmt)
897 struct sm_state *sm, *old;
898 int printed = 0;
900 if (!trace_variable)
901 return;
902 if (__inline_fn)
903 return;
905 FOR_EACH_SM(__get_cur_stree(), sm) {
906 if (strcmp(sm->name, trace_variable) != 0)
907 continue;
908 old = get_sm_state_stree(old_stree, sm->owner, sm->name, sm->sym);
909 if (old && old->state == sm->state)
910 continue;
911 sm_msg("[%d] %s '%s': '%s' => '%s'", stmt->type,
912 check_name(sm->owner),
913 sm->name, old ? old->state->name : "<none>", sm->state->name);
914 printed = 1;
915 } END_FOR_EACH_SM(sm);
917 if (printed) {
918 free_stree(&old_stree);
919 old_stree = clone_stree(__get_cur_stree());
923 static void free_old_stree(struct symbol *sym)
925 free_stree(&old_stree);
928 void check_debug(int id)
930 my_id = id;
931 add_function_hook("__smatch_about", &match_about, NULL);
932 add_function_hook("__smatch_all_values", &match_all_values, NULL);
933 add_function_hook("__smatch_state", &match_state, NULL);
934 add_function_hook("__smatch_states", &match_states, NULL);
935 add_function_hook("__smatch_value", &match_print_value, NULL);
936 add_function_hook("__smatch_known", &match_print_known, NULL);
937 add_function_hook("__smatch_implied", &match_print_implied, NULL);
938 add_function_hook("__smatch_implied_min", &match_print_implied_min, NULL);
939 add_function_hook("__smatch_implied_max", &match_print_implied_max, NULL);
940 add_function_hook("__smatch_user_rl", &match_user_rl, NULL);
941 add_function_hook("__smatch_host_rl", &match_host_rl, NULL);
942 add_function_hook("__smatch_capped", &match_capped, NULL);
943 add_function_hook("__smatch_hard_max", &match_print_hard_max, NULL);
944 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max, NULL);
945 add_function_hook("__smatch_absolute", &match_print_absolute, NULL);
946 add_function_hook("__smatch_absolute_min", &match_print_absolute_min, NULL);
947 add_function_hook("__smatch_absolute_max", &match_print_absolute_max, NULL);
948 add_function_hook("__smatch_real_absolute", &match_real_absolute, NULL);
949 add_function_hook("__smatch_sval_info", &match_sval_info, NULL);
950 add_function_hook("__smatch_member_name", &match_member_name, NULL);
951 add_function_hook("__smatch_possible", &match_possible, NULL);
952 add_function_hook("__smatch_cur_stree", &match_cur_stree, NULL);
953 add_function_hook("__smatch_strlen", &match_strlen, NULL);
954 add_function_hook("__smatch_buf_size", &match_buf_size, NULL);
955 add_function_hook("__smatch_note", &match_note, NULL);
956 add_function_hook("__smatch_dump_related", &match_dump_related, NULL);
957 add_function_hook("__smatch_compare", &match_compare, NULL);
958 add_function_hook("__smatch_debug_on", &match_debug_on, NULL);
959 add_function_hook("__smatch_debug_check", &match_debug_check, NULL);
960 add_function_hook("__smatch_debug_var", &match_debug_var, NULL);
961 add_function_hook("__smatch_debug_off", &match_debug_off, NULL);
962 add_function_hook("__smatch_start_skip", &match_start_skip, NULL);
963 add_function_hook("__smatch_local_debug_on", &match_local_debug_on, NULL);
964 add_function_hook("__smatch_local_debug_off", &match_local_debug_off, NULL);
965 add_function_hook("__smatch_debug_db_on", &match_debug_db_on, NULL);
966 add_function_hook("__smatch_debug_db_off", &match_debug_db_off, NULL);
967 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on, NULL);
968 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off, NULL);
969 add_function_hook("__smatch_intersection", &match_intersection, NULL);
970 add_function_hook("__smatch_type", &match_type, NULL);
971 add_implied_return_hook("__smatch_type_rl_helper", &match_type_rl_return, NULL);
972 add_function_hook("__smatch_merge_tree", &match_print_merge_tree, NULL);
973 add_function_hook("__smatch_stree_id", &match_print_stree_id, NULL);
974 add_function_hook("__smatch_bits", &match_bits, NULL);
975 add_function_hook("__smatch_mtag", &match_mtag, NULL);
976 add_function_hook("__smatch_mtag_data", &match_mtag_data_offset, NULL);
977 add_function_hook("__smatch_expr", &match_expr, NULL);
978 add_function_hook("__smatch_state_count", &match_state_count, NULL);
979 add_function_hook("__smatch_mem", &match_mem, NULL);
980 add_function_hook("__smatch_exit", &match_exit, NULL);
981 add_function_hook("__smatch_units", &match_units, NULL);
982 add_function_hook("__smatch_container", &match_container, NULL);
983 add_function_hook("__smatch_param_key", &match_param_key, NULL);
984 add_function_hook("__smatch_timer_start", &match_timer_start, NULL);
985 add_function_hook("__smatch_timer_stop", &match_timer_stop, NULL);
987 add_hook(free_old_stree, AFTER_FUNC_HOOK);
988 add_hook(trace_var, STMT_HOOK_AFTER);