slist: put get_sm_state_stree_stack() in the .h file
[smatch.git] / check_debug.c
blob02932674184797bd05ebcd8f81c9c26f24398b02
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;
25 static void match_all_values(const char *fn, struct expression *expr, void *info)
27 struct stree *stree;
29 stree = get_all_states_stree(SMATCH_EXTRA);
30 __print_stree(stree);
31 free_stree(&stree);
34 static void match_cur_stree(const char *fn, struct expression *expr, void *info)
36 __print_cur_stree();
39 static void match_state(const char *fn, struct expression *expr, void *info)
41 struct expression *check_arg, *state_arg;
42 struct sm_state *sm;
43 int found = 0;
45 check_arg = get_argument_from_call_expr(expr->args, 0);
46 if (check_arg->type != EXPR_STRING) {
47 sm_msg("error: the check_name argument to %s is supposed to be a string literal", fn);
48 return;
50 state_arg = get_argument_from_call_expr(expr->args, 1);
51 if (!state_arg || state_arg->type != EXPR_STRING) {
52 sm_msg("error: the state_name argument to %s is supposed to be a string literal", fn);
53 return;
56 FOR_EACH_SM(__get_cur_stree(), sm) {
57 if (strcmp(check_name(sm->owner), check_arg->string->data) != 0)
58 continue;
59 if (strcmp(sm->name, state_arg->string->data) != 0)
60 continue;
61 sm_msg("'%s' = '%s'", sm->name, sm->state->name);
62 found = 1;
63 } END_FOR_EACH_SM(sm);
65 if (!found)
66 sm_msg("%s '%s' not found", check_arg->string->data, state_arg->string->data);
69 static void match_states(const char *fn, struct expression *expr, void *info)
71 struct expression *check_arg;
72 struct sm_state *sm;
73 int found = 0;
75 check_arg = get_argument_from_call_expr(expr->args, 0);
76 if (check_arg->type != EXPR_STRING) {
77 sm_msg("error: the check_name argument to %s is supposed to be a string literal", fn);
78 return;
81 FOR_EACH_SM(__get_cur_stree(), sm) {
82 if (strcmp(check_name(sm->owner), check_arg->string->data) != 0)
83 continue;
84 sm_msg("'%s' = '%s'", sm->name, sm->state->name);
85 found = 1;
86 } END_FOR_EACH_SM(sm);
88 if (!found)
89 sm_msg("%s: no states", check_arg->string->data);
92 static void match_print_value(const char *fn, struct expression *expr, void *info)
94 struct stree *stree;
95 struct sm_state *tmp;
96 struct expression *arg_expr;
98 arg_expr = get_argument_from_call_expr(expr->args, 0);
99 if (arg_expr->type != EXPR_STRING) {
100 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
101 return;
104 stree = __get_cur_stree();
105 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
106 if (!strcmp(tmp->name, arg_expr->string->data))
107 sm_msg("%s = %s", tmp->name, tmp->state->name);
108 } END_FOR_EACH_SM(tmp);
111 static void match_print_implied(const char *fn, struct expression *expr, void *info)
113 struct expression *arg;
114 struct range_list *rl = NULL;
115 char *name;
117 arg = get_argument_from_call_expr(expr->args, 0);
118 get_implied_rl(arg, &rl);
120 name = expr_to_str(arg);
121 sm_msg("implied: %s = '%s'", name, show_rl(rl));
122 free_string(name);
125 static void match_print_implied_min(const char *fn, struct expression *expr, void *info)
127 struct expression *arg;
128 sval_t sval;
129 char *name;
131 arg = get_argument_from_call_expr(expr->args, 0);
132 name = expr_to_str(arg);
134 if (get_implied_min(arg, &sval))
135 sm_msg("implied min: %s = %s", name, sval_to_str(sval));
136 else
137 sm_msg("implied min: %s = <unknown>", name);
139 free_string(name);
142 static void match_print_implied_max(const char *fn, struct expression *expr, void *info)
144 struct expression *arg;
145 sval_t sval;
146 char *name;
148 arg = get_argument_from_call_expr(expr->args, 0);
149 name = expr_to_str(arg);
151 if (get_implied_max(arg, &sval))
152 sm_msg("implied max: %s = %s", name, sval_to_str(sval));
153 else
154 sm_msg("implied max: %s = <unknown>", name);
156 free_string(name);
159 static void match_print_hard_max(const char *fn, struct expression *expr, void *info)
161 struct expression *arg;
162 sval_t sval;
163 char *name;
165 arg = get_argument_from_call_expr(expr->args, 0);
166 name = expr_to_str(arg);
168 if (get_hard_max(arg, &sval))
169 sm_msg("hard max: %s = %s", name, sval_to_str(sval));
170 else
171 sm_msg("hard max: %s = <unknown>", name);
173 free_string(name);
176 static void match_print_fuzzy_max(const char *fn, struct expression *expr, void *info)
178 struct expression *arg;
179 sval_t sval;
180 char *name;
182 arg = get_argument_from_call_expr(expr->args, 0);
183 name = expr_to_str(arg);
185 if (get_fuzzy_max(arg, &sval))
186 sm_msg("fuzzy max: %s = %s", name, sval_to_str(sval));
187 else
188 sm_msg("fuzzy max: %s = <unknown>", name);
190 free_string(name);
193 static void match_print_absolute_min(const char *fn, struct expression *expr, void *info)
195 struct expression *arg;
196 sval_t sval;
197 char *name;
199 arg = get_argument_from_call_expr(expr->args, 0);
200 name = expr_to_str(arg);
202 if (get_absolute_min(arg, &sval))
203 sm_msg("absolute min: %s = %s", name, sval_to_str(sval));
204 else
205 sm_msg("absolute min: %s = <unknown>", name);
207 free_string(name);
210 static void match_print_absolute_max(const char *fn, struct expression *expr, void *info)
212 struct expression *arg;
213 sval_t sval;
214 char *name;
216 arg = get_argument_from_call_expr(expr->args, 0);
217 get_absolute_max(arg, &sval);
219 name = expr_to_str(arg);
220 sm_msg("absolute max: %s = %s", name, sval_to_str(sval));
221 free_string(name);
224 static void match_sval_info(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_implied_value(arg, &sval)) {
234 sm_msg("no sval for '%s'", name);
235 goto free;
238 sm_msg("implied: %s %c%d ->value = %llx", name, sval_unsigned(sval) ? 'u' : 's', sval_bits(sval), sval.value);
239 free:
240 free_string(name);
243 static void match_member_name(const char *fn, struct expression *expr, void *info)
245 struct expression *arg;
246 char *name, *member_name;
248 arg = get_argument_from_call_expr(expr->args, 0);
249 name = expr_to_str(arg);
250 member_name = get_member_name(arg);
251 sm_msg("member name: '%s => %s'", name, member_name);
252 free_string(member_name);
253 free_string(name);
256 static void print_possible(struct sm_state *sm)
258 struct sm_state *tmp;
260 sm_msg("Possible values for %s", sm->name);
261 FOR_EACH_PTR(sm->possible, tmp) {
262 printf("%s\n", tmp->state->name);
263 } END_FOR_EACH_PTR(tmp);
264 sm_msg("===");
267 static void match_possible(const char *fn, struct expression *expr, void *info)
269 struct stree *stree;
270 struct sm_state *tmp;
271 struct expression *arg_expr;
273 arg_expr = get_argument_from_call_expr(expr->args, 0);
274 if (arg_expr->type != EXPR_STRING) {
275 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
276 return;
279 stree = __get_cur_stree();
280 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
281 if (!strcmp(tmp->name, arg_expr->string->data))
282 print_possible(tmp);
283 } END_FOR_EACH_SM(tmp);
286 static void match_strlen(const char *fn, struct expression *expr, void *info)
288 struct expression *arg;
289 struct range_list *rl;
290 char *name;
292 arg = get_argument_from_call_expr(expr->args, 0);
293 get_implied_strlen(arg, &rl);
295 name = expr_to_str(arg);
296 sm_msg("strlen: '%s' %s characters", name, show_rl(rl));
297 free_string(name);
300 static void match_buf_size(const char *fn, struct expression *expr, void *info)
302 struct expression *arg;
303 int elements, bytes;
304 char *name;
306 arg = get_argument_from_call_expr(expr->args, 0);
307 elements = get_array_size(arg);
308 bytes = get_array_size_bytes(arg);
310 name = expr_to_str(arg);
311 sm_msg("buf size: '%s' %d elements, %d bytes", name, elements, bytes);
312 free_string(name);
315 static void match_buf_size_rl(const char *fn, struct expression *expr, void *info)
317 struct expression *arg;
318 struct range_list *rl;
319 int elements, bytes;
320 char *name;
322 arg = get_argument_from_call_expr(expr->args, 0);
323 rl = get_array_size_bytes_rl(arg);
324 elements = get_array_size(arg);
325 bytes = get_array_size_bytes(arg);
327 name = expr_to_str(arg);
328 sm_msg("buf size: '%s' %s %d elements, %d bytes", name, show_rl(rl), elements, bytes);
329 free_string(name);
332 static void match_note(const char *fn, struct expression *expr, void *info)
334 struct expression *arg_expr;
336 arg_expr = get_argument_from_call_expr(expr->args, 0);
337 if (arg_expr->type != EXPR_STRING) {
338 sm_msg("error: the argument to %s is supposed to be a string literal", fn);
339 return;
341 sm_msg("%s", arg_expr->string->data);
344 static void print_related(struct sm_state *sm)
346 struct relation *rel;
348 if (!estate_related(sm->state))
349 return;
351 sm_prefix();
352 sm_printf("%s: ", sm->name);
353 FOR_EACH_PTR(estate_related(sm->state), rel) {
354 sm_printf("%s ", rel->name);
355 } END_FOR_EACH_PTR(rel);
356 sm_printf("\n");
359 static void match_dump_related(const char *fn, struct expression *expr, void *info)
361 struct stree *stree;
362 struct sm_state *tmp;
364 stree = __get_cur_stree();
365 FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
366 print_related(tmp);
367 } END_FOR_EACH_SM(tmp);
370 static void match_compare(const char *fn, struct expression *expr, void *info)
372 struct expression *one, *two;
373 char *one_name, *two_name;
374 int comparison;
375 char buf[16];
377 one = get_argument_from_call_expr(expr->args, 0);
378 two = get_argument_from_call_expr(expr->args, 1);
380 comparison = get_comparison(one, two);
381 if (!comparison)
382 snprintf(buf, sizeof(buf), "<none>");
383 else
384 snprintf(buf, sizeof(buf), "%s", show_special(comparison));
386 one_name = expr_to_str(one);
387 two_name = expr_to_str(two);
389 sm_msg("%s %s %s", one_name, buf, two_name);
391 free_string(one_name);
392 free_string(two_name);
395 static void match_debug_on(const char *fn, struct expression *expr, void *info)
397 option_debug = 1;
400 static void match_debug_check(const char *fn, struct expression *expr, void *info)
402 struct expression *arg;
404 arg = get_argument_from_call_expr(expr->args, 0);
405 if (!arg || arg->type != EXPR_STRING)
406 return;
407 option_debug_check = arg->string->data;
408 sm_msg("arg = '%s'", option_debug_check);
411 static void match_debug_off(const char *fn, struct expression *expr, void *info)
413 option_debug_check = (char *)"";
414 option_debug = 0;
417 static void match_local_debug_on(const char *fn, struct expression *expr, void *info)
419 local_debug = 1;
422 static void match_local_debug_off(const char *fn, struct expression *expr, void *info)
424 local_debug = 0;
427 static void match_debug_implied_on(const char *fn, struct expression *expr, void *info)
429 option_debug_implied = 1;
432 static void match_debug_implied_off(const char *fn, struct expression *expr, void *info)
434 option_debug_implied = 0;
437 static void match_about(const char *fn, struct expression *expr, void *info)
439 struct expression *arg;
440 struct sm_state *sm;
441 char *name;
443 sm_msg("---- about ----");
444 match_print_implied(fn, expr, NULL);
445 match_buf_size(fn, expr, NULL);
446 match_strlen(fn, expr, NULL);
448 arg = get_argument_from_call_expr(expr->args, 0);
449 name = expr_to_str(arg);
450 if (!name) {
451 sm_msg("info: not a straight forward variable.");
452 return;
455 FOR_EACH_SM(__get_cur_stree(), sm) {
456 if (strcmp(sm->name, name) != 0)
457 continue;
458 sm_msg("%s '%s' = '%s'", check_name(sm->owner), sm->name, sm->state->name);
459 } END_FOR_EACH_SM(sm);
462 void check_debug(int id)
464 my_id = id;
465 add_function_hook("__smatch_about", &match_about, NULL);
466 add_function_hook("__smatch_all_values", &match_all_values, NULL);
467 add_function_hook("__smatch_state", &match_state, NULL);
468 add_function_hook("__smatch_states", &match_states, NULL);
469 add_function_hook("__smatch_value", &match_print_value, NULL);
470 add_function_hook("__smatch_implied", &match_print_implied, NULL);
471 add_function_hook("__smatch_implied_min", &match_print_implied_min, NULL);
472 add_function_hook("__smatch_implied_max", &match_print_implied_max, NULL);
473 add_function_hook("__smatch_hard_max", &match_print_hard_max, NULL);
474 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max, NULL);
475 add_function_hook("__smatch_absolute_min", &match_print_absolute_min, NULL);
476 add_function_hook("__smatch_absolute_max", &match_print_absolute_max, NULL);
477 add_function_hook("__smatch_sval_info", &match_sval_info, NULL);
478 add_function_hook("__smatch_member_name", &match_member_name, NULL);
479 add_function_hook("__smatch_possible", &match_possible, NULL);
480 add_function_hook("__smatch_cur_stree", &match_cur_stree, NULL);
481 add_function_hook("__smatch_strlen", &match_strlen, NULL);
482 add_function_hook("__smatch_buf_size", &match_buf_size, NULL);
483 add_function_hook("__smatch_buf_size_rl", &match_buf_size_rl, NULL);
484 add_function_hook("__smatch_note", &match_note, NULL);
485 add_function_hook("__smatch_dump_related", &match_dump_related, NULL);
486 add_function_hook("__smatch_compare", &match_compare, NULL);
487 add_function_hook("__smatch_debug_on", &match_debug_on, NULL);
488 add_function_hook("__smatch_debug_check", &match_debug_check, NULL);
489 add_function_hook("__smatch_debug_off", &match_debug_off, NULL);
490 add_function_hook("__smatch_local_debug_on", &match_local_debug_on, NULL);
491 add_function_hook("__smatch_local_debug_off", &match_local_debug_off, NULL);
492 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on, NULL);
493 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off, NULL);