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
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
26 static void match_all_values(const char *fn
, struct expression
*expr
, void *info
)
30 stree
= get_all_states_stree(SMATCH_EXTRA
);
35 static void match_cur_stree(const char *fn
, struct expression
*expr
, void *info
)
40 static void match_state(const char *fn
, struct expression
*expr
, void *info
)
42 struct expression
*check_arg
, *state_arg
;
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
);
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
);
57 FOR_EACH_SM(__get_cur_stree(), sm
) {
58 if (strcmp(check_name(sm
->owner
), check_arg
->string
->data
) != 0)
60 if (strcmp(sm
->name
, state_arg
->string
->data
) != 0)
62 sm_msg("'%s' = '%s'", sm
->name
, sm
->state
->name
);
64 } END_FOR_EACH_SM(sm
);
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
;
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
);
82 FOR_EACH_SM(__get_cur_stree(), sm
) {
83 if (strcmp(check_name(sm
->owner
), check_arg
->string
->data
) != 0)
85 sm_msg("%s", show_sm(sm
));
87 } END_FOR_EACH_SM(sm
);
90 sm_msg("%s: no states", check_arg
->string
->data
);
93 static void match_print_value(const char *fn
, struct expression
*expr
, void *info
)
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
);
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_known(const char *fn
, struct expression
*expr
, void *info
)
114 struct expression
*arg
;
115 struct range_list
*rl
= NULL
;
120 arg
= get_argument_from_call_expr(expr
->args
, 0);
121 if (get_value(arg
, &sval
))
124 get_implied_rl(arg
, &rl
);
126 name
= expr_to_str(arg
);
127 sm_msg("known: '%s' = '%s'. implied = '%s'", name
, known
? sval_to_str(sval
) : "<unknown>", show_rl(rl
));
131 static void match_print_implied(const char *fn
, struct expression
*expr
, void *info
)
133 struct expression
*arg
;
134 struct range_list
*rl
= NULL
;
137 arg
= get_argument_from_call_expr(expr
->args
, 0);
138 get_implied_rl(arg
, &rl
);
140 name
= expr_to_str(arg
);
141 sm_msg("implied: %s = '%s'", name
, show_rl(rl
));
145 static void match_real_absolute(const char *fn
, struct expression
*expr
, void *info
)
147 struct expression
*arg
;
148 struct range_list
*rl
= NULL
;
151 arg
= get_argument_from_call_expr(expr
->args
, 0);
152 get_real_absolute_rl(arg
, &rl
);
154 name
= expr_to_str(arg
);
155 sm_msg("real absolute: %s = '%s'", name
, show_rl(rl
));
159 static void match_print_implied_min(const char *fn
, struct expression
*expr
, void *info
)
161 struct expression
*arg
;
165 arg
= get_argument_from_call_expr(expr
->args
, 0);
166 name
= expr_to_str(arg
);
168 if (get_implied_min(arg
, &sval
))
169 sm_msg("implied min: %s = %s", name
, sval_to_str(sval
));
171 sm_msg("implied min: %s = <unknown>", name
);
176 static void match_print_implied_max(const char *fn
, struct expression
*expr
, void *info
)
178 struct expression
*arg
;
182 arg
= get_argument_from_call_expr(expr
->args
, 0);
183 name
= expr_to_str(arg
);
185 if (get_implied_max(arg
, &sval
))
186 sm_msg("implied max: %s = %s", name
, sval_to_str(sval
));
188 sm_msg("implied max: %s = <unknown>", name
);
193 static void match_user_rl(const char *fn
, struct expression
*expr
, void *info
)
195 struct expression
*arg
;
196 struct range_list
*rl
;
199 arg
= get_argument_from_call_expr(expr
->args
, 0);
200 name
= expr_to_str(arg
);
202 get_user_rl(arg
, &rl
);
203 sm_msg("user rl: '%s' = '%s'", name
, show_rl(rl
));
208 static void match_print_hard_max(const char *fn
, struct expression
*expr
, void *info
)
210 struct expression
*arg
;
214 arg
= get_argument_from_call_expr(expr
->args
, 0);
215 name
= expr_to_str(arg
);
217 if (get_hard_max(arg
, &sval
))
218 sm_msg("hard max: %s = %s", name
, sval_to_str(sval
));
220 sm_msg("hard max: %s = <unknown>", name
);
225 static void match_print_fuzzy_max(const char *fn
, struct expression
*expr
, void *info
)
227 struct expression
*arg
;
231 arg
= get_argument_from_call_expr(expr
->args
, 0);
232 name
= expr_to_str(arg
);
234 if (get_fuzzy_max(arg
, &sval
))
235 sm_msg("fuzzy max: %s = %s", name
, sval_to_str(sval
));
237 sm_msg("fuzzy max: %s = <unknown>", name
);
242 static void match_print_absolute_min(const char *fn
, struct expression
*expr
, void *info
)
244 struct expression
*arg
;
248 arg
= get_argument_from_call_expr(expr
->args
, 0);
249 name
= expr_to_str(arg
);
251 if (get_absolute_min(arg
, &sval
))
252 sm_msg("absolute min: %s = %s", name
, sval_to_str(sval
));
254 sm_msg("absolute min: %s = <unknown>", name
);
259 static void match_print_absolute_max(const char *fn
, struct expression
*expr
, void *info
)
261 struct expression
*arg
;
265 arg
= get_argument_from_call_expr(expr
->args
, 0);
266 get_absolute_max(arg
, &sval
);
268 name
= expr_to_str(arg
);
269 sm_msg("absolute max: %s = %s", name
, sval_to_str(sval
));
273 static void match_sval_info(const char *fn
, struct expression
*expr
, void *info
)
275 struct expression
*arg
;
279 arg
= get_argument_from_call_expr(expr
->args
, 0);
280 name
= expr_to_str(arg
);
282 if (!get_implied_value(arg
, &sval
)) {
283 sm_msg("no sval for '%s'", name
);
287 sm_msg("implied: %s %c%d ->value = %llx", name
, sval_unsigned(sval
) ? 'u' : 's', sval_bits(sval
), sval
.value
);
292 static void match_member_name(const char *fn
, struct expression
*expr
, void *info
)
294 struct expression
*arg
;
295 char *name
, *member_name
;
297 arg
= get_argument_from_call_expr(expr
->args
, 0);
298 name
= expr_to_str(arg
);
299 member_name
= get_member_name(arg
);
300 sm_msg("member name: '%s => %s'", name
, member_name
);
301 free_string(member_name
);
305 static void print_possible(struct sm_state
*sm
)
307 struct sm_state
*tmp
;
309 sm_msg("Possible values for %s", sm
->name
);
310 FOR_EACH_PTR(sm
->possible
, tmp
) {
311 printf("%s\n", tmp
->state
->name
);
312 } END_FOR_EACH_PTR(tmp
);
316 static void match_possible(const char *fn
, struct expression
*expr
, void *info
)
319 struct sm_state
*tmp
;
320 struct expression
*arg_expr
;
322 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
323 if (arg_expr
->type
!= EXPR_STRING
) {
324 sm_msg("error: the argument to %s is supposed to be a string literal", fn
);
328 stree
= __get_cur_stree();
329 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
330 if (!strcmp(tmp
->name
, arg_expr
->string
->data
))
332 } END_FOR_EACH_SM(tmp
);
335 static void match_strlen(const char *fn
, struct expression
*expr
, void *info
)
337 struct expression
*arg
;
338 struct range_list
*rl
;
341 arg
= get_argument_from_call_expr(expr
->args
, 0);
342 get_implied_strlen(arg
, &rl
);
344 name
= expr_to_str(arg
);
345 sm_msg("strlen: '%s' %s characters", name
, show_rl(rl
));
349 static void match_buf_size(const char *fn
, struct expression
*expr
, void *info
)
351 struct expression
*arg
;
355 arg
= get_argument_from_call_expr(expr
->args
, 0);
356 elements
= get_array_size(arg
);
357 bytes
= get_array_size_bytes(arg
);
359 name
= expr_to_str(arg
);
360 sm_msg("buf size: '%s' %d elements, %d bytes", name
, elements
, bytes
);
364 static void match_buf_size_rl(const char *fn
, struct expression
*expr
, void *info
)
366 struct expression
*arg
;
367 struct range_list
*rl
;
371 arg
= get_argument_from_call_expr(expr
->args
, 0);
372 rl
= get_array_size_bytes_rl(arg
);
373 elements
= get_array_size(arg
);
374 bytes
= get_array_size_bytes(arg
);
376 name
= expr_to_str(arg
);
377 sm_msg("buf size: '%s' %s %d elements, %d bytes", name
, show_rl(rl
), elements
, bytes
);
381 static void match_note(const char *fn
, struct expression
*expr
, void *info
)
383 struct expression
*arg_expr
;
385 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
386 if (arg_expr
->type
!= EXPR_STRING
) {
387 sm_msg("error: the argument to %s is supposed to be a string literal", fn
);
390 sm_msg("%s", arg_expr
->string
->data
);
393 static void print_related(struct sm_state
*sm
)
395 struct relation
*rel
;
397 if (!estate_related(sm
->state
))
401 sm_printf("%s: ", sm
->name
);
402 FOR_EACH_PTR(estate_related(sm
->state
), rel
) {
403 sm_printf("%s ", rel
->name
);
404 } END_FOR_EACH_PTR(rel
);
408 static void match_dump_related(const char *fn
, struct expression
*expr
, void *info
)
411 struct sm_state
*tmp
;
413 stree
= __get_cur_stree();
414 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
416 } END_FOR_EACH_SM(tmp
);
419 static void match_compare(const char *fn
, struct expression
*expr
, void *info
)
421 struct expression
*one
, *two
;
422 char *one_name
, *two_name
;
426 one
= get_argument_from_call_expr(expr
->args
, 0);
427 two
= get_argument_from_call_expr(expr
->args
, 1);
429 comparison
= get_comparison(one
, two
);
431 snprintf(buf
, sizeof(buf
), "<none>");
433 snprintf(buf
, sizeof(buf
), "%s", show_special(comparison
));
435 one_name
= expr_to_str(one
);
436 two_name
= expr_to_str(two
);
438 sm_msg("%s %s %s", one_name
, buf
, two_name
);
440 free_string(one_name
);
441 free_string(two_name
);
444 static void match_debug_on(const char *fn
, struct expression
*expr
, void *info
)
449 static void match_debug_check(const char *fn
, struct expression
*expr
, void *info
)
451 struct expression
*arg
;
453 arg
= get_argument_from_call_expr(expr
->args
, 0);
454 if (!arg
|| arg
->type
!= EXPR_STRING
)
456 option_debug_check
= arg
->string
->data
;
457 sm_msg("arg = '%s'", option_debug_check
);
460 static void match_debug_off(const char *fn
, struct expression
*expr
, void *info
)
462 option_debug_check
= (char *)"";
466 static void match_local_debug_on(const char *fn
, struct expression
*expr
, void *info
)
471 static void match_local_debug_off(const char *fn
, struct expression
*expr
, void *info
)
476 static void match_debug_implied_on(const char *fn
, struct expression
*expr
, void *info
)
478 option_debug_implied
= 1;
481 static void match_debug_implied_off(const char *fn
, struct expression
*expr
, void *info
)
483 option_debug_implied
= 0;
486 static void match_about(const char *fn
, struct expression
*expr
, void *info
)
488 struct expression
*arg
;
492 sm_msg("---- about ----");
493 match_print_implied(fn
, expr
, NULL
);
494 match_buf_size(fn
, expr
, NULL
);
495 match_strlen(fn
, expr
, NULL
);
496 match_real_absolute(fn
, expr
, NULL
);
498 arg
= get_argument_from_call_expr(expr
->args
, 0);
499 name
= expr_to_str(arg
);
501 sm_msg("info: not a straight forward variable.");
505 FOR_EACH_SM(__get_cur_stree(), sm
) {
506 if (strcmp(sm
->name
, name
) != 0)
508 sm_msg("%s", show_sm(sm
));
509 } END_FOR_EACH_SM(sm
);
512 static void match_intersection(const char *fn
, struct expression
*expr
, void *info
)
514 struct expression
*one
, *two
;
515 struct range_list
*one_rl
, *two_rl
;
516 struct range_list
*res
;
518 one
= get_argument_from_call_expr(expr
->args
, 0);
519 two
= get_argument_from_call_expr(expr
->args
, 1);
521 get_absolute_rl(one
, &one_rl
);
522 get_absolute_rl(two
, &two_rl
);
524 res
= rl_intersection(one_rl
, two_rl
);
525 sm_msg("'%s' intersect '%s' is '%s'", show_rl(one_rl
), show_rl(two_rl
), show_rl(res
));
528 static void match_type(const char *fn
, struct expression
*expr
, void *info
)
530 struct expression
*one
;
534 one
= get_argument_from_call_expr(expr
->args
, 0);
535 type
= get_type(one
);
536 name
= expr_to_str(one
);
537 sm_msg("type of '%s' is: '%s'", name
, type_to_str(type
));
541 static void match_type_rl(const char *fn
, struct expression
*expr
, void *info
)
543 struct expression
*one
, *two
;
545 struct range_list
*rl
;
547 one
= get_argument_from_call_expr(expr
->args
, 0);
548 type
= get_type(one
);
550 two
= get_argument_from_call_expr(expr
->args
, 1);
551 if (!two
|| two
->type
!= EXPR_STRING
) {
552 sm_msg("expected: __smatch_type_rl(type, \"string\")");
555 call_results_to_rl(expr
, type
, two
->string
->data
, &rl
);
556 sm_msg("'%s' => '%s'", two
->string
->data
, show_rl(rl
));
559 static struct stree
*old_stree
;
560 static void trace_var(struct statement
*stmt
)
562 struct sm_state
*sm
, *old
;
569 FOR_EACH_SM(__get_cur_stree(), sm
) {
570 if (strcmp(sm
->name
, trace_variable
) != 0)
572 old
= get_sm_state_stree(old_stree
, sm
->owner
, sm
->name
, sm
->sym
);
573 if (old
&& old
->state
== sm
->state
)
575 sm_msg("[%d] %s '%s': '%s' => '%s'", stmt
->type
,
576 check_name(sm
->owner
),
577 sm
->name
, old
? old
->state
->name
: "<none>", sm
->state
->name
);
578 } END_FOR_EACH_SM(sm
);
580 free_stree(&old_stree
);
581 old_stree
= clone_stree(__get_cur_stree());
584 static void free_old_stree(struct symbol
*sym
)
586 free_stree(&old_stree
);
589 void check_debug(int id
)
592 add_function_hook("__smatch_about", &match_about
, NULL
);
593 add_function_hook("__smatch_all_values", &match_all_values
, NULL
);
594 add_function_hook("__smatch_state", &match_state
, NULL
);
595 add_function_hook("__smatch_states", &match_states
, NULL
);
596 add_function_hook("__smatch_value", &match_print_value
, NULL
);
597 add_function_hook("__smatch_known", &match_print_known
, NULL
);
598 add_function_hook("__smatch_implied", &match_print_implied
, NULL
);
599 add_function_hook("__smatch_implied_min", &match_print_implied_min
, NULL
);
600 add_function_hook("__smatch_implied_max", &match_print_implied_max
, NULL
);
601 add_function_hook("__smatch_user_rl", &match_user_rl
, NULL
);
602 add_function_hook("__smatch_hard_max", &match_print_hard_max
, NULL
);
603 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max
, NULL
);
604 add_function_hook("__smatch_absolute_min", &match_print_absolute_min
, NULL
);
605 add_function_hook("__smatch_absolute_max", &match_print_absolute_max
, NULL
);
606 add_function_hook("__smatch_real_absolute", &match_real_absolute
, NULL
);
607 add_function_hook("__smatch_sval_info", &match_sval_info
, NULL
);
608 add_function_hook("__smatch_member_name", &match_member_name
, NULL
);
609 add_function_hook("__smatch_possible", &match_possible
, NULL
);
610 add_function_hook("__smatch_cur_stree", &match_cur_stree
, NULL
);
611 add_function_hook("__smatch_strlen", &match_strlen
, NULL
);
612 add_function_hook("__smatch_buf_size", &match_buf_size
, NULL
);
613 add_function_hook("__smatch_buf_size_rl", &match_buf_size_rl
, NULL
);
614 add_function_hook("__smatch_note", &match_note
, NULL
);
615 add_function_hook("__smatch_dump_related", &match_dump_related
, NULL
);
616 add_function_hook("__smatch_compare", &match_compare
, NULL
);
617 add_function_hook("__smatch_debug_on", &match_debug_on
, NULL
);
618 add_function_hook("__smatch_debug_check", &match_debug_check
, NULL
);
619 add_function_hook("__smatch_debug_off", &match_debug_off
, NULL
);
620 add_function_hook("__smatch_local_debug_on", &match_local_debug_on
, NULL
);
621 add_function_hook("__smatch_local_debug_off", &match_local_debug_off
, NULL
);
622 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on
, NULL
);
623 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off
, NULL
);
624 add_function_hook("__smatch_intersection", &match_intersection
, NULL
);
625 add_function_hook("__smatch_type", &match_type
, NULL
);
626 add_function_hook("__smatch_type_rl_helper", match_type_rl
, NULL
);
628 add_hook(free_old_stree
, END_FUNC_HOOK
);
629 add_hook(trace_var
, STMT_HOOK_AFTER
);