check_kernel_printf.c: handle new definition of KERN_CONT
[smatch.git] / check_debug.c
blob752ac52b73f0a92cde3fd6df721f868c44125001
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", 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_msg("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_print_hard_max(const char *fn, struct expression *expr, void *info)
215 struct expression *arg;
216 sval_t sval;
217 char *name;
219 arg = get_argument_from_call_expr(expr->args, 0);
220 name = expr_to_str(arg);
222 if (get_hard_max(arg, &sval))
223 sm_msg("hard max: %s = %s", name, sval_to_str(sval));
224 else
225 sm_msg("hard max: %s = <unknown>", name);
227 free_string(name);
230 static void match_print_fuzzy_max(const char *fn, struct expression *expr, void *info)
232 struct expression *arg;
233 sval_t sval;
234 char *name;
236 arg = get_argument_from_call_expr(expr->args, 0);
237 name = expr_to_str(arg);
239 if (get_fuzzy_max(arg, &sval))
240 sm_msg("fuzzy max: %s = %s", name, sval_to_str(sval));
241 else
242 sm_msg("fuzzy max: %s = <unknown>", name);
244 free_string(name);
247 static void match_print_absolute(const char *fn, struct expression *expr, void *info)
249 struct expression *arg;
250 struct range_list *rl;
251 char *name;
253 arg = get_argument_from_call_expr(expr->args, 0);
254 name = expr_to_str(arg);
256 get_absolute_rl(arg, &rl);
257 sm_msg("absolute: %s = %s", name, show_rl(rl));
259 free_string(name);
262 static void match_print_absolute_min(const char *fn, struct expression *expr, void *info)
264 struct expression *arg;
265 sval_t sval;
266 char *name;
268 arg = get_argument_from_call_expr(expr->args, 0);
269 name = expr_to_str(arg);
271 if (get_absolute_min(arg, &sval))
272 sm_msg("absolute min: %s = %s", name, sval_to_str(sval));
273 else
274 sm_msg("absolute min: %s = <unknown>", name);
276 free_string(name);
279 static void match_print_absolute_max(const char *fn, struct expression *expr, void *info)
281 struct expression *arg;
282 sval_t sval;
283 char *name;
285 arg = get_argument_from_call_expr(expr->args, 0);
286 get_absolute_max(arg, &sval);
288 name = expr_to_str(arg);
289 sm_msg("absolute max: %s = %s", name, sval_to_str(sval));
290 free_string(name);
293 static void match_sval_info(const char *fn, struct expression *expr, void *info)
295 struct expression *arg;
296 sval_t sval;
297 char *name;
299 arg = get_argument_from_call_expr(expr->args, 0);
300 name = expr_to_str(arg);
302 if (!get_implied_value(arg, &sval)) {
303 sm_msg("no sval for '%s'", name);
304 goto free;
307 sm_msg("implied: %s %c%d ->value = %llx", name, sval_unsigned(sval) ? 'u' : 's', sval_bits(sval), sval.value);
308 free:
309 free_string(name);
312 static void match_member_name(const char *fn, struct expression *expr, void *info)
314 struct expression *arg;
315 char *name, *member_name;
317 arg = get_argument_from_call_expr(expr->args, 0);
318 name = expr_to_str(arg);
319 member_name = get_member_name(arg);
320 sm_msg("member name: '%s => %s'", name, member_name);
321 free_string(member_name);
322 free_string(name);
325 static void print_possible(struct sm_state *sm)
327 struct sm_state *tmp;
329 sm_msg("Possible values for %s", sm->name);
330 FOR_EACH_PTR(sm->possible, tmp) {
331 printf("%s\n", tmp->state->name);
332 } END_FOR_EACH_PTR(tmp);
333 sm_msg("===");
336 static void match_possible(const char *fn, struct expression *expr, void *info)
338 struct stree *stree;
339 struct sm_state *tmp;
340 struct expression *arg_expr;
342 arg_expr = get_argument_from_call_expr(expr->args, 0);
343 if (arg_expr->type != EXPR_STRING) {
344 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
345 return;
348 stree = __get_cur_stree();
349 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
350 if (!strcmp(tmp->name, arg_expr->string->data))
351 print_possible(tmp);
352 } END_FOR_EACH_SM(tmp);
355 static void match_strlen(const char *fn, struct expression *expr, void *info)
357 struct expression *arg;
358 struct range_list *rl;
359 char *name;
361 arg = get_argument_from_call_expr(expr->args, 0);
362 get_implied_strlen(arg, &rl);
364 name = expr_to_str(arg);
365 sm_msg("strlen: '%s' %s characters", name, show_rl(rl));
366 free_string(name);
369 static void match_buf_size(const char *fn, struct expression *expr, void *info)
371 struct expression *arg;
372 int elements, bytes;
373 char *name;
375 arg = get_argument_from_call_expr(expr->args, 0);
376 elements = get_array_size(arg);
377 bytes = get_array_size_bytes_max(arg);
379 name = expr_to_str(arg);
380 sm_msg("buf size: '%s' %d elements, %d bytes", name, elements, bytes);
381 free_string(name);
384 static void match_buf_size_rl(const char *fn, struct expression *expr, void *info)
386 struct expression *arg;
387 struct range_list *rl;
388 int elements, bytes;
389 char *name;
391 arg = get_argument_from_call_expr(expr->args, 0);
392 rl = get_array_size_bytes_rl(arg);
393 elements = get_array_size(arg);
394 bytes = get_array_size_bytes(arg);
396 name = expr_to_str(arg);
397 sm_msg("buf size: '%s' %s %d elements, %d bytes", name, show_rl(rl), elements, bytes);
398 free_string(name);
401 static void match_note(const char *fn, struct expression *expr, void *info)
403 struct expression *arg_expr;
405 arg_expr = get_argument_from_call_expr(expr->args, 0);
406 if (arg_expr->type != EXPR_STRING) {
407 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
408 return;
410 sm_msg("%s", arg_expr->string->data);
413 static void print_related(struct sm_state *sm)
415 struct relation *rel;
417 if (!estate_related(sm->state))
418 return;
420 sm_prefix();
421 sm_printf("%s: ", sm->name);
422 FOR_EACH_PTR(estate_related(sm->state), rel) {
423 sm_printf("%s ", rel->name);
424 } END_FOR_EACH_PTR(rel);
425 sm_printf("\n");
428 static void match_dump_related(const char *fn, struct expression *expr, void *info)
430 struct stree *stree;
431 struct sm_state *tmp;
433 stree = __get_cur_stree();
434 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
435 print_related(tmp);
436 } END_FOR_EACH_SM(tmp);
439 static void match_compare(const char *fn, struct expression *expr, void *info)
441 struct expression *one, *two;
442 char *one_name, *two_name;
443 int comparison;
444 char buf[16];
446 one = get_argument_from_call_expr(expr->args, 0);
447 two = get_argument_from_call_expr(expr->args, 1);
449 comparison = get_comparison(one, two);
450 if (!comparison)
451 snprintf(buf, sizeof(buf), "<none>");
452 else
453 snprintf(buf, sizeof(buf), "%s", show_special(comparison));
455 one_name = expr_to_str(one);
456 two_name = expr_to_str(two);
458 sm_msg("%s %s %s", one_name, buf, two_name);
460 free_string(one_name);
461 free_string(two_name);
464 static void match_debug_on(const char *fn, struct expression *expr, void *info)
466 option_debug = 1;
469 static void match_debug_check(const char *fn, struct expression *expr, void *info)
471 struct expression *arg;
473 arg = get_argument_from_call_expr(expr->args, 0);
474 if (!arg || arg->type != EXPR_STRING)
475 return;
476 option_debug_check = arg->string->data;
477 sm_msg("arg = '%s'", option_debug_check);
480 static void match_debug_off(const char *fn, struct expression *expr, void *info)
482 option_debug_check = (char *)"";
483 option_debug = 0;
486 static void match_local_debug_on(const char *fn, struct expression *expr, void *info)
488 local_debug = 1;
491 static void match_local_debug_off(const char *fn, struct expression *expr, void *info)
493 local_debug = 0;
496 static void match_debug_implied_on(const char *fn, struct expression *expr, void *info)
498 option_debug_implied = 1;
501 static void match_debug_implied_off(const char *fn, struct expression *expr, void *info)
503 option_debug_implied = 0;
506 static void match_about(const char *fn, struct expression *expr, void *info)
508 struct expression *arg;
509 struct sm_state *sm;
510 char *name;
512 sm_msg("---- about ----");
513 match_print_implied(fn, expr, NULL);
514 match_buf_size(fn, expr, NULL);
515 match_strlen(fn, expr, NULL);
516 match_real_absolute(fn, expr, NULL);
518 arg = get_argument_from_call_expr(expr->args, 0);
519 name = expr_to_str(arg);
520 if (!name) {
521 sm_msg("info: not a straight forward variable.");
522 return;
525 FOR_EACH_SM(__get_cur_stree(), sm) {
526 if (strcmp(sm->name, name) != 0)
527 continue;
528 sm_msg("%s", show_sm(sm));
529 } END_FOR_EACH_SM(sm);
532 static void match_intersection(const char *fn, struct expression *expr, void *info)
534 struct expression *one, *two;
535 struct range_list *one_rl, *two_rl;
536 struct range_list *res;
538 one = get_argument_from_call_expr(expr->args, 0);
539 two = get_argument_from_call_expr(expr->args, 1);
541 get_absolute_rl(one, &one_rl);
542 get_absolute_rl(two, &two_rl);
544 res = rl_intersection(one_rl, two_rl);
545 sm_msg("'%s' intersect '%s' is '%s'", show_rl(one_rl), show_rl(two_rl), show_rl(res));
548 static void match_type(const char *fn, struct expression *expr, void *info)
550 struct expression *one;
551 struct symbol *type;
552 char *name;
554 one = get_argument_from_call_expr(expr->args, 0);
555 type = get_type(one);
556 name = expr_to_str(one);
557 sm_msg("type of '%s' is: '%s'", name, type_to_str(type));
558 free_string(name);
561 static void match_type_rl(const char *fn, struct expression *expr, void *info)
563 struct expression *one, *two;
564 struct symbol *type;
565 struct range_list *rl;
567 one = get_argument_from_call_expr(expr->args, 0);
568 type = get_type(one);
570 two = get_argument_from_call_expr(expr->args, 1);
571 if (!two || two->type != EXPR_STRING) {
572 sm_msg("expected: __smatch_type_rl(type, \"string\")");
573 return;
575 call_results_to_rl(expr, type, two->string->data, &rl);
576 sm_msg("'%s' => '%s'", two->string->data, show_rl(rl));
579 static struct stree *old_stree;
580 static void trace_var(struct statement *stmt)
582 struct sm_state *sm, *old;
584 if (!trace_variable)
585 return;
586 if (__inline_fn)
587 return;
589 FOR_EACH_SM(__get_cur_stree(), sm) {
590 if (strcmp(sm->name, trace_variable) != 0)
591 continue;
592 old = get_sm_state_stree(old_stree, sm->owner, sm->name, sm->sym);
593 if (old && old->state == sm->state)
594 continue;
595 sm_msg("[%d] %s '%s': '%s' => '%s'", stmt->type,
596 check_name(sm->owner),
597 sm->name, old ? old->state->name : "<none>", sm->state->name);
598 } END_FOR_EACH_SM(sm);
600 free_stree(&old_stree);
601 old_stree = clone_stree(__get_cur_stree());
604 static void free_old_stree(struct symbol *sym)
606 free_stree(&old_stree);
609 void check_debug(int id)
611 my_id = id;
612 add_function_hook("__smatch_about", &match_about, NULL);
613 add_function_hook("__smatch_all_values", &match_all_values, NULL);
614 add_function_hook("__smatch_state", &match_state, NULL);
615 add_function_hook("__smatch_states", &match_states, NULL);
616 add_function_hook("__smatch_value", &match_print_value, NULL);
617 add_function_hook("__smatch_known", &match_print_known, NULL);
618 add_function_hook("__smatch_implied", &match_print_implied, NULL);
619 add_function_hook("__smatch_implied_min", &match_print_implied_min, NULL);
620 add_function_hook("__smatch_implied_max", &match_print_implied_max, NULL);
621 add_function_hook("__smatch_user_rl", &match_user_rl, NULL);
622 add_function_hook("__smatch_hard_max", &match_print_hard_max, NULL);
623 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max, NULL);
624 add_function_hook("__smatch_absolute", &match_print_absolute, NULL);
625 add_function_hook("__smatch_absolute_min", &match_print_absolute_min, NULL);
626 add_function_hook("__smatch_absolute_max", &match_print_absolute_max, NULL);
627 add_function_hook("__smatch_real_absolute", &match_real_absolute, NULL);
628 add_function_hook("__smatch_sval_info", &match_sval_info, NULL);
629 add_function_hook("__smatch_member_name", &match_member_name, NULL);
630 add_function_hook("__smatch_possible", &match_possible, NULL);
631 add_function_hook("__smatch_cur_stree", &match_cur_stree, NULL);
632 add_function_hook("__smatch_strlen", &match_strlen, NULL);
633 add_function_hook("__smatch_buf_size", &match_buf_size, NULL);
634 add_function_hook("__smatch_buf_size_rl", &match_buf_size_rl, NULL);
635 add_function_hook("__smatch_note", &match_note, NULL);
636 add_function_hook("__smatch_dump_related", &match_dump_related, NULL);
637 add_function_hook("__smatch_compare", &match_compare, NULL);
638 add_function_hook("__smatch_debug_on", &match_debug_on, NULL);
639 add_function_hook("__smatch_debug_check", &match_debug_check, NULL);
640 add_function_hook("__smatch_debug_off", &match_debug_off, NULL);
641 add_function_hook("__smatch_local_debug_on", &match_local_debug_on, NULL);
642 add_function_hook("__smatch_local_debug_off", &match_local_debug_off, NULL);
643 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on, NULL);
644 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off, NULL);
645 add_function_hook("__smatch_intersection", &match_intersection, NULL);
646 add_function_hook("__smatch_type", &match_type, NULL);
647 add_function_hook("__smatch_type_rl_helper", match_type_rl, NULL);
649 add_hook(free_old_stree, END_FUNC_HOOK);
650 add_hook(trace_var, STMT_HOOK_AFTER);