read_config_file.c revision 13c939cb455d4881701e2da1bd770de0b3afc788
1/* 2 * This file is part of ltrace. 3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc. 4 * Copyright (C) 1998,1999,2003,2007,2008,2009 Juan Cespedes 5 * Copyright (C) 2006 Ian Wienand 6 * Copyright (C) 2006 Steve Fink 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as 10 * published by the Free Software Foundation; either version 2 of the 11 * License, or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 21 * 02110-1301 USA 22 */ 23 24/* getline is POSIX.1-2008. It was originally a GNU extension, and 25 * chances are uClibc still needs _GNU_SOURCE, but for now try it this 26 * way. */ 27#define _POSIX_C_SOURCE 200809L 28 29#include "config.h" 30 31#include <string.h> 32#include <stdlib.h> 33#include <ctype.h> 34#include <errno.h> 35#include <assert.h> 36 37#include "common.h" 38#include "output.h" 39#include "expr.h" 40#include "param.h" 41#include "printf.h" 42#include "prototype.h" 43#include "zero.h" 44#include "type.h" 45#include "lens.h" 46#include "lens_default.h" 47#include "lens_enum.h" 48 49/* Lifted from GCC: The ctype functions are often implemented as 50 * macros which do lookups in arrays using the parameter as the 51 * offset. If the ctype function parameter is a char, then gcc will 52 * (appropriately) warn that a "subscript has type char". Using a 53 * (signed) char as a subscript is bad because you may get negative 54 * offsets and thus it is not 8-bit safe. The CTYPE_CONV macro 55 * ensures that the parameter is cast to an unsigned char when a char 56 * is passed in. When an int is passed in, the parameter is left 57 * alone so we don't lose EOF. */ 58 59#define CTYPE_CONV(CH) \ 60 (sizeof(CH) == sizeof(unsigned char) ? (int)(unsigned char)(CH) : (int)(CH)) 61 62struct locus 63{ 64 const char *filename; 65 int line_no; 66}; 67 68static struct arg_type_info *parse_nonpointer_type(struct protolib *plib, 69 struct locus *loc, 70 char **str, 71 struct param **extra_param, 72 size_t param_num, 73 int *ownp, int *forwardp); 74static struct arg_type_info *parse_type(struct protolib *plib, 75 struct locus *loc, 76 char **str, struct param **extra_param, 77 size_t param_num, int *ownp, 78 int *forwardp); 79static struct arg_type_info *parse_lens(struct protolib *plib, 80 struct locus *loc, 81 char **str, struct param **extra_param, 82 size_t param_num, int *ownp, 83 int *forwardp); 84static int parse_enum(struct protolib *plib, struct locus *loc, 85 char **str, struct arg_type_info **retp, int *ownp); 86 87struct prototype *list_of_functions = NULL; 88 89static int 90parse_arg_type(char **name, enum arg_type *ret) 91{ 92 char *rest = NULL; 93 enum arg_type candidate; 94 95#define KEYWORD(KWD, TYPE) \ 96 do { \ 97 if (strncmp(*name, KWD, sizeof(KWD) - 1) == 0) { \ 98 rest = *name + sizeof(KWD) - 1; \ 99 candidate = TYPE; \ 100 goto ok; \ 101 } \ 102 } while (0) 103 104 KEYWORD("void", ARGTYPE_VOID); 105 KEYWORD("int", ARGTYPE_INT); 106 KEYWORD("uint", ARGTYPE_UINT); 107 KEYWORD("long", ARGTYPE_LONG); 108 KEYWORD("ulong", ARGTYPE_ULONG); 109 KEYWORD("char", ARGTYPE_CHAR); 110 KEYWORD("short", ARGTYPE_SHORT); 111 KEYWORD("ushort", ARGTYPE_USHORT); 112 KEYWORD("float", ARGTYPE_FLOAT); 113 KEYWORD("double", ARGTYPE_DOUBLE); 114 KEYWORD("array", ARGTYPE_ARRAY); 115 KEYWORD("struct", ARGTYPE_STRUCT); 116 117 /* Misspelling of int used in ltrace.conf that we used to 118 * ship. */ 119 KEYWORD("itn", ARGTYPE_INT); 120 121 assert(rest == NULL); 122 return -1; 123 124#undef KEYWORD 125 126ok: 127 if (isalnum(CTYPE_CONV(*rest))) 128 return -1; 129 130 *name = rest; 131 *ret = candidate; 132 return 0; 133} 134 135static void 136eat_spaces(char **str) { 137 while (**str == ' ') { 138 (*str)++; 139 } 140} 141 142static char * 143xstrndup(char *str, size_t len) { 144 char *ret = (char *) malloc(len + 1); 145 if (ret == NULL) { 146 report_global_error("malloc: %s", strerror(errno)); 147 return NULL; 148 } 149 strncpy(ret, str, len); 150 ret[len] = 0; 151 return ret; 152} 153 154static char * 155parse_ident(struct locus *loc, char **str) 156{ 157 char *ident = *str; 158 159 if (!isalpha(CTYPE_CONV(**str)) && **str != '_') { 160 report_error(loc->filename, loc->line_no, "bad identifier"); 161 return NULL; 162 } 163 164 while (**str && (isalnum(CTYPE_CONV(**str)) || **str == '_')) { 165 ++(*str); 166 } 167 168 return xstrndup(ident, *str - ident); 169} 170 171/* 172 Returns position in string at the left parenthesis which starts the 173 function's argument signature. Returns NULL on error. 174*/ 175static char * 176start_of_arg_sig(char *str) { 177 char *pos; 178 int stacked = 0; 179 180 if (!strlen(str)) 181 return NULL; 182 183 pos = &str[strlen(str)]; 184 do { 185 pos--; 186 if (pos < str) 187 return NULL; 188 while ((pos > str) && (*pos != ')') && (*pos != '(')) 189 pos--; 190 191 if (*pos == ')') 192 stacked++; 193 else if (*pos == '(') 194 stacked--; 195 else 196 return NULL; 197 198 } while (stacked > 0); 199 200 return (stacked == 0) ? pos : NULL; 201} 202 203static int 204parse_int(struct locus *loc, char **str, long *ret) 205{ 206 char *end; 207 long n = strtol(*str, &end, 0); 208 if (end == *str) { 209 report_error(loc->filename, loc->line_no, "bad number"); 210 return -1; 211 } 212 213 *str = end; 214 if (ret != NULL) 215 *ret = n; 216 return 0; 217} 218 219static int 220check_nonnegative(struct locus *loc, long l) 221{ 222 if (l < 0) { 223 report_error(loc->filename, loc->line_no, 224 "expected non-negative value, got %ld", l); 225 return -1; 226 } 227 return 0; 228} 229 230static int 231check_int(struct locus *loc, long l) 232{ 233 int i = l; 234 if ((long)i != l) { 235 report_error(loc->filename, loc->line_no, 236 "Number too large: %ld", l); 237 return -1; 238 } 239 return 0; 240} 241 242static int 243parse_char(struct locus *loc, char **str, char expected) 244{ 245 if (**str != expected) { 246 report_error(loc->filename, loc->line_no, 247 "expected '%c', got '%c'", expected, **str); 248 return -1; 249 } 250 251 ++*str; 252 return 0; 253} 254 255static struct expr_node *parse_argnum(struct locus *loc, 256 char **str, int *ownp, int zero); 257 258static struct expr_node * 259parse_zero(struct locus *loc, char **str, struct expr_node *ret, int *ownp) 260{ 261 eat_spaces(str); 262 if (**str == '(') { 263 ++*str; 264 int own; 265 struct expr_node *arg = parse_argnum(loc, str, &own, 0); 266 if (arg == NULL) 267 return NULL; 268 if (parse_char(loc, str, ')') < 0) { 269 fail: 270 expr_destroy(arg); 271 free(arg); 272 return NULL; 273 } 274 275 struct expr_node *ret = build_zero_w_arg(arg, own); 276 if (ret == NULL) 277 goto fail; 278 *ownp = 1; 279 return ret; 280 281 } else { 282 free(ret); 283 *ownp = 0; 284 return expr_node_zero(); 285 } 286} 287 288static int 289wrap_in_zero(struct expr_node **nodep) 290{ 291 struct expr_node *n = build_zero_w_arg(*nodep, 1); 292 if (n == NULL) 293 return -1; 294 *nodep = n; 295 return 0; 296} 297 298/* 299 * Input: 300 * argN : The value of argument #N, counting from 1 301 * eltN : The value of element #N of the containing structure 302 * retval : The return value 303 * N : The numeric value N 304 */ 305static struct expr_node * 306parse_argnum(struct locus *loc, char **str, int *ownp, int zero) 307{ 308 struct expr_node *expr = malloc(sizeof(*expr)); 309 if (expr == NULL) 310 return NULL; 311 312 if (isdigit(CTYPE_CONV(**str))) { 313 long l; 314 if (parse_int(loc, str, &l) < 0 315 || check_nonnegative(loc, l) < 0 316 || check_int(loc, l) < 0) 317 goto fail; 318 319 expr_init_const_word(expr, l, type_get_simple(ARGTYPE_LONG), 0); 320 321 if (zero && wrap_in_zero(&expr) < 0) 322 goto fail; 323 324 *ownp = 1; 325 return expr; 326 327 } else { 328 char *const name = parse_ident(loc, str); 329 if (name == NULL) { 330 fail_ident: 331 free(name); 332 goto fail; 333 } 334 335 int is_arg = strncmp(name, "arg", 3) == 0; 336 if (is_arg || strncmp(name, "elt", 3) == 0) { 337 long l; 338 char *num = name + 3; 339 if (parse_int(loc, &num, &l) < 0 340 || check_int(loc, l) < 0) 341 goto fail_ident; 342 343 if (is_arg) { 344 if (l == 0) 345 expr_init_named(expr, "retval", 0); 346 else 347 expr_init_argno(expr, l - 1); 348 } else { 349 struct expr_node *e_up = malloc(sizeof(*e_up)); 350 struct expr_node *e_ix = malloc(sizeof(*e_ix)); 351 if (e_up == NULL || e_ix == NULL) { 352 free(e_up); 353 free(e_ix); 354 goto fail_ident; 355 } 356 357 expr_init_up(e_up, expr_self(), 0); 358 struct arg_type_info *ti 359 = type_get_simple(ARGTYPE_LONG); 360 expr_init_const_word(e_ix, l - 1, ti, 0); 361 expr_init_index(expr, e_up, 1, e_ix, 1); 362 } 363 364 } else if (strcmp(name, "retval") == 0) { 365 expr_init_named(expr, "retval", 0); 366 367 } else if (strcmp(name, "zero") == 0) { 368 struct expr_node *ret 369 = parse_zero(loc, str, expr, ownp); 370 if (ret == NULL) 371 goto fail_ident; 372 free(name); 373 return ret; 374 375 } else { 376 report_error(loc->filename, loc->line_no, 377 "Unknown length specifier: '%s'", name); 378 goto fail_ident; 379 } 380 381 if (zero && wrap_in_zero(&expr) < 0) 382 goto fail_ident; 383 384 free(name); 385 *ownp = 1; 386 return expr; 387 } 388 389fail: 390 free(expr); 391 return NULL; 392} 393 394static struct arg_type_info * 395parse_typedef_name(struct protolib *plib, char **str) 396{ 397 char *end = *str; 398 while (*end && (isalnum(CTYPE_CONV(*end)) || *end == '_')) 399 ++end; 400 if (end == *str) 401 return NULL; 402 403 size_t len = end - *str; 404 char buf[len + 1]; 405 memcpy(buf, *str, len); 406 *str += len; 407 buf[len] = 0; 408 409 struct named_type *nt = protolib_lookup_type(plib, buf); 410 if (nt == NULL) 411 return NULL; 412 return nt->info; 413} 414 415static int 416parse_typedef(struct protolib *plib, struct locus *loc, char **str) 417{ 418 (*str) += strlen("typedef"); 419 eat_spaces(str); 420 char *name = parse_ident(loc, str); 421 422 /* Look through the typedef list whether we already have a 423 * forward of this type. If we do, it must be forward 424 * structure. */ 425 struct named_type *forward = protolib_lookup_type(plib, name); 426 if (forward != NULL 427 && (forward->info->type != ARGTYPE_STRUCT 428 || !forward->forward)) { 429 report_error(loc->filename, loc->line_no, 430 "Redefinition of typedef '%s'", name); 431 err: 432 free(name); 433 return -1; 434 } 435 436 // Skip = sign 437 eat_spaces(str); 438 if (parse_char(loc, str, '=') < 0) 439 goto err; 440 eat_spaces(str); 441 442 int fwd = 0; 443 int own = 0; 444 struct arg_type_info *info 445 = parse_lens(plib, loc, str, NULL, 0, &own, &fwd); 446 if (info == NULL) 447 goto err; 448 449 struct named_type this_nt; 450 named_type_init(&this_nt, info, own); 451 this_nt.forward = fwd; 452 453 if (forward == NULL) { 454 if (protolib_add_named_type(plib, name, 1, &this_nt) < 0) { 455 named_type_destroy(&this_nt); 456 goto err; 457 } 458 return 0; 459 } 460 461 /* If we are defining a forward, make sure the definition is a 462 * structure as well. */ 463 if (this_nt.info->type != ARGTYPE_STRUCT) { 464 report_error(loc->filename, loc->line_no, 465 "Definition of forward '%s' must be a structure.", 466 name); 467 named_type_destroy(&this_nt); 468 goto err; 469 } 470 471 /* Now move guts of the actual type over to the forward type. 472 * We can't just move pointers around, because references to 473 * forward must stay intact. */ 474 assert(this_nt.own_type); 475 type_destroy(forward->info); 476 *forward->info = *this_nt.info; 477 forward->forward = 0; 478 free(this_nt.info); 479 free(name); 480 return 0; 481} 482 483/* Syntax: struct ( type,type,type,... ) */ 484static int 485parse_struct(struct protolib *plib, struct locus *loc, 486 char **str, struct arg_type_info *info, 487 int *forwardp) 488{ 489 eat_spaces(str); 490 491 if (**str == ';') { 492 if (forwardp == NULL) { 493 report_error(loc->filename, loc->line_no, 494 "Forward struct can be declared only " 495 "directly after a typedef."); 496 return -1; 497 } 498 499 /* Forward declaration is currently handled as an 500 * empty struct. */ 501 type_init_struct(info); 502 *forwardp = 1; 503 return 0; 504 } 505 506 if (parse_char(loc, str, '(') < 0) 507 return -1; 508 509 eat_spaces(str); // Empty arg list with whitespace inside 510 511 type_init_struct(info); 512 513 while (1) { 514 eat_spaces(str); 515 if (**str == 0 || **str == ')') { 516 parse_char(loc, str, ')'); 517 return 0; 518 } 519 520 /* Field delimiter. */ 521 if (type_struct_size(info) > 0) 522 parse_char(loc, str, ','); 523 524 eat_spaces(str); 525 int own; 526 struct arg_type_info *field 527 = parse_lens(plib, loc, str, NULL, 0, &own, NULL); 528 if (field == NULL || type_struct_add(info, field, own)) { 529 type_destroy(info); 530 return -1; 531 } 532 } 533} 534 535static int 536parse_string(struct protolib *plib, struct locus *loc, 537 char **str, struct arg_type_info **retp, int *ownp) 538{ 539 struct arg_type_info *info = malloc(sizeof(*info) * 2); 540 if (info == NULL) { 541 fail: 542 free(info); 543 return -1; 544 } 545 546 struct expr_node *length; 547 int own_length; 548 int with_arg = 0; 549 550 if (isdigit(CTYPE_CONV(**str))) { 551 /* string0 is string[retval], length is zero(retval) 552 * stringN is string[argN], length is zero(argN) */ 553 long l; 554 if (parse_int(loc, str, &l) < 0 555 || check_int(loc, l) < 0) 556 goto fail; 557 558 struct expr_node *length_arg = malloc(sizeof(*length_arg)); 559 if (length_arg == NULL) 560 goto fail; 561 562 if (l == 0) 563 expr_init_named(length_arg, "retval", 0); 564 else 565 expr_init_argno(length_arg, l - 1); 566 567 length = build_zero_w_arg(length_arg, 1); 568 if (length == NULL) { 569 expr_destroy(length_arg); 570 free(length_arg); 571 goto fail; 572 } 573 own_length = 1; 574 575 } else { 576 eat_spaces(str); 577 if (**str == '[') { 578 (*str)++; 579 eat_spaces(str); 580 581 length = parse_argnum(loc, str, &own_length, 1); 582 if (length == NULL) 583 goto fail; 584 585 eat_spaces(str); 586 parse_char(loc, str, ']'); 587 588 } else if (**str == '(') { 589 /* Usage of "string" as lens. */ 590 ++*str; 591 592 free(info); 593 594 eat_spaces(str); 595 info = parse_type(plib, loc, str, NULL, 0, ownp, NULL); 596 if (info == NULL) 597 goto fail; 598 599 eat_spaces(str); 600 parse_char(loc, str, ')'); 601 602 with_arg = 1; 603 604 } else { 605 /* It was just a simple string after all. */ 606 length = expr_node_zero(); 607 own_length = 0; 608 } 609 } 610 611 /* String is a pointer to array of chars. */ 612 if (!with_arg) { 613 type_init_array(&info[1], type_get_simple(ARGTYPE_CHAR), 0, 614 length, own_length); 615 616 type_init_pointer(&info[0], &info[1], 0); 617 *ownp = 1; 618 } 619 620 info->lens = &string_lens; 621 info->own_lens = 0; 622 623 *retp = info; 624 return 0; 625} 626 627static int 628build_printf_pack(struct locus *loc, struct param **packp, size_t param_num) 629{ 630 if (packp == NULL) { 631 report_error(loc->filename, loc->line_no, 632 "'format' type in unexpected context"); 633 return -1; 634 } 635 if (*packp != NULL) { 636 report_error(loc->filename, loc->line_no, 637 "only one 'format' type per function supported"); 638 return -1; 639 } 640 641 *packp = malloc(sizeof(**packp)); 642 if (*packp == NULL) 643 return -1; 644 645 struct expr_node *node = malloc(sizeof(*node)); 646 if (node == NULL) { 647 free(*packp); 648 return -1; 649 } 650 651 expr_init_argno(node, param_num); 652 653 param_pack_init_printf(*packp, node, 1); 654 655 return 0; 656} 657 658/* Match and consume KWD if it's next in stream, and return 0. 659 * Otherwise return negative number. */ 660static int 661try_parse_kwd(char **str, const char *kwd) 662{ 663 size_t len = strlen(kwd); 664 if (strncmp(*str, kwd, len) == 0 665 && !isalnum(CTYPE_CONV((*str)[len]))) { 666 (*str) += len; 667 return 0; 668 } 669 return -1; 670} 671 672/* Make a copy of INFO and set the *OWN bit if it's not already 673 * owned. */ 674static int 675unshare_type_info(struct locus *loc, struct arg_type_info **infop, int *ownp) 676{ 677 if (*ownp) 678 return 0; 679 680 struct arg_type_info *ninfo = malloc(sizeof(*ninfo)); 681 if (ninfo == NULL) { 682 report_error(loc->filename, loc->line_no, 683 "malloc: %s", strerror(errno)); 684 return -1; 685 } 686 *ninfo = **infop; 687 *infop = ninfo; 688 *ownp = 1; 689 return 0; 690} 691 692/* XXX extra_param and param_num are a kludge to get in 693 * backward-compatible support for "format" parameter type. The 694 * latter is only valid if the former is non-NULL, which is only in 695 * top-level context. */ 696static int 697parse_alias(struct protolib *plib, struct locus *loc, 698 char **str, struct arg_type_info **retp, int *ownp, 699 struct param **extra_param, size_t param_num) 700{ 701 /* For backward compatibility, we need to support things like 702 * stringN (which is like string[argN], string[N], and also 703 * bare string. We might, in theory, replace this by 704 * preprocessing configure file sources with M4, but for now, 705 * "string" is syntax. */ 706 if (strncmp(*str, "string", 6) == 0) { 707 (*str) += 6; 708 return parse_string(plib, loc, str, retp, ownp); 709 710 } else if (try_parse_kwd(str, "format") >= 0 711 && extra_param != NULL) { 712 /* For backward compatibility, format is parsed as 713 * "string", but it smuggles to the parameter list of 714 * a function a "printf" argument pack with this 715 * parameter as argument. */ 716 if (parse_string(plib, loc, str, retp, ownp) < 0) 717 return -1; 718 719 return build_printf_pack(loc, extra_param, param_num); 720 721 } else if (try_parse_kwd(str, "enum") >=0) { 722 723 return parse_enum(plib, loc, str, retp, ownp); 724 725 } else { 726 *retp = NULL; 727 return 0; 728 } 729} 730 731/* Syntax: array ( type, N|argN ) */ 732static int 733parse_array(struct protolib *plib, struct locus *loc, 734 char **str, struct arg_type_info *info) 735{ 736 eat_spaces(str); 737 if (parse_char(loc, str, '(') < 0) 738 return -1; 739 740 eat_spaces(str); 741 int own; 742 struct arg_type_info *elt_info 743 = parse_lens(plib, loc, str, NULL, 0, &own, NULL); 744 if (elt_info == NULL) 745 return -1; 746 747 eat_spaces(str); 748 parse_char(loc, str, ','); 749 750 eat_spaces(str); 751 int own_length; 752 struct expr_node *length = parse_argnum(loc, str, &own_length, 0); 753 if (length == NULL) { 754 if (own) { 755 type_destroy(elt_info); 756 free(elt_info); 757 } 758 return -1; 759 } 760 761 type_init_array(info, elt_info, own, length, own_length); 762 763 eat_spaces(str); 764 parse_char(loc, str, ')'); 765 return 0; 766} 767 768/* Syntax: 769 * enum (keyname[=value],keyname[=value],... ) 770 * enum<type> (keyname[=value],keyname[=value],... ) 771 */ 772static int 773parse_enum(struct protolib *plib, struct locus *loc, char **str, 774 struct arg_type_info **retp, int *ownp) 775{ 776 /* Optional type argument. */ 777 eat_spaces(str); 778 if (**str == '[') { 779 parse_char(loc, str, '['); 780 eat_spaces(str); 781 *retp = parse_nonpointer_type(plib, loc, str, NULL, 0, ownp, 0); 782 if (*retp == NULL) 783 return -1; 784 785 if (!type_is_integral((*retp)->type)) { 786 report_error(loc->filename, loc->line_no, 787 "integral type required as enum argument"); 788 fail: 789 if (*ownp) { 790 /* This also releases associated lens 791 * if any was set so far. */ 792 type_destroy(*retp); 793 free(*retp); 794 } 795 return -1; 796 } 797 798 eat_spaces(str); 799 if (parse_char(loc, str, ']') < 0) 800 goto fail; 801 802 } else { 803 *retp = type_get_simple(ARGTYPE_INT); 804 *ownp = 0; 805 } 806 807 /* We'll need to set the lens, so unshare. */ 808 if (unshare_type_info(loc, retp, ownp) < 0) 809 goto fail; 810 811 eat_spaces(str); 812 if (parse_char(loc, str, '(') < 0) 813 goto fail; 814 815 struct enum_lens *lens = malloc(sizeof(*lens)); 816 if (lens == NULL) { 817 report_error(loc->filename, loc->line_no, 818 "malloc enum lens: %s", strerror(errno)); 819 return -1; 820 } 821 822 lens_init_enum(lens); 823 (*retp)->lens = &lens->super; 824 (*retp)->own_lens = 1; 825 826 long last_val = 0; 827 while (1) { 828 eat_spaces(str); 829 if (**str == 0 || **str == ')') { 830 parse_char(loc, str, ')'); 831 return 0; 832 } 833 834 /* Field delimiter. XXX should we support the C 835 * syntax, where the enumeration can end in pending 836 * comma? */ 837 if (lens_enum_size(lens) > 0) 838 parse_char(loc, str, ','); 839 840 eat_spaces(str); 841 char *key = parse_ident(loc, str); 842 if (key == NULL) { 843 err: 844 free(key); 845 goto fail; 846 } 847 848 if (**str == '=') { 849 ++*str; 850 eat_spaces(str); 851 if (parse_int(loc, str, &last_val) < 0) 852 goto err; 853 } 854 855 struct value *value = malloc(sizeof(*value)); 856 if (value == NULL) 857 goto err; 858 value_init_detached(value, NULL, *retp, 0); 859 value_set_word(value, last_val); 860 861 if (lens_enum_add(lens, key, 1, value, 1) < 0) 862 goto err; 863 864 last_val++; 865 } 866 867 return 0; 868} 869 870static struct arg_type_info * 871parse_nonpointer_type(struct protolib *plib, struct locus *loc, 872 char **str, struct param **extra_param, size_t param_num, 873 int *ownp, int *forwardp) 874{ 875 const char *orig_str = *str; 876 enum arg_type type; 877 if (parse_arg_type(str, &type) < 0) { 878 struct arg_type_info *simple; 879 if (parse_alias(plib, loc, str, &simple, 880 ownp, extra_param, param_num) < 0) 881 return NULL; 882 if (simple == NULL) 883 simple = parse_typedef_name(plib, str); 884 if (simple != NULL) { 885 *ownp = 0; 886 return simple; 887 } 888 889 report_error(loc->filename, loc->line_no, 890 "unknown type around '%s'", orig_str); 891 return NULL; 892 } 893 894 /* For some types that's all we need. */ 895 switch (type) { 896 case ARGTYPE_VOID: 897 case ARGTYPE_INT: 898 case ARGTYPE_UINT: 899 case ARGTYPE_LONG: 900 case ARGTYPE_ULONG: 901 case ARGTYPE_CHAR: 902 case ARGTYPE_SHORT: 903 case ARGTYPE_USHORT: 904 case ARGTYPE_FLOAT: 905 case ARGTYPE_DOUBLE: 906 *ownp = 0; 907 return type_get_simple(type); 908 909 case ARGTYPE_ARRAY: 910 case ARGTYPE_STRUCT: 911 break; 912 913 case ARGTYPE_POINTER: 914 /* Pointer syntax is not based on keyword, so we 915 * should never get this type. */ 916 assert(type != ARGTYPE_POINTER); 917 abort(); 918 } 919 920 struct arg_type_info *info = malloc(sizeof(*info)); 921 if (info == NULL) { 922 report_error(loc->filename, loc->line_no, 923 "malloc: %s", strerror(errno)); 924 return NULL; 925 } 926 *ownp = 1; 927 928 if (type == ARGTYPE_ARRAY) { 929 if (parse_array(plib, loc, str, info) < 0) { 930 fail: 931 free(info); 932 return NULL; 933 } 934 } else { 935 assert(type == ARGTYPE_STRUCT); 936 if (parse_struct(plib, loc, str, info, forwardp) < 0) 937 goto fail; 938 } 939 940 return info; 941} 942 943static struct named_lens { 944 const char *name; 945 struct lens *lens; 946} lenses[] = { 947 { "hide", &blind_lens }, 948 { "octal", &octal_lens }, 949 { "oct", &octal_lens }, 950 { "bitvec", &bitvect_lens }, 951 { "hex", &hex_lens }, 952 { "bool", &bool_lens }, 953 { "guess", &guess_lens }, 954}; 955 956static struct lens * 957name2lens(char **str, int *own_lensp) 958{ 959 size_t i; 960 for (i = 0; i < sizeof(lenses)/sizeof(*lenses); ++i) 961 if (try_parse_kwd(str, lenses[i].name) == 0) { 962 *own_lensp = 0; 963 return lenses[i].lens; 964 } 965 966 return NULL; 967} 968 969static struct arg_type_info * 970parse_type(struct protolib *plib, struct locus *loc, char **str, 971 struct param **extra_param, size_t param_num, 972 int *ownp, int *forwardp) 973{ 974 struct arg_type_info *info 975 = parse_nonpointer_type(plib, loc, str, extra_param, 976 param_num, ownp, forwardp); 977 if (info == NULL) 978 return NULL; 979 980 while (1) { 981 eat_spaces(str); 982 if (**str == '*') { 983 struct arg_type_info *outer = malloc(sizeof(*outer)); 984 if (outer == NULL) { 985 if (*ownp) { 986 type_destroy(info); 987 free(info); 988 } 989 report_error(loc->filename, loc->line_no, 990 "malloc: %s", strerror(errno)); 991 return NULL; 992 } 993 type_init_pointer(outer, info, *ownp); 994 *ownp = 1; 995 (*str)++; 996 info = outer; 997 } else 998 break; 999 } 1000 return info; 1001} 1002 1003static struct arg_type_info * 1004parse_lens(struct protolib *plib, struct locus *loc, 1005 char **str, struct param **extra_param, 1006 size_t param_num, int *ownp, int *forwardp) 1007{ 1008 int own_lens; 1009 struct lens *lens = name2lens(str, &own_lens); 1010 int has_args = 1; 1011 struct arg_type_info *info; 1012 if (lens != NULL) { 1013 eat_spaces(str); 1014 1015 /* Octal lens gets special treatment, because of 1016 * backward compatibility. */ 1017 if (lens == &octal_lens && **str != '(') { 1018 has_args = 0; 1019 info = type_get_simple(ARGTYPE_INT); 1020 *ownp = 0; 1021 } else if (parse_char(loc, str, '(') < 0) { 1022 report_error(loc->filename, loc->line_no, 1023 "expected type argument after the lens"); 1024 return NULL; 1025 } 1026 } 1027 1028 if (has_args) { 1029 eat_spaces(str); 1030 info = parse_type(plib, loc, str, extra_param, param_num, 1031 ownp, forwardp); 1032 if (info == NULL) { 1033 fail: 1034 if (own_lens && lens != NULL) 1035 lens_destroy(lens); 1036 return NULL; 1037 } 1038 } 1039 1040 if (lens != NULL && has_args) { 1041 eat_spaces(str); 1042 parse_char(loc, str, ')'); 1043 } 1044 1045 /* We can't modify shared types. Make a copy if we have a 1046 * lens. */ 1047 if (lens != NULL && unshare_type_info(loc, &info, ownp) < 0) 1048 goto fail; 1049 1050 if (lens != NULL) { 1051 info->lens = lens; 1052 info->own_lens = own_lens; 1053 } 1054 1055 return info; 1056} 1057 1058static int 1059param_is_void(struct param *param) 1060{ 1061 return param->flavor == PARAM_FLAVOR_TYPE 1062 && param->u.type.type->type == ARGTYPE_VOID; 1063} 1064 1065static struct arg_type_info * 1066get_hidden_int(void) 1067{ 1068 static struct arg_type_info info, *pinfo = NULL; 1069 if (pinfo != NULL) 1070 return pinfo; 1071 1072 info = *type_get_simple(ARGTYPE_INT); 1073 info.lens = &blind_lens; 1074 pinfo = &info; 1075 1076 return pinfo; 1077} 1078 1079static enum callback_status 1080void_to_hidden_int(struct prototype *proto, struct param *param, void *data) 1081{ 1082 struct locus *loc = data; 1083 if (param_is_void(param)) { 1084 report_warning(loc->filename, loc->line_no, 1085 "void parameter assumed to be 'hide(int)'"); 1086 1087 static struct arg_type_info *type = NULL; 1088 if (type == NULL) 1089 type = get_hidden_int(); 1090 param_destroy(param); 1091 param_init_type(param, type, 0); 1092 } 1093 return CBS_CONT; 1094} 1095 1096static int 1097process_line(struct protolib *plib, struct locus *loc, char *buf) 1098{ 1099 char *str = buf; 1100 char *tmp; 1101 1102 debug(3, "Reading line %d of `%s'", loc->line_no, loc->filename); 1103 eat_spaces(&str); 1104 1105 /* A comment or empty line. */ 1106 if (*str == ';' || *str == 0 || *str == '\n') 1107 return 0; 1108 1109 if (strncmp(str, "typedef", 7) == 0) { 1110 parse_typedef(plib, loc, &str); 1111 return 0; 1112 } 1113 1114 struct prototype fun; 1115 prototype_init(&fun); 1116 1117 char *proto_name = NULL; 1118 int own; 1119 fun.return_info = parse_lens(plib, loc, &str, NULL, 0, &own, NULL); 1120 if (fun.return_info == NULL) { 1121 err: 1122 debug(3, " Skipping line %d", loc->line_no); 1123 prototype_destroy(&fun); 1124 free(proto_name); 1125 return -1; 1126 } 1127 fun.own_return_info = own; 1128 debug(4, " return_type = %d", fun.return_info->type); 1129 1130 eat_spaces(&str); 1131 tmp = start_of_arg_sig(str); 1132 if (tmp == NULL) { 1133 report_error(loc->filename, loc->line_no, "syntax error"); 1134 goto err; 1135 } 1136 *tmp = '\0'; 1137 1138 proto_name = strdup(str); 1139 if (proto_name == NULL) { 1140 oom: 1141 report_error(loc->filename, loc->line_no, 1142 "%s", strerror(errno)); 1143 goto err; 1144 } 1145 1146 str = tmp + 1; 1147 debug(3, " name = %s", proto_name); 1148 1149 struct param *extra_param = NULL; 1150 int have_stop = 0; 1151 1152 while (1) { 1153 eat_spaces(&str); 1154 if (*str == ')') 1155 break; 1156 1157 if (str[0] == '+') { 1158 if (have_stop == 0) { 1159 struct param param; 1160 param_init_stop(¶m); 1161 if (prototype_push_param(&fun, ¶m) < 0) 1162 goto oom; 1163 have_stop = 1; 1164 } 1165 str++; 1166 } 1167 1168 int own; 1169 size_t param_num = prototype_num_params(&fun) - have_stop; 1170 struct arg_type_info *type 1171 = parse_lens(plib, loc, &str, &extra_param, 1172 param_num, &own, NULL); 1173 if (type == NULL) { 1174 report_error(loc->filename, loc->line_no, 1175 "unknown argument type"); 1176 goto err; 1177 } 1178 1179 struct param param; 1180 param_init_type(¶m, type, own); 1181 if (prototype_push_param(&fun, ¶m) < 0) 1182 goto oom; 1183 1184 eat_spaces(&str); 1185 if (*str == ',') { 1186 str++; 1187 continue; 1188 } else if (*str == ')') { 1189 continue; 1190 } else { 1191 if (str[strlen(str) - 1] == '\n') 1192 str[strlen(str) - 1] = '\0'; 1193 report_error(loc->filename, loc->line_no, 1194 "syntax error around \"%s\"", str); 1195 goto err; 1196 } 1197 } 1198 1199 /* We used to allow void parameter as a synonym to an argument 1200 * that shouldn't be displayed. But backends really need to 1201 * know the exact type that they are dealing with. The proper 1202 * way to do this these days is to use the hide lens. 1203 * 1204 * So if there are any voids in the parameter list, show a 1205 * warning and assume that they are ints. If there's a sole 1206 * void, assume the function doesn't take any arguments. The 1207 * latter is conservative, we can drop the argument 1208 * altogether, instead of fetching and then not showing it, 1209 * without breaking any observable behavior. */ 1210 if (prototype_num_params(&fun) == 1 1211 && param_is_void(prototype_get_nth_param(&fun, 0))) { 1212 if (0) 1213 /* Don't show this warning. Pre-0.7.0 1214 * ltrace.conf often used this idiom. This 1215 * should be postponed until much later, when 1216 * extant uses are likely gone. */ 1217 report_warning(loc->filename, loc->line_no, 1218 "sole void parameter ignored"); 1219 prototype_destroy_nth_param(&fun, 0); 1220 } else { 1221 prototype_each_param(&fun, NULL, void_to_hidden_int, loc); 1222 } 1223 1224 if (extra_param != NULL) { 1225 prototype_push_param(&fun, extra_param); 1226 free(extra_param); 1227 } 1228 1229 if (protolib_add_prototype(plib, proto_name, 1, &fun) < 0) { 1230 report_error(loc->filename, loc->line_no, 1231 "couldn't add prototype: %s", 1232 strerror(errno)); 1233 goto err; 1234 } 1235 1236 return 0; 1237} 1238 1239int 1240read_config_file(struct protolib *plib, const char *path) 1241{ 1242 FILE *stream = fopen(path, "r"); 1243 if (stream == NULL) 1244 return -1; 1245 1246 debug(DEBUG_FUNCTION, "Reading config file `%s'...", path); 1247 1248 struct locus loc = { path, 0 }; 1249 char *line = NULL; 1250 size_t len = 0; 1251 while (getline(&line, &len, stream) >= 0) { 1252 loc.line_no++; 1253 process_line(plib, &loc, line); 1254 } 1255 1256 free(line); 1257 fclose(stream); 1258 return 0; 1259} 1260 1261void 1262init_global_config(void) 1263{ 1264 protolib_init(&g_prototypes); 1265 1266 struct arg_type_info *void_info = type_get_simple(ARGTYPE_VOID); 1267 static struct arg_type_info ptr_info; 1268 type_init_pointer(&ptr_info, void_info, 0); 1269 1270 static struct named_type voidptr_type; 1271 named_type_init(&voidptr_type, &ptr_info, 0); 1272 1273 if (protolib_add_named_type(&g_prototypes, "addr", 0, 1274 &voidptr_type) < 0 1275 || protolib_add_named_type(&g_prototypes, "file", 0, 1276 &voidptr_type) < 0) { 1277 fprintf(stderr, 1278 "Couldn't initialize aliases `addr' and `file'.\n"); 1279 exit(1); 1280 } 1281} 1282 1283void 1284destroy_global_config(void) 1285{ 1286 protolib_destroy(&g_prototypes); 1287} 1288