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