1 /*
   2  * Copyright (C) 2009 Dan Carpenter.
   3  *
   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.
   8  *
   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.
  13  *
  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
  16  */
  17 
  18 #include "smatch.h"
  19 #include "smatch_slist.h"
  20 #include "smatch_extra.h"
  21 
  22 void show_sname_alloc(void);
  23 void show_data_range_alloc(void);
  24 void show_ptrlist_alloc(void);
  25 void show_rl_ptrlist_alloc(void);
  26 void show_sm_state_alloc(void);
  27 
  28 int local_debug;
  29 static int my_id;
  30 char *trace_variable;
  31 
  32 static void match_all_values(const char *fn, struct expression *expr, void *info)
  33 {
  34         struct stree *stree;
  35 
  36         stree = get_all_states_stree(SMATCH_EXTRA);
  37         __print_stree(stree);
  38         free_stree(&stree);
  39 }
  40 
  41 static void match_cur_stree(const char *fn, struct expression *expr, void *info)
  42 {
  43         __print_cur_stree();
  44 }
  45 
  46 static void match_state(const char *fn, struct expression *expr, void *info)
  47 {
  48         struct expression *check_arg, *state_arg;
  49         struct sm_state *sm;
  50         int found = 0;
  51 
  52         check_arg = get_argument_from_call_expr(expr->args, 0);
  53         if (check_arg->type != EXPR_STRING) {
  54                 sm_error("the check_name argument to %s is supposed to be a string literal", fn);
  55                 return;
  56         }
  57         state_arg = get_argument_from_call_expr(expr->args, 1);
  58         if (!state_arg || state_arg->type != EXPR_STRING) {
  59                 sm_error("the state_name argument to %s is supposed to be a string literal", fn);
  60                 return;
  61         }
  62 
  63         FOR_EACH_SM(__get_cur_stree(), sm) {
  64                 if (strcmp(check_name(sm->owner), check_arg->string->data) != 0)
  65                         continue;
  66                 if (strcmp(sm->name, state_arg->string->data) != 0)
  67                         continue;
  68                 sm_msg("'%s' = '%s'", sm->name, sm->state->name);
  69                 found = 1;
  70         } END_FOR_EACH_SM(sm);
  71 
  72         if (!found)
  73                 sm_msg("%s '%s' not found", check_arg->string->data, state_arg->string->data);
  74 }
  75 
  76 static void match_states(const char *fn, struct expression *expr, void *info)
  77 {
  78         struct expression *check_arg;
  79         struct sm_state *sm;
  80         int found = 0;
  81 
  82         check_arg = get_argument_from_call_expr(expr->args, 0);
  83         if (check_arg->type != EXPR_STRING) {
  84                 sm_error("the check_name argument to %s is supposed to be a string literal", fn);
  85                 return;
  86         }
  87 
  88         FOR_EACH_SM(__get_cur_stree(), sm) {
  89                 if (strcmp(check_name(sm->owner), check_arg->string->data) != 0)
  90                         continue;
  91                 sm_msg("%s", show_sm(sm));
  92                 found = 1;
  93         } END_FOR_EACH_SM(sm);
  94 
  95         if (found)
  96                 return;
  97 
  98         if (!id_from_name(check_arg->string->data))
  99                 sm_msg("invalid check name '%s'", check_arg->string->data);
 100         else
 101                 sm_msg("%s: no states", check_arg->string->data);
 102 }
 103 
 104 static void match_print_value(const char *fn, struct expression *expr, void *info)
 105 {
 106         struct stree *stree;
 107         struct sm_state *tmp;
 108         struct expression *arg_expr;
 109 
 110         arg_expr = get_argument_from_call_expr(expr->args, 0);
 111         if (arg_expr->type != EXPR_STRING) {
 112                 sm_error("the argument to %s is supposed to be a string literal", fn);
 113                 return;
 114         }
 115 
 116         stree = __get_cur_stree();
 117         FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
 118                 if (!strcmp(tmp->name, arg_expr->string->data))
 119                         sm_msg("%s = %s", tmp->name, tmp->state->name);
 120         } END_FOR_EACH_SM(tmp);
 121 }
 122 
 123 static void match_print_known(const char *fn, struct expression *expr, void *info)
 124 {
 125         struct expression *arg;
 126         struct range_list *rl = NULL;
 127         char *name;
 128         int known = 0;
 129         sval_t sval;
 130 
 131         arg = get_argument_from_call_expr(expr->args, 0);
 132         if (get_value(arg, &sval))
 133                 known = 1;
 134 
 135         get_implied_rl(arg, &rl);
 136 
 137         name = expr_to_str(arg);
 138         sm_msg("known: '%s' = '%s'.  implied = '%s'", name, known ? sval_to_str(sval) : "<unknown>", show_rl(rl));
 139         free_string(name);
 140 }
 141 
 142 static void match_print_implied(const char *fn, struct expression *expr, void *info)
 143 {
 144         struct expression *arg;
 145         struct range_list *rl = NULL;
 146         char *name;
 147 
 148         arg = get_argument_from_call_expr(expr->args, 0);
 149         get_implied_rl(arg, &rl);
 150 
 151         name = expr_to_str(arg);
 152         sm_msg("implied: %s = '%s'", name, show_rl(rl));
 153         free_string(name);
 154 }
 155 
 156 static void match_real_absolute(const char *fn, struct expression *expr, void *info)
 157 {
 158         struct expression *arg;
 159         struct range_list *rl = NULL;
 160         char *name;
 161 
 162         arg = get_argument_from_call_expr(expr->args, 0);
 163         get_real_absolute_rl(arg, &rl);
 164 
 165         name = expr_to_str(arg);
 166         sm_msg("real absolute: %s = '%s'", name, show_rl(rl));
 167         free_string(name);
 168 }
 169 
 170 static void match_print_implied_min(const char *fn, struct expression *expr, void *info)
 171 {
 172         struct expression *arg;
 173         sval_t sval;
 174         char *name;
 175 
 176         arg = get_argument_from_call_expr(expr->args, 0);
 177         name = expr_to_str(arg);
 178 
 179         if (get_implied_min(arg, &sval))
 180                 sm_msg("implied min: %s = %s", name, sval_to_str(sval));
 181         else
 182                 sm_msg("implied min: %s = <unknown>", name);
 183 
 184         free_string(name);
 185 }
 186 
 187 static void match_print_implied_max(const char *fn, struct expression *expr, void *info)
 188 {
 189         struct expression *arg;
 190         sval_t sval;
 191         char *name;
 192 
 193         arg = get_argument_from_call_expr(expr->args, 0);
 194         name = expr_to_str(arg);
 195 
 196         if (get_implied_max(arg, &sval))
 197                 sm_msg("implied max: %s = %s", name, sval_to_str(sval));
 198         else
 199                 sm_msg("implied max: %s = <unknown>", name);
 200 
 201         free_string(name);
 202 }
 203 
 204 static void match_user_rl(const char *fn, struct expression *expr, void *info)
 205 {
 206         struct expression *arg;
 207         struct range_list *rl = NULL;
 208         bool capped = false;
 209         char *name;
 210 
 211         arg = get_argument_from_call_expr(expr->args, 0);
 212         name = expr_to_str(arg);
 213 
 214         get_user_rl(arg, &rl);
 215         if (rl)
 216                 capped = user_rl_capped(arg);
 217         sm_msg("user rl: '%s' = '%s'%s", name, show_rl(rl), capped ? " (capped)" : "");
 218 
 219         free_string(name);
 220 }
 221 
 222 static void match_capped(const char *fn, struct expression *expr, void *info)
 223 {
 224         struct expression *arg;
 225         char *name;
 226 
 227         arg = get_argument_from_call_expr(expr->args, 0);
 228         name = expr_to_str(arg);
 229         sm_msg("'%s' = '%s'", name, is_capped(arg) ? "capped" : "not capped");
 230         free_string(name);
 231 }
 232 
 233 static void match_print_hard_max(const char *fn, struct expression *expr, void *info)
 234 {
 235         struct expression *arg;
 236         sval_t sval;
 237         char *name;
 238 
 239         arg = get_argument_from_call_expr(expr->args, 0);
 240         name = expr_to_str(arg);
 241 
 242         if (get_hard_max(arg, &sval))
 243                 sm_msg("hard max: %s = %s", name, sval_to_str(sval));
 244         else
 245                 sm_msg("hard max: %s = <unknown>", name);
 246 
 247         free_string(name);
 248 }
 249 
 250 static void match_print_fuzzy_max(const char *fn, struct expression *expr, void *info)
 251 {
 252         struct expression *arg;
 253         sval_t sval;
 254         char *name;
 255 
 256         arg = get_argument_from_call_expr(expr->args, 0);
 257         name = expr_to_str(arg);
 258 
 259         if (get_fuzzy_max(arg, &sval))
 260                 sm_msg("fuzzy max: %s = %s", name, sval_to_str(sval));
 261         else
 262                 sm_msg("fuzzy max: %s = <unknown>", name);
 263 
 264         free_string(name);
 265 }
 266 
 267 static void match_print_absolute(const char *fn, struct expression *expr, void *info)
 268 {
 269         struct expression *arg;
 270         struct range_list *rl;
 271         char *name;
 272 
 273         arg = get_argument_from_call_expr(expr->args, 0);
 274         name = expr_to_str(arg);
 275 
 276         get_absolute_rl(arg, &rl);
 277         sm_msg("absolute: %s = %s", name, show_rl(rl));
 278 
 279         free_string(name);
 280 }
 281 
 282 static void match_print_absolute_min(const char *fn, struct expression *expr, void *info)
 283 {
 284         struct expression *arg;
 285         sval_t sval;
 286         char *name;
 287 
 288         arg = get_argument_from_call_expr(expr->args, 0);
 289         name = expr_to_str(arg);
 290 
 291         if (get_absolute_min(arg, &sval))
 292                 sm_msg("absolute min: %s = %s", name, sval_to_str(sval));
 293         else
 294                 sm_msg("absolute min: %s = <unknown>", name);
 295 
 296         free_string(name);
 297 }
 298 
 299 static void match_print_absolute_max(const char *fn, struct expression *expr, void *info)
 300 {
 301         struct expression *arg;
 302         sval_t sval;
 303         char *name;
 304 
 305         arg = get_argument_from_call_expr(expr->args, 0);
 306         get_absolute_max(arg, &sval);
 307 
 308         name = expr_to_str(arg);
 309         sm_msg("absolute max: %s = %s", name, sval_to_str(sval));
 310         free_string(name);
 311 }
 312 
 313 static void match_sval_info(const char *fn, struct expression *expr, void *info)
 314 {
 315         struct expression *arg;
 316         sval_t sval;
 317         char *name;
 318 
 319         arg = get_argument_from_call_expr(expr->args, 0);
 320         name = expr_to_str(arg);
 321 
 322         if (!get_implied_value(arg, &sval)) {
 323                 sm_msg("no sval for '%s'", name);
 324                 goto free;
 325         }
 326 
 327         sm_msg("implied: %s %c%d ->value = %llx", name, sval_unsigned(sval) ? 'u' : 's', sval_bits(sval), sval.value);
 328 free:
 329         free_string(name);
 330 }
 331 
 332 static void match_member_name(const char *fn, struct expression *expr, void *info)
 333 {
 334         struct expression *arg;
 335         char *name, *member_name;
 336 
 337         arg = get_argument_from_call_expr(expr->args, 0);
 338         name = expr_to_str(arg);
 339         member_name = get_member_name(arg);
 340         sm_msg("member name: '%s => %s'", name, member_name);
 341         free_string(member_name);
 342         free_string(name);
 343 }
 344 
 345 static void print_possible(struct sm_state *sm)
 346 {
 347         struct sm_state *tmp;
 348 
 349         sm_msg("Possible values for %s", sm->name);
 350         FOR_EACH_PTR(sm->possible, tmp) {
 351                 printf("%s\n", tmp->state->name);
 352         } END_FOR_EACH_PTR(tmp);
 353         sm_msg("===");
 354 }
 355 
 356 static void match_possible(const char *fn, struct expression *expr, void *info)
 357 {
 358         struct stree *stree;
 359         struct sm_state *tmp;
 360         struct expression *arg_expr;
 361 
 362         arg_expr = get_argument_from_call_expr(expr->args, 0);
 363         if (arg_expr->type != EXPR_STRING) {
 364                 sm_error("the argument to %s is supposed to be a string literal", fn);
 365                 return;
 366         }
 367 
 368         stree = __get_cur_stree();
 369         FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
 370                 if (!strcmp(tmp->name, arg_expr->string->data))
 371                         print_possible(tmp);
 372         } END_FOR_EACH_SM(tmp);
 373 }
 374 
 375 static void match_strlen(const char *fn, struct expression *expr, void *info)
 376 {
 377         struct expression *arg;
 378         struct range_list *rl;
 379         char *name;
 380 
 381         arg = get_argument_from_call_expr(expr->args, 0);
 382         get_implied_strlen(arg, &rl);
 383 
 384         name = expr_to_str(arg);
 385         sm_msg("strlen: '%s' %s characters", name, show_rl(rl));
 386         free_string(name);
 387 }
 388 
 389 static void match_buf_size(const char *fn, struct expression *expr, void *info)
 390 {
 391         struct expression *arg, *comp;
 392         struct range_list *rl;
 393         int elements, bytes;
 394         char *name;
 395         char buf[256] = "";
 396         int limit_type;
 397         int n;
 398         sval_t sval;
 399 
 400         arg = get_argument_from_call_expr(expr->args, 0);
 401 
 402         elements = get_array_size(arg);
 403         bytes = get_array_size_bytes_max(arg);
 404         rl = get_array_size_bytes_rl(arg);
 405         comp = get_size_variable(arg, &limit_type);
 406 
 407         name = expr_to_str(arg);
 408         n = snprintf(buf, sizeof(buf), "buf size: '%s' %d elements, %d bytes", name, elements, bytes);
 409         free_string(name);
 410 
 411         if (!rl_to_sval(rl, &sval))
 412                 n += snprintf(buf + n, sizeof(buf) - n, " (rl = %s)", show_rl(rl));
 413 
 414         if (comp) {
 415                 name = expr_to_str(comp);
 416                 snprintf(buf + n, sizeof(buf) - n, "[size_var=%s %s]", limit_type_str(limit_type), name);
 417                 free_string(name);
 418         }
 419         sm_msg("%s", buf);
 420 }
 421 
 422 static void match_note(const char *fn, struct expression *expr, void *info)
 423 {
 424         struct expression *arg_expr;
 425 
 426         arg_expr = get_argument_from_call_expr(expr->args, 0);
 427         if (arg_expr->type != EXPR_STRING) {
 428                 sm_error("the argument to %s is supposed to be a string literal", fn);
 429                 return;
 430         }
 431         sm_msg("%s", arg_expr->string->data);
 432 }
 433 
 434 static void print_related(struct sm_state *sm)
 435 {
 436         struct relation *rel;
 437 
 438         if (!estate_related(sm->state))
 439                 return;
 440 
 441         sm_prefix();
 442         sm_printf("%s: ", sm->name);
 443         FOR_EACH_PTR(estate_related(sm->state), rel) {
 444                 sm_printf("%s ", rel->name);
 445         } END_FOR_EACH_PTR(rel);
 446         sm_printf("\n");
 447 }
 448 
 449 static void match_dump_related(const char *fn, struct expression *expr, void *info)
 450 {
 451         struct stree *stree;
 452         struct sm_state *tmp;
 453 
 454         stree = __get_cur_stree();
 455         FOR_EACH_MY_SM(SMATCH_EXTRA, stree, tmp) {
 456                 print_related(tmp);
 457         } END_FOR_EACH_SM(tmp);
 458 }
 459 
 460 static void match_compare(const char *fn, struct expression *expr, void *info)
 461 {
 462         struct expression *one, *two;
 463         char *one_name, *two_name;
 464         int comparison;
 465         char buf[16];
 466 
 467         one = get_argument_from_call_expr(expr->args, 0);
 468         two = get_argument_from_call_expr(expr->args, 1);
 469 
 470         comparison = get_comparison(one, two);
 471         if (!comparison)
 472                 snprintf(buf, sizeof(buf), "<none>");
 473         else
 474                 snprintf(buf, sizeof(buf), "%s", show_special(comparison));
 475 
 476         one_name = expr_to_str(one);
 477         two_name = expr_to_str(two);
 478 
 479         sm_msg("%s %s %s", one_name, buf, two_name);
 480 
 481         free_string(one_name);
 482         free_string(two_name);
 483 }
 484 
 485 static void match_debug_on(const char *fn, struct expression *expr, void *info)
 486 {
 487         option_debug = 1;
 488 }
 489 
 490 static void match_debug_check(const char *fn, struct expression *expr, void *info)
 491 {
 492         struct expression *arg;
 493 
 494         arg = get_argument_from_call_expr(expr->args, 0);
 495         if (!arg || arg->type != EXPR_STRING)
 496                 return;
 497         option_debug_check = arg->string->data;
 498         sm_msg("arg = '%s'", option_debug_check);
 499 }
 500 
 501 static void match_debug_off(const char *fn, struct expression *expr, void *info)
 502 {
 503         option_debug_check = (char *)"";
 504         option_debug = 0;
 505 }
 506 
 507 static void match_local_debug_on(const char *fn, struct expression *expr, void *info)
 508 {
 509         local_debug = 1;
 510 }
 511 
 512 static void match_local_debug_off(const char *fn, struct expression *expr, void *info)
 513 {
 514         local_debug = 0;
 515 }
 516 
 517 static void match_about(const char *fn, struct expression *expr, void *info)
 518 {
 519         struct expression *arg;
 520         struct sm_state *sm;
 521         char *name;
 522 
 523         sm_msg("---- about ----");
 524         match_print_implied(fn, expr, NULL);
 525         match_buf_size(fn, expr, NULL);
 526         match_strlen(fn, expr, NULL);
 527         match_real_absolute(fn, expr, NULL);
 528 
 529         arg = get_argument_from_call_expr(expr->args, 0);
 530         name = expr_to_str(arg);
 531         if (!name) {
 532                 sm_msg("info: not a straight forward variable.");
 533                 return;
 534         }
 535 
 536         FOR_EACH_SM(__get_cur_stree(), sm) {
 537                 if (strcmp(sm->name, name) != 0)
 538                         continue;
 539                 sm_msg("%s", show_sm(sm));
 540         } END_FOR_EACH_SM(sm);
 541 }
 542 
 543 static void match_intersection(const char *fn, struct expression *expr, void *info)
 544 {
 545         struct expression *one, *two;
 546         struct range_list *one_rl, *two_rl;
 547         struct range_list *res;
 548 
 549         one = get_argument_from_call_expr(expr->args, 0);
 550         two = get_argument_from_call_expr(expr->args, 1);
 551 
 552         get_absolute_rl(one, &one_rl);
 553         get_absolute_rl(two, &two_rl);
 554 
 555         res = rl_intersection(one_rl, two_rl);
 556         sm_msg("'%s' intersect '%s' is '%s'", show_rl(one_rl), show_rl(two_rl), show_rl(res));
 557 }
 558 
 559 static void match_type(const char *fn, struct expression *expr, void *info)
 560 {
 561         struct expression *one;
 562         struct symbol *type;
 563         char *name;
 564 
 565         one = get_argument_from_call_expr(expr->args, 0);
 566         type = get_type(one);
 567         name = expr_to_str(one);
 568         sm_msg("type of '%s' is: '%s'", name, type_to_str(type));
 569         free_string(name);
 570 }
 571 
 572 static int match_type_rl_return(struct expression *call, void *unused, struct range_list **rl)
 573 {
 574         struct expression *one, *two;
 575         struct symbol *type;
 576 
 577         one = get_argument_from_call_expr(call->args, 0);
 578         type = get_type(one);
 579 
 580         two = get_argument_from_call_expr(call->args, 1);
 581         if (!two || two->type != EXPR_STRING) {
 582                 sm_msg("expected: __smatch_type_rl(type, \"string\")");
 583                 return 0;
 584         }
 585         call_results_to_rl(call, type, two->string->data, rl);
 586         return 1;
 587 }
 588 
 589 static void print_left_right(struct sm_state *sm)
 590 {
 591         if (!sm)
 592                 return;
 593         if (!sm->left && !sm->right)
 594                 return;
 595 
 596         sm_printf("[ ");
 597         if (sm->left)
 598                 sm_printf("(%d: %s->'%s')", get_stree_id(sm->left->pool),  sm->left->name, sm->left->state->name);
 599         else
 600                 sm_printf(" - ");
 601 
 602 
 603         print_left_right(sm->left);
 604 
 605         if (sm->right)
 606                 sm_printf("(%d: %s->'%s')", get_stree_id(sm->right->pool),  sm->right->name, sm->right->state->name);
 607         else
 608                 sm_printf(" - ");
 609 
 610         print_left_right(sm->right);
 611 }
 612 
 613 static void match_print_merge_tree(const char *fn, struct expression *expr, void *info)
 614 {
 615         struct sm_state *sm;
 616         struct expression *arg;
 617         char *name;
 618 
 619         arg = get_argument_from_call_expr(expr->args, 0);
 620         name = expr_to_str(arg);
 621 
 622         sm = get_sm_state_expr(SMATCH_EXTRA, arg);
 623         if (!sm) {
 624                 sm_msg("no sm state for '%s'", name);
 625                 goto free;
 626         }
 627 
 628         sm_prefix();
 629         sm_printf("merge tree: %s -> %s", name, sm->state->name);
 630         print_left_right(sm);
 631         sm_printf("\n");
 632 
 633 free:
 634         free_string(name);
 635 }
 636 
 637 static void match_print_stree_id(const char *fn, struct expression *expr, void *info)
 638 {
 639         sm_msg("stree_id %d", __stree_id);
 640 }
 641 
 642 static void match_mtag(const char *fn, struct expression *expr, void *info)
 643 {
 644         struct expression *arg;
 645         char *name;
 646         mtag_t tag = 0;
 647         int offset = 0;
 648 
 649         arg = get_argument_from_call_expr(expr->args, 0);
 650         name = expr_to_str(arg);
 651         expr_to_mtag_offset(arg, &tag, &offset);
 652         sm_msg("mtag: '%s' => tag: %llu %d", name, tag, offset);
 653         free_string(name);
 654 }
 655 
 656 static void match_mtag_data_offset(const char *fn, struct expression *expr, void *info)
 657 {
 658         struct expression *arg;
 659         char *name;
 660         mtag_t tag = 0;
 661         int offset = -1;
 662 
 663         arg = get_argument_from_call_expr(expr->args, 0);
 664         name = expr_to_str(arg);
 665         expr_to_mtag_offset(arg, &tag, &offset);
 666         sm_msg("mtag: '%s' => tag: %lld, offset: %d", name, tag, offset);
 667         free_string(name);
 668 }
 669 
 670 static void match_container(const char *fn, struct expression *expr, void *info)
 671 {
 672         struct expression *container, *x;
 673         char *cont, *name, *str;
 674 
 675         container = get_argument_from_call_expr(expr->args, 0);
 676         x = get_argument_from_call_expr(expr->args, 1);
 677 
 678         str = get_container_name(container, x);
 679         cont = expr_to_str(container);
 680         name = expr_to_str(x);
 681         sm_msg("container: '%s' vs '%s' --> '%s'", cont, name, str);
 682         free_string(cont);
 683         free_string(name);
 684 }
 685 
 686 static void match_state_count(const char *fn, struct expression *expr, void *info)
 687 {
 688         sm_msg("state_count = %d\n", sm_state_counter);
 689 }
 690 
 691 static void match_mem(const char *fn, struct expression *expr, void *info)
 692 {
 693         show_sname_alloc();
 694         show_data_range_alloc();
 695         show_rl_ptrlist_alloc();
 696         show_ptrlist_alloc();
 697         sm_msg("%lu pools", get_pool_count());
 698         sm_msg("%d strees", unfree_stree);
 699         show_smatch_state_alloc();
 700         show_sm_state_alloc();
 701 }
 702 
 703 static void match_exit(const char *fn, struct expression *expr, void *info)
 704 {
 705         exit(0);
 706 }
 707 
 708 static struct stree *old_stree;
 709 static void trace_var(struct statement *stmt)
 710 {
 711         struct sm_state *sm, *old;
 712         int printed = 0;
 713 
 714         if (!trace_variable)
 715                 return;
 716         if (__inline_fn)
 717                 return;
 718 
 719         FOR_EACH_SM(__get_cur_stree(), sm) {
 720                 if (strcmp(sm->name, trace_variable) != 0)
 721                         continue;
 722                 old = get_sm_state_stree(old_stree, sm->owner, sm->name, sm->sym);
 723                 if (old && old->state == sm->state)
 724                         continue;
 725                 sm_msg("[%d] %s '%s': '%s' => '%s'", stmt->type,
 726                        check_name(sm->owner),
 727                        sm->name, old ? old->state->name : "<none>", sm->state->name);
 728                 printed = 1;
 729         } END_FOR_EACH_SM(sm);
 730 
 731         if (printed) {
 732                 free_stree(&old_stree);
 733                 old_stree = clone_stree(__get_cur_stree());
 734         }
 735 }
 736 
 737 static void free_old_stree(struct symbol *sym)
 738 {
 739         free_stree(&old_stree);
 740 }
 741 
 742 void check_debug(int id)
 743 {
 744         my_id = id;
 745         add_function_hook("__smatch_about", &match_about, NULL);
 746         add_function_hook("__smatch_all_values", &match_all_values, NULL);
 747         add_function_hook("__smatch_state", &match_state, NULL);
 748         add_function_hook("__smatch_states", &match_states, NULL);
 749         add_function_hook("__smatch_value", &match_print_value, NULL);
 750         add_function_hook("__smatch_known", &match_print_known, NULL);
 751         add_function_hook("__smatch_implied", &match_print_implied, NULL);
 752         add_function_hook("__smatch_implied_min", &match_print_implied_min, NULL);
 753         add_function_hook("__smatch_implied_max", &match_print_implied_max, NULL);
 754         add_function_hook("__smatch_user_rl", &match_user_rl, NULL);
 755         add_function_hook("__smatch_capped", &match_capped, NULL);
 756         add_function_hook("__smatch_hard_max", &match_print_hard_max, NULL);
 757         add_function_hook("__smatch_fuzzy_max", &match_print_fuzzy_max, NULL);
 758         add_function_hook("__smatch_absolute", &match_print_absolute, NULL);
 759         add_function_hook("__smatch_absolute_min", &match_print_absolute_min, NULL);
 760         add_function_hook("__smatch_absolute_max", &match_print_absolute_max, NULL);
 761         add_function_hook("__smatch_real_absolute", &match_real_absolute, NULL);
 762         add_function_hook("__smatch_sval_info", &match_sval_info, NULL);
 763         add_function_hook("__smatch_member_name", &match_member_name, NULL);
 764         add_function_hook("__smatch_possible", &match_possible, NULL);
 765         add_function_hook("__smatch_cur_stree", &match_cur_stree, NULL);
 766         add_function_hook("__smatch_strlen", &match_strlen, NULL);
 767         add_function_hook("__smatch_buf_size", &match_buf_size, NULL);
 768         add_function_hook("__smatch_note", &match_note, NULL);
 769         add_function_hook("__smatch_dump_related", &match_dump_related, NULL);
 770         add_function_hook("__smatch_compare", &match_compare, NULL);
 771         add_function_hook("__smatch_debug_on", &match_debug_on, NULL);
 772         add_function_hook("__smatch_debug_check", &match_debug_check, NULL);
 773         add_function_hook("__smatch_debug_off", &match_debug_off, NULL);
 774         add_function_hook("__smatch_local_debug_on", &match_local_debug_on, NULL);
 775         add_function_hook("__smatch_local_debug_off", &match_local_debug_off, NULL);
 776         add_function_hook("__smatch_intersection", &match_intersection, NULL);
 777         add_function_hook("__smatch_type", &match_type, NULL);
 778         add_implied_return_hook("__smatch_type_rl_helper", &match_type_rl_return, NULL);
 779         add_function_hook("__smatch_merge_tree", &match_print_merge_tree, NULL);
 780         add_function_hook("__smatch_stree_id", &match_print_stree_id, NULL);
 781         add_function_hook("__smatch_mtag", &match_mtag, NULL);
 782         add_function_hook("__smatch_mtag_data", &match_mtag_data_offset, NULL);
 783         add_function_hook("__smatch_state_count", &match_state_count, NULL);
 784         add_function_hook("__smatch_mem", &match_mem, NULL);
 785         add_function_hook("__smatch_exit", &match_exit, NULL);
 786         add_function_hook("__smatch_container", &match_container, NULL);
 787 
 788         add_hook(free_old_stree, AFTER_FUNC_HOOK);
 789         add_hook(trace_var, STMT_HOOK_AFTER);
 790 }