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' = '%s'", sm
->name
, sm
->state
->name
);
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_implied(const char *fn
, struct expression
*expr
, void *info
)
114 struct expression
*arg
;
115 struct range_list
*rl
= NULL
;
118 arg
= get_argument_from_call_expr(expr
->args
, 0);
119 get_implied_rl(arg
, &rl
);
121 name
= expr_to_str(arg
);
122 sm_msg("implied: %s = '%s'", name
, show_rl(rl
));
126 static void match_print_implied_min(const char *fn
, struct expression
*expr
, void *info
)
128 struct expression
*arg
;
132 arg
= get_argument_from_call_expr(expr
->args
, 0);
133 name
= expr_to_str(arg
);
135 if (get_implied_min(arg
, &sval
))
136 sm_msg("implied min: %s = %s", name
, sval_to_str(sval
));
138 sm_msg("implied min: %s = <unknown>", name
);
143 static void match_print_implied_max(const char *fn
, struct expression
*expr
, void *info
)
145 struct expression
*arg
;
149 arg
= get_argument_from_call_expr(expr
->args
, 0);
150 name
= expr_to_str(arg
);
152 if (get_implied_max(arg
, &sval
))
153 sm_msg("implied max: %s = %s", name
, sval_to_str(sval
));
155 sm_msg("implied max: %s = <unknown>", name
);
160 static void match_user_rl(const char *fn
, struct expression
*expr
, void *info
)
162 struct expression
*arg
;
163 struct range_list
*rl
;
166 arg
= get_argument_from_call_expr(expr
->args
, 0);
167 name
= expr_to_str(arg
);
169 get_user_rl(arg
, &rl
);
170 sm_msg("user rl: '%s' = '%s'", name
, show_rl(rl
));
175 static void match_print_hard_max(const char *fn
, struct expression
*expr
, void *info
)
177 struct expression
*arg
;
181 arg
= get_argument_from_call_expr(expr
->args
, 0);
182 name
= expr_to_str(arg
);
184 if (get_hard_max(arg
, &sval
))
185 sm_msg("hard max: %s = %s", name
, sval_to_str(sval
));
187 sm_msg("hard max: %s = <unknown>", name
);
192 static void match_print_fuzzy_max(const char *fn
, struct expression
*expr
, void *info
)
194 struct expression
*arg
;
198 arg
= get_argument_from_call_expr(expr
->args
, 0);
199 name
= expr_to_str(arg
);
201 if (get_fuzzy_max(arg
, &sval
))
202 sm_msg("fuzzy max: %s = %s", name
, sval_to_str(sval
));
204 sm_msg("fuzzy max: %s = <unknown>", name
);
209 static void match_print_absolute_min(const char *fn
, struct expression
*expr
, void *info
)
211 struct expression
*arg
;
215 arg
= get_argument_from_call_expr(expr
->args
, 0);
216 name
= expr_to_str(arg
);
218 if (get_absolute_min(arg
, &sval
))
219 sm_msg("absolute min: %s = %s", name
, sval_to_str(sval
));
221 sm_msg("absolute min: %s = <unknown>", name
);
226 static void match_print_absolute_max(const char *fn
, struct expression
*expr
, void *info
)
228 struct expression
*arg
;
232 arg
= get_argument_from_call_expr(expr
->args
, 0);
233 get_absolute_max(arg
, &sval
);
235 name
= expr_to_str(arg
);
236 sm_msg("absolute max: %s = %s", name
, sval_to_str(sval
));
240 static void match_sval_info(const char *fn
, struct expression
*expr
, void *info
)
242 struct expression
*arg
;
246 arg
= get_argument_from_call_expr(expr
->args
, 0);
247 name
= expr_to_str(arg
);
249 if (!get_implied_value(arg
, &sval
)) {
250 sm_msg("no sval for '%s'", name
);
254 sm_msg("implied: %s %c%d ->value = %llx", name
, sval_unsigned(sval
) ? 'u' : 's', sval_bits(sval
), sval
.value
);
259 static void match_member_name(const char *fn
, struct expression
*expr
, void *info
)
261 struct expression
*arg
;
262 char *name
, *member_name
;
264 arg
= get_argument_from_call_expr(expr
->args
, 0);
265 name
= expr_to_str(arg
);
266 member_name
= get_member_name(arg
);
267 sm_msg("member name: '%s => %s'", name
, member_name
);
268 free_string(member_name
);
272 static void print_possible(struct sm_state
*sm
)
274 struct sm_state
*tmp
;
276 sm_msg("Possible values for %s", sm
->name
);
277 FOR_EACH_PTR(sm
->possible
, tmp
) {
278 printf("%s\n", tmp
->state
->name
);
279 } END_FOR_EACH_PTR(tmp
);
283 static void match_possible(const char *fn
, struct expression
*expr
, void *info
)
286 struct sm_state
*tmp
;
287 struct expression
*arg_expr
;
289 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
290 if (arg_expr
->type
!= EXPR_STRING
) {
291 sm_msg("error: the argument to %s is supposed to be a string literal", fn
);
295 stree
= __get_cur_stree();
296 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
297 if (!strcmp(tmp
->name
, arg_expr
->string
->data
))
299 } END_FOR_EACH_SM(tmp
);
302 static void match_strlen(const char *fn
, struct expression
*expr
, void *info
)
304 struct expression
*arg
;
305 struct range_list
*rl
;
308 arg
= get_argument_from_call_expr(expr
->args
, 0);
309 get_implied_strlen(arg
, &rl
);
311 name
= expr_to_str(arg
);
312 sm_msg("strlen: '%s' %s characters", name
, show_rl(rl
));
316 static void match_buf_size(const char *fn
, struct expression
*expr
, void *info
)
318 struct expression
*arg
;
322 arg
= get_argument_from_call_expr(expr
->args
, 0);
323 elements
= get_array_size(arg
);
324 bytes
= get_array_size_bytes(arg
);
326 name
= expr_to_str(arg
);
327 sm_msg("buf size: '%s' %d elements, %d bytes", name
, elements
, bytes
);
331 static void match_buf_size_rl(const char *fn
, struct expression
*expr
, void *info
)
333 struct expression
*arg
;
334 struct range_list
*rl
;
338 arg
= get_argument_from_call_expr(expr
->args
, 0);
339 rl
= get_array_size_bytes_rl(arg
);
340 elements
= get_array_size(arg
);
341 bytes
= get_array_size_bytes(arg
);
343 name
= expr_to_str(arg
);
344 sm_msg("buf size: '%s' %s %d elements, %d bytes", name
, show_rl(rl
), elements
, bytes
);
348 static void match_note(const char *fn
, struct expression
*expr
, void *info
)
350 struct expression
*arg_expr
;
352 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
353 if (arg_expr
->type
!= EXPR_STRING
) {
354 sm_msg("error: the argument to %s is supposed to be a string literal", fn
);
357 sm_msg("%s", arg_expr
->string
->data
);
360 static void print_related(struct sm_state
*sm
)
362 struct relation
*rel
;
364 if (!estate_related(sm
->state
))
368 sm_printf("%s: ", sm
->name
);
369 FOR_EACH_PTR(estate_related(sm
->state
), rel
) {
370 sm_printf("%s ", rel
->name
);
371 } END_FOR_EACH_PTR(rel
);
375 static void match_dump_related(const char *fn
, struct expression
*expr
, void *info
)
378 struct sm_state
*tmp
;
380 stree
= __get_cur_stree();
381 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
383 } END_FOR_EACH_SM(tmp
);
386 static void match_compare(const char *fn
, struct expression
*expr
, void *info
)
388 struct expression
*one
, *two
;
389 char *one_name
, *two_name
;
393 one
= get_argument_from_call_expr(expr
->args
, 0);
394 two
= get_argument_from_call_expr(expr
->args
, 1);
396 comparison
= get_comparison(one
, two
);
398 snprintf(buf
, sizeof(buf
), "<none>");
400 snprintf(buf
, sizeof(buf
), "%s", show_special(comparison
));
402 one_name
= expr_to_str(one
);
403 two_name
= expr_to_str(two
);
405 sm_msg("%s %s %s", one_name
, buf
, two_name
);
407 free_string(one_name
);
408 free_string(two_name
);
411 static void match_debug_on(const char *fn
, struct expression
*expr
, void *info
)
416 static void match_debug_check(const char *fn
, struct expression
*expr
, void *info
)
418 struct expression
*arg
;
420 arg
= get_argument_from_call_expr(expr
->args
, 0);
421 if (!arg
|| arg
->type
!= EXPR_STRING
)
423 option_debug_check
= arg
->string
->data
;
424 sm_msg("arg = '%s'", option_debug_check
);
427 static void match_debug_off(const char *fn
, struct expression
*expr
, void *info
)
429 option_debug_check
= (char *)"";
433 static void match_local_debug_on(const char *fn
, struct expression
*expr
, void *info
)
438 static void match_local_debug_off(const char *fn
, struct expression
*expr
, void *info
)
443 static void match_debug_implied_on(const char *fn
, struct expression
*expr
, void *info
)
445 option_debug_implied
= 1;
448 static void match_debug_implied_off(const char *fn
, struct expression
*expr
, void *info
)
450 option_debug_implied
= 0;
453 static void match_about(const char *fn
, struct expression
*expr
, void *info
)
455 struct expression
*arg
;
459 sm_msg("---- about ----");
460 match_print_implied(fn
, expr
, NULL
);
461 match_buf_size(fn
, expr
, NULL
);
462 match_strlen(fn
, expr
, NULL
);
464 arg
= get_argument_from_call_expr(expr
->args
, 0);
465 name
= expr_to_str(arg
);
467 sm_msg("info: not a straight forward variable.");
471 FOR_EACH_SM(__get_cur_stree(), sm
) {
472 if (strcmp(sm
->name
, name
) != 0)
474 sm_msg("%s '%s' = '%s'", check_name(sm
->owner
), sm
->name
, sm
->state
->name
);
475 } END_FOR_EACH_SM(sm
);
478 static void match_intersection(const char *fn
, struct expression
*expr
, void *info
)
480 struct expression
*one
, *two
;
481 struct range_list
*one_rl
, *two_rl
;
482 struct range_list
*res
;
484 one
= get_argument_from_call_expr(expr
->args
, 0);
485 two
= get_argument_from_call_expr(expr
->args
, 1);
487 get_absolute_rl(one
, &one_rl
);
488 get_absolute_rl(two
, &two_rl
);
490 res
= rl_intersection(one_rl
, two_rl
);
491 sm_msg("'%s' intersect '%s' is '%s'", show_rl(one_rl
), show_rl(two_rl
), show_rl(res
));
494 static void match_type(const char *fn
, struct expression
*expr
, void *info
)
496 struct expression
*one
;
500 one
= get_argument_from_call_expr(expr
->args
, 0);
501 type
= get_type(one
);
502 name
= expr_to_str(one
);
503 sm_msg("type of '%s' is: '%s'", name
, type_to_str(type
));
507 static struct stree
*old_stree
;
508 static void trace_var(struct statement
*stmt
)
510 struct sm_state
*sm
, *old
;
517 FOR_EACH_SM(__get_cur_stree(), sm
) {
518 if (strcmp(sm
->name
, trace_variable
) != 0)
520 old
= get_sm_state_stree(old_stree
, sm
->owner
, sm
->name
, sm
->sym
);
521 if (old
&& old
->state
== sm
->state
)
523 sm_msg("[%d] %s '%s': '%s' => '%s'", stmt
->type
,
524 check_name(sm
->owner
),
525 sm
->name
, old
? old
->state
->name
: "<none>", sm
->state
->name
);
526 } END_FOR_EACH_SM(sm
);
528 free_stree(&old_stree
);
529 old_stree
= clone_stree(__get_cur_stree());
532 static void free_old_stree(struct symbol
*sym
)
534 free_stree(&old_stree
);
537 void check_debug(int id
)
540 add_function_hook("__smatch_about", &match_about
, NULL
);
541 add_function_hook("__smatch_all_values", &match_all_values
, NULL
);
542 add_function_hook("__smatch_state", &match_state
, NULL
);
543 add_function_hook("__smatch_states", &match_states
, NULL
);
544 add_function_hook("__smatch_value", &match_print_value
, NULL
);
545 add_function_hook("__smatch_implied", &match_print_implied
, NULL
);
546 add_function_hook("__smatch_implied_min", &match_print_implied_min
, NULL
);
547 add_function_hook("__smatch_implied_max", &match_print_implied_max
, NULL
);
548 add_function_hook("__smatch_user_rl", &match_user_rl
, NULL
);
549 add_function_hook("__smatch_hard_max", &match_print_hard_max
, NULL
);
550 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max
, NULL
);
551 add_function_hook("__smatch_absolute_min", &match_print_absolute_min
, NULL
);
552 add_function_hook("__smatch_absolute_max", &match_print_absolute_max
, NULL
);
553 add_function_hook("__smatch_sval_info", &match_sval_info
, NULL
);
554 add_function_hook("__smatch_member_name", &match_member_name
, NULL
);
555 add_function_hook("__smatch_possible", &match_possible
, NULL
);
556 add_function_hook("__smatch_cur_stree", &match_cur_stree
, NULL
);
557 add_function_hook("__smatch_strlen", &match_strlen
, NULL
);
558 add_function_hook("__smatch_buf_size", &match_buf_size
, NULL
);
559 add_function_hook("__smatch_buf_size_rl", &match_buf_size_rl
, NULL
);
560 add_function_hook("__smatch_note", &match_note
, NULL
);
561 add_function_hook("__smatch_dump_related", &match_dump_related
, NULL
);
562 add_function_hook("__smatch_compare", &match_compare
, NULL
);
563 add_function_hook("__smatch_debug_on", &match_debug_on
, NULL
);
564 add_function_hook("__smatch_debug_check", &match_debug_check
, NULL
);
565 add_function_hook("__smatch_debug_off", &match_debug_off
, NULL
);
566 add_function_hook("__smatch_local_debug_on", &match_local_debug_on
, NULL
);
567 add_function_hook("__smatch_local_debug_off", &match_local_debug_off
, NULL
);
568 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on
, NULL
);
569 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off
, NULL
);
570 add_function_hook("__smatch_intersection", &match_intersection
, NULL
);
571 add_function_hook("__smatch_type", &match_type
, NULL
);
573 add_hook(free_old_stree
, END_FUNC_HOOK
);
574 add_hook(trace_var
, STMT_HOOK_AFTER
);