aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/perf/Documentation/perf-stat.txt3
-rw-r--r--tools/perf/builtin-annotate.c61
-rw-r--r--tools/perf/builtin-probe.c10
-rw-r--r--tools/perf/builtin-record.c72
-rw-r--r--tools/perf/builtin-report.c64
-rw-r--r--tools/perf/builtin-stat.c18
-rw-r--r--tools/perf/builtin-top.c2
-rw-r--r--tools/perf/perf.c25
-rw-r--r--tools/perf/util/abspath.c81
-rw-r--r--tools/perf/util/build-id.c22
-rw-r--r--tools/perf/util/build-id.h2
-rw-r--r--tools/perf/util/cache.h57
-rw-r--r--tools/perf/util/callchain.c1
-rw-r--r--tools/perf/util/callchain.h1
-rw-r--r--tools/perf/util/config.c461
-rw-r--r--tools/perf/util/debug.c2
-rw-r--r--tools/perf/util/exec_cmd.c6
-rw-r--r--tools/perf/util/exec_cmd.h1
-rw-r--r--tools/perf/util/header.c84
-rw-r--r--tools/perf/util/help.c30
-rw-r--r--tools/perf/util/hist.c42
-rw-r--r--tools/perf/util/hist.h24
-rw-r--r--tools/perf/util/newt.c151
-rw-r--r--tools/perf/util/path.c204
-rw-r--r--tools/perf/util/probe-finder.c33
-rw-r--r--tools/perf/util/probe-finder.h3
-rw-r--r--tools/perf/util/quote.c433
-rw-r--r--tools/perf/util/quote.h39
-rw-r--r--tools/perf/util/run-command.c90
-rw-r--r--tools/perf/util/run-command.h30
-rw-r--r--tools/perf/util/session.c8
-rw-r--r--tools/perf/util/session.h8
-rw-r--r--tools/perf/util/sigchain.c2
-rw-r--r--tools/perf/util/sigchain.h1
-rw-r--r--tools/perf/util/strbuf.c229
-rw-r--r--tools/perf/util/strbuf.h45
-rw-r--r--tools/perf/util/symbol.c52
-rw-r--r--tools/perf/util/symbol.h3
-rw-r--r--tools/perf/util/trace-event-read.c19
-rw-r--r--tools/perf/util/trace-event.h7
-rw-r--r--tools/perf/util/util.h177
-rw-r--r--tools/perf/util/wrapper.c110
42 files changed, 535 insertions, 2178 deletions
diff --git a/tools/perf/Documentation/perf-stat.txt b/tools/perf/Documentation/perf-stat.txt
index 2cab8e8c33d0..909fa766fa1c 100644
--- a/tools/perf/Documentation/perf-stat.txt
+++ b/tools/perf/Documentation/perf-stat.txt
@@ -43,6 +43,9 @@ OPTIONS
43-c:: 43-c::
44 scale counter values 44 scale counter values
45 45
46-B::
47 print large numbers with thousands' separators according to locale
48
46EXAMPLES 49EXAMPLES
47-------- 50--------
48 51
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 77bcc9b130f5..96db5248e995 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -277,7 +277,7 @@ static void hist_entry__print_hits(struct hist_entry *self)
277 printf("%*s: %Lu\n", BITS_PER_LONG / 2, "h->sum", h->sum); 277 printf("%*s: %Lu\n", BITS_PER_LONG / 2, "h->sum", h->sum);
278} 278}
279 279
280static void annotate_sym(struct hist_entry *he) 280static int hist_entry__tty_annotate(struct hist_entry *he)
281{ 281{
282 struct map *map = he->ms.map; 282 struct map *map = he->ms.map;
283 struct dso *dso = map->dso; 283 struct dso *dso = map->dso;
@@ -288,7 +288,7 @@ static void annotate_sym(struct hist_entry *he)
288 struct objdump_line *pos, *n; 288 struct objdump_line *pos, *n;
289 289
290 if (hist_entry__annotate(he, &head) < 0) 290 if (hist_entry__annotate(he, &head) < 0)
291 return; 291 return -1;
292 292
293 if (full_paths) 293 if (full_paths)
294 d_filename = filename; 294 d_filename = filename;
@@ -317,30 +317,59 @@ static void annotate_sym(struct hist_entry *he)
317 317
318 if (print_line) 318 if (print_line)
319 free_source_line(he, len); 319 free_source_line(he, len);
320
321 return 0;
320} 322}
321 323
322static void hists__find_annotations(struct hists *self) 324static void hists__find_annotations(struct hists *self)
323{ 325{
324 struct rb_node *nd; 326 struct rb_node *first = rb_first(&self->entries), *nd = first;
327 int key = KEY_RIGHT;
325 328
326 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) { 329 while (nd) {
327 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); 330 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
328 struct sym_priv *priv; 331 struct sym_priv *priv;
329 332
330 if (he->ms.sym == NULL) 333 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
331 continue; 334 goto find_next;
332 335
333 priv = symbol__priv(he->ms.sym); 336 priv = symbol__priv(he->ms.sym);
334 if (priv->hist == NULL) 337 if (priv->hist == NULL) {
338find_next:
339 if (key == KEY_LEFT)
340 nd = rb_prev(nd);
341 else
342 nd = rb_next(nd);
335 continue; 343 continue;
344 }
336 345
337 annotate_sym(he); 346 if (use_browser > 0) {
338 /* 347 key = hist_entry__tui_annotate(he);
339 * Since we have a hist_entry per IP for the same symbol, free 348 if (is_exit_key(key))
340 * he->ms.sym->hist to signal we already processed this symbol. 349 break;
341 */ 350 switch (key) {
342 free(priv->hist); 351 case KEY_RIGHT:
343 priv->hist = NULL; 352 case '\t':
353 nd = rb_next(nd);
354 break;
355 case KEY_LEFT:
356 if (nd == first)
357 continue;
358 nd = rb_prev(nd);
359 default:
360 break;
361 }
362 } else {
363 hist_entry__tty_annotate(he);
364 nd = rb_next(nd);
365 /*
366 * Since we have a hist_entry per IP for the same
367 * symbol, free he->ms.sym->hist to signal we already
368 * processed this symbol.
369 */
370 free(priv->hist);
371 priv->hist = NULL;
372 }
344 } 373 }
345} 374}
346 375
@@ -416,6 +445,8 @@ int cmd_annotate(int argc, const char **argv, const char *prefix __used)
416{ 445{
417 argc = parse_options(argc, argv, options, annotate_usage, 0); 446 argc = parse_options(argc, argv, options, annotate_usage, 0);
418 447
448 setup_browser();
449
419 symbol_conf.priv_size = sizeof(struct sym_priv); 450 symbol_conf.priv_size = sizeof(struct sym_priv);
420 symbol_conf.try_vmlinux_path = true; 451 symbol_conf.try_vmlinux_path = true;
421 452
@@ -435,8 +466,6 @@ int cmd_annotate(int argc, const char **argv, const char *prefix __used)
435 sym_hist_filter = argv[0]; 466 sym_hist_filter = argv[0];
436 } 467 }
437 468
438 setup_pager();
439
440 if (field_sep && *field_sep == '.') { 469 if (field_sep && *field_sep == '.') {
441 pr_err("'.' is the only non valid --field-separator argument\n"); 470 pr_err("'.' is the only non valid --field-separator argument\n");
442 return -1; 471 return -1;
diff --git a/tools/perf/builtin-probe.c b/tools/perf/builtin-probe.c
index 61c6d70732c9..e4a4da32a568 100644
--- a/tools/perf/builtin-probe.c
+++ b/tools/perf/builtin-probe.c
@@ -65,8 +65,10 @@ static int parse_probe_event(const char *str)
65 int ret; 65 int ret;
66 66
67 pr_debug("probe-definition(%d): %s\n", params.nevents, str); 67 pr_debug("probe-definition(%d): %s\n", params.nevents, str);
68 if (++params.nevents == MAX_PROBES) 68 if (++params.nevents == MAX_PROBES) {
69 die("Too many probes (> %d) are specified.", MAX_PROBES); 69 pr_err("Too many probes (> %d) were specified.", MAX_PROBES);
70 return -1;
71 }
70 72
71 /* Parse a perf-probe command into event */ 73 /* Parse a perf-probe command into event */
72 ret = parse_perf_probe_command(str, pev); 74 ret = parse_perf_probe_command(str, pev);
@@ -84,7 +86,9 @@ static int parse_probe_event_argv(int argc, const char **argv)
84 len = 0; 86 len = 0;
85 for (i = 0; i < argc; i++) 87 for (i = 0; i < argc; i++)
86 len += strlen(argv[i]) + 1; 88 len += strlen(argv[i]) + 1;
87 buf = xzalloc(len + 1); 89 buf = zalloc(len + 1);
90 if (buf == NULL)
91 return -ENOMEM;
88 len = 0; 92 len = 0;
89 for (i = 0; i < argc; i++) 93 for (i = 0; i < argc; i++)
90 len += sprintf(&buf[len], "%s ", argv[i]); 94 len += sprintf(&buf[len], "%s ", argv[i]);
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index cb46c7d0ea99..9bc89050e6f8 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -25,6 +25,7 @@
25 25
26#include <unistd.h> 26#include <unistd.h>
27#include <sched.h> 27#include <sched.h>
28#include <sys/mman.h>
28 29
29enum write_mode_t { 30enum write_mode_t {
30 WRITE_FORCE, 31 WRITE_FORCE,
@@ -60,13 +61,8 @@ static bool call_graph = false;
60static bool inherit_stat = false; 61static bool inherit_stat = false;
61static bool no_samples = false; 62static bool no_samples = false;
62static bool sample_address = false; 63static bool sample_address = false;
63static bool multiplex = false;
64static int multiplex_fd = -1;
65 64
66static long samples = 0; 65static long samples = 0;
67static struct timeval last_read;
68static struct timeval this_read;
69
70static u64 bytes_written = 0; 66static u64 bytes_written = 0;
71 67
72static struct pollfd *event_array; 68static struct pollfd *event_array;
@@ -86,7 +82,7 @@ struct mmap_data {
86 unsigned int prev; 82 unsigned int prev;
87}; 83};
88 84
89static struct mmap_data *mmap_array[MAX_NR_CPUS][MAX_COUNTERS]; 85static struct mmap_data mmap_array[MAX_NR_CPUS];
90 86
91static unsigned long mmap_read_head(struct mmap_data *md) 87static unsigned long mmap_read_head(struct mmap_data *md)
92{ 88{
@@ -146,8 +142,6 @@ static void mmap_read(struct mmap_data *md)
146 void *buf; 142 void *buf;
147 int diff; 143 int diff;
148 144
149 gettimeofday(&this_read, NULL);
150
151 /* 145 /*
152 * If we're further behind than half the buffer, there's a chance 146 * If we're further behind than half the buffer, there's a chance
153 * the writer will bite our tail and mess up the samples under us. 147 * the writer will bite our tail and mess up the samples under us.
@@ -158,23 +152,13 @@ static void mmap_read(struct mmap_data *md)
158 */ 152 */
159 diff = head - old; 153 diff = head - old;
160 if (diff < 0) { 154 if (diff < 0) {
161 struct timeval iv; 155 fprintf(stderr, "WARNING: failed to keep up with mmap data\n");
162 unsigned long msecs;
163
164 timersub(&this_read, &last_read, &iv);
165 msecs = iv.tv_sec*1000 + iv.tv_usec/1000;
166
167 fprintf(stderr, "WARNING: failed to keep up with mmap data."
168 " Last read %lu msecs ago.\n", msecs);
169
170 /* 156 /*
171 * head points to a known good entry, start there. 157 * head points to a known good entry, start there.
172 */ 158 */
173 old = head; 159 old = head;
174 } 160 }
175 161
176 last_read = this_read;
177
178 if (old != head) 162 if (old != head)
179 samples++; 163 samples++;
180 164
@@ -380,27 +364,30 @@ try_again:
380 */ 364 */
381 if (group && group_fd == -1) 365 if (group && group_fd == -1)
382 group_fd = fd[nr_cpu][counter][thread_index]; 366 group_fd = fd[nr_cpu][counter][thread_index];
383 if (multiplex && multiplex_fd == -1)
384 multiplex_fd = fd[nr_cpu][counter][thread_index];
385 367
386 if (multiplex && fd[nr_cpu][counter][thread_index] != multiplex_fd) { 368 if (counter || thread_index) {
387 369 ret = ioctl(fd[nr_cpu][counter][thread_index],
388 ret = ioctl(fd[nr_cpu][counter][thread_index], PERF_EVENT_IOC_SET_OUTPUT, multiplex_fd); 370 PERF_EVENT_IOC_SET_OUTPUT,
389 assert(ret != -1); 371 fd[nr_cpu][0][0]);
372 if (ret) {
373 error("failed to set output: %d (%s)\n", errno,
374 strerror(errno));
375 exit(-1);
376 }
390 } else { 377 } else {
391 event_array[nr_poll].fd = fd[nr_cpu][counter][thread_index]; 378 mmap_array[nr_cpu].counter = counter;
392 event_array[nr_poll].events = POLLIN; 379 mmap_array[nr_cpu].prev = 0;
393 nr_poll++; 380 mmap_array[nr_cpu].mask = mmap_pages*page_size - 1;
394 381 mmap_array[nr_cpu].base = mmap(NULL, (mmap_pages+1)*page_size,
395 mmap_array[nr_cpu][counter][thread_index].counter = counter;
396 mmap_array[nr_cpu][counter][thread_index].prev = 0;
397 mmap_array[nr_cpu][counter][thread_index].mask = mmap_pages*page_size - 1;
398 mmap_array[nr_cpu][counter][thread_index].base = mmap(NULL, (mmap_pages+1)*page_size,
399 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter][thread_index], 0); 382 PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter][thread_index], 0);
400 if (mmap_array[nr_cpu][counter][thread_index].base == MAP_FAILED) { 383 if (mmap_array[nr_cpu].base == MAP_FAILED) {
401 error("failed to mmap with %d (%s)\n", errno, strerror(errno)); 384 error("failed to mmap with %d (%s)\n", errno, strerror(errno));
402 exit(-1); 385 exit(-1);
403 } 386 }
387
388 event_array[nr_poll].fd = fd[nr_cpu][counter][thread_index];
389 event_array[nr_poll].events = POLLIN;
390 nr_poll++;
404 } 391 }
405 392
406 if (filter != NULL) { 393 if (filter != NULL) {
@@ -501,16 +488,11 @@ static struct perf_event_header finished_round_event = {
501 488
502static void mmap_read_all(void) 489static void mmap_read_all(void)
503{ 490{
504 int i, counter, thread; 491 int i;
505 492
506 for (i = 0; i < nr_cpu; i++) { 493 for (i = 0; i < nr_cpu; i++) {
507 for (counter = 0; counter < nr_counters; counter++) { 494 if (mmap_array[i].base)
508 for (thread = 0; thread < thread_num; thread++) { 495 mmap_read(&mmap_array[i]);
509 if (mmap_array[i][counter][thread].base)
510 mmap_read(&mmap_array[i][counter][thread]);
511 }
512
513 }
514 } 496 }
515 497
516 if (perf_header__has_feat(&session->header, HEADER_TRACE_INFO)) 498 if (perf_header__has_feat(&session->header, HEADER_TRACE_INFO))
@@ -834,8 +816,6 @@ static const struct option options[] = {
834 "Sample addresses"), 816 "Sample addresses"),
835 OPT_BOOLEAN('n', "no-samples", &no_samples, 817 OPT_BOOLEAN('n', "no-samples", &no_samples,
836 "don't sample"), 818 "don't sample"),
837 OPT_BOOLEAN('M', "multiplex", &multiplex,
838 "multiplex counter output in a single channel"),
839 OPT_END() 819 OPT_END()
840}; 820};
841 821
@@ -887,9 +867,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
887 for (i = 0; i < MAX_NR_CPUS; i++) { 867 for (i = 0; i < MAX_NR_CPUS; i++) {
888 for (j = 0; j < MAX_COUNTERS; j++) { 868 for (j = 0; j < MAX_COUNTERS; j++) {
889 fd[i][j] = malloc(sizeof(int)*thread_num); 869 fd[i][j] = malloc(sizeof(int)*thread_num);
890 mmap_array[i][j] = zalloc( 870 if (!fd[i][j])
891 sizeof(struct mmap_data)*thread_num);
892 if (!fd[i][j] || !mmap_array[i][j])
893 return -ENOMEM; 871 return -ENOMEM;
894 } 872 }
895 } 873 }
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 1d3c1003b43a..359205782964 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -116,7 +116,7 @@ static int perf_session__add_hist_entry(struct perf_session *self,
116 * so we don't allocated the extra space needed because the stdio 116 * so we don't allocated the extra space needed because the stdio
117 * code will not use it. 117 * code will not use it.
118 */ 118 */
119 if (use_browser) 119 if (use_browser > 0)
120 err = hist_entry__inc_addr_samples(he, al->addr); 120 err = hist_entry__inc_addr_samples(he, al->addr);
121out_free_syms: 121out_free_syms:
122 free(syms); 122 free(syms);
@@ -288,6 +288,38 @@ static size_t hists__fprintf_nr_sample_events(struct hists *self,
288 return ret + fprintf(fp, "\n#\n"); 288 return ret + fprintf(fp, "\n#\n");
289} 289}
290 290
291static int hists__tty_browse_tree(struct rb_root *tree, const char *help)
292{
293 struct rb_node *next = rb_first(tree);
294
295 while (next) {
296 struct hists *hists = rb_entry(next, struct hists, rb_node);
297 const char *evname = NULL;
298
299 if (rb_first(&hists->entries) != rb_last(&hists->entries))
300 evname = __event_name(hists->type, hists->config);
301
302 hists__fprintf_nr_sample_events(hists, evname, stdout);
303 hists__fprintf(hists, NULL, false, stdout);
304 fprintf(stdout, "\n\n");
305 next = rb_next(&hists->rb_node);
306 }
307
308 if (sort_order == default_sort_order &&
309 parent_pattern == default_parent_pattern) {
310 fprintf(stdout, "#\n# (%s)\n#\n", help);
311
312 if (show_threads) {
313 bool style = !strcmp(pretty_printing_style, "raw");
314 perf_read_values_display(stdout, &show_threads_values,
315 style);
316 perf_read_values_destroy(&show_threads_values);
317 }
318 }
319
320 return 0;
321}
322
291static int __cmd_report(void) 323static int __cmd_report(void)
292{ 324{
293 int ret = -EINVAL; 325 int ret = -EINVAL;
@@ -330,34 +362,14 @@ static int __cmd_report(void)
330 hists = rb_entry(next, struct hists, rb_node); 362 hists = rb_entry(next, struct hists, rb_node);
331 hists__collapse_resort(hists); 363 hists__collapse_resort(hists);
332 hists__output_resort(hists); 364 hists__output_resort(hists);
333 if (use_browser)
334 hists__browse(hists, help, input_name);
335 else {
336 const char *evname = NULL;
337 if (rb_first(&session->hists.entries) !=
338 rb_last(&session->hists.entries))
339 evname = __event_name(hists->type, hists->config);
340
341 hists__fprintf_nr_sample_events(hists, evname, stdout);
342
343 hists__fprintf(hists, NULL, false, stdout);
344 fprintf(stdout, "\n\n");
345 }
346
347 next = rb_next(&hists->rb_node); 365 next = rb_next(&hists->rb_node);
348 } 366 }
349 367
350 if (!use_browser && sort_order == default_sort_order && 368 if (use_browser > 0)
351 parent_pattern == default_parent_pattern) { 369 hists__tui_browse_tree(&session->hists_tree, help);
352 fprintf(stdout, "#\n# (%s)\n#\n", help); 370 else
371 hists__tty_browse_tree(&session->hists_tree, help);
353 372
354 if (show_threads) {
355 bool style = !strcmp(pretty_printing_style, "raw");
356 perf_read_values_display(stdout, &show_threads_values,
357 style);
358 perf_read_values_destroy(&show_threads_values);
359 }
360 }
361out_delete: 373out_delete:
362 perf_session__delete(session); 374 perf_session__delete(session);
363 return ret; 375 return ret;
@@ -491,7 +503,7 @@ int cmd_report(int argc, const char **argv, const char *prefix __used)
491 * so don't allocate extra space that won't be used in the stdio 503 * so don't allocate extra space that won't be used in the stdio
492 * implementation. 504 * implementation.
493 */ 505 */
494 if (use_browser) 506 if (use_browser > 0)
495 symbol_conf.priv_size = sizeof(struct sym_priv); 507 symbol_conf.priv_size = sizeof(struct sym_priv);
496 508
497 if (symbol__init() < 0) 509 if (symbol__init() < 0)
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index ff8c413b7e73..9a39ca3c3ac4 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -50,6 +50,7 @@
50 50
51#include <sys/prctl.h> 51#include <sys/prctl.h>
52#include <math.h> 52#include <math.h>
53#include <locale.h>
53 54
54static struct perf_event_attr default_attrs[] = { 55static struct perf_event_attr default_attrs[] = {
55 56
@@ -80,6 +81,8 @@ static pid_t *all_tids = NULL;
80static int thread_num = 0; 81static int thread_num = 0;
81static pid_t child_pid = -1; 82static pid_t child_pid = -1;
82static bool null_run = false; 83static bool null_run = false;
84static bool big_num = false;
85
83 86
84static int *fd[MAX_NR_CPUS][MAX_COUNTERS]; 87static int *fd[MAX_NR_CPUS][MAX_COUNTERS];
85 88
@@ -377,7 +380,7 @@ static void nsec_printout(int counter, double avg)
377{ 380{
378 double msecs = avg / 1e6; 381 double msecs = avg / 1e6;
379 382
380 fprintf(stderr, " %14.6f %-24s", msecs, event_name(counter)); 383 fprintf(stderr, " %18.6f %-24s", msecs, event_name(counter));
381 384
382 if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) { 385 if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) {
383 fprintf(stderr, " # %10.3f CPUs ", 386 fprintf(stderr, " # %10.3f CPUs ",
@@ -389,7 +392,10 @@ static void abs_printout(int counter, double avg)
389{ 392{
390 double total, ratio = 0.0; 393 double total, ratio = 0.0;
391 394
392 fprintf(stderr, " %14.0f %-24s", avg, event_name(counter)); 395 if (big_num)
396 fprintf(stderr, " %'18.0f %-24s", avg, event_name(counter));
397 else
398 fprintf(stderr, " %18.0f %-24s", avg, event_name(counter));
393 399
394 if (MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) { 400 if (MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) {
395 total = avg_stats(&runtime_cycles_stats); 401 total = avg_stats(&runtime_cycles_stats);
@@ -426,7 +432,7 @@ static void print_counter(int counter)
426 int scaled = event_scaled[counter]; 432 int scaled = event_scaled[counter];
427 433
428 if (scaled == -1) { 434 if (scaled == -1) {
429 fprintf(stderr, " %14s %-24s\n", 435 fprintf(stderr, " %18s %-24s\n",
430 "<not counted>", event_name(counter)); 436 "<not counted>", event_name(counter));
431 return; 437 return;
432 } 438 }
@@ -477,7 +483,7 @@ static void print_stat(int argc, const char **argv)
477 print_counter(counter); 483 print_counter(counter);
478 484
479 fprintf(stderr, "\n"); 485 fprintf(stderr, "\n");
480 fprintf(stderr, " %14.9f seconds time elapsed", 486 fprintf(stderr, " %18.9f seconds time elapsed",
481 avg_stats(&walltime_nsecs_stats)/1e9); 487 avg_stats(&walltime_nsecs_stats)/1e9);
482 if (run_count > 1) { 488 if (run_count > 1) {
483 fprintf(stderr, " ( +- %7.3f%% )", 489 fprintf(stderr, " ( +- %7.3f%% )",
@@ -534,6 +540,8 @@ static const struct option options[] = {
534 "repeat command and print average + stddev (max: 100)"), 540 "repeat command and print average + stddev (max: 100)"),
535 OPT_BOOLEAN('n', "null", &null_run, 541 OPT_BOOLEAN('n', "null", &null_run,
536 "null run - dont start any counters"), 542 "null run - dont start any counters"),
543 OPT_BOOLEAN('B', "big-num", &big_num,
544 "print large numbers with thousands\' separators"),
537 OPT_END() 545 OPT_END()
538}; 546};
539 547
@@ -542,6 +550,8 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
542 int status; 550 int status;
543 int i,j; 551 int i,j;
544 552
553 setlocale(LC_ALL, "");
554
545 argc = parse_options(argc, argv, options, stat_usage, 555 argc = parse_options(argc, argv, options, stat_usage,
546 PARSE_OPT_STOP_AT_NON_OPTION); 556 PARSE_OPT_STOP_AT_NON_OPTION);
547 if (!argc && target_pid == -1 && target_tid == -1) 557 if (!argc && target_pid == -1 && target_tid == -1)
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 397290a0a76e..a66f4272b994 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -1060,7 +1060,7 @@ static void event__process_sample(const event_t *self,
1060 pr_err("Can't annotate %s", sym->name); 1060 pr_err("Can't annotate %s", sym->name);
1061 if (sym_filter_entry->map->dso->origin == DSO__ORIG_KERNEL) { 1061 if (sym_filter_entry->map->dso->origin == DSO__ORIG_KERNEL) {
1062 pr_err(": No vmlinux file was found in the path:\n"); 1062 pr_err(": No vmlinux file was found in the path:\n");
1063 vmlinux_path__fprintf(stderr); 1063 machine__fprintf_vmlinux_path(machine, stderr);
1064 } else 1064 } else
1065 pr_err(".\n"); 1065 pr_err(".\n");
1066 exit(1); 1066 exit(1);
diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index 08e0e5d2b50e..6e4871191138 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -15,15 +15,15 @@
15#include "util/parse-events.h" 15#include "util/parse-events.h"
16#include "util/debugfs.h" 16#include "util/debugfs.h"
17 17
18bool use_browser;
19
20const char perf_usage_string[] = 18const char perf_usage_string[] =
21 "perf [--version] [--help] COMMAND [ARGS]"; 19 "perf [--version] [--help] COMMAND [ARGS]";
22 20
23const char perf_more_info_string[] = 21const char perf_more_info_string[] =
24 "See 'perf help COMMAND' for more information on a specific command."; 22 "See 'perf help COMMAND' for more information on a specific command.";
25 23
24int use_browser = -1;
26static int use_pager = -1; 25static int use_pager = -1;
26
27struct pager_config { 27struct pager_config {
28 const char *cmd; 28 const char *cmd;
29 int val; 29 int val;
@@ -49,6 +49,24 @@ int check_pager_config(const char *cmd)
49 return c.val; 49 return c.val;
50} 50}
51 51
52static int tui_command_config(const char *var, const char *value, void *data)
53{
54 struct pager_config *c = data;
55 if (!prefixcmp(var, "tui.") && !strcmp(var + 4, c->cmd))
56 c->val = perf_config_bool(var, value);
57 return 0;
58}
59
60/* returns 0 for "no tui", 1 for "use tui", and -1 for "not specified" */
61static int check_tui_config(const char *cmd)
62{
63 struct pager_config c;
64 c.cmd = cmd;
65 c.val = -1;
66 perf_config(tui_command_config, &c);
67 return c.val;
68}
69
52static void commit_pager_choice(void) 70static void commit_pager_choice(void)
53{ 71{
54 switch (use_pager) { 72 switch (use_pager) {
@@ -255,6 +273,9 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
255 if (p->option & RUN_SETUP) 273 if (p->option & RUN_SETUP)
256 prefix = NULL; /* setup_perf_directory(); */ 274 prefix = NULL; /* setup_perf_directory(); */
257 275
276 if (use_browser == -1)
277 use_browser = check_tui_config(p->cmd);
278
258 if (use_pager == -1 && p->option & RUN_SETUP) 279 if (use_pager == -1 && p->option & RUN_SETUP)
259 use_pager = check_pager_config(p->cmd); 280 use_pager = check_pager_config(p->cmd);
260 if (use_pager == -1 && p->option & USE_PAGER) 281 if (use_pager == -1 && p->option & USE_PAGER)
diff --git a/tools/perf/util/abspath.c b/tools/perf/util/abspath.c
index a791dd467261..0e76affe9c36 100644
--- a/tools/perf/util/abspath.c
+++ b/tools/perf/util/abspath.c
@@ -1,86 +1,5 @@
1#include "cache.h" 1#include "cache.h"
2 2
3/*
4 * Do not use this for inspecting *tracked* content. When path is a
5 * symlink to a directory, we do not want to say it is a directory when
6 * dealing with tracked content in the working tree.
7 */
8static int is_directory(const char *path)
9{
10 struct stat st;
11 return (!stat(path, &st) && S_ISDIR(st.st_mode));
12}
13
14/* We allow "recursive" symbolic links. Only within reason, though. */
15#define MAXDEPTH 5
16
17const char *make_absolute_path(const char *path)
18{
19 static char bufs[2][PATH_MAX + 1], *buf = bufs[0], *next_buf = bufs[1];
20 char cwd[1024] = "";
21 int buf_index = 1, len;
22
23 int depth = MAXDEPTH;
24 char *last_elem = NULL;
25 struct stat st;
26
27 if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
28 die ("Too long path: %.*s", 60, path);
29
30 while (depth--) {
31 if (!is_directory(buf)) {
32 char *last_slash = strrchr(buf, '/');
33 if (last_slash) {
34 *last_slash = '\0';
35 last_elem = xstrdup(last_slash + 1);
36 } else {
37 last_elem = xstrdup(buf);
38 *buf = '\0';
39 }
40 }
41
42 if (*buf) {
43 if (!*cwd && !getcwd(cwd, sizeof(cwd)))
44 die ("Could not get current working directory");
45
46 if (chdir(buf))
47 die ("Could not switch to '%s'", buf);
48 }
49 if (!getcwd(buf, PATH_MAX))
50 die ("Could not get current working directory");
51
52 if (last_elem) {
53 len = strlen(buf);
54
55 if (len + strlen(last_elem) + 2 > PATH_MAX)
56 die ("Too long path name: '%s/%s'",
57 buf, last_elem);
58 buf[len] = '/';
59 strcpy(buf + len + 1, last_elem);
60 free(last_elem);
61 last_elem = NULL;
62 }
63
64 if (!lstat(buf, &st) && S_ISLNK(st.st_mode)) {
65 len = readlink(buf, next_buf, PATH_MAX);
66 if (len < 0)
67 die ("Invalid symlink: %s", buf);
68 if (PATH_MAX <= len)
69 die("symbolic link too long: %s", buf);
70 next_buf[len] = '\0';
71 buf = next_buf;
72 buf_index = 1 - buf_index;
73 next_buf = bufs[buf_index];
74 } else
75 break;
76 }
77
78 if (*cwd && chdir(cwd))
79 die ("Could not change back to '%s'", cwd);
80
81 return buf;
82}
83
84static const char *get_pwd_cwd(void) 3static const char *get_pwd_cwd(void)
85{ 4{
86 static char cwd[PATH_MAX + 1]; 5 static char cwd[PATH_MAX + 1];
diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c
index 0f60a3906808..70c5cf87d020 100644
--- a/tools/perf/util/build-id.c
+++ b/tools/perf/util/build-id.c
@@ -6,6 +6,8 @@
6 * Copyright (C) 2009, 2010 Red Hat Inc. 6 * Copyright (C) 2009, 2010 Red Hat Inc.
7 * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com> 7 * Copyright (C) 2009, 2010 Arnaldo Carvalho de Melo <acme@redhat.com>
8 */ 8 */
9#include "util.h"
10#include <stdio.h>
9#include "build-id.h" 11#include "build-id.h"
10#include "event.h" 12#include "event.h"
11#include "symbol.h" 13#include "symbol.h"
@@ -37,3 +39,23 @@ struct perf_event_ops build_id__mark_dso_hit_ops = {
37 .mmap = event__process_mmap, 39 .mmap = event__process_mmap,
38 .fork = event__process_task, 40 .fork = event__process_task,
39}; 41};
42
43char *dso__build_id_filename(struct dso *self, char *bf, size_t size)
44{
45 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
46 const char *home;
47
48 if (!self->has_build_id)
49 return NULL;
50
51 build_id__sprintf(self->build_id, sizeof(self->build_id), build_id_hex);
52 home = getenv("HOME");
53 if (bf == NULL) {
54 if (asprintf(&bf, "%s/%s/.build-id/%.2s/%s", home,
55 DEBUG_CACHE_DIR, build_id_hex, build_id_hex + 2) < 0)
56 return NULL;
57 } else
58 snprintf(bf, size, "%s/%s/.build-id/%.2s/%s", home,
59 DEBUG_CACHE_DIR, build_id_hex, build_id_hex + 2);
60 return bf;
61}
diff --git a/tools/perf/util/build-id.h b/tools/perf/util/build-id.h
index 1d981d63cf9a..5dafb00eaa06 100644
--- a/tools/perf/util/build-id.h
+++ b/tools/perf/util/build-id.h
@@ -5,4 +5,6 @@
5 5
6extern struct perf_event_ops build_id__mark_dso_hit_ops; 6extern struct perf_event_ops build_id__mark_dso_hit_ops;
7 7
8char *dso__build_id_filename(struct dso *self, char *bf, size_t size);
9
8#endif 10#endif
diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h
index 4b9aab7f0405..65fe664fddf6 100644
--- a/tools/perf/util/cache.h
+++ b/tools/perf/util/cache.h
@@ -13,56 +13,16 @@
13 13
14#define PERF_DIR_ENVIRONMENT "PERF_DIR" 14#define PERF_DIR_ENVIRONMENT "PERF_DIR"
15#define PERF_WORK_TREE_ENVIRONMENT "PERF_WORK_TREE" 15#define PERF_WORK_TREE_ENVIRONMENT "PERF_WORK_TREE"
16#define DEFAULT_PERF_DIR_ENVIRONMENT ".perf"
17#define DB_ENVIRONMENT "PERF_OBJECT_DIRECTORY"
18#define INDEX_ENVIRONMENT "PERF_INDEX_FILE"
19#define GRAFT_ENVIRONMENT "PERF_GRAFT_FILE"
20#define TEMPLATE_DIR_ENVIRONMENT "PERF_TEMPLATE_DIR"
21#define CONFIG_ENVIRONMENT "PERF_CONFIG"
22#define EXEC_PATH_ENVIRONMENT "PERF_EXEC_PATH" 16#define EXEC_PATH_ENVIRONMENT "PERF_EXEC_PATH"
23#define CEILING_DIRECTORIES_ENVIRONMENT "PERF_CEILING_DIRECTORIES" 17#define DEFAULT_PERF_DIR_ENVIRONMENT ".perf"
24#define PERFATTRIBUTES_FILE ".perfattributes"
25#define INFOATTRIBUTES_FILE "info/attributes"
26#define ATTRIBUTE_MACRO_PREFIX "[attr]"
27#define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR" 18#define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR"
28 19
29typedef int (*config_fn_t)(const char *, const char *, void *); 20typedef int (*config_fn_t)(const char *, const char *, void *);
30extern int perf_default_config(const char *, const char *, void *); 21extern int perf_default_config(const char *, const char *, void *);
31extern int perf_config_from_file(config_fn_t fn, const char *, void *);
32extern int perf_config(config_fn_t fn, void *); 22extern int perf_config(config_fn_t fn, void *);
33extern int perf_parse_ulong(const char *, unsigned long *);
34extern int perf_config_int(const char *, const char *); 23extern int perf_config_int(const char *, const char *);
35extern unsigned long perf_config_ulong(const char *, const char *);
36extern int perf_config_bool_or_int(const char *, const char *, int *);
37extern int perf_config_bool(const char *, const char *); 24extern int perf_config_bool(const char *, const char *);
38extern int perf_config_string(const char **, const char *, const char *);
39extern int perf_config_set(const char *, const char *);
40extern int perf_config_set_multivar(const char *, const char *, const char *, int);
41extern int perf_config_rename_section(const char *, const char *);
42extern const char *perf_etc_perfconfig(void);
43extern int check_repository_format_version(const char *var, const char *value, void *cb);
44extern int perf_config_system(void);
45extern int perf_config_global(void);
46extern int config_error_nonbool(const char *); 25extern int config_error_nonbool(const char *);
47extern const char *config_exclusive_filename;
48
49#define MAX_PERFNAME (1000)
50extern char perf_default_email[MAX_PERFNAME];
51extern char perf_default_name[MAX_PERFNAME];
52extern int user_ident_explicitly_given;
53
54extern const char *perf_log_output_encoding;
55extern const char *perf_mailmap_file;
56
57/* IO helper functions */
58extern void maybe_flush_or_die(FILE *, const char *);
59extern int copy_fd(int ifd, int ofd);
60extern int copy_file(const char *dst, const char *src, int mode);
61extern ssize_t write_in_full(int fd, const void *buf, size_t count);
62extern void write_or_die(int fd, const void *buf, size_t count);
63extern int write_or_whine(int fd, const void *buf, size_t count, const char *msg);
64extern int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg);
65extern void fsync_or_die(int fd, const char *);
66 26
67/* pager.c */ 27/* pager.c */
68extern void setup_pager(void); 28extern void setup_pager(void);
@@ -70,7 +30,7 @@ extern const char *pager_program;
70extern int pager_in_use(void); 30extern int pager_in_use(void);
71extern int pager_use_color; 31extern int pager_use_color;
72 32
73extern bool use_browser; 33extern int use_browser;
74 34
75#ifdef NO_NEWT_SUPPORT 35#ifdef NO_NEWT_SUPPORT
76static inline void setup_browser(void) 36static inline void setup_browser(void)
@@ -83,9 +43,6 @@ void setup_browser(void);
83void exit_browser(bool wait_for_ok); 43void exit_browser(bool wait_for_ok);
84#endif 44#endif
85 45
86extern const char *editor_program;
87extern const char *excludes_file;
88
89char *alias_lookup(const char *alias); 46char *alias_lookup(const char *alias);
90int split_cmdline(char *cmdline, const char ***argv); 47int split_cmdline(char *cmdline, const char ***argv);
91 48
@@ -115,22 +72,12 @@ static inline int is_absolute_path(const char *path)
115 return path[0] == '/'; 72 return path[0] == '/';
116} 73}
117 74
118const char *make_absolute_path(const char *path);
119const char *make_nonrelative_path(const char *path); 75const char *make_nonrelative_path(const char *path);
120const char *make_relative_path(const char *abs, const char *base);
121int normalize_path_copy(char *dst, const char *src);
122int longest_ancestor_length(const char *path, const char *prefix_list);
123char *strip_path_suffix(const char *path, const char *suffix); 76char *strip_path_suffix(const char *path, const char *suffix);
124 77
125extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2))); 78extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
126extern char *perf_path(const char *fmt, ...) __attribute__((format (printf, 1, 2))); 79extern char *perf_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
127/* perf_mkstemp() - create tmp file honoring TMPDIR variable */
128extern int perf_mkstemp(char *path, size_t len, const char *template);
129 80
130extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
131 __attribute__((format (printf, 3, 4)));
132extern char *perf_snpath(char *buf, size_t n, const char *fmt, ...)
133 __attribute__((format (printf, 3, 4)));
134extern char *perf_pathdup(const char *fmt, ...) 81extern char *perf_pathdup(const char *fmt, ...)
135 __attribute__((format (printf, 1, 2))); 82 __attribute__((format (printf, 1, 2)));
136 83
diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
index 21a52e0a4435..62b69ad4aa73 100644
--- a/tools/perf/util/callchain.c
+++ b/tools/perf/util/callchain.c
@@ -15,6 +15,7 @@
15#include <errno.h> 15#include <errno.h>
16#include <math.h> 16#include <math.h>
17 17
18#include "util.h"
18#include "callchain.h" 19#include "callchain.h"
19 20
20bool ip_callchain__valid(struct ip_callchain *chain, event_t *event) 21bool ip_callchain__valid(struct ip_callchain *chain, event_t *event)
diff --git a/tools/perf/util/callchain.h b/tools/perf/util/callchain.h
index 1cba1f5504e7..1ca73e4a2723 100644
--- a/tools/perf/util/callchain.h
+++ b/tools/perf/util/callchain.h
@@ -5,7 +5,6 @@
5#include <linux/list.h> 5#include <linux/list.h>
6#include <linux/rbtree.h> 6#include <linux/rbtree.h>
7#include "event.h" 7#include "event.h"
8#include "util.h"
9#include "symbol.h" 8#include "symbol.h"
10 9
11enum chain_mode { 10enum chain_mode {
diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
index 8784649109ce..dabe892d0e53 100644
--- a/tools/perf/util/config.c
+++ b/tools/perf/util/config.c
@@ -16,7 +16,7 @@ static const char *config_file_name;
16static int config_linenr; 16static int config_linenr;
17static int config_file_eof; 17static int config_file_eof;
18 18
19const char *config_exclusive_filename = NULL; 19static const char *config_exclusive_filename;
20 20
21static int get_next_char(void) 21static int get_next_char(void)
22{ 22{
@@ -291,19 +291,6 @@ static int perf_parse_long(const char *value, long *ret)
291 return 0; 291 return 0;
292} 292}
293 293
294int perf_parse_ulong(const char *value, unsigned long *ret)
295{
296 if (value && *value) {
297 char *end;
298 unsigned long val = strtoul(value, &end, 0);
299 if (!parse_unit_factor(end, &val))
300 return 0;
301 *ret = val;
302 return 1;
303 }
304 return 0;
305}
306
307static void die_bad_config(const char *name) 294static void die_bad_config(const char *name)
308{ 295{
309 if (config_file_name) 296 if (config_file_name)
@@ -319,15 +306,7 @@ int perf_config_int(const char *name, const char *value)
319 return ret; 306 return ret;
320} 307}
321 308
322unsigned long perf_config_ulong(const char *name, const char *value) 309static int perf_config_bool_or_int(const char *name, const char *value, int *is_bool)
323{
324 unsigned long ret;
325 if (!perf_parse_ulong(value, &ret))
326 die_bad_config(name);
327 return ret;
328}
329
330int perf_config_bool_or_int(const char *name, const char *value, int *is_bool)
331{ 310{
332 *is_bool = 1; 311 *is_bool = 1;
333 if (!value) 312 if (!value)
@@ -348,14 +327,6 @@ int perf_config_bool(const char *name, const char *value)
348 return !!perf_config_bool_or_int(name, value, &discard); 327 return !!perf_config_bool_or_int(name, value, &discard);
349} 328}
350 329
351int perf_config_string(const char **dest, const char *var, const char *value)
352{
353 if (!value)
354 return config_error_nonbool(var);
355 *dest = strdup(value);
356 return 0;
357}
358
359static int perf_default_core_config(const char *var __used, const char *value __used) 330static int perf_default_core_config(const char *var __used, const char *value __used)
360{ 331{
361 /* Add other config variables here and to Documentation/config.txt. */ 332 /* Add other config variables here and to Documentation/config.txt. */
@@ -371,7 +342,7 @@ int perf_default_config(const char *var, const char *value, void *dummy __used)
371 return 0; 342 return 0;
372} 343}
373 344
374int perf_config_from_file(config_fn_t fn, const char *filename, void *data) 345static int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
375{ 346{
376 int ret; 347 int ret;
377 FILE *f = fopen(filename, "r"); 348 FILE *f = fopen(filename, "r");
@@ -389,7 +360,7 @@ int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
389 return ret; 360 return ret;
390} 361}
391 362
392const char *perf_etc_perfconfig(void) 363static const char *perf_etc_perfconfig(void)
393{ 364{
394 static const char *system_wide; 365 static const char *system_wide;
395 if (!system_wide) 366 if (!system_wide)
@@ -403,12 +374,12 @@ static int perf_env_bool(const char *k, int def)
403 return v ? perf_config_bool(k, v) : def; 374 return v ? perf_config_bool(k, v) : def;
404} 375}
405 376
406int perf_config_system(void) 377static int perf_config_system(void)
407{ 378{
408 return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0); 379 return !perf_env_bool("PERF_CONFIG_NOSYSTEM", 0);
409} 380}
410 381
411int perf_config_global(void) 382static int perf_config_global(void)
412{ 383{
413 return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0); 384 return !perf_env_bool("PERF_CONFIG_NOGLOBAL", 0);
414} 385}
@@ -450,426 +421,6 @@ int perf_config(config_fn_t fn, void *data)
450} 421}
451 422
452/* 423/*
453 * Find all the stuff for perf_config_set() below.
454 */
455
456#define MAX_MATCHES 512
457
458static struct {
459 int baselen;
460 char* key;
461 int do_not_match;
462 regex_t* value_regex;
463 int multi_replace;
464 size_t offset[MAX_MATCHES];
465 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
466 int seen;
467} store;
468
469static int matches(const char* key, const char* value)
470{
471 return !strcmp(key, store.key) &&
472 (store.value_regex == NULL ||
473 (store.do_not_match ^
474 !regexec(store.value_regex, value, 0, NULL, 0)));
475}
476
477static int store_aux(const char* key, const char* value, void *cb __used)
478{
479 int section_len;
480 const char *ep;
481
482 switch (store.state) {
483 case KEY_SEEN:
484 if (matches(key, value)) {
485 if (store.seen == 1 && store.multi_replace == 0) {
486 warning("%s has multiple values", key);
487 } else if (store.seen >= MAX_MATCHES) {
488 error("too many matches for %s", key);
489 return 1;
490 }
491
492 store.offset[store.seen] = ftell(config_file);
493 store.seen++;
494 }
495 break;
496 case SECTION_SEEN:
497 /*
498 * What we are looking for is in store.key (both
499 * section and var), and its section part is baselen
500 * long. We found key (again, both section and var).
501 * We would want to know if this key is in the same
502 * section as what we are looking for. We already
503 * know we are in the same section as what should
504 * hold store.key.
505 */
506 ep = strrchr(key, '.');
507 section_len = ep - key;
508
509 if ((section_len != store.baselen) ||
510 memcmp(key, store.key, section_len+1)) {
511 store.state = SECTION_END_SEEN;
512 break;
513 }
514
515 /*
516 * Do not increment matches: this is no match, but we
517 * just made sure we are in the desired section.
518 */
519 store.offset[store.seen] = ftell(config_file);
520 /* fallthru */
521 case SECTION_END_SEEN:
522 case START:
523 if (matches(key, value)) {
524 store.offset[store.seen] = ftell(config_file);
525 store.state = KEY_SEEN;
526 store.seen++;
527 } else {
528 if (strrchr(key, '.') - key == store.baselen &&
529 !strncmp(key, store.key, store.baselen)) {
530 store.state = SECTION_SEEN;
531 store.offset[store.seen] = ftell(config_file);
532 }
533 }
534 default:
535 break;
536 }
537 return 0;
538}
539
540static int store_write_section(int fd, const char* key)
541{
542 const char *dot;
543 int i, success;
544 struct strbuf sb = STRBUF_INIT;
545
546 dot = memchr(key, '.', store.baselen);
547 if (dot) {
548 strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
549 for (i = dot - key + 1; i < store.baselen; i++) {
550 if (key[i] == '"' || key[i] == '\\')
551 strbuf_addch(&sb, '\\');
552 strbuf_addch(&sb, key[i]);
553 }
554 strbuf_addstr(&sb, "\"]\n");
555 } else {
556 strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
557 }
558
559 success = (write_in_full(fd, sb.buf, sb.len) == (ssize_t)sb.len);
560 strbuf_release(&sb);
561
562 return success;
563}
564
565static int store_write_pair(int fd, const char* key, const char* value)
566{
567 int i, success;
568 int length = strlen(key + store.baselen + 1);
569 const char *quote = "";
570 struct strbuf sb = STRBUF_INIT;
571
572 /*
573 * Check to see if the value needs to be surrounded with a dq pair.
574 * Note that problematic characters are always backslash-quoted; this
575 * check is about not losing leading or trailing SP and strings that
576 * follow beginning-of-comment characters (i.e. ';' and '#') by the
577 * configuration parser.
578 */
579 if (value[0] == ' ')
580 quote = "\"";
581 for (i = 0; value[i]; i++)
582 if (value[i] == ';' || value[i] == '#')
583 quote = "\"";
584 if (i && value[i - 1] == ' ')
585 quote = "\"";
586
587 strbuf_addf(&sb, "\t%.*s = %s",
588 length, key + store.baselen + 1, quote);
589
590 for (i = 0; value[i]; i++)
591 switch (value[i]) {
592 case '\n':
593 strbuf_addstr(&sb, "\\n");
594 break;
595 case '\t':
596 strbuf_addstr(&sb, "\\t");
597 break;
598 case '"':
599 case '\\':
600 strbuf_addch(&sb, '\\');
601 default:
602 strbuf_addch(&sb, value[i]);
603 break;
604 }
605 strbuf_addf(&sb, "%s\n", quote);
606
607 success = (write_in_full(fd, sb.buf, sb.len) == (ssize_t)sb.len);
608 strbuf_release(&sb);
609
610 return success;
611}
612
613static ssize_t find_beginning_of_line(const char* contents, size_t size,
614 size_t offset_, int* found_bracket)
615{
616 size_t equal_offset = size, bracket_offset = size;
617 ssize_t offset;
618
619contline:
620 for (offset = offset_-2; offset > 0
621 && contents[offset] != '\n'; offset--)
622 switch (contents[offset]) {
623 case '=': equal_offset = offset; break;
624 case ']': bracket_offset = offset; break;
625 default: break;
626 }
627 if (offset > 0 && contents[offset-1] == '\\') {
628 offset_ = offset;
629 goto contline;
630 }
631 if (bracket_offset < equal_offset) {
632 *found_bracket = 1;
633 offset = bracket_offset+1;
634 } else
635 offset++;
636
637 return offset;
638}
639
640int perf_config_set(const char* key, const char* value)
641{
642 return perf_config_set_multivar(key, value, NULL, 0);
643}
644
645/*
646 * If value==NULL, unset in (remove from) config,
647 * if value_regex!=NULL, disregard key/value pairs where value does not match.
648 * if multi_replace==0, nothing, or only one matching key/value is replaced,
649 * else all matching key/values (regardless how many) are removed,
650 * before the new pair is written.
651 *
652 * Returns 0 on success.
653 *
654 * This function does this:
655 *
656 * - it locks the config file by creating ".perf/config.lock"
657 *
658 * - it then parses the config using store_aux() as validator to find
659 * the position on the key/value pair to replace. If it is to be unset,
660 * it must be found exactly once.
661 *
662 * - the config file is mmap()ed and the part before the match (if any) is
663 * written to the lock file, then the changed part and the rest.
664 *
665 * - the config file is removed and the lock file rename()d to it.
666 *
667 */
668int perf_config_set_multivar(const char* key, const char* value,
669 const char* value_regex, int multi_replace)
670{
671 int i, dot;
672 int fd = -1, in_fd;
673 int ret = 0;
674 char* config_filename;
675 const char* last_dot = strrchr(key, '.');
676
677 if (config_exclusive_filename)
678 config_filename = strdup(config_exclusive_filename);
679 else
680 config_filename = perf_pathdup("config");
681
682 /*
683 * Since "key" actually contains the section name and the real
684 * key name separated by a dot, we have to know where the dot is.
685 */
686
687 if (last_dot == NULL) {
688 error("key does not contain a section: %s", key);
689 ret = 2;
690 goto out_free;
691 }
692 store.baselen = last_dot - key;
693
694 store.multi_replace = multi_replace;
695
696 /*
697 * Validate the key and while at it, lower case it for matching.
698 */
699 store.key = malloc(strlen(key) + 1);
700 dot = 0;
701 for (i = 0; key[i]; i++) {
702 unsigned char c = key[i];
703 if (c == '.')
704 dot = 1;
705 /* Leave the extended basename untouched.. */
706 if (!dot || i > store.baselen) {
707 if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
708 error("invalid key: %s", key);
709 free(store.key);
710 ret = 1;
711 goto out_free;
712 }
713 c = tolower(c);
714 } else if (c == '\n') {
715 error("invalid key (newline): %s", key);
716 free(store.key);
717 ret = 1;
718 goto out_free;
719 }
720 store.key[i] = c;
721 }
722 store.key[i] = 0;
723
724 /*
725 * If .perf/config does not exist yet, write a minimal version.
726 */
727 in_fd = open(config_filename, O_RDONLY);
728 if ( in_fd < 0 ) {
729 free(store.key);
730
731 if ( ENOENT != errno ) {
732 error("opening %s: %s", config_filename,
733 strerror(errno));
734 ret = 3; /* same as "invalid config file" */
735 goto out_free;
736 }
737 /* if nothing to unset, error out */
738 if (value == NULL) {
739 ret = 5;
740 goto out_free;
741 }
742
743 store.key = (char*)key;
744 if (!store_write_section(fd, key) ||
745 !store_write_pair(fd, key, value))
746 goto write_err_out;
747 } else {
748 struct stat st;
749 char *contents;
750 ssize_t contents_sz, copy_begin, copy_end;
751 int new_line = 0;
752
753 if (value_regex == NULL)
754 store.value_regex = NULL;
755 else {
756 if (value_regex[0] == '!') {
757 store.do_not_match = 1;
758 value_regex++;
759 } else
760 store.do_not_match = 0;
761
762 store.value_regex = (regex_t*)malloc(sizeof(regex_t));
763 if (regcomp(store.value_regex, value_regex,
764 REG_EXTENDED)) {
765 error("invalid pattern: %s", value_regex);
766 free(store.value_regex);
767 ret = 6;
768 goto out_free;
769 }
770 }
771
772 store.offset[0] = 0;
773 store.state = START;
774 store.seen = 0;
775
776 /*
777 * After this, store.offset will contain the *end* offset
778 * of the last match, or remain at 0 if no match was found.
779 * As a side effect, we make sure to transform only a valid
780 * existing config file.
781 */
782 if (perf_config_from_file(store_aux, config_filename, NULL)) {
783 error("invalid config file %s", config_filename);
784 free(store.key);
785 if (store.value_regex != NULL) {
786 regfree(store.value_regex);
787 free(store.value_regex);
788 }
789 ret = 3;
790 goto out_free;
791 }
792
793 free(store.key);
794 if (store.value_regex != NULL) {
795 regfree(store.value_regex);
796 free(store.value_regex);
797 }
798
799 /* if nothing to unset, or too many matches, error out */
800 if ((store.seen == 0 && value == NULL) ||
801 (store.seen > 1 && multi_replace == 0)) {
802 ret = 5;
803 goto out_free;
804 }
805
806 fstat(in_fd, &st);
807 contents_sz = xsize_t(st.st_size);
808 contents = mmap(NULL, contents_sz, PROT_READ,
809 MAP_PRIVATE, in_fd, 0);
810 close(in_fd);
811
812 if (store.seen == 0)
813 store.seen = 1;
814
815 for (i = 0, copy_begin = 0; i < store.seen; i++) {
816 if (store.offset[i] == 0) {
817 store.offset[i] = copy_end = contents_sz;
818 } else if (store.state != KEY_SEEN) {
819 copy_end = store.offset[i];
820 } else
821 copy_end = find_beginning_of_line(
822 contents, contents_sz,
823 store.offset[i]-2, &new_line);
824
825 if (copy_end > 0 && contents[copy_end-1] != '\n')
826 new_line = 1;
827
828 /* write the first part of the config */
829 if (copy_end > copy_begin) {
830 if (write_in_full(fd, contents + copy_begin,
831 copy_end - copy_begin) <
832 copy_end - copy_begin)
833 goto write_err_out;
834 if (new_line &&
835 write_in_full(fd, "\n", 1) != 1)
836 goto write_err_out;
837 }
838 copy_begin = store.offset[i];
839 }
840
841 /* write the pair (value == NULL means unset) */
842 if (value != NULL) {
843 if (store.state == START) {
844 if (!store_write_section(fd, key))
845 goto write_err_out;
846 }
847 if (!store_write_pair(fd, key, value))
848 goto write_err_out;
849 }
850
851 /* write the rest of the config */
852 if (copy_begin < contents_sz)
853 if (write_in_full(fd, contents + copy_begin,
854 contents_sz - copy_begin) <
855 contents_sz - copy_begin)
856 goto write_err_out;
857
858 munmap(contents, contents_sz);
859 }
860
861 ret = 0;
862
863out_free:
864 free(config_filename);
865 return ret;
866
867write_err_out:
868 goto out_free;
869
870}
871
872/*
873 * Call this to report error for your variable that should not 424 * Call this to report error for your variable that should not
874 * get a boolean value (i.e. "[my] var" means "true"). 425 * get a boolean value (i.e. "[my] var" means "true").
875 */ 426 */
diff --git a/tools/perf/util/debug.c b/tools/perf/util/debug.c
index dd824cf3b628..6cddff2bc970 100644
--- a/tools/perf/util/debug.c
+++ b/tools/perf/util/debug.c
@@ -22,7 +22,7 @@ int eprintf(int level, const char *fmt, ...)
22 22
23 if (verbose >= level) { 23 if (verbose >= level) {
24 va_start(args, fmt); 24 va_start(args, fmt);
25 if (use_browser) 25 if (use_browser > 0)
26 ret = browser__show_help(fmt, args); 26 ret = browser__show_help(fmt, args);
27 else 27 else
28 ret = vfprintf(stderr, fmt, args); 28 ret = vfprintf(stderr, fmt, args);
diff --git a/tools/perf/util/exec_cmd.c b/tools/perf/util/exec_cmd.c
index 2745605dba11..67eeff571568 100644
--- a/tools/perf/util/exec_cmd.c
+++ b/tools/perf/util/exec_cmd.c
@@ -53,8 +53,8 @@ const char *perf_extract_argv0_path(const char *argv0)
53 slash--; 53 slash--;
54 54
55 if (slash >= argv0) { 55 if (slash >= argv0) {
56 argv0_path = xstrndup(argv0, slash - argv0); 56 argv0_path = strndup(argv0, slash - argv0);
57 return slash + 1; 57 return argv0_path ? slash + 1 : NULL;
58 } 58 }
59 59
60 return argv0; 60 return argv0;
@@ -116,7 +116,7 @@ void setup_path(void)
116 strbuf_release(&new_path); 116 strbuf_release(&new_path);
117} 117}
118 118
119const char **prepare_perf_cmd(const char **argv) 119static const char **prepare_perf_cmd(const char **argv)
120{ 120{
121 int argc; 121 int argc;
122 const char **nargv; 122 const char **nargv;
diff --git a/tools/perf/util/exec_cmd.h b/tools/perf/util/exec_cmd.h
index 31647ac92ed1..bc4b915963f5 100644
--- a/tools/perf/util/exec_cmd.h
+++ b/tools/perf/util/exec_cmd.h
@@ -5,7 +5,6 @@ extern void perf_set_argv_exec_path(const char *exec_path);
5extern const char *perf_extract_argv0_path(const char *path); 5extern const char *perf_extract_argv0_path(const char *path);
6extern const char *perf_exec_path(void); 6extern const char *perf_exec_path(void);
7extern void setup_path(void); 7extern void setup_path(void);
8extern const char **prepare_perf_cmd(const char **argv);
9extern int execv_perf_cmd(const char **argv); /* NULL terminated */ 8extern int execv_perf_cmd(const char **argv); /* NULL terminated */
10extern int execl_perf_cmd(const char *cmd, ...); 9extern int execl_perf_cmd(const char *cmd, ...);
11extern const char *system_path(const char *path); 10extern const char *system_path(const char *path);
diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index 8847bec64c54..1f62435f96c2 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -221,29 +221,38 @@ static int __dsos__write_buildid_table(struct list_head *head, pid_t pid,
221 return 0; 221 return 0;
222} 222}
223 223
224static int machine__write_buildid_table(struct machine *self, int fd)
225{
226 int err;
227 u16 kmisc = PERF_RECORD_MISC_KERNEL,
228 umisc = PERF_RECORD_MISC_USER;
229
230 if (!machine__is_host(self)) {
231 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
232 umisc = PERF_RECORD_MISC_GUEST_USER;
233 }
234
235 err = __dsos__write_buildid_table(&self->kernel_dsos, self->pid,
236 kmisc, fd);
237 if (err == 0)
238 err = __dsos__write_buildid_table(&self->user_dsos,
239 self->pid, umisc, fd);
240 return err;
241}
242
224static int dsos__write_buildid_table(struct perf_header *header, int fd) 243static int dsos__write_buildid_table(struct perf_header *header, int fd)
225{ 244{
226 struct perf_session *session = container_of(header, 245 struct perf_session *session = container_of(header,
227 struct perf_session, header); 246 struct perf_session, header);
228 struct rb_node *nd; 247 struct rb_node *nd;
229 int err = 0; 248 int err = machine__write_buildid_table(&session->host_machine, fd);
230 u16 kmisc, umisc; 249
250 if (err)
251 return err;
231 252
232 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { 253 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) {
233 struct machine *pos = rb_entry(nd, struct machine, rb_node); 254 struct machine *pos = rb_entry(nd, struct machine, rb_node);
234 if (machine__is_host(pos)) { 255 err = machine__write_buildid_table(pos, fd);
235 kmisc = PERF_RECORD_MISC_KERNEL;
236 umisc = PERF_RECORD_MISC_USER;
237 } else {
238 kmisc = PERF_RECORD_MISC_GUEST_KERNEL;
239 umisc = PERF_RECORD_MISC_GUEST_USER;
240 }
241
242 err = __dsos__write_buildid_table(&pos->kernel_dsos, pos->pid,
243 kmisc, fd);
244 if (err == 0)
245 err = __dsos__write_buildid_table(&pos->user_dsos,
246 pos->pid, umisc, fd);
247 if (err) 256 if (err)
248 break; 257 break;
249 } 258 }
@@ -363,12 +372,17 @@ static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir)
363 return err; 372 return err;
364} 373}
365 374
366static int dsos__cache_build_ids(struct perf_header *self) 375static int machine__cache_build_ids(struct machine *self, const char *debugdir)
376{
377 int ret = __dsos__cache_build_ids(&self->kernel_dsos, debugdir);
378 ret |= __dsos__cache_build_ids(&self->user_dsos, debugdir);
379 return ret;
380}
381
382static int perf_session__cache_build_ids(struct perf_session *self)
367{ 383{
368 struct perf_session *session = container_of(self,
369 struct perf_session, header);
370 struct rb_node *nd; 384 struct rb_node *nd;
371 int ret = 0; 385 int ret;
372 char debugdir[PATH_MAX]; 386 char debugdir[PATH_MAX];
373 387
374 snprintf(debugdir, sizeof(debugdir), "%s/%s", getenv("HOME"), 388 snprintf(debugdir, sizeof(debugdir), "%s/%s", getenv("HOME"),
@@ -377,25 +391,30 @@ static int dsos__cache_build_ids(struct perf_header *self)
377 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST) 391 if (mkdir(debugdir, 0755) != 0 && errno != EEXIST)
378 return -1; 392 return -1;
379 393
380 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { 394 ret = machine__cache_build_ids(&self->host_machine, debugdir);
395
396 for (nd = rb_first(&self->machines); nd; nd = rb_next(nd)) {
381 struct machine *pos = rb_entry(nd, struct machine, rb_node); 397 struct machine *pos = rb_entry(nd, struct machine, rb_node);
382 ret |= __dsos__cache_build_ids(&pos->kernel_dsos, debugdir); 398 ret |= machine__cache_build_ids(pos, debugdir);
383 ret |= __dsos__cache_build_ids(&pos->user_dsos, debugdir);
384 } 399 }
385 return ret ? -1 : 0; 400 return ret ? -1 : 0;
386} 401}
387 402
388static bool dsos__read_build_ids(struct perf_header *self, bool with_hits) 403static bool machine__read_build_ids(struct machine *self, bool with_hits)
404{
405 bool ret = __dsos__read_build_ids(&self->kernel_dsos, with_hits);
406 ret |= __dsos__read_build_ids(&self->user_dsos, with_hits);
407 return ret;
408}
409
410static bool perf_session__read_build_ids(struct perf_session *self, bool with_hits)
389{ 411{
390 bool ret = false;
391 struct perf_session *session = container_of(self,
392 struct perf_session, header);
393 struct rb_node *nd; 412 struct rb_node *nd;
413 bool ret = machine__read_build_ids(&self->host_machine, with_hits);
394 414
395 for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { 415 for (nd = rb_first(&self->machines); nd; nd = rb_next(nd)) {
396 struct machine *pos = rb_entry(nd, struct machine, rb_node); 416 struct machine *pos = rb_entry(nd, struct machine, rb_node);
397 ret |= __dsos__read_build_ids(&pos->kernel_dsos, with_hits); 417 ret |= machine__read_build_ids(pos, with_hits);
398 ret |= __dsos__read_build_ids(&pos->user_dsos, with_hits);
399 } 418 }
400 419
401 return ret; 420 return ret;
@@ -404,12 +423,14 @@ static bool dsos__read_build_ids(struct perf_header *self, bool with_hits)
404static int perf_header__adds_write(struct perf_header *self, int fd) 423static int perf_header__adds_write(struct perf_header *self, int fd)
405{ 424{
406 int nr_sections; 425 int nr_sections;
426 struct perf_session *session;
407 struct perf_file_section *feat_sec; 427 struct perf_file_section *feat_sec;
408 int sec_size; 428 int sec_size;
409 u64 sec_start; 429 u64 sec_start;
410 int idx = 0, err; 430 int idx = 0, err;
411 431
412 if (dsos__read_build_ids(self, true)) 432 session = container_of(self, struct perf_session, header);
433 if (perf_session__read_build_ids(session, true))
413 perf_header__set_feat(self, HEADER_BUILD_ID); 434 perf_header__set_feat(self, HEADER_BUILD_ID);
414 435
415 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS); 436 nr_sections = bitmap_weight(self->adds_features, HEADER_FEAT_BITS);
@@ -450,7 +471,7 @@ static int perf_header__adds_write(struct perf_header *self, int fd)
450 } 471 }
451 buildid_sec->size = lseek(fd, 0, SEEK_CUR) - 472 buildid_sec->size = lseek(fd, 0, SEEK_CUR) -
452 buildid_sec->offset; 473 buildid_sec->offset;
453 dsos__cache_build_ids(self); 474 perf_session__cache_build_ids(session);
454 } 475 }
455 476
456 lseek(fd, sec_start, SEEK_SET); 477 lseek(fd, sec_start, SEEK_SET);
@@ -490,7 +511,6 @@ int perf_header__write(struct perf_header *self, int fd, bool at_exit)
490 511
491 lseek(fd, sizeof(f_header), SEEK_SET); 512 lseek(fd, sizeof(f_header), SEEK_SET);
492 513
493
494 for (i = 0; i < self->attrs; i++) { 514 for (i = 0; i < self->attrs; i++) {
495 attr = self->attr[i]; 515 attr = self->attr[i];
496 516
diff --git a/tools/perf/util/help.c b/tools/perf/util/help.c
index fbb00978b2e2..6f2975a00358 100644
--- a/tools/perf/util/help.c
+++ b/tools/perf/util/help.c
@@ -4,28 +4,6 @@
4#include "levenshtein.h" 4#include "levenshtein.h"
5#include "help.h" 5#include "help.h"
6 6
7/* most GUI terminals set COLUMNS (although some don't export it) */
8static int term_columns(void)
9{
10 char *col_string = getenv("COLUMNS");
11 int n_cols;
12
13 if (col_string && (n_cols = atoi(col_string)) > 0)
14 return n_cols;
15
16#ifdef TIOCGWINSZ
17 {
18 struct winsize ws;
19 if (!ioctl(1, TIOCGWINSZ, &ws)) {
20 if (ws.ws_col)
21 return ws.ws_col;
22 }
23 }
24#endif
25
26 return 80;
27}
28
29void add_cmdname(struct cmdnames *cmds, const char *name, size_t len) 7void add_cmdname(struct cmdnames *cmds, const char *name, size_t len)
30{ 8{
31 struct cmdname *ent = malloc(sizeof(*ent) + len + 1); 9 struct cmdname *ent = malloc(sizeof(*ent) + len + 1);
@@ -96,9 +74,13 @@ static void pretty_print_string_list(struct cmdnames *cmds, int longest)
96{ 74{
97 int cols = 1, rows; 75 int cols = 1, rows;
98 int space = longest + 1; /* min 1 SP between words */ 76 int space = longest + 1; /* min 1 SP between words */
99 int max_cols = term_columns() - 1; /* don't print *on* the edge */ 77 struct winsize win;
78 int max_cols;
100 int i, j; 79 int i, j;
101 80
81 get_term_dimensions(&win);
82 max_cols = win.ws_col - 1; /* don't print *on* the edge */
83
102 if (space < max_cols) 84 if (space < max_cols)
103 cols = max_cols / space; 85 cols = max_cols / space;
104 rows = (cmds->cnt + cols - 1) / cols; 86 rows = (cmds->cnt + cols - 1) / cols;
@@ -324,7 +306,7 @@ const char *help_unknown_cmd(const char *cmd)
324 306
325 main_cmds.names[0] = NULL; 307 main_cmds.names[0] = NULL;
326 clean_cmdnames(&main_cmds); 308 clean_cmdnames(&main_cmds);
327 fprintf(stderr, "WARNING: You called a Git program named '%s', " 309 fprintf(stderr, "WARNING: You called a perf program named '%s', "
328 "which does not exist.\n" 310 "which does not exist.\n"
329 "Continuing under the assumption that you meant '%s'\n", 311 "Continuing under the assumption that you meant '%s'\n",
330 cmd, assumed); 312 cmd, assumed);
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
index 9a71c94f057a..cbf7eae2ce09 100644
--- a/tools/perf/util/hist.c
+++ b/tools/perf/util/hist.c
@@ -1,4 +1,5 @@
1#include "util.h" 1#include "util.h"
2#include "build-id.h"
2#include "hist.h" 3#include "hist.h"
3#include "session.h" 4#include "session.h"
4#include "sort.h" 5#include "sort.h"
@@ -988,22 +989,42 @@ int hist_entry__annotate(struct hist_entry *self, struct list_head *head)
988 struct symbol *sym = self->ms.sym; 989 struct symbol *sym = self->ms.sym;
989 struct map *map = self->ms.map; 990 struct map *map = self->ms.map;
990 struct dso *dso = map->dso; 991 struct dso *dso = map->dso;
991 const char *filename = dso->long_name; 992 char *filename = dso__build_id_filename(dso, NULL, 0);
993 bool free_filename = true;
992 char command[PATH_MAX * 2]; 994 char command[PATH_MAX * 2];
993 FILE *file; 995 FILE *file;
996 int err = 0;
994 u64 len; 997 u64 len;
995 998
996 if (!filename) 999 if (filename == NULL) {
997 return -1; 1000 if (dso->has_build_id) {
1001 pr_err("Can't annotate %s: not enough memory\n",
1002 sym->name);
1003 return -ENOMEM;
1004 }
1005 goto fallback;
1006 } else if (readlink(filename, command, sizeof(command)) < 0 ||
1007 strstr(command, "[kernel.kallsyms]") ||
1008 access(filename, R_OK)) {
1009 free(filename);
1010fallback:
1011 /*
1012 * If we don't have build-ids or the build-id file isn't in the
1013 * cache, or is just a kallsyms file, well, lets hope that this
1014 * DSO is the same as when 'perf record' ran.
1015 */
1016 filename = dso->long_name;
1017 free_filename = false;
1018 }
998 1019
999 if (dso->origin == DSO__ORIG_KERNEL) { 1020 if (dso->origin == DSO__ORIG_KERNEL) {
1000 if (dso->annotate_warned) 1021 if (dso->annotate_warned)
1001 return 0; 1022 goto out_free_filename;
1023 err = -ENOENT;
1002 dso->annotate_warned = 1; 1024 dso->annotate_warned = 1;
1003 pr_err("Can't annotate %s: No vmlinux file was found in the " 1025 pr_err("Can't annotate %s: No vmlinux file was found in the "
1004 "path:\n", sym->name); 1026 "path\n", sym->name);
1005 vmlinux_path__fprintf(stderr); 1027 goto out_free_filename;
1006 return -1;
1007 } 1028 }
1008 1029
1009 pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__, 1030 pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__,
@@ -1025,14 +1046,17 @@ int hist_entry__annotate(struct hist_entry *self, struct list_head *head)
1025 1046
1026 file = popen(command, "r"); 1047 file = popen(command, "r");
1027 if (!file) 1048 if (!file)
1028 return -1; 1049 goto out_free_filename;
1029 1050
1030 while (!feof(file)) 1051 while (!feof(file))
1031 if (hist_entry__parse_objdump_line(self, file, head) < 0) 1052 if (hist_entry__parse_objdump_line(self, file, head) < 0)
1032 break; 1053 break;
1033 1054
1034 pclose(file); 1055 pclose(file);
1035 return 0; 1056out_free_filename:
1057 if (free_filename)
1058 free(filename);
1059 return err;
1036} 1060}
1037 1061
1038void hists__inc_nr_events(struct hists *self, u32 type) 1062void hists__inc_nr_events(struct hists *self, u32 type)
diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h
index 6f17dcd8412c..83fa33a7b38b 100644
--- a/tools/perf/util/hist.h
+++ b/tools/perf/util/hist.h
@@ -98,12 +98,32 @@ void hists__filter_by_thread(struct hists *self, const struct thread *thread);
98#ifdef NO_NEWT_SUPPORT 98#ifdef NO_NEWT_SUPPORT
99static inline int hists__browse(struct hists *self __used, 99static inline int hists__browse(struct hists *self __used,
100 const char *helpline __used, 100 const char *helpline __used,
101 const char *input_name __used) 101 const char *ev_name __used)
102{ 102{
103 return 0; 103 return 0;
104} 104}
105
106static inline int hists__tui_browse_tree(struct rb_root *self __used,
107 const char *help __used)
108{
109 return 0;
110}
111
112static inline int hist_entry__tui_annotate(struct hist_entry *self __used)
113{
114 return 0;
115}
116#define KEY_LEFT -1
117#define KEY_RIGHT -2
105#else 118#else
119#include <newt.h>
106int hists__browse(struct hists *self, const char *helpline, 120int hists__browse(struct hists *self, const char *helpline,
107 const char *input_name); 121 const char *ev_name);
122int hist_entry__tui_annotate(struct hist_entry *self);
123
124#define KEY_LEFT NEWT_KEY_LEFT
125#define KEY_RIGHT NEWT_KEY_RIGHT
126
127int hists__tui_browse_tree(struct rb_root *self, const char *help);
108#endif 128#endif
109#endif /* __PERF_HIST_H */ 129#endif /* __PERF_HIST_H */
diff --git a/tools/perf/util/newt.c b/tools/perf/util/newt.c
index ccb7c5bb269e..cf182ca132fe 100644
--- a/tools/perf/util/newt.c
+++ b/tools/perf/util/newt.c
@@ -1,7 +1,15 @@
1#define _GNU_SOURCE 1#define _GNU_SOURCE
2#include <stdio.h> 2#include <stdio.h>
3#undef _GNU_SOURCE 3#undef _GNU_SOURCE
4 4/*
5 * slang versions <= 2.0.6 have a "#if HAVE_LONG_LONG" that breaks
6 * the build if it isn't defined. Use the equivalent one that glibc
7 * has on features.h.
8 */
9#include <features.h>
10#ifndef HAVE_LONG_LONG
11#define HAVE_LONG_LONG __GLIBC_HAVE_LONG_LONG
12#endif
5#include <slang.h> 13#include <slang.h>
6#include <stdlib.h> 14#include <stdlib.h>
7#include <newt.h> 15#include <newt.h>
@@ -227,6 +235,15 @@ static bool dialog_yesno(const char *msg)
227 return newtWinChoice(NULL, yes, no, (char *)msg) == 1; 235 return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
228} 236}
229 237
238static void ui__error_window(const char *fmt, ...)
239{
240 va_list ap;
241
242 va_start(ap, fmt);
243 newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
244 va_end(ap);
245}
246
230#define HE_COLORSET_TOP 50 247#define HE_COLORSET_TOP 50
231#define HE_COLORSET_MEDIUM 51 248#define HE_COLORSET_MEDIUM 51
232#define HE_COLORSET_NORMAL 52 249#define HE_COLORSET_NORMAL 52
@@ -375,8 +392,11 @@ static int ui_browser__run(struct ui_browser *self, const char *title,
375 newtFormAddHotKey(self->form, NEWT_KEY_DOWN); 392 newtFormAddHotKey(self->form, NEWT_KEY_DOWN);
376 newtFormAddHotKey(self->form, NEWT_KEY_PGUP); 393 newtFormAddHotKey(self->form, NEWT_KEY_PGUP);
377 newtFormAddHotKey(self->form, NEWT_KEY_PGDN); 394 newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
395 newtFormAddHotKey(self->form, ' ');
378 newtFormAddHotKey(self->form, NEWT_KEY_HOME); 396 newtFormAddHotKey(self->form, NEWT_KEY_HOME);
379 newtFormAddHotKey(self->form, NEWT_KEY_END); 397 newtFormAddHotKey(self->form, NEWT_KEY_END);
398 newtFormAddHotKey(self->form, NEWT_KEY_TAB);
399 newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
380 400
381 if (ui_browser__refresh_entries(self) < 0) 401 if (ui_browser__refresh_entries(self) < 0)
382 return -1; 402 return -1;
@@ -389,6 +409,8 @@ static int ui_browser__run(struct ui_browser *self, const char *title,
389 409
390 if (es->reason != NEWT_EXIT_HOTKEY) 410 if (es->reason != NEWT_EXIT_HOTKEY)
391 break; 411 break;
412 if (is_exit_key(es->u.key))
413 return es->u.key;
392 switch (es->u.key) { 414 switch (es->u.key) {
393 case NEWT_KEY_DOWN: 415 case NEWT_KEY_DOWN:
394 if (self->index == self->nr_entries - 1) 416 if (self->index == self->nr_entries - 1)
@@ -411,6 +433,7 @@ static int ui_browser__run(struct ui_browser *self, const char *title,
411 } 433 }
412 break; 434 break;
413 case NEWT_KEY_PGDN: 435 case NEWT_KEY_PGDN:
436 case ' ':
414 if (self->first_visible_entry_idx + self->height > self->nr_entries - 1) 437 if (self->first_visible_entry_idx + self->height > self->nr_entries - 1)
415 break; 438 break;
416 439
@@ -461,12 +484,10 @@ static int ui_browser__run(struct ui_browser *self, const char *title,
461 } 484 }
462 } 485 }
463 break; 486 break;
464 case NEWT_KEY_ESCAPE: 487 case NEWT_KEY_RIGHT:
465 case NEWT_KEY_LEFT: 488 case NEWT_KEY_LEFT:
466 case CTRL('c'): 489 case NEWT_KEY_TAB:
467 case 'Q': 490 return es->u.key;
468 case 'q':
469 return 0;
470 default: 491 default:
471 continue; 492 continue;
472 } 493 }
@@ -658,18 +679,24 @@ static size_t hist_entry__append_browser(struct hist_entry *self,
658 return ret; 679 return ret;
659} 680}
660 681
661static void hist_entry__annotate_browser(struct hist_entry *self) 682int hist_entry__tui_annotate(struct hist_entry *self)
662{ 683{
663 struct ui_browser browser; 684 struct ui_browser browser;
664 struct newtExitStruct es; 685 struct newtExitStruct es;
665 struct objdump_line *pos, *n; 686 struct objdump_line *pos, *n;
666 LIST_HEAD(head); 687 LIST_HEAD(head);
688 int ret;
667 689
668 if (self->ms.sym == NULL) 690 if (self->ms.sym == NULL)
669 return; 691 return -1;
670 692
671 if (hist_entry__annotate(self, &head) < 0) 693 if (self->ms.map->dso->annotate_warned)
672 return; 694 return -1;
695
696 if (hist_entry__annotate(self, &head) < 0) {
697 ui__error_window(browser__last_msg);
698 return -1;
699 }
673 700
674 ui_helpline__push("Press <- or ESC to exit"); 701 ui_helpline__push("Press <- or ESC to exit");
675 702
@@ -684,7 +711,7 @@ static void hist_entry__annotate_browser(struct hist_entry *self)
684 } 711 }
685 712
686 browser.width += 18; /* Percentage */ 713 browser.width += 18; /* Percentage */
687 ui_browser__run(&browser, self->ms.sym->name, &es); 714 ret = ui_browser__run(&browser, self->ms.sym->name, &es);
688 newtFormDestroy(browser.form); 715 newtFormDestroy(browser.form);
689 newtPopWindow(); 716 newtPopWindow();
690 list_for_each_entry_safe(pos, n, &head, node) { 717 list_for_each_entry_safe(pos, n, &head, node) {
@@ -692,6 +719,7 @@ static void hist_entry__annotate_browser(struct hist_entry *self)
692 objdump_line__free(pos); 719 objdump_line__free(pos);
693 } 720 }
694 ui_helpline__pop(); 721 ui_helpline__pop();
722 return ret;
695} 723}
696 724
697static const void *newt__symbol_tree_get_current(newtComponent self) 725static const void *newt__symbol_tree_get_current(newtComponent self)
@@ -814,6 +842,8 @@ static int hist_browser__populate(struct hist_browser *self, struct hists *hists
814 newtFormAddHotKey(self->form, 'h'); 842 newtFormAddHotKey(self->form, 'h');
815 newtFormAddHotKey(self->form, NEWT_KEY_F1); 843 newtFormAddHotKey(self->form, NEWT_KEY_F1);
816 newtFormAddHotKey(self->form, NEWT_KEY_RIGHT); 844 newtFormAddHotKey(self->form, NEWT_KEY_RIGHT);
845 newtFormAddHotKey(self->form, NEWT_KEY_TAB);
846 newtFormAddHotKey(self->form, NEWT_KEY_UNTAB);
817 newtFormAddComponents(self->form, self->tree, NULL); 847 newtFormAddComponents(self->form, self->tree, NULL);
818 self->selection = newt__symbol_tree_get_current(self->tree); 848 self->selection = newt__symbol_tree_get_current(self->tree);
819 849
@@ -845,7 +875,7 @@ static struct thread *hist_browser__selected_thread(struct hist_browser *self)
845 return he ? he->thread : NULL; 875 return he ? he->thread : NULL;
846} 876}
847 877
848static int hist_browser__title(char *bf, size_t size, const char *input_name, 878static int hist_browser__title(char *bf, size_t size, const char *ev_name,
849 const struct dso *dso, const struct thread *thread) 879 const struct dso *dso, const struct thread *thread)
850{ 880{
851 int printed = 0; 881 int printed = 0;
@@ -859,18 +889,18 @@ static int hist_browser__title(char *bf, size_t size, const char *input_name,
859 printed += snprintf(bf + printed, size - printed, 889 printed += snprintf(bf + printed, size - printed,
860 "%sDSO: %s", thread ? " " : "", 890 "%sDSO: %s", thread ? " " : "",
861 dso->short_name); 891 dso->short_name);
862 return printed ?: snprintf(bf, size, "Report: %s", input_name); 892 return printed ?: snprintf(bf, size, "Event: %s", ev_name);
863} 893}
864 894
865int hists__browse(struct hists *self, const char *helpline, const char *input_name) 895int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
866{ 896{
867 struct hist_browser *browser = hist_browser__new(); 897 struct hist_browser *browser = hist_browser__new();
868 struct pstack *fstack = pstack__new(2); 898 struct pstack *fstack;
869 const struct thread *thread_filter = NULL; 899 const struct thread *thread_filter = NULL;
870 const struct dso *dso_filter = NULL; 900 const struct dso *dso_filter = NULL;
871 struct newtExitStruct es; 901 struct newtExitStruct es;
872 char msg[160]; 902 char msg[160];
873 int err = -1; 903 int key = -1;
874 904
875 if (browser == NULL) 905 if (browser == NULL)
876 return -1; 906 return -1;
@@ -881,7 +911,7 @@ int hists__browse(struct hists *self, const char *helpline, const char *input_na
881 911
882 ui_helpline__push(helpline); 912 ui_helpline__push(helpline);
883 913
884 hist_browser__title(msg, sizeof(msg), input_name, 914 hist_browser__title(msg, sizeof(msg), ev_name,
885 dso_filter, thread_filter); 915 dso_filter, thread_filter);
886 if (hist_browser__populate(browser, self, msg) < 0) 916 if (hist_browser__populate(browser, self, msg) < 0)
887 goto out_free_stack; 917 goto out_free_stack;
@@ -899,11 +929,27 @@ int hists__browse(struct hists *self, const char *helpline, const char *input_na
899 dso = browser->selection->map ? browser->selection->map->dso : NULL; 929 dso = browser->selection->map ? browser->selection->map->dso : NULL;
900 930
901 if (es.reason == NEWT_EXIT_HOTKEY) { 931 if (es.reason == NEWT_EXIT_HOTKEY) {
902 if (es.u.key == NEWT_KEY_F1) 932 key = es.u.key;
933
934 switch (key) {
935 case NEWT_KEY_F1:
903 goto do_help; 936 goto do_help;
937 case NEWT_KEY_TAB:
938 case NEWT_KEY_UNTAB:
939 /*
940 * Exit the browser, let hists__browser_tree
941 * go to the next or previous
942 */
943 goto out_free_stack;
944 default:;
945 }
904 946
905 switch (toupper(es.u.key)) { 947 key = toupper(key);
948 switch (key) {
906 case 'A': 949 case 'A':
950 if (browser->selection->map == NULL &&
951 browser->selection->map->dso->annotate_warned)
952 continue;
907 goto do_annotate; 953 goto do_annotate;
908 case 'D': 954 case 'D':
909 goto zoom_dso; 955 goto zoom_dso;
@@ -922,14 +968,14 @@ do_help:
922 continue; 968 continue;
923 default:; 969 default:;
924 } 970 }
925 if (toupper(es.u.key) == 'Q' || 971 if (is_exit_key(key)) {
926 es.u.key == CTRL('c')) 972 if (key == NEWT_KEY_ESCAPE) {
927 break; 973 if (dialog_yesno("Do you really want to exit?"))
928 if (es.u.key == NEWT_KEY_ESCAPE) { 974 break;
929 if (dialog_yesno("Do you really want to exit?")) 975 else
976 continue;
977 } else
930 break; 978 break;
931 else
932 continue;
933 } 979 }
934 980
935 if (es.u.key == NEWT_KEY_LEFT) { 981 if (es.u.key == NEWT_KEY_LEFT) {
@@ -947,6 +993,7 @@ do_help:
947 } 993 }
948 994
949 if (browser->selection->sym != NULL && 995 if (browser->selection->sym != NULL &&
996 !browser->selection->map->dso->annotate_warned &&
950 asprintf(&options[nr_options], "Annotate %s", 997 asprintf(&options[nr_options], "Annotate %s",
951 browser->selection->sym->name) > 0) 998 browser->selection->sym->name) > 0)
952 annotate = nr_options++; 999 annotate = nr_options++;
@@ -981,6 +1028,7 @@ do_help:
981 struct hist_entry *he; 1028 struct hist_entry *he;
982do_annotate: 1029do_annotate:
983 if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) { 1030 if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
1031 browser->selection->map->dso->annotate_warned = 1;
984 ui_helpline__puts("No vmlinux file found, can't " 1032 ui_helpline__puts("No vmlinux file found, can't "
985 "annotate with just a " 1033 "annotate with just a "
986 "kallsyms file"); 1034 "kallsyms file");
@@ -991,7 +1039,7 @@ do_annotate:
991 if (he == NULL) 1039 if (he == NULL)
992 continue; 1040 continue;
993 1041
994 hist_entry__annotate_browser(he); 1042 hist_entry__tui_annotate(he);
995 } else if (choice == zoom_dso) { 1043 } else if (choice == zoom_dso) {
996zoom_dso: 1044zoom_dso:
997 if (dso_filter) { 1045 if (dso_filter) {
@@ -1008,7 +1056,7 @@ zoom_out_dso:
1008 pstack__push(fstack, &dso_filter); 1056 pstack__push(fstack, &dso_filter);
1009 } 1057 }
1010 hists__filter_by_dso(self, dso_filter); 1058 hists__filter_by_dso(self, dso_filter);
1011 hist_browser__title(msg, sizeof(msg), input_name, 1059 hist_browser__title(msg, sizeof(msg), ev_name,
1012 dso_filter, thread_filter); 1060 dso_filter, thread_filter);
1013 if (hist_browser__populate(browser, self, msg) < 0) 1061 if (hist_browser__populate(browser, self, msg) < 0)
1014 goto out; 1062 goto out;
@@ -1027,18 +1075,49 @@ zoom_out_thread:
1027 pstack__push(fstack, &thread_filter); 1075 pstack__push(fstack, &thread_filter);
1028 } 1076 }
1029 hists__filter_by_thread(self, thread_filter); 1077 hists__filter_by_thread(self, thread_filter);
1030 hist_browser__title(msg, sizeof(msg), input_name, 1078 hist_browser__title(msg, sizeof(msg), ev_name,
1031 dso_filter, thread_filter); 1079 dso_filter, thread_filter);
1032 if (hist_browser__populate(browser, self, msg) < 0) 1080 if (hist_browser__populate(browser, self, msg) < 0)
1033 goto out; 1081 goto out;
1034 } 1082 }
1035 } 1083 }
1036 err = 0;
1037out_free_stack: 1084out_free_stack:
1038 pstack__delete(fstack); 1085 pstack__delete(fstack);
1039out: 1086out:
1040 hist_browser__delete(browser); 1087 hist_browser__delete(browser);
1041 return err; 1088 return key;
1089}
1090
1091int hists__tui_browse_tree(struct rb_root *self, const char *help)
1092{
1093 struct rb_node *first = rb_first(self), *nd = first, *next;
1094 int key = 0;
1095
1096 while (nd) {
1097 struct hists *hists = rb_entry(nd, struct hists, rb_node);
1098 const char *ev_name = __event_name(hists->type, hists->config);
1099
1100 key = hists__browse(hists, help, ev_name);
1101
1102 if (is_exit_key(key))
1103 break;
1104
1105 switch (key) {
1106 case NEWT_KEY_TAB:
1107 next = rb_next(nd);
1108 if (next)
1109 nd = next;
1110 break;
1111 case NEWT_KEY_UNTAB:
1112 if (nd == first)
1113 continue;
1114 nd = rb_prev(nd);
1115 default:
1116 break;
1117 }
1118 }
1119
1120 return key;
1042} 1121}
1043 1122
1044static struct newtPercentTreeColors { 1123static struct newtPercentTreeColors {
@@ -1058,10 +1137,14 @@ static struct newtPercentTreeColors {
1058void setup_browser(void) 1137void setup_browser(void)
1059{ 1138{
1060 struct newtPercentTreeColors *c = &defaultPercentTreeColors; 1139 struct newtPercentTreeColors *c = &defaultPercentTreeColors;
1061 if (!isatty(1)) 1140
1141 if (!isatty(1) || !use_browser || dump_trace) {
1142 use_browser = 0;
1143 setup_pager();
1062 return; 1144 return;
1145 }
1063 1146
1064 use_browser = true; 1147 use_browser = 1;
1065 newtInit(); 1148 newtInit();
1066 newtCls(); 1149 newtCls();
1067 ui_helpline__puts(" "); 1150 ui_helpline__puts(" ");
@@ -1074,7 +1157,7 @@ void setup_browser(void)
1074 1157
1075void exit_browser(bool wait_for_ok) 1158void exit_browser(bool wait_for_ok)
1076{ 1159{
1077 if (use_browser) { 1160 if (use_browser > 0) {
1078 if (wait_for_ok) { 1161 if (wait_for_ok) {
1079 char title[] = "Fatal Error", ok[] = "Ok"; 1162 char title[] = "Fatal Error", ok[] = "Ok";
1080 newtWinMessage(title, ok, browser__last_msg); 1163 newtWinMessage(title, ok, browser__last_msg);
diff --git a/tools/perf/util/path.c b/tools/perf/util/path.c
index fd1f2faaade4..58a470d036dd 100644
--- a/tools/perf/util/path.c
+++ b/tools/perf/util/path.c
@@ -54,21 +54,6 @@ static char *cleanup_path(char *path)
54 return path; 54 return path;
55} 55}
56 56
57char *mksnpath(char *buf, size_t n, const char *fmt, ...)
58{
59 va_list args;
60 unsigned len;
61
62 va_start(args, fmt);
63 len = vsnprintf(buf, n, fmt, args);
64 va_end(args);
65 if (len >= n) {
66 strlcpy(buf, bad_path, n);
67 return buf;
68 }
69 return cleanup_path(buf);
70}
71
72static char *perf_vsnpath(char *buf, size_t n, const char *fmt, va_list args) 57static char *perf_vsnpath(char *buf, size_t n, const char *fmt, va_list args)
73{ 58{
74 const char *perf_dir = get_perf_dir(); 59 const char *perf_dir = get_perf_dir();
@@ -89,15 +74,6 @@ bad:
89 return buf; 74 return buf;
90} 75}
91 76
92char *perf_snpath(char *buf, size_t n, const char *fmt, ...)
93{
94 va_list args;
95 va_start(args, fmt);
96 (void)perf_vsnpath(buf, n, fmt, args);
97 va_end(args);
98 return buf;
99}
100
101char *perf_pathdup(const char *fmt, ...) 77char *perf_pathdup(const char *fmt, ...)
102{ 78{
103 char path[PATH_MAX]; 79 char path[PATH_MAX];
@@ -143,184 +119,6 @@ char *perf_path(const char *fmt, ...)
143 return cleanup_path(pathname); 119 return cleanup_path(pathname);
144} 120}
145 121
146
147/* perf_mkstemp() - create tmp file honoring TMPDIR variable */
148int perf_mkstemp(char *path, size_t len, const char *template)
149{
150 const char *tmp;
151 size_t n;
152
153 tmp = getenv("TMPDIR");
154 if (!tmp)
155 tmp = "/tmp";
156 n = snprintf(path, len, "%s/%s", tmp, template);
157 if (len <= n) {
158 errno = ENAMETOOLONG;
159 return -1;
160 }
161 return mkstemp(path);
162}
163
164
165const char *make_relative_path(const char *abs_path, const char *base)
166{
167 static char buf[PATH_MAX + 1];
168 int baselen;
169
170 if (!base)
171 return abs_path;
172
173 baselen = strlen(base);
174 if (prefixcmp(abs_path, base))
175 return abs_path;
176 if (abs_path[baselen] == '/')
177 baselen++;
178 else if (base[baselen - 1] != '/')
179 return abs_path;
180
181 strcpy(buf, abs_path + baselen);
182
183 return buf;
184}
185
186/*
187 * It is okay if dst == src, but they should not overlap otherwise.
188 *
189 * Performs the following normalizations on src, storing the result in dst:
190 * - Ensures that components are separated by '/' (Windows only)
191 * - Squashes sequences of '/'.
192 * - Removes "." components.
193 * - Removes ".." components, and the components the precede them.
194 * Returns failure (non-zero) if a ".." component appears as first path
195 * component anytime during the normalization. Otherwise, returns success (0).
196 *
197 * Note that this function is purely textual. It does not follow symlinks,
198 * verify the existence of the path, or make any system calls.
199 */
200int normalize_path_copy(char *dst, const char *src)
201{
202 char *dst0;
203
204 if (has_dos_drive_prefix(src)) {
205 *dst++ = *src++;
206 *dst++ = *src++;
207 }
208 dst0 = dst;
209
210 if (is_dir_sep(*src)) {
211 *dst++ = '/';
212 while (is_dir_sep(*src))
213 src++;
214 }
215
216 for (;;) {
217 char c = *src;
218
219 /*
220 * A path component that begins with . could be
221 * special:
222 * (1) "." and ends -- ignore and terminate.
223 * (2) "./" -- ignore them, eat slash and continue.
224 * (3) ".." and ends -- strip one and terminate.
225 * (4) "../" -- strip one, eat slash and continue.
226 */
227 if (c == '.') {
228 if (!src[1]) {
229 /* (1) */
230 src++;
231 } else if (is_dir_sep(src[1])) {
232 /* (2) */
233 src += 2;
234 while (is_dir_sep(*src))
235 src++;
236 continue;
237 } else if (src[1] == '.') {
238 if (!src[2]) {
239 /* (3) */
240 src += 2;
241 goto up_one;
242 } else if (is_dir_sep(src[2])) {
243 /* (4) */
244 src += 3;
245 while (is_dir_sep(*src))
246 src++;
247 goto up_one;
248 }
249 }
250 }
251
252 /* copy up to the next '/', and eat all '/' */
253 while ((c = *src++) != '\0' && !is_dir_sep(c))
254 *dst++ = c;
255 if (is_dir_sep(c)) {
256 *dst++ = '/';
257 while (is_dir_sep(c))
258 c = *src++;
259 src--;
260 } else if (!c)
261 break;
262 continue;
263
264 up_one:
265 /*
266 * dst0..dst is prefix portion, and dst[-1] is '/';
267 * go up one level.
268 */
269 dst--; /* go to trailing '/' */
270 if (dst <= dst0)
271 return -1;
272 /* Windows: dst[-1] cannot be backslash anymore */
273 while (dst0 < dst && dst[-1] != '/')
274 dst--;
275 }
276 *dst = '\0';
277 return 0;
278}
279
280/*
281 * path = Canonical absolute path
282 * prefix_list = Colon-separated list of absolute paths
283 *
284 * Determines, for each path in prefix_list, whether the "prefix" really
285 * is an ancestor directory of path. Returns the length of the longest
286 * ancestor directory, excluding any trailing slashes, or -1 if no prefix
287 * is an ancestor. (Note that this means 0 is returned if prefix_list is
288 * "/".) "/foo" is not considered an ancestor of "/foobar". Directories
289 * are not considered to be their own ancestors. path must be in a
290 * canonical form: empty components, or "." or ".." components are not
291 * allowed. prefix_list may be null, which is like "".
292 */
293int longest_ancestor_length(const char *path, const char *prefix_list)
294{
295 char buf[PATH_MAX+1];
296 const char *ceil, *colon;
297 int len, max_len = -1;
298
299 if (prefix_list == NULL || !strcmp(path, "/"))
300 return -1;
301
302 for (colon = ceil = prefix_list; *colon; ceil = colon+1) {
303 for (colon = ceil; *colon && *colon != PATH_SEP; colon++);
304 len = colon - ceil;
305 if (len == 0 || len > PATH_MAX || !is_absolute_path(ceil))
306 continue;
307 strlcpy(buf, ceil, len+1);
308 if (normalize_path_copy(buf, buf) < 0)
309 continue;
310 len = strlen(buf);
311 if (len > 0 && buf[len-1] == '/')
312 buf[--len] = '\0';
313
314 if (!strncmp(path, buf, len) &&
315 path[len] == '/' &&
316 len > max_len) {
317 max_len = len;
318 }
319 }
320
321 return max_len;
322}
323
324/* strip arbitrary amount of directory separators at end of path */ 122/* strip arbitrary amount of directory separators at end of path */
325static inline int chomp_trailing_dir_sep(const char *path, int len) 123static inline int chomp_trailing_dir_sep(const char *path, int len)
326{ 124{
@@ -354,5 +152,5 @@ char *strip_path_suffix(const char *path, const char *suffix)
354 152
355 if (path_len && !is_dir_sep(path[path_len - 1])) 153 if (path_len && !is_dir_sep(path[path_len - 1]))
356 return NULL; 154 return NULL;
357 return xstrndup(path, chomp_trailing_dir_sep(path, path_len)); 155 return strndup(path, chomp_trailing_dir_sep(path, path_len));
358} 156}
diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
index 562b1443e785..d964cb199c67 100644
--- a/tools/perf/util/probe-finder.c
+++ b/tools/perf/util/probe-finder.c
@@ -668,6 +668,7 @@ static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
668 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1); 668 ret = dwarf_getlocation_addr(&fb_attr, pf->addr, &pf->fb_ops, &nops, 1);
669 if (ret <= 0 || nops == 0) { 669 if (ret <= 0 || nops == 0) {
670 pf->fb_ops = NULL; 670 pf->fb_ops = NULL;
671#if _ELFUTILS_PREREQ(0, 142)
671 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa && 672 } else if (nops == 1 && pf->fb_ops[0].atom == DW_OP_call_frame_cfa &&
672 pf->cfi != NULL) { 673 pf->cfi != NULL) {
673 Dwarf_Frame *frame; 674 Dwarf_Frame *frame;
@@ -677,6 +678,7 @@ static int convert_probe_point(Dwarf_Die *sp_die, struct probe_finder *pf)
677 (uintmax_t)pf->addr); 678 (uintmax_t)pf->addr);
678 return -ENOENT; 679 return -ENOENT;
679 } 680 }
681#endif
680 } 682 }
681 683
682 /* Find each argument */ 684 /* Find each argument */
@@ -741,32 +743,36 @@ static int find_lazy_match_lines(struct list_head *head,
741 const char *fname, const char *pat) 743 const char *fname, const char *pat)
742{ 744{
743 char *fbuf, *p1, *p2; 745 char *fbuf, *p1, *p2;
744 int fd, ret, line, nlines = 0; 746 int fd, line, nlines = -1;
745 struct stat st; 747 struct stat st;
746 748
747 fd = open(fname, O_RDONLY); 749 fd = open(fname, O_RDONLY);
748 if (fd < 0) { 750 if (fd < 0) {
749 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd)); 751 pr_warning("Failed to open %s: %s\n", fname, strerror(-fd));
750 return fd; 752 return -errno;
751 } 753 }
752 754
753 ret = fstat(fd, &st); 755 if (fstat(fd, &st) < 0) {
754 if (ret < 0) {
755 pr_warning("Failed to get the size of %s: %s\n", 756 pr_warning("Failed to get the size of %s: %s\n",
756 fname, strerror(errno)); 757 fname, strerror(errno));
757 return ret; 758 nlines = -errno;
759 goto out_close;
758 } 760 }
759 fbuf = xmalloc(st.st_size + 2); 761
760 ret = read(fd, fbuf, st.st_size); 762 nlines = -ENOMEM;
761 if (ret < 0) { 763 fbuf = malloc(st.st_size + 2);
764 if (fbuf == NULL)
765 goto out_close;
766 if (read(fd, fbuf, st.st_size) < 0) {
762 pr_warning("Failed to read %s: %s\n", fname, strerror(errno)); 767 pr_warning("Failed to read %s: %s\n", fname, strerror(errno));
763 return ret; 768 nlines = -errno;
769 goto out_free_fbuf;
764 } 770 }
765 close(fd);
766 fbuf[st.st_size] = '\n'; /* Dummy line */ 771 fbuf[st.st_size] = '\n'; /* Dummy line */
767 fbuf[st.st_size + 1] = '\0'; 772 fbuf[st.st_size + 1] = '\0';
768 p1 = fbuf; 773 p1 = fbuf;
769 line = 1; 774 line = 1;
775 nlines = 0;
770 while ((p2 = strchr(p1, '\n')) != NULL) { 776 while ((p2 = strchr(p1, '\n')) != NULL) {
771 *p2 = '\0'; 777 *p2 = '\0';
772 if (strlazymatch(p1, pat)) { 778 if (strlazymatch(p1, pat)) {
@@ -776,7 +782,10 @@ static int find_lazy_match_lines(struct list_head *head,
776 line++; 782 line++;
777 p1 = p2 + 1; 783 p1 = p2 + 1;
778 } 784 }
785out_free_fbuf:
779 free(fbuf); 786 free(fbuf);
787out_close:
788 close(fd);
780 return nlines; 789 return nlines;
781} 790}
782 791
@@ -953,11 +962,15 @@ int find_kprobe_trace_events(int fd, struct perf_probe_event *pev,
953 if (!dbg) { 962 if (!dbg) {
954 pr_warning("No dwarf info found in the vmlinux - " 963 pr_warning("No dwarf info found in the vmlinux - "
955 "please rebuild with CONFIG_DEBUG_INFO=y.\n"); 964 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
965 free(pf.tevs);
966 *tevs = NULL;
956 return -EBADF; 967 return -EBADF;
957 } 968 }
958 969
970#if _ELFUTILS_PREREQ(0, 142)
959 /* Get the call frame information from this dwarf */ 971 /* Get the call frame information from this dwarf */
960 pf.cfi = dwarf_getcfi(dbg); 972 pf.cfi = dwarf_getcfi(dbg);
973#endif
961 974
962 off = 0; 975 off = 0;
963 line_list__init(&pf.lcache); 976 line_list__init(&pf.lcache);
diff --git a/tools/perf/util/probe-finder.h b/tools/perf/util/probe-finder.h
index 66f1980e3855..e1f61dcd18ff 100644
--- a/tools/perf/util/probe-finder.h
+++ b/tools/perf/util/probe-finder.h
@@ -29,6 +29,7 @@ extern int find_line_range(int fd, struct line_range *lr);
29 29
30#include <dwarf.h> 30#include <dwarf.h>
31#include <libdw.h> 31#include <libdw.h>
32#include <version.h>
32 33
33struct probe_finder { 34struct probe_finder {
34 struct perf_probe_event *pev; /* Target probe event */ 35 struct perf_probe_event *pev; /* Target probe event */
@@ -44,7 +45,9 @@ struct probe_finder {
44 struct list_head lcache; /* Line cache for lazy match */ 45 struct list_head lcache; /* Line cache for lazy match */
45 46
46 /* For variable searching */ 47 /* For variable searching */
48#if _ELFUTILS_PREREQ(0, 142)
47 Dwarf_CFI *cfi; /* Call Frame Information */ 49 Dwarf_CFI *cfi; /* Call Frame Information */
50#endif
48 Dwarf_Op *fb_ops; /* Frame base attribute */ 51 Dwarf_Op *fb_ops; /* Frame base attribute */
49 struct perf_probe_arg *pvar; /* Current target variable */ 52 struct perf_probe_arg *pvar; /* Current target variable */
50 struct kprobe_trace_arg *tvar; /* Current result variable */ 53 struct kprobe_trace_arg *tvar; /* Current result variable */
diff --git a/tools/perf/util/quote.c b/tools/perf/util/quote.c
index 2726fe40eb5d..01f03242b86a 100644
--- a/tools/perf/util/quote.c
+++ b/tools/perf/util/quote.c
@@ -1,8 +1,6 @@
1#include "cache.h" 1#include "cache.h"
2#include "quote.h" 2#include "quote.h"
3 3
4int quote_path_fully = 1;
5
6/* Help to copy the thing properly quoted for the shell safety. 4/* Help to copy the thing properly quoted for the shell safety.
7 * any single quote is replaced with '\'', any exclamation point 5 * any single quote is replaced with '\'', any exclamation point
8 * is replaced with '\!', and the whole thing is enclosed in a 6 * is replaced with '\!', and the whole thing is enclosed in a
@@ -19,7 +17,7 @@ static inline int need_bs_quote(char c)
19 return (c == '\'' || c == '!'); 17 return (c == '\'' || c == '!');
20} 18}
21 19
22void sq_quote_buf(struct strbuf *dst, const char *src) 20static void sq_quote_buf(struct strbuf *dst, const char *src)
23{ 21{
24 char *to_free = NULL; 22 char *to_free = NULL;
25 23
@@ -41,23 +39,6 @@ void sq_quote_buf(struct strbuf *dst, const char *src)
41 free(to_free); 39 free(to_free);
42} 40}
43 41
44void sq_quote_print(FILE *stream, const char *src)
45{
46 char c;
47
48 fputc('\'', stream);
49 while ((c = *src++)) {
50 if (need_bs_quote(c)) {
51 fputs("'\\", stream);
52 fputc(c, stream);
53 fputc('\'', stream);
54 } else {
55 fputc(c, stream);
56 }
57 }
58 fputc('\'', stream);
59}
60
61void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen) 42void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
62{ 43{
63 int i; 44 int i;
@@ -71,415 +52,3 @@ void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
71 die("Too many or long arguments"); 52 die("Too many or long arguments");
72 } 53 }
73} 54}
74
75char *sq_dequote_step(char *arg, char **next)
76{
77 char *dst = arg;
78 char *src = arg;
79 char c;
80
81 if (*src != '\'')
82 return NULL;
83 for (;;) {
84 c = *++src;
85 if (!c)
86 return NULL;
87 if (c != '\'') {
88 *dst++ = c;
89 continue;
90 }
91 /* We stepped out of sq */
92 switch (*++src) {
93 case '\0':
94 *dst = 0;
95 if (next)
96 *next = NULL;
97 return arg;
98 case '\\':
99 c = *++src;
100 if (need_bs_quote(c) && *++src == '\'') {
101 *dst++ = c;
102 continue;
103 }
104 /* Fallthrough */
105 default:
106 if (!next || !isspace(*src))
107 return NULL;
108 do {
109 c = *++src;
110 } while (isspace(c));
111 *dst = 0;
112 *next = src;
113 return arg;
114 }
115 }
116}
117
118char *sq_dequote(char *arg)
119{
120 return sq_dequote_step(arg, NULL);
121}
122
123int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc)
124{
125 char *next = arg;
126
127 if (!*arg)
128 return 0;
129 do {
130 char *dequoted = sq_dequote_step(next, &next);
131 if (!dequoted)
132 return -1;
133 ALLOC_GROW(*argv, *nr + 1, *alloc);
134 (*argv)[(*nr)++] = dequoted;
135 } while (next);
136
137 return 0;
138}
139
140/* 1 means: quote as octal
141 * 0 means: quote as octal if (quote_path_fully)
142 * -1 means: never quote
143 * c: quote as "\\c"
144 */
145#define X8(x) x, x, x, x, x, x, x, x
146#define X16(x) X8(x), X8(x)
147static signed char const sq_lookup[256] = {
148 /* 0 1 2 3 4 5 6 7 */
149 /* 0x00 */ 1, 1, 1, 1, 1, 1, 1, 'a',
150 /* 0x08 */ 'b', 't', 'n', 'v', 'f', 'r', 1, 1,
151 /* 0x10 */ X16(1),
152 /* 0x20 */ -1, -1, '"', -1, -1, -1, -1, -1,
153 /* 0x28 */ X16(-1), X16(-1), X16(-1),
154 /* 0x58 */ -1, -1, -1, -1,'\\', -1, -1, -1,
155 /* 0x60 */ X16(-1), X8(-1),
156 /* 0x78 */ -1, -1, -1, -1, -1, -1, -1, 1,
157 /* 0x80 */ /* set to 0 */
158};
159
160static inline int sq_must_quote(char c)
161{
162 return sq_lookup[(unsigned char)c] + quote_path_fully > 0;
163}
164
165/*
166 * Returns the longest prefix not needing a quote up to maxlen if
167 * positive.
168 * This stops at the first \0 because it's marked as a character
169 * needing an escape.
170 */
171static ssize_t next_quote_pos(const char *s, ssize_t maxlen)
172{
173 ssize_t len;
174
175 if (maxlen < 0) {
176 for (len = 0; !sq_must_quote(s[len]); len++);
177 } else {
178 for (len = 0; len < maxlen && !sq_must_quote(s[len]); len++);
179 }
180 return len;
181}
182
183/*
184 * C-style name quoting.
185 *
186 * (1) if sb and fp are both NULL, inspect the input name and counts the
187 * number of bytes that are needed to hold c_style quoted version of name,
188 * counting the double quotes around it but not terminating NUL, and
189 * returns it.
190 * However, if name does not need c_style quoting, it returns 0.
191 *
192 * (2) if sb or fp are not NULL, it emits the c_style quoted version
193 * of name, enclosed with double quotes if asked and needed only.
194 * Return value is the same as in (1).
195 */
196static size_t quote_c_style_counted(const char *name, ssize_t maxlen,
197 struct strbuf *sb, FILE *fp, int no_dq)
198{
199#define EMIT(c) \
200 do { \
201 if (sb) strbuf_addch(sb, (c)); \
202 if (fp) fputc((c), fp); \
203 count++; \
204 } while (0)
205
206#define EMITBUF(s, l) \
207 do { \
208 int __ret; \
209 if (sb) strbuf_add(sb, (s), (l)); \
210 if (fp) __ret = fwrite((s), (l), 1, fp); \
211 count += (l); \
212 } while (0)
213
214 ssize_t len, count = 0;
215 const char *p = name;
216
217 for (;;) {
218 int ch;
219
220 len = next_quote_pos(p, maxlen);
221 if (len == maxlen || !p[len])
222 break;
223
224 if (!no_dq && p == name)
225 EMIT('"');
226
227 EMITBUF(p, len);
228 EMIT('\\');
229 p += len;
230 ch = (unsigned char)*p++;
231 if (sq_lookup[ch] >= ' ') {
232 EMIT(sq_lookup[ch]);
233 } else {
234 EMIT(((ch >> 6) & 03) + '0');
235 EMIT(((ch >> 3) & 07) + '0');
236 EMIT(((ch >> 0) & 07) + '0');
237 }
238 }
239
240 EMITBUF(p, len);
241 if (p == name) /* no ending quote needed */
242 return 0;
243
244 if (!no_dq)
245 EMIT('"');
246 return count;
247}
248
249size_t quote_c_style(const char *name, struct strbuf *sb, FILE *fp, int nodq)
250{
251 return quote_c_style_counted(name, -1, sb, fp, nodq);
252}
253
254void quote_two_c_style(struct strbuf *sb, const char *prefix, const char *path, int nodq)
255{
256 if (quote_c_style(prefix, NULL, NULL, 0) ||
257 quote_c_style(path, NULL, NULL, 0)) {
258 if (!nodq)
259 strbuf_addch(sb, '"');
260 quote_c_style(prefix, sb, NULL, 1);
261 quote_c_style(path, sb, NULL, 1);
262 if (!nodq)
263 strbuf_addch(sb, '"');
264 } else {
265 strbuf_addstr(sb, prefix);
266 strbuf_addstr(sb, path);
267 }
268}
269
270void write_name_quoted(const char *name, FILE *fp, int terminator)
271{
272 if (terminator) {
273 quote_c_style(name, NULL, fp, 0);
274 } else {
275 fputs(name, fp);
276 }
277 fputc(terminator, fp);
278}
279
280void write_name_quotedpfx(const char *pfx, ssize_t pfxlen,
281 const char *name, FILE *fp, int terminator)
282{
283 int needquote = 0;
284
285 if (terminator) {
286 needquote = next_quote_pos(pfx, pfxlen) < pfxlen
287 || name[next_quote_pos(name, -1)];
288 }
289 if (needquote) {
290 fputc('"', fp);
291 quote_c_style_counted(pfx, pfxlen, NULL, fp, 1);
292 quote_c_style(name, NULL, fp, 1);
293 fputc('"', fp);
294 } else {
295 int ret;
296
297 ret = fwrite(pfx, pfxlen, 1, fp);
298 fputs(name, fp);
299 }
300 fputc(terminator, fp);
301}
302
303/* quote path as relative to the given prefix */
304char *quote_path_relative(const char *in, int len,
305 struct strbuf *out, const char *prefix)
306{
307 int needquote;
308
309 if (len < 0)
310 len = strlen(in);
311
312 /* "../" prefix itself does not need quoting, but "in" might. */
313 needquote = (next_quote_pos(in, len) < len);
314 strbuf_setlen(out, 0);
315 strbuf_grow(out, len);
316
317 if (needquote)
318 strbuf_addch(out, '"');
319 if (prefix) {
320 int off = 0;
321 while (off < len && prefix[off] && prefix[off] == in[off])
322 if (prefix[off] == '/') {
323 prefix += off + 1;
324 in += off + 1;
325 len -= off + 1;
326 off = 0;
327 } else
328 off++;
329
330 for (; *prefix; prefix++)
331 if (*prefix == '/')
332 strbuf_addstr(out, "../");
333 }
334
335 quote_c_style_counted (in, len, out, NULL, 1);
336
337 if (needquote)
338 strbuf_addch(out, '"');
339 if (!out->len)
340 strbuf_addstr(out, "./");
341
342 return out->buf;
343}
344
345/*
346 * C-style name unquoting.
347 *
348 * Quoted should point at the opening double quote.
349 * + Returns 0 if it was able to unquote the string properly, and appends the
350 * result in the strbuf `sb'.
351 * + Returns -1 in case of error, and doesn't touch the strbuf. Though note
352 * that this function will allocate memory in the strbuf, so calling
353 * strbuf_release is mandatory whichever result unquote_c_style returns.
354 *
355 * Updates endp pointer to point at one past the ending double quote if given.
356 */
357int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)
358{
359 size_t oldlen = sb->len, len;
360 int ch, ac;
361
362 if (*quoted++ != '"')
363 return -1;
364
365 for (;;) {
366 len = strcspn(quoted, "\"\\");
367 strbuf_add(sb, quoted, len);
368 quoted += len;
369
370 switch (*quoted++) {
371 case '"':
372 if (endp)
373 *endp = quoted;
374 return 0;
375 case '\\':
376 break;
377 default:
378 goto error;
379 }
380
381 switch ((ch = *quoted++)) {
382 case 'a': ch = '\a'; break;
383 case 'b': ch = '\b'; break;
384 case 'f': ch = '\f'; break;
385 case 'n': ch = '\n'; break;
386 case 'r': ch = '\r'; break;
387 case 't': ch = '\t'; break;
388 case 'v': ch = '\v'; break;
389
390 case '\\': case '"':
391 break; /* verbatim */
392
393 /* octal values with first digit over 4 overflow */
394 case '0': case '1': case '2': case '3':
395 ac = ((ch - '0') << 6);
396 if ((ch = *quoted++) < '0' || '7' < ch)
397 goto error;
398 ac |= ((ch - '0') << 3);
399 if ((ch = *quoted++) < '0' || '7' < ch)
400 goto error;
401 ac |= (ch - '0');
402 ch = ac;
403 break;
404 default:
405 goto error;
406 }
407 strbuf_addch(sb, ch);
408 }
409
410 error:
411 strbuf_setlen(sb, oldlen);
412 return -1;
413}
414
415/* quoting as a string literal for other languages */
416
417void perl_quote_print(FILE *stream, const char *src)
418{
419 const char sq = '\'';
420 const char bq = '\\';
421 char c;
422
423 fputc(sq, stream);
424 while ((c = *src++)) {
425 if (c == sq || c == bq)
426 fputc(bq, stream);
427 fputc(c, stream);
428 }
429 fputc(sq, stream);
430}
431
432void python_quote_print(FILE *stream, const char *src)
433{
434 const char sq = '\'';
435 const char bq = '\\';
436 const char nl = '\n';
437 char c;
438
439 fputc(sq, stream);
440 while ((c = *src++)) {
441 if (c == nl) {
442 fputc(bq, stream);
443 fputc('n', stream);
444 continue;
445 }
446 if (c == sq || c == bq)
447 fputc(bq, stream);
448 fputc(c, stream);
449 }
450 fputc(sq, stream);
451}
452
453void tcl_quote_print(FILE *stream, const char *src)
454{
455 char c;
456
457 fputc('"', stream);
458 while ((c = *src++)) {
459 switch (c) {
460 case '[': case ']':
461 case '{': case '}':
462 case '$': case '\\': case '"':
463 fputc('\\', stream);
464 default:
465 fputc(c, stream);
466 break;
467 case '\f':
468 fputs("\\f", stream);
469 break;
470 case '\r':
471 fputs("\\r", stream);
472 break;
473 case '\n':
474 fputs("\\n", stream);
475 break;
476 case '\t':
477 fputs("\\t", stream);
478 break;
479 case '\v':
480 fputs("\\v", stream);
481 break;
482 }
483 }
484 fputc('"', stream);
485}
diff --git a/tools/perf/util/quote.h b/tools/perf/util/quote.h
index b6a019733919..172889ea234f 100644
--- a/tools/perf/util/quote.h
+++ b/tools/perf/util/quote.h
@@ -22,47 +22,8 @@
22 * 22 *
23 * Note that the above examples leak memory! Remember to free result from 23 * Note that the above examples leak memory! Remember to free result from
24 * sq_quote() in a real application. 24 * sq_quote() in a real application.
25 *
26 * sq_quote_buf() writes to an existing buffer of specified size; it
27 * will return the number of characters that would have been written
28 * excluding the final null regardless of the buffer size.
29 */ 25 */
30 26
31extern void sq_quote_print(FILE *stream, const char *src);
32
33extern void sq_quote_buf(struct strbuf *, const char *src);
34extern void sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen); 27extern void sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen);
35 28
36/* This unwraps what sq_quote() produces in place, but returns
37 * NULL if the input does not look like what sq_quote would have
38 * produced.
39 */
40extern char *sq_dequote(char *);
41
42/*
43 * Same as the above, but can be used to unwrap many arguments in the
44 * same string separated by space. "next" is changed to point to the
45 * next argument that should be passed as first parameter. When there
46 * is no more argument to be dequoted, "next" is updated to point to NULL.
47 */
48extern char *sq_dequote_step(char *arg, char **next);
49extern int sq_dequote_to_argv(char *arg, const char ***argv, int *nr, int *alloc);
50
51extern int unquote_c_style(struct strbuf *, const char *quoted, const char **endp);
52extern size_t quote_c_style(const char *name, struct strbuf *, FILE *, int no_dq);
53extern void quote_two_c_style(struct strbuf *, const char *, const char *, int);
54
55extern void write_name_quoted(const char *name, FILE *, int terminator);
56extern void write_name_quotedpfx(const char *pfx, ssize_t pfxlen,
57 const char *name, FILE *, int terminator);
58
59/* quote path as relative to the given prefix */
60char *quote_path_relative(const char *in, int len,
61 struct strbuf *out, const char *prefix);
62
63/* quoting as a string literal for other languages */
64extern void perl_quote_print(FILE *stream, const char *src);
65extern void python_quote_print(FILE *stream, const char *src);
66extern void tcl_quote_print(FILE *stream, const char *src);
67
68#endif /* __PERF_QUOTE_H */ 29#endif /* __PERF_QUOTE_H */
diff --git a/tools/perf/util/run-command.c b/tools/perf/util/run-command.c
index 2b615acf94d7..da8e9b285f51 100644
--- a/tools/perf/util/run-command.c
+++ b/tools/perf/util/run-command.c
@@ -212,93 +212,3 @@ int run_command_v_opt(const char **argv, int opt)
212 prepare_run_command_v_opt(&cmd, argv, opt); 212 prepare_run_command_v_opt(&cmd, argv, opt);
213 return run_command(&cmd); 213 return run_command(&cmd);
214} 214}
215
216int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
217{
218 struct child_process cmd;
219 prepare_run_command_v_opt(&cmd, argv, opt);
220 cmd.dir = dir;
221 cmd.env = env;
222 return run_command(&cmd);
223}
224
225int start_async(struct async *async)
226{
227 int pipe_out[2];
228
229 if (pipe(pipe_out) < 0)
230 return error("cannot create pipe: %s", strerror(errno));
231 async->out = pipe_out[0];
232
233 /* Flush stdio before fork() to avoid cloning buffers */
234 fflush(NULL);
235
236 async->pid = fork();
237 if (async->pid < 0) {
238 error("fork (async) failed: %s", strerror(errno));
239 close_pair(pipe_out);
240 return -1;
241 }
242 if (!async->pid) {
243 close(pipe_out[0]);
244 exit(!!async->proc(pipe_out[1], async->data));
245 }
246 close(pipe_out[1]);
247
248 return 0;
249}
250
251int finish_async(struct async *async)
252{
253 int ret = 0;
254
255 if (wait_or_whine(async->pid))
256 ret = error("waitpid (async) failed");
257
258 return ret;
259}
260
261int run_hook(const char *index_file, const char *name, ...)
262{
263 struct child_process hook;
264 const char **argv = NULL, *env[2];
265 char idx[PATH_MAX];
266 va_list args;
267 int ret;
268 size_t i = 0, alloc = 0;
269
270 if (access(perf_path("hooks/%s", name), X_OK) < 0)
271 return 0;
272
273 va_start(args, name);
274 ALLOC_GROW(argv, i + 1, alloc);
275 argv[i++] = perf_path("hooks/%s", name);
276 while (argv[i-1]) {
277 ALLOC_GROW(argv, i + 1, alloc);
278 argv[i++] = va_arg(args, const char *);
279 }
280 va_end(args);
281
282 memset(&hook, 0, sizeof(hook));
283 hook.argv = argv;
284 hook.no_stdin = 1;
285 hook.stdout_to_stderr = 1;
286 if (index_file) {
287 snprintf(idx, sizeof(idx), "PERF_INDEX_FILE=%s", index_file);
288 env[0] = idx;
289 env[1] = NULL;
290 hook.env = env;
291 }
292
293 ret = start_command(&hook);
294 free(argv);
295 if (ret) {
296 warning("Could not spawn %s", argv[0]);
297 return ret;
298 }
299 ret = finish_command(&hook);
300 if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
301 warning("%s exited due to uncaught signal", argv[0]);
302
303 return ret;
304}
diff --git a/tools/perf/util/run-command.h b/tools/perf/util/run-command.h
index d79028727ce2..1ef264d5069c 100644
--- a/tools/perf/util/run-command.h
+++ b/tools/perf/util/run-command.h
@@ -50,39 +50,9 @@ int start_command(struct child_process *);
50int finish_command(struct child_process *); 50int finish_command(struct child_process *);
51int run_command(struct child_process *); 51int run_command(struct child_process *);
52 52
53extern int run_hook(const char *index_file, const char *name, ...);
54
55#define RUN_COMMAND_NO_STDIN 1 53#define RUN_COMMAND_NO_STDIN 1
56#define RUN_PERF_CMD 2 /*If this is to be perf sub-command */ 54#define RUN_PERF_CMD 2 /*If this is to be perf sub-command */
57#define RUN_COMMAND_STDOUT_TO_STDERR 4 55#define RUN_COMMAND_STDOUT_TO_STDERR 4
58int run_command_v_opt(const char **argv, int opt); 56int run_command_v_opt(const char **argv, int opt);
59 57
60/*
61 * env (the environment) is to be formatted like environ: "VAR=VALUE".
62 * To unset an environment variable use just "VAR".
63 */
64int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env);
65
66/*
67 * The purpose of the following functions is to feed a pipe by running
68 * a function asynchronously and providing output that the caller reads.
69 *
70 * It is expected that no synchronization and mutual exclusion between
71 * the caller and the feed function is necessary so that the function
72 * can run in a thread without interfering with the caller.
73 */
74struct async {
75 /*
76 * proc writes to fd and closes it;
77 * returns 0 on success, non-zero on failure
78 */
79 int (*proc)(int fd, void *data);
80 void *data;
81 int out; /* caller reads from here and closes it */
82 pid_t pid;
83};
84
85int start_async(struct async *async);
86int finish_async(struct async *async);
87
88#endif /* __PERF_RUN_COMMAND_H */ 58#endif /* __PERF_RUN_COMMAND_H */
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 25bfca4f10f0..8f83a1835766 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -5,6 +5,7 @@
5#include <byteswap.h> 5#include <byteswap.h>
6#include <unistd.h> 6#include <unistd.h>
7#include <sys/types.h> 7#include <sys/types.h>
8#include <sys/mman.h>
8 9
9#include "session.h" 10#include "session.h"
10#include "sort.h" 11#include "sort.h"
@@ -894,3 +895,10 @@ size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp)
894 __dsos__fprintf(&self->host_machine.user_dsos, fp) + 895 __dsos__fprintf(&self->host_machine.user_dsos, fp) +
895 machines__fprintf_dsos(&self->machines, fp); 896 machines__fprintf_dsos(&self->machines, fp);
896} 897}
898
899size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
900 bool with_hits)
901{
902 size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
903 return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
904}
diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h
index e7fce486ebe2..55c6881b218d 100644
--- a/tools/perf/util/session.h
+++ b/tools/perf/util/session.h
@@ -132,12 +132,8 @@ void perf_session__process_machines(struct perf_session *self,
132 132
133size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp); 133size_t perf_session__fprintf_dsos(struct perf_session *self, FILE *fp);
134 134
135static inline 135size_t perf_session__fprintf_dsos_buildid(struct perf_session *self,
136size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp, 136 FILE *fp, bool with_hits);
137 bool with_hits)
138{
139 return machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
140}
141 137
142static inline 138static inline
143size_t perf_session__fprintf_nr_events(struct perf_session *self, FILE *fp) 139size_t perf_session__fprintf_nr_events(struct perf_session *self, FILE *fp)
diff --git a/tools/perf/util/sigchain.c b/tools/perf/util/sigchain.c
index 1118b99e57d3..ba785e9b1841 100644
--- a/tools/perf/util/sigchain.c
+++ b/tools/perf/util/sigchain.c
@@ -16,7 +16,7 @@ static void check_signum(int sig)
16 die("BUG: signal out of range: %d", sig); 16 die("BUG: signal out of range: %d", sig);
17} 17}
18 18
19int sigchain_push(int sig, sigchain_fun f) 19static int sigchain_push(int sig, sigchain_fun f)
20{ 20{
21 struct sigchain_signal *s = signals + sig; 21 struct sigchain_signal *s = signals + sig;
22 check_signum(sig); 22 check_signum(sig);
diff --git a/tools/perf/util/sigchain.h b/tools/perf/util/sigchain.h
index 1a53c11265fd..959d64eb5557 100644
--- a/tools/perf/util/sigchain.h
+++ b/tools/perf/util/sigchain.h
@@ -3,7 +3,6 @@
3 3
4typedef void (*sigchain_fun)(int); 4typedef void (*sigchain_fun)(int);
5 5
6int sigchain_push(int sig, sigchain_fun f);
7int sigchain_pop(int sig); 6int sigchain_pop(int sig);
8 7
9void sigchain_push_common(sigchain_fun f); 8void sigchain_push_common(sigchain_fun f);
diff --git a/tools/perf/util/strbuf.c b/tools/perf/util/strbuf.c
index 5249d5a1b0c2..92e068517c1a 100644
--- a/tools/perf/util/strbuf.c
+++ b/tools/perf/util/strbuf.c
@@ -41,16 +41,6 @@ char *strbuf_detach(struct strbuf *sb, size_t *sz)
41 return res; 41 return res;
42} 42}
43 43
44void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
45{
46 strbuf_release(sb);
47 sb->buf = buf;
48 sb->len = len;
49 sb->alloc = alloc;
50 strbuf_grow(sb, 0);
51 sb->buf[sb->len] = '\0';
52}
53
54void strbuf_grow(struct strbuf *sb, size_t extra) 44void strbuf_grow(struct strbuf *sb, size_t extra)
55{ 45{
56 if (sb->len + extra + 1 <= sb->len) 46 if (sb->len + extra + 1 <= sb->len)
@@ -60,94 +50,7 @@ void strbuf_grow(struct strbuf *sb, size_t extra)
60 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc); 50 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
61} 51}
62 52
63void strbuf_trim(struct strbuf *sb) 53static void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
64{
65 char *b = sb->buf;
66 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
67 sb->len--;
68 while (sb->len > 0 && isspace(*b)) {
69 b++;
70 sb->len--;
71 }
72 memmove(sb->buf, b, sb->len);
73 sb->buf[sb->len] = '\0';
74}
75void strbuf_rtrim(struct strbuf *sb)
76{
77 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
78 sb->len--;
79 sb->buf[sb->len] = '\0';
80}
81
82void strbuf_ltrim(struct strbuf *sb)
83{
84 char *b = sb->buf;
85 while (sb->len > 0 && isspace(*b)) {
86 b++;
87 sb->len--;
88 }
89 memmove(sb->buf, b, sb->len);
90 sb->buf[sb->len] = '\0';
91}
92
93void strbuf_tolower(struct strbuf *sb)
94{
95 unsigned int i;
96
97 for (i = 0; i < sb->len; i++)
98 sb->buf[i] = tolower(sb->buf[i]);
99}
100
101struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
102{
103 int alloc = 2, pos = 0;
104 char *n, *p;
105 struct strbuf **ret;
106 struct strbuf *t;
107
108 ret = calloc(alloc, sizeof(struct strbuf *));
109 p = n = sb->buf;
110 while (n < sb->buf + sb->len) {
111 int len;
112 n = memchr(n, delim, sb->len - (n - sb->buf));
113 if (pos + 1 >= alloc) {
114 alloc = alloc * 2;
115 ret = realloc(ret, sizeof(struct strbuf *) * alloc);
116 }
117 if (!n)
118 n = sb->buf + sb->len - 1;
119 len = n - p + 1;
120 t = malloc(sizeof(struct strbuf));
121 strbuf_init(t, len);
122 strbuf_add(t, p, len);
123 ret[pos] = t;
124 ret[++pos] = NULL;
125 p = ++n;
126 }
127 return ret;
128}
129
130void strbuf_list_free(struct strbuf **sbs)
131{
132 struct strbuf **s = sbs;
133
134 while (*s) {
135 strbuf_release(*s);
136 free(*s++);
137 }
138 free(sbs);
139}
140
141int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
142{
143 int len = a->len < b->len ? a->len: b->len;
144 int cmp = memcmp(a->buf, b->buf, len);
145 if (cmp)
146 return cmp;
147 return a->len < b->len ? -1: a->len != b->len;
148}
149
150void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
151 const void *data, size_t dlen) 54 const void *data, size_t dlen)
152{ 55{
153 if (pos + len < pos) 56 if (pos + len < pos)
@@ -166,11 +69,6 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
166 strbuf_setlen(sb, sb->len + dlen - len); 69 strbuf_setlen(sb, sb->len + dlen - len);
167} 70}
168 71
169void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
170{
171 strbuf_splice(sb, pos, 0, data, len);
172}
173
174void strbuf_remove(struct strbuf *sb, size_t pos, size_t len) 72void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
175{ 73{
176 strbuf_splice(sb, pos, len, NULL, 0); 74 strbuf_splice(sb, pos, len, NULL, 0);
@@ -183,13 +81,6 @@ void strbuf_add(struct strbuf *sb, const void *data, size_t len)
183 strbuf_setlen(sb, sb->len + len); 81 strbuf_setlen(sb, sb->len + len);
184} 82}
185 83
186void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
187{
188 strbuf_grow(sb, len);
189 memcpy(sb->buf + sb->len, sb->buf + pos, len);
190 strbuf_setlen(sb, sb->len + len);
191}
192
193void strbuf_addf(struct strbuf *sb, const char *fmt, ...) 84void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
194{ 85{
195 int len; 86 int len;
@@ -214,57 +105,6 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
214 strbuf_setlen(sb, sb->len + len); 105 strbuf_setlen(sb, sb->len + len);
215} 106}
216 107
217void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
218 void *context)
219{
220 for (;;) {
221 const char *percent;
222 size_t consumed;
223
224 percent = strchrnul(format, '%');
225 strbuf_add(sb, format, percent - format);
226 if (!*percent)
227 break;
228 format = percent + 1;
229
230 consumed = fn(sb, format, context);
231 if (consumed)
232 format += consumed;
233 else
234 strbuf_addch(sb, '%');
235 }
236}
237
238size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
239 void *context)
240{
241 struct strbuf_expand_dict_entry *e = context;
242 size_t len;
243
244 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
245 if (!strncmp(placeholder, e->placeholder, len)) {
246 if (e->value)
247 strbuf_addstr(sb, e->value);
248 return len;
249 }
250 }
251 return 0;
252}
253
254size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
255{
256 size_t res;
257 size_t oldalloc = sb->alloc;
258
259 strbuf_grow(sb, size);
260 res = fread(sb->buf + sb->len, 1, size, f);
261 if (res > 0)
262 strbuf_setlen(sb, sb->len + res);
263 else if (oldalloc == 0)
264 strbuf_release(sb);
265 return res;
266}
267
268ssize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint) 108ssize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint)
269{ 109{
270 size_t oldlen = sb->len; 110 size_t oldlen = sb->len;
@@ -291,70 +131,3 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, ssize_t hint)
291 sb->buf[sb->len] = '\0'; 131 sb->buf[sb->len] = '\0';
292 return sb->len - oldlen; 132 return sb->len - oldlen;
293} 133}
294
295#define STRBUF_MAXLINK (2*PATH_MAX)
296
297int strbuf_readlink(struct strbuf *sb, const char *path, ssize_t hint)
298{
299 size_t oldalloc = sb->alloc;
300
301 if (hint < 32)
302 hint = 32;
303
304 while (hint < STRBUF_MAXLINK) {
305 ssize_t len;
306
307 strbuf_grow(sb, hint);
308 len = readlink(path, sb->buf, hint);
309 if (len < 0) {
310 if (errno != ERANGE)
311 break;
312 } else if (len < hint) {
313 strbuf_setlen(sb, len);
314 return 0;
315 }
316
317 /* .. the buffer was too small - try again */
318 hint *= 2;
319 }
320 if (oldalloc == 0)
321 strbuf_release(sb);
322 return -1;
323}
324
325int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
326{
327 int ch;
328
329 strbuf_grow(sb, 0);
330 if (feof(fp))
331 return EOF;
332
333 strbuf_reset(sb);
334 while ((ch = fgetc(fp)) != EOF) {
335 if (ch == term)
336 break;
337 strbuf_grow(sb, 1);
338 sb->buf[sb->len++] = ch;
339 }
340 if (ch == EOF && sb->len == 0)
341 return EOF;
342
343 sb->buf[sb->len] = '\0';
344 return 0;
345}
346
347int strbuf_read_file(struct strbuf *sb, const char *path, ssize_t hint)
348{
349 int fd, len;
350
351 fd = open(path, O_RDONLY);
352 if (fd < 0)
353 return -1;
354 len = strbuf_read(sb, fd, hint);
355 close(fd);
356 if (len < 0)
357 return -1;
358
359 return len;
360}
diff --git a/tools/perf/util/strbuf.h b/tools/perf/util/strbuf.h
index a3d121d6c83e..436ac319f6c7 100644
--- a/tools/perf/util/strbuf.h
+++ b/tools/perf/util/strbuf.h
@@ -53,12 +53,6 @@ struct strbuf {
53extern void strbuf_init(struct strbuf *buf, ssize_t hint); 53extern void strbuf_init(struct strbuf *buf, ssize_t hint);
54extern void strbuf_release(struct strbuf *); 54extern void strbuf_release(struct strbuf *);
55extern char *strbuf_detach(struct strbuf *, size_t *); 55extern char *strbuf_detach(struct strbuf *, size_t *);
56extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
57static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) {
58 struct strbuf tmp = *a;
59 *a = *b;
60 *b = tmp;
61}
62 56
63/*----- strbuf size related -----*/ 57/*----- strbuf size related -----*/
64static inline ssize_t strbuf_avail(const struct strbuf *sb) { 58static inline ssize_t strbuf_avail(const struct strbuf *sb) {
@@ -74,17 +68,6 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
74 sb->len = len; 68 sb->len = len;
75 sb->buf[len] = '\0'; 69 sb->buf[len] = '\0';
76} 70}
77#define strbuf_reset(sb) strbuf_setlen(sb, 0)
78
79/*----- content related -----*/
80extern void strbuf_trim(struct strbuf *);
81extern void strbuf_rtrim(struct strbuf *);
82extern void strbuf_ltrim(struct strbuf *);
83extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
84extern void strbuf_tolower(struct strbuf *);
85
86extern struct strbuf **strbuf_split(const struct strbuf *, int delim);
87extern void strbuf_list_free(struct strbuf **);
88 71
89/*----- add data in your buffer -----*/ 72/*----- add data in your buffer -----*/
90static inline void strbuf_addch(struct strbuf *sb, int c) { 73static inline void strbuf_addch(struct strbuf *sb, int c) {
@@ -93,45 +76,17 @@ static inline void strbuf_addch(struct strbuf *sb, int c) {
93 sb->buf[sb->len] = '\0'; 76 sb->buf[sb->len] = '\0';
94} 77}
95 78
96extern void strbuf_insert(struct strbuf *, size_t pos, const void *, size_t);
97extern void strbuf_remove(struct strbuf *, size_t pos, size_t len); 79extern void strbuf_remove(struct strbuf *, size_t pos, size_t len);
98 80
99/* splice pos..pos+len with given data */
100extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,
101 const void *, size_t);
102
103extern void strbuf_add(struct strbuf *, const void *, size_t); 81extern void strbuf_add(struct strbuf *, const void *, size_t);
104static inline void strbuf_addstr(struct strbuf *sb, const char *s) { 82static inline void strbuf_addstr(struct strbuf *sb, const char *s) {
105 strbuf_add(sb, s, strlen(s)); 83 strbuf_add(sb, s, strlen(s));
106} 84}
107static inline void strbuf_addbuf(struct strbuf *sb, const struct strbuf *sb2) {
108 strbuf_add(sb, sb2->buf, sb2->len);
109}
110extern void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len);
111
112typedef size_t (*expand_fn_t) (struct strbuf *sb, const char *placeholder, void *context);
113extern void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn, void *context);
114struct strbuf_expand_dict_entry {
115 const char *placeholder;
116 const char *value;
117};
118extern size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder, void *context);
119 85
120__attribute__((format(printf,2,3))) 86__attribute__((format(printf,2,3)))
121extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...); 87extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
122 88
123extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
124/* XXX: if read fails, any partial read is undone */ 89/* XXX: if read fails, any partial read is undone */
125extern ssize_t strbuf_read(struct strbuf *, int fd, ssize_t hint); 90extern ssize_t strbuf_read(struct strbuf *, int fd, ssize_t hint);
126extern int strbuf_read_file(struct strbuf *sb, const char *path, ssize_t hint);
127extern int strbuf_readlink(struct strbuf *sb, const char *path, ssize_t hint);
128
129extern int strbuf_getline(struct strbuf *, FILE *, int);
130
131extern void stripspace(struct strbuf *buf, int skip_comments);
132extern int launch_editor(const char *path, struct strbuf *buffer, const char *const *env);
133
134extern int strbuf_branchname(struct strbuf *sb, const char *name);
135extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
136 91
137#endif /* __PERF_STRBUF_H */ 92#endif /* __PERF_STRBUF_H */
diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
index a06131f6259a..7fd6b151feb5 100644
--- a/tools/perf/util/symbol.c
+++ b/tools/perf/util/symbol.c
@@ -11,6 +11,7 @@
11#include <sys/param.h> 11#include <sys/param.h>
12#include <fcntl.h> 12#include <fcntl.h>
13#include <unistd.h> 13#include <unistd.h>
14#include "build-id.h"
14#include "symbol.h" 15#include "symbol.h"
15#include "strlist.h" 16#include "strlist.h"
16 17
@@ -1131,6 +1132,10 @@ bool __dsos__read_build_ids(struct list_head *head, bool with_hits)
1131 list_for_each_entry(pos, head, node) { 1132 list_for_each_entry(pos, head, node) {
1132 if (with_hits && !pos->hit) 1133 if (with_hits && !pos->hit)
1133 continue; 1134 continue;
1135 if (pos->has_build_id) {
1136 have_build_id = true;
1137 continue;
1138 }
1134 if (filename__read_build_id(pos->long_name, pos->build_id, 1139 if (filename__read_build_id(pos->long_name, pos->build_id,
1135 sizeof(pos->build_id)) > 0) { 1140 sizeof(pos->build_id)) > 0) {
1136 have_build_id = true; 1141 have_build_id = true;
@@ -1289,7 +1294,6 @@ int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1289 int size = PATH_MAX; 1294 int size = PATH_MAX;
1290 char *name; 1295 char *name;
1291 u8 build_id[BUILD_ID_SIZE]; 1296 u8 build_id[BUILD_ID_SIZE];
1292 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1293 int ret = -1; 1297 int ret = -1;
1294 int fd; 1298 int fd;
1295 struct machine *machine; 1299 struct machine *machine;
@@ -1321,15 +1325,8 @@ int dso__load(struct dso *self, struct map *map, symbol_filter_t filter)
1321 } 1325 }
1322 1326
1323 self->origin = DSO__ORIG_BUILD_ID_CACHE; 1327 self->origin = DSO__ORIG_BUILD_ID_CACHE;
1324 1328 if (dso__build_id_filename(self, name, size) != NULL)
1325 if (self->has_build_id) {
1326 build_id__sprintf(self->build_id, sizeof(self->build_id),
1327 build_id_hex);
1328 snprintf(name, size, "%s/%s/.build-id/%.2s/%s",
1329 getenv("HOME"), DEBUG_CACHE_DIR,
1330 build_id_hex, build_id_hex + 2);
1331 goto open_file; 1329 goto open_file;
1332 }
1333more: 1330more:
1334 do { 1331 do {
1335 self->origin++; 1332 self->origin++;
@@ -1345,6 +1342,7 @@ more:
1345 case DSO__ORIG_BUILDID: 1342 case DSO__ORIG_BUILDID:
1346 if (filename__read_build_id(self->long_name, build_id, 1343 if (filename__read_build_id(self->long_name, build_id,
1347 sizeof(build_id))) { 1344 sizeof(build_id))) {
1345 char build_id_hex[BUILD_ID_SIZE * 2 + 1];
1348 build_id__sprintf(build_id, sizeof(build_id), 1346 build_id__sprintf(build_id, sizeof(build_id),
1349 build_id_hex); 1347 build_id_hex);
1350 snprintf(name, size, 1348 snprintf(name, size,
@@ -1697,9 +1695,20 @@ int dso__load_vmlinux_path(struct dso *self, struct map *map,
1697 symbol_filter_t filter) 1695 symbol_filter_t filter)
1698{ 1696{
1699 int i, err = 0; 1697 int i, err = 0;
1698 char *filename;
1700 1699
1701 pr_debug("Looking at the vmlinux_path (%d entries long)\n", 1700 pr_debug("Looking at the vmlinux_path (%d entries long)\n",
1702 vmlinux_path__nr_entries); 1701 vmlinux_path__nr_entries + 1);
1702
1703 filename = dso__build_id_filename(self, NULL, 0);
1704 if (filename != NULL) {
1705 err = dso__load_vmlinux(self, map, filename, filter);
1706 if (err > 0) {
1707 dso__set_long_name(self, filename);
1708 goto out;
1709 }
1710 free(filename);
1711 }
1703 1712
1704 for (i = 0; i < vmlinux_path__nr_entries; ++i) { 1713 for (i = 0; i < vmlinux_path__nr_entries; ++i) {
1705 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter); 1714 err = dso__load_vmlinux(self, map, vmlinux_path[i], filter);
@@ -1708,7 +1717,7 @@ int dso__load_vmlinux_path(struct dso *self, struct map *map,
1708 break; 1717 break;
1709 } 1718 }
1710 } 1719 }
1711 1720out:
1712 return err; 1721 return err;
1713} 1722}
1714 1723
@@ -1933,6 +1942,12 @@ static size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp,
1933 return ret; 1942 return ret;
1934} 1943}
1935 1944
1945size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits)
1946{
1947 return __dsos__fprintf_buildid(&self->kernel_dsos, fp, with_hits) +
1948 __dsos__fprintf_buildid(&self->user_dsos, fp, with_hits);
1949}
1950
1936size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits) 1951size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits)
1937{ 1952{
1938 struct rb_node *nd; 1953 struct rb_node *nd;
@@ -1940,8 +1955,7 @@ size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_
1940 1955
1941 for (nd = rb_first(self); nd; nd = rb_next(nd)) { 1956 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
1942 struct machine *pos = rb_entry(nd, struct machine, rb_node); 1957 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1943 ret += __dsos__fprintf_buildid(&pos->kernel_dsos, fp, with_hits); 1958 ret += machine__fprintf_dsos_buildid(pos, fp, with_hits);
1944 ret += __dsos__fprintf_buildid(&pos->user_dsos, fp, with_hits);
1945 } 1959 }
1946 return ret; 1960 return ret;
1947} 1961}
@@ -2099,13 +2113,21 @@ out_fail:
2099 return -1; 2113 return -1;
2100} 2114}
2101 2115
2102size_t vmlinux_path__fprintf(FILE *fp) 2116size_t machine__fprintf_vmlinux_path(struct machine *self, FILE *fp)
2103{ 2117{
2104 int i; 2118 int i;
2105 size_t printed = 0; 2119 size_t printed = 0;
2120 struct dso *kdso = self->vmlinux_maps[MAP__FUNCTION]->dso;
2121
2122 if (kdso->has_build_id) {
2123 char filename[PATH_MAX];
2124 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
2125 printed += fprintf(fp, "[0] %s\n", filename);
2126 }
2106 2127
2107 for (i = 0; i < vmlinux_path__nr_entries; ++i) 2128 for (i = 0; i < vmlinux_path__nr_entries; ++i)
2108 printed += fprintf(fp, "[%d] %s\n", i, vmlinux_path[i]); 2129 printed += fprintf(fp, "[%d] %s\n",
2130 i + kdso->has_build_id, vmlinux_path[i]);
2109 2131
2110 return printed; 2132 return printed;
2111} 2133}
diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
index 032469e41876..5e02d2c17154 100644
--- a/tools/perf/util/symbol.h
+++ b/tools/perf/util/symbol.h
@@ -170,6 +170,7 @@ int machine__load_vmlinux_path(struct machine *self, enum map_type type,
170 170
171size_t __dsos__fprintf(struct list_head *head, FILE *fp); 171size_t __dsos__fprintf(struct list_head *head, FILE *fp);
172 172
173size_t machine__fprintf_dsos_buildid(struct machine *self, FILE *fp, bool with_hits);
173size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp); 174size_t machines__fprintf_dsos(struct rb_root *self, FILE *fp);
174size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits); 175size_t machines__fprintf_dsos_buildid(struct rb_root *self, FILE *fp, bool with_hits);
175 176
@@ -215,6 +216,6 @@ int machines__create_guest_kernel_maps(struct rb_root *self);
215int symbol__init(void); 216int symbol__init(void);
216bool symbol_type__is_a(char symbol_type, enum map_type map_type); 217bool symbol_type__is_a(char symbol_type, enum map_type map_type);
217 218
218size_t vmlinux_path__fprintf(FILE *fp); 219size_t machine__fprintf_vmlinux_path(struct machine *self, FILE *fp);
219 220
220#endif /* __PERF_SYMBOL */ 221#endif /* __PERF_SYMBOL */
diff --git a/tools/perf/util/trace-event-read.c b/tools/perf/util/trace-event-read.c
index cb54cd002f49..f55cc3a765a1 100644
--- a/tools/perf/util/trace-event-read.c
+++ b/tools/perf/util/trace-event-read.c
@@ -53,12 +53,6 @@ static unsigned long page_size;
53static ssize_t calc_data_size; 53static ssize_t calc_data_size;
54static bool repipe; 54static bool repipe;
55 55
56/* If it fails, the next read will report it */
57static void skip(int size)
58{
59 lseek(input_fd, size, SEEK_CUR);
60}
61
62static int do_read(int fd, void *buf, int size) 56static int do_read(int fd, void *buf, int size)
63{ 57{
64 int rsize = size; 58 int rsize = size;
@@ -98,6 +92,19 @@ static int read_or_die(void *data, int size)
98 return r; 92 return r;
99} 93}
100 94
95/* If it fails, the next read will report it */
96static void skip(int size)
97{
98 char buf[BUFSIZ];
99 int r;
100
101 while (size) {
102 r = size > BUFSIZ ? BUFSIZ : size;
103 read_or_die(buf, r);
104 size -= r;
105 };
106}
107
101static unsigned int read4(void) 108static unsigned int read4(void)
102{ 109{
103 unsigned int data; 110 unsigned int data;
diff --git a/tools/perf/util/trace-event.h b/tools/perf/util/trace-event.h
index 406d452956db..b3e86b1e4444 100644
--- a/tools/perf/util/trace-event.h
+++ b/tools/perf/util/trace-event.h
@@ -233,7 +233,12 @@ static inline unsigned long long __data2host8(unsigned long long data)
233 233
234#define data2host2(ptr) __data2host2(*(unsigned short *)ptr) 234#define data2host2(ptr) __data2host2(*(unsigned short *)ptr)
235#define data2host4(ptr) __data2host4(*(unsigned int *)ptr) 235#define data2host4(ptr) __data2host4(*(unsigned int *)ptr)
236#define data2host8(ptr) __data2host8(*(unsigned long long *)ptr) 236#define data2host8(ptr) ({ \
237 unsigned long long __val; \
238 \
239 memcpy(&__val, (ptr), sizeof(unsigned long long)); \
240 __data2host8(__val); \
241})
237 242
238extern int header_page_ts_offset; 243extern int header_page_ts_offset;
239extern int header_page_ts_size; 244extern int header_page_ts_size;
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index 0795bf304b19..4e8b6b0c551c 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -81,7 +81,7 @@
81#include <inttypes.h> 81#include <inttypes.h>
82#include "../../../include/linux/magic.h" 82#include "../../../include/linux/magic.h"
83#include "types.h" 83#include "types.h"
84 84#include <sys/ttydefaults.h>
85 85
86#ifndef NO_ICONV 86#ifndef NO_ICONV
87#include <iconv.h> 87#include <iconv.h>
@@ -152,7 +152,6 @@ extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)))
152extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN); 152extern void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN);
153 153
154extern int prefixcmp(const char *str, const char *prefix); 154extern int prefixcmp(const char *str, const char *prefix);
155extern time_t tm_to_time_t(const struct tm *tm);
156 155
157static inline const char *skip_prefix(const char *str, const char *prefix) 156static inline const char *skip_prefix(const char *str, const char *prefix)
158{ 157{
@@ -160,119 +159,6 @@ static inline const char *skip_prefix(const char *str, const char *prefix)
160 return strncmp(str, prefix, len) ? NULL : str + len; 159 return strncmp(str, prefix, len) ? NULL : str + len;
161} 160}
162 161
163#if defined(NO_MMAP) || defined(USE_WIN32_MMAP)
164
165#ifndef PROT_READ
166#define PROT_READ 1
167#define PROT_WRITE 2
168#define MAP_PRIVATE 1
169#define MAP_FAILED ((void*)-1)
170#endif
171
172#define mmap git_mmap
173#define munmap git_munmap
174extern void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
175extern int git_munmap(void *start, size_t length);
176
177#else /* NO_MMAP || USE_WIN32_MMAP */
178
179#include <sys/mman.h>
180
181#endif /* NO_MMAP || USE_WIN32_MMAP */
182
183#ifdef NO_MMAP
184
185/* This value must be multiple of (pagesize * 2) */
186#define DEFAULT_PACKED_GIT_WINDOW_SIZE (1 * 1024 * 1024)
187
188#else /* NO_MMAP */
189
190/* This value must be multiple of (pagesize * 2) */
191#define DEFAULT_PACKED_GIT_WINDOW_SIZE \
192 (sizeof(void*) >= 8 \
193 ? 1 * 1024 * 1024 * 1024 \
194 : 32 * 1024 * 1024)
195
196#endif /* NO_MMAP */
197
198#ifdef NO_ST_BLOCKS_IN_STRUCT_STAT
199#define on_disk_bytes(st) ((st).st_size)
200#else
201#define on_disk_bytes(st) ((st).st_blocks * 512)
202#endif
203
204#define DEFAULT_PACKED_GIT_LIMIT \
205 ((1024L * 1024L) * (sizeof(void*) >= 8 ? 8192 : 256))
206
207#ifdef NO_PREAD
208#define pread git_pread
209extern ssize_t git_pread(int fd, void *buf, size_t count, off_t offset);
210#endif
211/*
212 * Forward decl that will remind us if its twin in cache.h changes.
213 * This function is used in compat/pread.c. But we can't include
214 * cache.h there.
215 */
216extern ssize_t read_in_full(int fd, void *buf, size_t count);
217
218#ifdef NO_SETENV
219#define setenv gitsetenv
220extern int gitsetenv(const char *, const char *, int);
221#endif
222
223#ifdef NO_MKDTEMP
224#define mkdtemp gitmkdtemp
225extern char *gitmkdtemp(char *);
226#endif
227
228#ifdef NO_UNSETENV
229#define unsetenv gitunsetenv
230extern void gitunsetenv(const char *);
231#endif
232
233#ifdef NO_STRCASESTR
234#define strcasestr gitstrcasestr
235extern char *gitstrcasestr(const char *haystack, const char *needle);
236#endif
237
238#ifdef NO_STRLCPY
239#define strlcpy gitstrlcpy
240extern size_t gitstrlcpy(char *, const char *, size_t);
241#endif
242
243#ifdef NO_STRTOUMAX
244#define strtoumax gitstrtoumax
245extern uintmax_t gitstrtoumax(const char *, char **, int);
246#endif
247
248#ifdef NO_HSTRERROR
249#define hstrerror githstrerror
250extern const char *githstrerror(int herror);
251#endif
252
253#ifdef NO_MEMMEM
254#define memmem gitmemmem
255void *gitmemmem(const void *haystack, size_t haystacklen,
256 const void *needle, size_t needlelen);
257#endif
258
259#ifdef FREAD_READS_DIRECTORIES
260#ifdef fopen
261#undef fopen
262#endif
263#define fopen(a,b) git_fopen(a,b)
264extern FILE *git_fopen(const char*, const char*);
265#endif
266
267#ifdef SNPRINTF_RETURNS_BOGUS
268#define snprintf git_snprintf
269extern int git_snprintf(char *str, size_t maxsize,
270 const char *format, ...);
271#define vsnprintf git_vsnprintf
272extern int git_vsnprintf(char *str, size_t maxsize,
273 const char *format, va_list ap);
274#endif
275
276#ifdef __GLIBC_PREREQ 162#ifdef __GLIBC_PREREQ
277#if __GLIBC_PREREQ(2, 1) 163#if __GLIBC_PREREQ(2, 1)
278#define HAVE_STRCHRNUL 164#define HAVE_STRCHRNUL
@@ -293,28 +179,14 @@ static inline char *gitstrchrnul(const char *s, int c)
293 * Wrappers: 179 * Wrappers:
294 */ 180 */
295extern char *xstrdup(const char *str); 181extern char *xstrdup(const char *str);
296extern void *xmalloc(size_t size) __attribute__((weak));
297extern void *xmemdupz(const void *data, size_t len);
298extern char *xstrndup(const char *str, size_t len);
299extern void *xrealloc(void *ptr, size_t size) __attribute__((weak)); 182extern void *xrealloc(void *ptr, size_t size) __attribute__((weak));
300 183
301static inline void *xzalloc(size_t size)
302{
303 void *buf = xmalloc(size);
304
305 return memset(buf, 0, size);
306}
307 184
308static inline void *zalloc(size_t size) 185static inline void *zalloc(size_t size)
309{ 186{
310 return calloc(1, size); 187 return calloc(1, size);
311} 188}
312 189
313static inline size_t xsize_t(off_t len)
314{
315 return (size_t)len;
316}
317
318static inline int has_extension(const char *filename, const char *ext) 190static inline int has_extension(const char *filename, const char *ext)
319{ 191{
320 size_t len = strlen(filename); 192 size_t len = strlen(filename);
@@ -351,8 +223,6 @@ extern unsigned char sane_ctype[256];
351#define isalpha(x) sane_istest(x,GIT_ALPHA) 223#define isalpha(x) sane_istest(x,GIT_ALPHA)
352#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT) 224#define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
353#define isprint(x) sane_istest(x,GIT_PRINT) 225#define isprint(x) sane_istest(x,GIT_PRINT)
354#define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
355#define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
356#define tolower(x) sane_case((unsigned char)(x), 0x20) 226#define tolower(x) sane_case((unsigned char)(x), 0x20)
357#define toupper(x) sane_case((unsigned char)(x), 0) 227#define toupper(x) sane_case((unsigned char)(x), 0)
358 228
@@ -363,38 +233,6 @@ static inline int sane_case(int x, int high)
363 return x; 233 return x;
364} 234}
365 235
366static inline int strtoul_ui(char const *s, int base, unsigned int *result)
367{
368 unsigned long ul;
369 char *p;
370
371 errno = 0;
372 ul = strtoul(s, &p, base);
373 if (errno || *p || p == s || (unsigned int) ul != ul)
374 return -1;
375 *result = ul;
376 return 0;
377}
378
379static inline int strtol_i(char const *s, int base, int *result)
380{
381 long ul;
382 char *p;
383
384 errno = 0;
385 ul = strtol(s, &p, base);
386 if (errno || *p || p == s || (int) ul != ul)
387 return -1;
388 *result = ul;
389 return 0;
390}
391
392#ifdef INTERNAL_QSORT
393void git_qsort(void *base, size_t nmemb, size_t size,
394 int(*compar)(const void *, const void *));
395#define qsort git_qsort
396#endif
397
398#ifndef DIR_HAS_BSD_GROUP_SEMANTICS 236#ifndef DIR_HAS_BSD_GROUP_SEMANTICS
399# define FORCE_DIR_SET_GID S_ISGID 237# define FORCE_DIR_SET_GID S_ISGID
400#else 238#else
@@ -425,6 +263,19 @@ bool strglobmatch(const char *str, const char *pat);
425bool strlazymatch(const char *str, const char *pat); 263bool strlazymatch(const char *str, const char *pat);
426unsigned long convert_unit(unsigned long value, char *unit); 264unsigned long convert_unit(unsigned long value, char *unit);
427 265
266#ifndef ESC
267#define ESC 27
268#endif
269
270static inline bool is_exit_key(int key)
271{
272 char up;
273 if (key == CTRL('c') || key == ESC)
274 return true;
275 up = toupper(key);
276 return up == 'Q';
277}
278
428#define _STR(x) #x 279#define _STR(x) #x
429#define STR(x) _STR(x) 280#define STR(x) _STR(x)
430 281
diff --git a/tools/perf/util/wrapper.c b/tools/perf/util/wrapper.c
index bf44ca85d23b..73e900edb5a2 100644
--- a/tools/perf/util/wrapper.c
+++ b/tools/perf/util/wrapper.c
@@ -23,46 +23,6 @@ char *xstrdup(const char *str)
23 return ret; 23 return ret;
24} 24}
25 25
26void *xmalloc(size_t size)
27{
28 void *ret = malloc(size);
29 if (!ret && !size)
30 ret = malloc(1);
31 if (!ret) {
32 release_pack_memory(size, -1);
33 ret = malloc(size);
34 if (!ret && !size)
35 ret = malloc(1);
36 if (!ret)
37 die("Out of memory, malloc failed");
38 }
39#ifdef XMALLOC_POISON
40 memset(ret, 0xA5, size);
41#endif
42 return ret;
43}
44
45/*
46 * xmemdupz() allocates (len + 1) bytes of memory, duplicates "len" bytes of
47 * "data" to the allocated memory, zero terminates the allocated memory,
48 * and returns a pointer to the allocated memory. If the allocation fails,
49 * the program dies.
50 */
51void *xmemdupz(const void *data, size_t len)
52{
53 char *p = xmalloc(len + 1);
54 memcpy(p, data, len);
55 p[len] = '\0';
56 return p;
57}
58
59char *xstrndup(const char *str, size_t len)
60{
61 char *p = memchr(str, '\0', len);
62
63 return xmemdupz(str, p ? (size_t)(p - str) : len);
64}
65
66void *xrealloc(void *ptr, size_t size) 26void *xrealloc(void *ptr, size_t size)
67{ 27{
68 void *ret = realloc(ptr, size); 28 void *ret = realloc(ptr, size);
@@ -78,73 +38,3 @@ void *xrealloc(void *ptr, size_t size)
78 } 38 }
79 return ret; 39 return ret;
80} 40}
81
82/*
83 * xread() is the same a read(), but it automatically restarts read()
84 * operations with a recoverable error (EAGAIN and EINTR). xread()
85 * DOES NOT GUARANTEE that "len" bytes is read even if the data is available.
86 */
87static ssize_t xread(int fd, void *buf, size_t len)
88{
89 ssize_t nr;
90 while (1) {
91 nr = read(fd, buf, len);
92 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
93 continue;
94 return nr;
95 }
96}
97
98/*
99 * xwrite() is the same a write(), but it automatically restarts write()
100 * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
101 * GUARANTEE that "len" bytes is written even if the operation is successful.
102 */
103static ssize_t xwrite(int fd, const void *buf, size_t len)
104{
105 ssize_t nr;
106 while (1) {
107 nr = write(fd, buf, len);
108 if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
109 continue;
110 return nr;
111 }
112}
113
114ssize_t read_in_full(int fd, void *buf, size_t count)
115{
116 char *p = buf;
117 ssize_t total = 0;
118
119 while (count > 0) {
120 ssize_t loaded = xread(fd, p, count);
121 if (loaded <= 0)
122 return total ? total : loaded;
123 count -= loaded;
124 p += loaded;
125 total += loaded;
126 }
127
128 return total;
129}
130
131ssize_t write_in_full(int fd, const void *buf, size_t count)
132{
133 const char *p = buf;
134 ssize_t total = 0;
135
136 while (count > 0) {
137 ssize_t written = xwrite(fd, p, count);
138 if (written < 0)
139 return -1;
140 if (!written) {
141 errno = ENOSPC;
142 return -1;
143 }
144 count -= written;
145 p += written;
146 total += written;
147 }
148
149 return total;
150}