config.c 11 KB

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