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"
25 static void match_all_values(const char *fn
, struct expression
*expr
, void *info
)
29 stree
= get_all_states_stree(SMATCH_EXTRA
);
34 static void match_cur_stree(const char *fn
, struct expression
*expr
, void *info
)
39 static void match_state(const char *fn
, struct expression
*expr
, void *info
)
41 struct expression
*check_arg
, *state_arg
;
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
);
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
);
56 FOR_EACH_SM(__get_cur_stree(), sm
) {
57 if (strcmp(check_name(sm
->owner
), check_arg
->string
->data
) != 0)
59 if (strcmp(sm
->name
, state_arg
->string
->data
) != 0)
61 sm_msg("'%s' = '%s'", sm
->name
, sm
->state
->name
);
63 } END_FOR_EACH_SM(sm
);
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
;
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
);
81 FOR_EACH_SM(__get_cur_stree(), sm
) {
82 if (strcmp(check_name(sm
->owner
), check_arg
->string
->data
) != 0)
84 sm_msg("'%s' = '%s'", sm
->name
, sm
->state
->name
);
86 } END_FOR_EACH_SM(sm
);
89 sm_msg("%s: no states", check_arg
->string
->data
);
92 static void match_print_value(const char *fn
, struct expression
*expr
, void *info
)
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
);
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
;
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
));
125 static void match_print_implied_min(const char *fn
, struct expression
*expr
, void *info
)
127 struct expression
*arg
;
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
));
137 sm_msg("implied min: %s = <unknown>", name
);
142 static void match_print_implied_max(const char *fn
, struct expression
*expr
, void *info
)
144 struct expression
*arg
;
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
));
154 sm_msg("implied max: %s = <unknown>", name
);
159 static void match_print_hard_max(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_hard_max(arg
, &sval
))
169 sm_msg("hard max: %s = %s", name
, sval_to_str(sval
));
171 sm_msg("hard max: %s = <unknown>", name
);
176 static void match_print_fuzzy_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_fuzzy_max(arg
, &sval
))
186 sm_msg("fuzzy max: %s = %s", name
, sval_to_str(sval
));
188 sm_msg("fuzzy max: %s = <unknown>", name
);
193 static void match_print_absolute_min(const char *fn
, struct expression
*expr
, void *info
)
195 struct expression
*arg
;
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
));
205 sm_msg("absolute min: %s = <unknown>", name
);
210 static void match_print_absolute_max(const char *fn
, struct expression
*expr
, void *info
)
212 struct expression
*arg
;
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
));
224 static void match_sval_info(const char *fn
, struct expression
*expr
, void *info
)
226 struct expression
*arg
;
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
);
238 sm_msg("implied: %s %c%d ->value = %llx", name
, sval_unsigned(sval
) ? 'u' : 's', sval_bits(sval
), sval
.value
);
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
);
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
);
267 static void match_possible(const char *fn
, struct expression
*expr
, void *info
)
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
);
279 stree
= __get_cur_stree();
280 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, tmp
) {
281 if (!strcmp(tmp
->name
, arg_expr
->string
->data
))
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
;
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
));
300 static void match_buf_size(const char *fn
, struct expression
*expr
, void *info
)
302 struct expression
*arg
;
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
);
315 static void match_buf_size_rl(const char *fn
, struct expression
*expr
, void *info
)
317 struct expression
*arg
;
318 struct range_list
*rl
;
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
);
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
);
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
))
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
);
359 static void match_dump_related(const char *fn
, struct expression
*expr
, void *info
)
362 struct sm_state
*tmp
;
364 stree
= __get_cur_stree();
365 FOR_EACH_MY_SM(SMATCH_EXTRA
, stree
, 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
;
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
);
382 snprintf(buf
, sizeof(buf
), "<none>");
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
)
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
)
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 *)"";
417 static void match_local_debug_on(const char *fn
, struct expression
*expr
, void *info
)
422 static void match_local_debug_off(const char *fn
, struct expression
*expr
, void *info
)
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
;
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
);
451 sm_msg("info: not a straight forward variable.");
455 FOR_EACH_SM(__get_cur_stree(), sm
) {
456 if (strcmp(sm
->name
, name
) != 0)
458 sm_msg("%s '%s' = '%s'", check_name(sm
->owner
), sm
->name
, sm
->state
->name
);
459 } END_FOR_EACH_SM(sm
);
462 static void match_intersection(const char *fn
, struct expression
*expr
, void *info
)
464 struct expression
*one
, *two
;
465 struct range_list
*one_rl
, *two_rl
;
466 struct range_list
*res
;
468 one
= get_argument_from_call_expr(expr
->args
, 0);
469 two
= get_argument_from_call_expr(expr
->args
, 1);
471 get_absolute_rl(one
, &one_rl
);
472 get_absolute_rl(two
, &two_rl
);
474 res
= rl_intersection(one_rl
, two_rl
);
475 sm_msg("'%s' intersect '%s' is '%s'", show_rl(one_rl
), show_rl(two_rl
), show_rl(res
));
478 static void match_type(const char *fn
, struct expression
*expr
, void *info
)
480 struct expression
*one
;
484 one
= get_argument_from_call_expr(expr
->args
, 0);
485 type
= get_type(one
);
486 name
= expr_to_str(one
);
487 sm_msg("type of '%s' is: '%s'", name
, type_to_str(type
));
491 void check_debug(int id
)
494 add_function_hook("__smatch_about", &match_about
, NULL
);
495 add_function_hook("__smatch_all_values", &match_all_values
, NULL
);
496 add_function_hook("__smatch_state", &match_state
, NULL
);
497 add_function_hook("__smatch_states", &match_states
, NULL
);
498 add_function_hook("__smatch_value", &match_print_value
, NULL
);
499 add_function_hook("__smatch_implied", &match_print_implied
, NULL
);
500 add_function_hook("__smatch_implied_min", &match_print_implied_min
, NULL
);
501 add_function_hook("__smatch_implied_max", &match_print_implied_max
, NULL
);
502 add_function_hook("__smatch_hard_max", &match_print_hard_max
, NULL
);
503 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max
, NULL
);
504 add_function_hook("__smatch_absolute_min", &match_print_absolute_min
, NULL
);
505 add_function_hook("__smatch_absolute_max", &match_print_absolute_max
, NULL
);
506 add_function_hook("__smatch_sval_info", &match_sval_info
, NULL
);
507 add_function_hook("__smatch_member_name", &match_member_name
, NULL
);
508 add_function_hook("__smatch_possible", &match_possible
, NULL
);
509 add_function_hook("__smatch_cur_stree", &match_cur_stree
, NULL
);
510 add_function_hook("__smatch_strlen", &match_strlen
, NULL
);
511 add_function_hook("__smatch_buf_size", &match_buf_size
, NULL
);
512 add_function_hook("__smatch_buf_size_rl", &match_buf_size_rl
, NULL
);
513 add_function_hook("__smatch_note", &match_note
, NULL
);
514 add_function_hook("__smatch_dump_related", &match_dump_related
, NULL
);
515 add_function_hook("__smatch_compare", &match_compare
, NULL
);
516 add_function_hook("__smatch_debug_on", &match_debug_on
, NULL
);
517 add_function_hook("__smatch_debug_check", &match_debug_check
, NULL
);
518 add_function_hook("__smatch_debug_off", &match_debug_off
, NULL
);
519 add_function_hook("__smatch_local_debug_on", &match_local_debug_on
, NULL
);
520 add_function_hook("__smatch_local_debug_off", &match_local_debug_off
, NULL
);
521 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on
, NULL
);
522 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off
, NULL
);
523 add_function_hook("__smatch_intersection", &match_intersection
, NULL
);
524 add_function_hook("__smatch_type", &match_type
, NULL
);