]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - tools/perf/util/util.c
4 #ifdef BACKTRACE_SUPPORT
11 * XXX We need to find a better place for these things...
13 unsigned int page_size
;
15 bool perf_host
= true;
16 bool perf_guest
= false;
18 void event_attr_init(struct perf_event_attr
*attr
)
21 attr
->exclude_host
= 1;
23 attr
->exclude_guest
= 1;
24 /* to capture ABI version */
25 attr
->size
= sizeof(*attr
);
28 int mkdir_p(char *path
, mode_t mode
)
37 if (stat(path
, &st
) == 0)
42 while ((d
= strchr(d
, '/'))) {
44 err
= stat(path
, &st
) && mkdir(path
, mode
);
51 return (stat(path
, &st
) && mkdir(path
, mode
)) ? -1 : 0;
54 static int slow_copyfile(const char *from
, const char *to
)
59 FILE *from_fp
= fopen(from
, "r"), *to_fp
;
64 to_fp
= fopen(to
, "w");
68 while (getline(&line
, &n
, from_fp
) > 0)
69 if (fputs(line
, to_fp
) == EOF
)
81 int copyfile(const char *from
, const char *to
)
91 if (st
.st_size
== 0) /* /proc? do it slowly... */
92 return slow_copyfile(from
, to
);
94 fromfd
= open(from
, O_RDONLY
);
98 tofd
= creat(to
, 0755);
102 addr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
, fromfd
, 0);
103 if (addr
== MAP_FAILED
)
106 if (write(tofd
, addr
, st
.st_size
) == st
.st_size
)
109 munmap(addr
, st
.st_size
);
120 unsigned long convert_unit(unsigned long value
, char *unit
)
142 int readn(int fd
, void *buf
, size_t n
)
144 void *buf_start
= buf
;
147 int ret
= read(fd
, buf
, n
);
156 return buf
- buf_start
;
159 size_t hex_width(u64 v
)
169 static int hex(char ch
)
171 if ((ch
>= '0') && (ch
<= '9'))
173 if ((ch
>= 'a') && (ch
<= 'f'))
174 return ch
- 'a' + 10;
175 if ((ch
>= 'A') && (ch
<= 'F'))
176 return ch
- 'A' + 10;
181 * While we find nice hex chars, build a long_val.
182 * Return number of chars processed.
184 int hex2u64(const char *ptr
, u64
*long_val
)
190 const int hex_val
= hex(*p
);
195 *long_val
= (*long_val
<< 4) | hex_val
;
202 /* Obtain a backtrace and print it to stdout. */
203 #ifdef BACKTRACE_SUPPORT
204 void dump_stack(void)
207 size_t size
= backtrace(array
, ARRAY_SIZE(array
));
208 char **strings
= backtrace_symbols(array
, size
);
211 printf("Obtained %zd stack frames.\n", size
);
213 for (i
= 0; i
< size
; i
++)
214 printf("%s\n", strings
[i
]);
219 void dump_stack(void) {}