config.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * config.c
  3. *
  4. * Helper functions for parsing config items.
  5. * Originally copied from GIT source.
  6. *
  7. * Copyright (C) Linus Torvalds, 2005
  8. * Copyright (C) Johannes Schindelin, 2005
  9. *
  10. */
  11. #include "util.h"
  12. #include "cache.h"
  13. #include "exec_cmd.h"
  14. #include "util/hist.h" /* perf_hist_config */
  15. #include "util/llvm-utils.h" /* perf_llvm_config */
  16. #define MAXNAME (256)
  17. #define DEBUG_CACHE_DIR ".debug"
  18. char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */
  19. static FILE *config_file;
  20. static const char *config_file_name;
  21. static int config_linenr;
  22. static int config_file_eof;
  23. static const char *config_exclusive_filename;
  24. static int get_next_char(void)
  25. {
  26. int c;
  27. FILE *f;
  28. c = '\n';
  29. if ((f = config_file) != NULL) {
  30. c = fgetc(f);
  31. if (c == '\r') {
  32. /* DOS like systems */
  33. c = fgetc(f);
  34. if (c != '\n') {
  35. ungetc(c, f);
  36. c = '\r';
  37. }
  38. }
  39. if (c == '\n')
  40. config_linenr++;
  41. if (c == EOF) {
  42. config_file_eof = 1;
  43. c = '\n';
  44. }
  45. }
  46. return c;
  47. }
  48. static char *parse_value(void)
  49. {
  50. static char value[1024];
  51. int quote = 0, comment = 0, space = 0;
  52. size_t len = 0;
  53. for (;;) {
  54. int c = get_next_char();
  55. if (len >= sizeof(value) - 1)
  56. return NULL;
  57. if (c == '\n') {
  58. if (quote)
  59. return NULL;
  60. value[len] = 0;
  61. return value;
  62. }
  63. if (comment)
  64. continue;
  65. if (isspace(c) && !quote) {
  66. space = 1;
  67. continue;
  68. }
  69. if (!quote) {
  70. if (c == ';' || c == '#') {
  71. comment = 1;
  72. continue;
  73. }
  74. }
  75. if (space) {
  76. if (len)
  77. value[len++] = ' ';
  78. space = 0;
  79. }
  80. if (c == '\\') {
  81. c = get_next_char();
  82. switch (c) {
  83. case '\n':
  84. continue;
  85. case 't':
  86. c = '\t';
  87. break;
  88. case 'b':
  89. c = '\b';
  90. break;
  91. case 'n':
  92. c = '\n';
  93. break;
  94. /* Some characters escape as themselves */
  95. case '\\': case '"':
  96. break;
  97. /* Reject unknown escape sequences */
  98. default:
  99. return NULL;
  100. }
  101. value[len++] = c;
  102. continue;
  103. }
  104. if (c == '"') {
  105. quote = 1-quote;
  106. continue;
  107. }
  108. value[len++] = c;
  109. }
  110. }
  111. static inline int iskeychar(int c)
  112. {
  113. return isalnum(c) || c == '-' || c == '_';
  114. }
  115. static int get_value(config_fn_t fn, void *data, char *name, unsigned int len)
  116. {
  117. int c;
  118. char *value;
  119. /* Get the full name */
  120. for (;;) {
  121. c = get_next_char();
  122. if (config_file_eof)
  123. break;
  124. if (!iskeychar(c))
  125. break;
  126. name[len++] = c;
  127. if (len >= MAXNAME)
  128. return -1;
  129. }
  130. name[len] = 0;
  131. while (c == ' ' || c == '\t')
  132. c = get_next_char();
  133. value = NULL;
  134. if (c != '\n') {
  135. if (c != '=')
  136. return -1;
  137. value = parse_value();
  138. if (!value)
  139. return -1;
  140. }
  141. return fn(name, value, data);
  142. }
  143. static int get_extended_base_var(char *name, int baselen, int c)
  144. {
  145. do {
  146. if (c == '\n')
  147. return -1;
  148. c = get_next_char();
  149. } while (isspace(c));
  150. /* We require the format to be '[base "extension"]' */
  151. if (c != '"')
  152. return -1;
  153. name[baselen++] = '.';
  154. for (;;) {
  155. int ch = get_next_char();
  156. if (ch == '\n')
  157. return -1;
  158. if (ch == '"')
  159. break;
  160. if (ch == '\\') {
  161. ch = get_next_char();
  162. if (ch == '\n')
  163. return -1;
  164. }
  165. name[baselen++] = ch;
  166. if (baselen > MAXNAME / 2)
  167. return -1;
  168. }
  169. /* Final ']' */
  170. if (get_next_char() != ']')
  171. return -1;
  172. return baselen;
  173. }
  174. static int get_base_var(char *name)
  175. {
  176. int baselen = 0;
  177. for (;;) {
  178. int c = get_next_char();
  179. if (config_file_eof)
  180. return -1;
  181. if (c == ']')
  182. return baselen;
  183. if (isspace(c))
  184. return get_extended_base_var(name, baselen, c);
  185. if (!iskeychar(c) && c != '.')
  186. return -1;
  187. if (baselen > MAXNAME / 2)
  188. return -1;
  189. name[baselen++] = tolower(c);
  190. }
  191. }
  192. static int perf_parse_file(config_fn_t fn, void *data)
  193. {
  194. int comment = 0;
  195. int baselen = 0;
  196. static char var[MAXNAME];
  197. /* U+FEFF Byte Order Mark in UTF8 */
  198. static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
  199. const unsigned char *bomptr = utf8_bom;
  200. for (;;) {
  201. int line, c = get_next_char();
  202. if (bomptr && *bomptr) {
  203. /* We are at the file beginning; skip UTF8-encoded BOM
  204. * if present. Sane editors won't put this in on their
  205. * own, but e.g. Windows Notepad will do it happily. */
  206. if ((unsigned char) c == *bomptr) {
  207. bomptr++;
  208. continue;
  209. } else {
  210. /* Do not tolerate partial BOM. */
  211. if (bomptr != utf8_bom)
  212. break;
  213. /* No BOM at file beginning. Cool. */
  214. bomptr = NULL;
  215. }
  216. }
  217. if (c == '\n') {
  218. if (config_file_eof)
  219. return 0;
  220. comment = 0;
  221. continue;
  222. }
  223. if (comment || isspace(c))
  224. continue;
  225. if (c == '#' || c == ';') {
  226. comment = 1;
  227. continue;
  228. }
  229. if (c == '[') {
  230. baselen = get_base_var(var);
  231. if (baselen <= 0)
  232. break;
  233. var[baselen++] = '.';
  234. var[baselen] = 0;
  235. continue;
  236. }
  237. if (!isalpha(c))
  238. break;
  239. var[baselen] = tolower(c);
  240. /*
  241. * The get_value function might or might not reach the '\n',
  242. * so saving the current line number for error reporting.
  243. */
  244. line = config_linenr;
  245. if (get_value(fn, data, var, baselen+1) < 0) {
  246. config_linenr = line;
  247. break;
  248. }
  249. }
  250. die("bad config file line %d in %s", config_linenr, config_file_name);
  251. }
  252. static int parse_unit_factor(const char *end, unsigned long *val)
  253. {
  254. if (!*end)
  255. return 1;
  256. else if (!strcasecmp(end, "k")) {
  257. *val *= 1024;
  258. return 1;
  259. }
  260. else if (!strcasecmp(end, "m")) {
  261. *val *= 1024 * 1024;
  262. return 1;
  263. }
  264. else if (!strcasecmp(end, "g")) {
  265. *val *= 1024 * 1024 * 1024;
  266. return 1;
  267. }
  268. return 0;
  269. }
  270. static int perf_parse_llong(const char *value, long long *ret)
  271. {
  272. if (value && *value) {
  273. char *end;
  274. long long val = strtoll(value, &end, 0);
  275. unsigned long factor = 1;
  276. if (!parse_unit_factor(end, &factor))
  277. return 0;
  278. *ret = val * factor;
  279. return 1;
  280. }
  281. return 0;
  282. }
  283. static int perf_parse_long(const char *value, long *ret)
  284. {
  285. if (value && *value) {
  286. char *end;
  287. long val = strtol(value, &end, 0);
  288. unsigned long factor = 1;
  289. if (!parse_unit_factor(end, &factor))
  290. return 0;
  291. *ret = val * factor;
  292. return 1;
  293. }
  294. return 0;
  295. }
  296. static void die_bad_config(const char *name)
  297. {
  298. if (config_file_name)
  299. die("bad config value for '%s' in %s", name, config_file_name);
  300. die("bad config value for '%s'", name);
  301. }
  302. u64 perf_config_u64(const char *name, const char *value)
  303. {
  304. long long ret = 0;
  305. if (!perf_parse_llong(value, &ret))
  306. die_bad_config(name);
  307. return (u64) ret;
  308. }
  309. int perf_config_int(const char *name, const char *value)
  310. {
  311. long ret = 0;
  312. if (!perf_parse_long(value, &ret))
  313. die_bad_config(name);
  314. return ret;
  315. }
  316. static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool)
  317. {
  318. *is_bool = 1;
  319. if (!value)
  320. return 1;
  321. if (!*value)
  322. return 0;
  323. if (!strcasecmp(value, "true") || !strcasecmp(value, "yes") || !strcasecmp(value, "on"))
  324. return 1;
  325. if (!strcasecmp(value, "false") || !strcasecmp(value, "no") || !strcasecmp(value, "off"))
  326. return 0;
  327. *is_bool = 0;
  328. return perf_config_int(name, value);
  329. }
  330. int perf_config_bool(const char *name, const char *value)
  331. {
  332. int discard;
  333. return !!perf_config_bool_or_int(name, value, &discard);
  334. }
  335. const char *perf_config_dirname(const char *name, const char *value)
  336. {
  337. if (!name)
  338. return NULL;
  339. return value;
  340. }
  341. static int perf_default_core_config(const char *var __maybe_unused,
  342. const char *value __maybe_unused)
  343. {
  344. /* Add other config variables here. */
  345. return 0;
  346. }
  347. static int perf_ui_config(const char *var, const char *value)
  348. {
  349. /* Add other config variables here. */
  350. if (!strcmp(var, "ui.show-headers")) {
  351. symbol_conf.show_hist_headers = perf_config_bool(var, value);
  352. return 0;
  353. }
  354. return 0;
  355. }
  356. int perf_default_config(const char *var, const char *value,
  357. void *dummy __maybe_unused)
  358. {
  359. if (!prefixcmp(var, "core."))
  360. return perf_default_core_config(var, value);
  361. if (!prefixcmp(var, "hist."))
  362. return perf_hist_config(var, value);
  363. if (!prefixcmp(var, "ui."))
  364. return perf_ui_config(var, value);
  365. if (!prefixcmp(var, "call-graph."))
  366. return perf_callchain_config(var, value);
  367. if (!prefixcmp(var, "llvm."))
  368. return perf_llvm_config(var, value);
  369. /* Add other config variables here. */
  370. return 0;
  371. }
  372. static int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
  373. {
  374. int ret;
  375. FILE *f = fopen(filename, "r");
  376. ret = -1;
  377. if (f) {
  378. config_file = f;
  379. config_file_name = filename;
  380. config_linenr = 1;
  381. config_file_eof = 0;
  382. ret = perf_parse_file(fn, data);
  383. fclose(f);
  384. config_file_name = NULL;
  385. }
  386. return ret;
  387. }
  388. static const char *perf_etc_perfconfig(void)
  389. {
  390. static const char *system_wide;
  391. if (!system_wide)
  392. system_wide = system_path(ETC_PERFCONFIG);
  393. return system_wide;
  394. }
  395. static int perf_env_bool(const char *k, int def)
  396. {
  397. const char *v = getenv(k);
  398. return v ? perf_config_bool(k, v) : def;
  399. }
  400. static int perf_config_system(void)
  401. {
  402. return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
  403. }
  404. static int perf_config_global(void)
  405. {
  406. return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
  407. }
  408. int perf_config(config_fn_t fn, void *data)
  409. {
  410. int ret = 0, found = 0;
  411. const char *home = NULL;
  412. /* Setting $PERF_CONFIG makes perf read _only_ the given config file. */
  413. if (config_exclusive_filename)
  414. return perf_config_from_file(fn, config_exclusive_filename, data);
  415. if (perf_config_system() && !access(perf_etc_perfconfig(), R_OK)) {
  416. ret += perf_config_from_file(fn, perf_etc_perfconfig(),
  417. data);
  418. found += 1;
  419. }
  420. home = getenv("HOME");
  421. if (perf_config_global() && home) {
  422. char *user_config = strdup(mkpath("%s/.perfconfig", home));
  423. struct stat st;
  424. if (user_config == NULL) {
  425. warning("Not enough memory to process %s/.perfconfig, "
  426. "ignoring it.", home);
  427. goto out;
  428. }
  429. if (stat(user_config, &st) < 0)
  430. goto out_free;
  431. if (st.st_uid && (st.st_uid != geteuid())) {
  432. warning("File %s not owned by current user or root, "
  433. "ignoring it.", user_config);
  434. goto out_free;
  435. }
  436. if (!st.st_size)
  437. goto out_free;
  438. ret += perf_config_from_file(fn, user_config, data);
  439. found += 1;
  440. out_free:
  441. free(user_config);
  442. }
  443. out:
  444. if (found == 0)
  445. return -1;
  446. return ret;
  447. }
  448. /*
  449. * Call this to report error for your variable that should not
  450. * get a boolean value (i.e. "[my] var" means "true").
  451. */
  452. int config_error_nonbool(const char *var)
  453. {
  454. return error("Missing value for '%s'", var);
  455. }
  456. struct buildid_dir_config {
  457. char *dir;
  458. };
  459. static int buildid_dir_command_config(const char *var, const char *value,
  460. void *data)
  461. {
  462. struct buildid_dir_config *c = data;
  463. const char *v;
  464. /* same dir for all commands */
  465. if (!strcmp(var, "buildid.dir")) {
  466. v = perf_config_dirname(var, value);
  467. if (!v)
  468. return -1;
  469. strncpy(c->dir, v, MAXPATHLEN-1);
  470. c->dir[MAXPATHLEN-1] = '\0';
  471. }
  472. return 0;
  473. }
  474. static void check_buildid_dir_config(void)
  475. {
  476. struct buildid_dir_config c;
  477. c.dir = buildid_dir;
  478. perf_config(buildid_dir_command_config, &c);
  479. }
  480. void set_buildid_dir(const char *dir)
  481. {
  482. if (dir)
  483. scnprintf(buildid_dir, MAXPATHLEN-1, "%s", dir);
  484. /* try config file */
  485. if (buildid_dir[0] == '\0')
  486. check_buildid_dir_config();
  487. /* default to $HOME/.debug */
  488. if (buildid_dir[0] == '\0') {
  489. char *v = getenv("HOME");
  490. if (v) {
  491. snprintf(buildid_dir, MAXPATHLEN-1, "%s/%s",
  492. v, DEBUG_CACHE_DIR);
  493. } else {
  494. strncpy(buildid_dir, DEBUG_CACHE_DIR, MAXPATHLEN-1);
  495. }
  496. buildid_dir[MAXPATHLEN-1] = '\0';
  497. }
  498. /* for communicating with external commands */
  499. setenv("PERF_BUILDID_DIR", buildid_dir, 1);
  500. }