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_off(const char *fn
, struct expression
*expr
, void *info
)
405 static void match_local_debug_on(const char *fn
, struct expression
*expr
, void *info
)
410 static void match_local_debug_off(const char *fn
, struct expression
*expr
, void *info
)
415 static void match_debug_implied_on(const char *fn
, struct expression
*expr
, void *info
)
417 option_debug_implied
= 1;
420 static void match_debug_implied_off(const char *fn
, struct expression
*expr
, void *info
)
422 option_debug_implied
= 0;
425 static void match_about(const char *fn
, struct expression
*expr
, void *info
)
427 struct expression
*arg
;
431 sm_msg("---- about ----");
432 match_print_implied(fn
, expr
, NULL
);
433 match_buf_size(fn
, expr
, NULL
);
434 match_strlen(fn
, expr
, NULL
);
436 arg
= get_argument_from_call_expr(expr
->args
, 0);
437 name
= expr_to_str(arg
);
439 sm_msg("info: not a straight forward variable.");
443 FOR_EACH_SM(__get_cur_stree(), sm
) {
444 if (strcmp(sm
->name
, name
) != 0)
446 sm_msg("%s '%s' = '%s'", check_name(sm
->owner
), sm
->name
, sm
->state
->name
);
447 } END_FOR_EACH_SM(sm
);
450 void check_debug(int id
)
453 add_function_hook("__smatch_about", &match_about
, NULL
);
454 add_function_hook("__smatch_all_values", &match_all_values
, NULL
);
455 add_function_hook("__smatch_state", &match_state
, NULL
);
456 add_function_hook("__smatch_states", &match_states
, NULL
);
457 add_function_hook("__smatch_value", &match_print_value
, NULL
);
458 add_function_hook("__smatch_implied", &match_print_implied
, NULL
);
459 add_function_hook("__smatch_implied_min", &match_print_implied_min
, NULL
);
460 add_function_hook("__smatch_implied_max", &match_print_implied_max
, NULL
);
461 add_function_hook("__smatch_hard_max", &match_print_hard_max
, NULL
);
462 add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max
, NULL
);
463 add_function_hook("__smatch_absolute_min", &match_print_absolute_min
, NULL
);
464 add_function_hook("__smatch_absolute_max", &match_print_absolute_max
, NULL
);
465 add_function_hook("__smatch_sval_info", &match_sval_info
, NULL
);
466 add_function_hook("__smatch_member_name", &match_member_name
, NULL
);
467 add_function_hook("__smatch_possible", &match_possible
, NULL
);
468 add_function_hook("__smatch_cur_stree", &match_cur_stree
, NULL
);
469 add_function_hook("__smatch_strlen", &match_strlen
, NULL
);
470 add_function_hook("__smatch_buf_size", &match_buf_size
, NULL
);
471 add_function_hook("__smatch_buf_size_rl", &match_buf_size_rl
, NULL
);
472 add_function_hook("__smatch_note", &match_note
, NULL
);
473 add_function_hook("__smatch_dump_related", &match_dump_related
, NULL
);
474 add_function_hook("__smatch_compare", &match_compare
, NULL
);
475 add_function_hook("__smatch_debug_on", &match_debug_on
, NULL
);
476 add_function_hook("__smatch_debug_off", &match_debug_off
, NULL
);
477 add_function_hook("__smatch_local_debug_on", &match_local_debug_on
, NULL
);
478 add_function_hook("__smatch_local_debug_off", &match_local_debug_off
, NULL
);
479 add_function_hook("__smatch_debug_implied_on", &match_debug_implied_on
, NULL
);
480 add_function_hook("__smatch_debug_implied_off", &match_debug_implied_off
, NULL
);