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_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_print_implied_min(const char *fn
, struct expression
*expr
, void *info
)
147 struct expression
*arg
;
151 arg
= get_argument_from_call_expr(expr
->args
, 0);
152 name
= expr_to_str(arg
);
154 if (get_implied_min(arg
, &sval
))
155 sm_msg("implied min: %s = %s", name
, sval_to_str(sval
));
157 sm_msg("implied min: %s = <unknown>", name
);
162 static void match_print_implied_max(const char *fn
, struct expression
*expr
, void *info
)
164 struct expression
*arg
;
168 arg
= get_argument_from_call_expr(expr
->args
, 0);
169 name
= expr_to_str(arg
);
171 if (get_implied_max(arg
, &sval
))
172 sm_msg("implied max: %s = %s", name
, sval_to_str(sval
));
174 sm_msg("implied max: %s = <unknown>", name
);
179 static void match_user_rl(const char *fn
, struct expression
*expr
, void *info
)
181 struct expression
*arg
;
182 struct range_list
*rl
;
185 arg
= get_argument_from_call_expr(expr
->args
, 0);
186 name
= expr_to_str(arg
);
188 get_user_rl(arg
, &rl
);
189 sm_msg("user rl: '%s' = '%s'", name
, show_rl(rl
));
194 static void match_print_hard_max(const char *fn
, struct expression
*expr
, void *info
)
196 struct expression
*arg
;
200 arg
= get_argument_from_call_expr(expr
->args
, 0);
201 name
= expr_to_str(arg
);
203 if (get_hard_max(arg
, &sval
))
204 sm_msg("hard max: %s = %s", name
, sval_to_str(sval
));
206 sm_msg("hard max: %s = <unknown>", name
);
211 static void match_print_fuzzy_max(const char *fn
, struct expression
*expr
, void *info
)
213 struct expression
*arg
;
217 arg
= get_argument_from_call_expr(expr
->args
, 0);
218 name
= expr_to_str(arg
);
220 if (get_fuzzy_max(arg
, &sval
))
221 sm_msg("fuzzy max: %s = %s", name
, sval_to_str(sval
));
223 sm_msg("fuzzy max: %s = <unknown>", name
);
228 static void match_print_absolute_min(const char *fn
, struct expression
*expr
, void *info
)
230 struct expression
*arg
;
234 arg
= get_argument_from_call_expr(expr
->args
, 0);
235 name
= expr_to_str(arg
);
237 if (get_absolute_min(arg
, &sval
))
238 sm_msg("absolute min: %s = %s", name
, sval_to_str(sval
));
240 sm_msg("absolute min: %s = <unknown>", name
);
245 static void match_print_absolute_max(const char *fn
, struct expression
*expr
, void *info
)
247 struct expression
*arg
;
251 arg
= get_argument_from_call_expr(expr
->args
, 0);
252 get_absolute_max(arg
, &sval
);
254 name
= expr_to_str(arg
);
255 sm_msg("absolute max: %s = %s", name
, sval_to_str(sval
));
259 static void match_sval_info(const char *fn
, struct expression
*expr
, void *info
)
261 struct expression
*arg
;
265 arg
= get_argument_from_call_expr(expr
->args
, 0);
266 name
= expr_to_str(arg
);
268 if (!get_implied_value(arg
, &sval
)) {
269 sm_msg("no sval for '%s'", name
);
273 sm_msg("implied: %s %c%d ->value = %llx", name
, sval_unsigned(sval
) ? 'u' : 's', sval_bits(sval
), sval
.value
);
278 static void match_member_name(const char *fn
, struct expression
*expr
, void *info
)
280 struct expression
*arg
;
281 char *name
, *member_name
;
283 arg
= get_argument_from_call_expr(expr
->args
, 0);
284 name
= expr_to_str(arg
);
285 member_name
= get_member_name(arg
);
286 sm_msg("member name: '%s => %s'", name
, member_name
);
287 free_string(member_name
);
291 static void print_possible(struct sm_state
*sm
)
293 struct sm_state
*tmp
;
295 sm_msg("Possible values for %s", sm
->name
);
296 FOR_EACH_PTR(sm
->possible
, tmp
) {
297 printf("%s\n", tmp
->state
->name
);
298 } END_FOR_EACH_PTR(tmp
);
302 static void match_possible(const char *fn
, struct expression
*expr
, void *info
)
305 struct sm_state
*tmp
;
306 struct expression
*arg_expr
;
308 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
309 if (arg_expr
->type
!= EXPR_STRING
) {
310 sm_msg("error: the argument to %s is supposed to be a string literal", fn
);
314 stree
= __get_cur_stree();
315 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
316 if (!strcmp(tmp
->name
, arg_expr
->string
->data
))
318 } END_FOR_EACH_SM(tmp
);
321 static void match_strlen(const char *fn
, struct expression
*expr
, void *info
)
323 struct expression
*arg
;
324 struct range_list
*rl
;
327 arg
= get_argument_from_call_expr(expr
->args
, 0);
328 get_implied_strlen(arg
, &rl
);
330 name
= expr_to_str(arg
);
331 sm_msg("strlen: '%s' %s characters", name
, show_rl(rl
));
335 static void match_buf_size(const char *fn
, struct expression
*expr
, void *info
)
337 struct expression
*arg
;
341 arg
= get_argument_from_call_expr(expr
->args
, 0);
342 elements
= get_array_size(arg
);
343 bytes
= get_array_size_bytes(arg
);
345 name
= expr_to_str(arg
);
346 sm_msg("buf size: '%s' %d elements, %d bytes", name
, elements
, bytes
);
350 static void match_buf_size_rl(const char *fn
, struct expression
*expr
, void *info
)
352 struct expression
*arg
;
353 struct range_list
*rl
;
357 arg
= get_argument_from_call_expr(expr
->args
, 0);
358 rl
= get_array_size_bytes_rl(arg
);
359 elements
= get_array_size(arg
);
360 bytes
= get_array_size_bytes(arg
);
362 name
= expr_to_str(arg
);
363 sm_msg("buf size: '%s' %s %d elements, %d bytes", name
, show_rl(rl
), elements
, bytes
);
367 static void match_note(const char *fn
, struct expression
*expr
, void *info
)
369 struct expression
*arg_expr
;
371 arg_expr
= get_argument_from_call_expr(expr
->args
, 0);
372 if (arg_expr
->type
!= EXPR_STRING
) {
373 sm_msg("error: the argument to %s is supposed to be a string literal", fn
);
376 sm_msg("%s", arg_expr
->string
->data
);
379 static void print_related(struct sm_state
*sm
)
381 struct relation
*rel
;
383 if (!estate_related(sm
->state
))
387 sm_printf("%s: ", sm
->name
);
388 FOR_EACH_PTR(estate_related(sm
->state
), rel
) {
389 sm_printf("%s ", rel
->name
);
390 } END_FOR_EACH_PTR(rel
);
394 static void match_dump_related(const char *fn
, struct expression
*expr
, void *info
)
397 struct sm_state
*tmp
;
399 stree
= __get_cur_stree();
400 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
402 } END_FOR_EACH_SM(tmp
);
405 static void match_compare(const char *fn
, struct expression
*expr
, void *info
)
407 struct expression
*one
, *two
;
408 char *one_name
, *two_name
;
412 one
= get_argument_from_call_expr(expr
->args
, 0);
413 two
= get_argument_from_call_expr(expr
->args
, 1);
415 comparison
= get_comparison(one
, two
);
417 snprintf(buf
, sizeof(buf
), "<none>");
419 snprintf(buf
, sizeof(buf
), "%s", show_special(comparison
));
421 one_name
= expr_to_str(one
);
422 two_name
= expr_to_str(two
);
424 sm_msg("%s %s %s", one_name
, buf
, two_name
);
426 free_string(one_name
);
427 free_string(two_name
);
430 static void match_debug_on(const char *fn
, struct expression
*expr
, void *info
)
435 static void match_debug_check(const char *fn
, struct expression
*expr
, void *info
)
437 struct expression
*arg
;
439 arg
= get_argument_from_call_expr(expr
->args
, 0);
440 if (!arg
|| arg
->type
!= EXPR_STRING
)
442 option_debug_check
= arg
->string
->data
;
443 sm_msg("arg = '%s'", option_debug_check
);
446 static void match_debug_off(const char *fn
, struct expression
*expr
, void *info
)
448 option_debug_check
= (char *)"";
452 static void match_local_debug_on(const char *fn
, struct expression
*expr
, void *info
)
457 static void match_local_debug_off(const char *fn
, struct expression
*expr
, void *info
)
462 static void match_debug_implied_on(const char *fn
, struct expression
*expr
, void *info
)
464 option_debug_implied
= 1;
467 static void match_debug_implied_off(const char *fn
, struct expression
*expr
, void *info
)
469 option_debug_implied
= 0;
472 static void match_about(const char *fn
, struct expression
*expr
, void *info
)
474 struct expression
*arg
;
478 sm_msg("---- about ----");
479 match_print_implied(fn
, expr
, NULL
);
480 match_buf_size(fn
, expr
, NULL
);
481 match_strlen(fn
, expr
, NULL
);
483 arg
= get_argument_from_call_expr(expr
->args
, 0);
484 name
= expr_to_str(arg
);
486 sm_msg("info: not a straight forward variable.");
490 FOR_EACH_SM(__get_cur_stree(), sm
) {
491 if (strcmp(sm
->name
, name
) != 0)
493 sm_msg("%s '%s' = '%s'", check_name(sm
->owner
), sm
->name
, sm
->state
->name
);
494 } END_FOR_EACH_SM(sm
);
497 static void match_intersection(const char *fn
, struct expression
*expr
, void *info
)
499 struct expression
*one
, *two
;
500 struct range_list
*one_rl
, *two_rl
;
501 struct range_list
*res
;
503 one
= get_argument_from_call_expr(expr
->args
, 0);
504 two
= get_argument_from_call_expr(expr
->args
, 1);
506 get_absolute_rl(one
, &one_rl
);
507 get_absolute_rl(two
, &two_rl
);
509 res
= rl_intersection(one_rl
, two_rl
);
510 sm_msg("'%s' intersect '%s' is '%s'", show_rl(one_rl
), show_rl(two_rl
), show_rl(res
));
513 static void match_type(const char *fn
, struct expression
*expr
, void *info
)
515 struct expression
*one
;
519 one
= get_argument_from_call_expr(expr
->args
, 0);
520 type
= get_type(one
);
521 name
= expr_to_str(one
);
522 sm_msg("type of '%s' is: '%s'", name
, type_to_str(type
));
526 static struct stree
*old_stree
;
527 static void trace_var(struct statement
*stmt
)
529 struct sm_state
*sm
, *old
;
536 FOR_EACH_SM(__get_cur_stree(), sm
) {
537 if (strcmp(sm
->name
, trace_variable
) != 0)
539 old
= get_sm_state_stree(old_stree
, sm
->owner
, sm
->name
, sm
->sym
);
540 if (old
&& old
->state
== sm
->state
)
542 sm_msg("[%d] %s '%s': '%s' => '%s'", stmt
->type
,
543 check_name(sm
->owner
),
544 sm
->name
, old
? old
->state
->name
: "<none>", sm
->state
->name
);
545 } END_FOR_EACH_SM(sm
);
547 free_stree(&old_stree
);
548 old_stree
= clone_stree(__get_cur_stree());
551 static void free_old_stree(struct symbol
*sym
)
553 free_stree(&old_stree
);
556 void check_debug(int id
)
559 add_function_hook("__smatch_about", &match_about
, NULL
);
560 add_function_hook("__smatch_all_values", &match_all_values
, NULL
);
561 add_function_hook("__smatch_state", &match_state
, NULL
);
562 add_function_hook("__smatch_states", &match_states
, NULL
);
563 add_function_hook("__smatch_value", &match_print_value
, NULL
);
564 add_function_hook("__smatch_known", &match_print_known
, NULL
);
565 add_function_hook("__smatch_implied", &match_print_implied
, NULL
);
566 add_function_hook("__smatch_implied_min", &match_print_implied_min
, NULL
);
567 add_function_hook("__smatch_implied_max", &match_print_implied_max
, NULL
);
568 add_function_hook("__smatch_user_rl", &match_user_rl
, NULL
);
569 add_function_hook("__smatch_hard_max", &match_print_hard_max
, NULL
);
570 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max
, NULL
);
571 add_function_hook("__smatch_absolute_min", &match_print_absolute_min
, NULL
);
572 add_function_hook("__smatch_absolute_max", &match_print_absolute_max
, NULL
);
573 add_function_hook("__smatch_sval_info", &match_sval_info
, NULL
);
574 add_function_hook("__smatch_member_name", &match_member_name
, NULL
);
575 add_function_hook("__smatch_possible", &match_possible
, NULL
);
576 add_function_hook("__smatch_cur_stree", &match_cur_stree
, NULL
);
577 add_function_hook("__smatch_strlen", &match_strlen
, NULL
);
578 add_function_hook("__smatch_buf_size", &match_buf_size
, NULL
);
579 add_function_hook("__smatch_buf_size_rl", &match_buf_size_rl
, NULL
);
580 add_function_hook("__smatch_note", &match_note
, NULL
);
581 add_function_hook("__smatch_dump_related", &match_dump_related
, NULL
);
582 add_function_hook("__smatch_compare", &match_compare
, NULL
);
583 add_function_hook("__smatch_debug_on", &match_debug_on
, NULL
);
584 add_function_hook("__smatch_debug_check", &match_debug_check
, NULL
);
585 add_function_hook("__smatch_debug_off", &match_debug_off
, NULL
);
586 add_function_hook("__smatch_local_debug_on", &match_local_debug_on
, NULL
);
587 add_function_hook("__smatch_local_debug_off", &match_local_debug_off
, NULL
);
588 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on
, NULL
);
589 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off
, NULL
);
590 add_function_hook("__smatch_intersection", &match_intersection
, NULL
);
591 add_function_hook("__smatch_type", &match_type
, NULL
);
593 add_hook(free_old_stree
, END_FUNC_HOOK
);
594 add_hook(trace_var
, STMT_HOOK_AFTER
);