diff options
| author | Namhyung Kim <namhyung.kim@lge.com> | 2013-03-21 03:18:49 -0400 |
|---|---|---|
| committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2013-03-21 12:32:46 -0400 |
| commit | a4c983670e0f4285fe115cb2ad697c978c7950b6 (patch) | |
| tree | f3b4f5a206c991213b9de583e31e76ba85597bf4 /tools/perf | |
| parent | 3dce2ce3cc40ece2562a5a83e879b4bfb451476c (diff) | |
perf tools: Get rid of malloc_or_die() in trace-event-read.c
Check return value of malloc() and fail if error. Now read_string()
can return NULL also check its return value and bail out.
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Steven Rostedt <rostedt@goodmis.org>
Link: http://lkml.kernel.org/r/1363850332-25297-7-git-send-email-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf')
| -rw-r--r-- | tools/perf/util/trace-event-read.c | 100 |
1 files changed, 67 insertions, 33 deletions
diff --git a/tools/perf/util/trace-event-read.c b/tools/perf/util/trace-event-read.c index ba752d765ac3..22ded8000ef6 100644 --- a/tools/perf/util/trace-event-read.c +++ b/tools/perf/util/trace-event-read.c | |||
| @@ -46,16 +46,6 @@ static int long_size; | |||
| 46 | static ssize_t calc_data_size; | 46 | static ssize_t calc_data_size; |
| 47 | static bool repipe; | 47 | static bool repipe; |
| 48 | 48 | ||
| 49 | static void *malloc_or_die(int size) | ||
| 50 | { | ||
| 51 | void *ret; | ||
| 52 | |||
| 53 | ret = malloc(size); | ||
| 54 | if (!ret) | ||
| 55 | die("malloc"); | ||
| 56 | return ret; | ||
| 57 | } | ||
| 58 | |||
| 59 | static int do_read(int fd, void *buf, int size) | 49 | static int do_read(int fd, void *buf, int size) |
| 60 | { | 50 | { |
| 61 | int rsize = size; | 51 | int rsize = size; |
| @@ -156,48 +146,57 @@ static char *read_string(void) | |||
| 156 | if (calc_data_size) | 146 | if (calc_data_size) |
| 157 | calc_data_size += size; | 147 | calc_data_size += size; |
| 158 | 148 | ||
| 159 | str = malloc_or_die(size); | 149 | str = malloc(size); |
| 160 | memcpy(str, buf, size); | 150 | if (str) |
| 151 | memcpy(str, buf, size); | ||
| 161 | 152 | ||
| 162 | return str; | 153 | return str; |
| 163 | } | 154 | } |
| 164 | 155 | ||
| 165 | static void read_proc_kallsyms(struct pevent *pevent) | 156 | static int read_proc_kallsyms(struct pevent *pevent) |
| 166 | { | 157 | { |
| 167 | unsigned int size; | 158 | unsigned int size; |
| 168 | char *buf; | 159 | char *buf; |
| 169 | 160 | ||
| 170 | size = read4(pevent); | 161 | size = read4(pevent); |
| 171 | if (!size) | 162 | if (!size) |
| 172 | return; | 163 | return 0; |
| 164 | |||
| 165 | buf = malloc(size + 1); | ||
| 166 | if (buf == NULL) | ||
| 167 | return -1; | ||
| 173 | 168 | ||
| 174 | buf = malloc_or_die(size + 1); | ||
| 175 | read_or_die(buf, size); | 169 | read_or_die(buf, size); |
| 176 | buf[size] = '\0'; | 170 | buf[size] = '\0'; |
| 177 | 171 | ||
| 178 | parse_proc_kallsyms(pevent, buf, size); | 172 | parse_proc_kallsyms(pevent, buf, size); |
| 179 | 173 | ||
| 180 | free(buf); | 174 | free(buf); |
| 175 | return 0; | ||
| 181 | } | 176 | } |
| 182 | 177 | ||
| 183 | static void read_ftrace_printk(struct pevent *pevent) | 178 | static int read_ftrace_printk(struct pevent *pevent) |
| 184 | { | 179 | { |
| 185 | unsigned int size; | 180 | unsigned int size; |
| 186 | char *buf; | 181 | char *buf; |
| 187 | 182 | ||
| 188 | size = read4(pevent); | 183 | size = read4(pevent); |
| 189 | if (!size) | 184 | if (!size) |
| 190 | return; | 185 | return 0; |
| 186 | |||
| 187 | buf = malloc(size); | ||
| 188 | if (buf == NULL) | ||
| 189 | return -1; | ||
| 191 | 190 | ||
| 192 | buf = malloc_or_die(size); | ||
| 193 | read_or_die(buf, size); | 191 | read_or_die(buf, size); |
| 194 | 192 | ||
| 195 | parse_ftrace_printk(pevent, buf, size); | 193 | parse_ftrace_printk(pevent, buf, size); |
| 196 | 194 | ||
| 197 | free(buf); | 195 | free(buf); |
| 196 | return 0; | ||
| 198 | } | 197 | } |
| 199 | 198 | ||
| 200 | static void read_header_files(struct pevent *pevent) | 199 | static int read_header_files(struct pevent *pevent) |
| 201 | { | 200 | { |
| 202 | unsigned long long size; | 201 | unsigned long long size; |
| 203 | char *header_event; | 202 | char *header_event; |
| @@ -222,65 +221,87 @@ static void read_header_files(struct pevent *pevent) | |||
| 222 | die("did not read header event"); | 221 | die("did not read header event"); |
| 223 | 222 | ||
| 224 | size = read8(pevent); | 223 | size = read8(pevent); |
| 225 | header_event = malloc_or_die(size); | 224 | header_event = malloc(size); |
| 225 | if (header_event == NULL) | ||
| 226 | return -1; | ||
| 227 | |||
| 226 | read_or_die(header_event, size); | 228 | read_or_die(header_event, size); |
| 227 | free(header_event); | 229 | free(header_event); |
| 230 | return 0; | ||
| 228 | } | 231 | } |
| 229 | 232 | ||
| 230 | static void read_ftrace_file(struct pevent *pevent, unsigned long long size) | 233 | static int read_ftrace_file(struct pevent *pevent, unsigned long long size) |
| 231 | { | 234 | { |
| 232 | char *buf; | 235 | char *buf; |
| 233 | 236 | ||
| 234 | buf = malloc_or_die(size); | 237 | buf = malloc(size); |
| 238 | if (buf == NULL) | ||
| 239 | return -1; | ||
| 240 | |||
| 235 | read_or_die(buf, size); | 241 | read_or_die(buf, size); |
| 236 | parse_ftrace_file(pevent, buf, size); | 242 | parse_ftrace_file(pevent, buf, size); |
| 237 | free(buf); | 243 | free(buf); |
| 244 | return 0; | ||
| 238 | } | 245 | } |
| 239 | 246 | ||
| 240 | static void read_event_file(struct pevent *pevent, char *sys, | 247 | static int read_event_file(struct pevent *pevent, char *sys, |
| 241 | unsigned long long size) | 248 | unsigned long long size) |
| 242 | { | 249 | { |
| 243 | char *buf; | 250 | char *buf; |
| 244 | 251 | ||
| 245 | buf = malloc_or_die(size); | 252 | buf = malloc(size); |
| 253 | if (buf == NULL) | ||
| 254 | return -1; | ||
| 255 | |||
| 246 | read_or_die(buf, size); | 256 | read_or_die(buf, size); |
| 247 | parse_event_file(pevent, buf, size, sys); | 257 | parse_event_file(pevent, buf, size, sys); |
| 248 | free(buf); | 258 | free(buf); |
| 259 | return 0; | ||
| 249 | } | 260 | } |
| 250 | 261 | ||
| 251 | static void read_ftrace_files(struct pevent *pevent) | 262 | static int read_ftrace_files(struct pevent *pevent) |
| 252 | { | 263 | { |
| 253 | unsigned long long size; | 264 | unsigned long long size; |
| 254 | int count; | 265 | int count; |
| 255 | int i; | 266 | int i; |
| 267 | int ret; | ||
| 256 | 268 | ||
| 257 | count = read4(pevent); | 269 | count = read4(pevent); |
| 258 | 270 | ||
| 259 | for (i = 0; i < count; i++) { | 271 | for (i = 0; i < count; i++) { |
| 260 | size = read8(pevent); | 272 | size = read8(pevent); |
| 261 | read_ftrace_file(pevent, size); | 273 | ret = read_ftrace_file(pevent, size); |
| 274 | if (ret) | ||
| 275 | return ret; | ||
| 262 | } | 276 | } |
| 277 | return 0; | ||
| 263 | } | 278 | } |
| 264 | 279 | ||
| 265 | static void read_event_files(struct pevent *pevent) | 280 | static int read_event_files(struct pevent *pevent) |
| 266 | { | 281 | { |
| 267 | unsigned long long size; | 282 | unsigned long long size; |
| 268 | char *sys; | 283 | char *sys; |
| 269 | int systems; | 284 | int systems; |
| 270 | int count; | 285 | int count; |
| 271 | int i,x; | 286 | int i,x; |
| 287 | int ret; | ||
| 272 | 288 | ||
| 273 | systems = read4(pevent); | 289 | systems = read4(pevent); |
| 274 | 290 | ||
| 275 | for (i = 0; i < systems; i++) { | 291 | for (i = 0; i < systems; i++) { |
| 276 | sys = read_string(); | 292 | sys = read_string(); |
| 293 | if (sys == NULL) | ||
| 294 | return -1; | ||
| 277 | 295 | ||
| 278 | count = read4(pevent); | 296 | count = read4(pevent); |
| 279 | for (x=0; x < count; x++) { | 297 | for (x=0; x < count; x++) { |
| 280 | size = read8(pevent); | 298 | size = read8(pevent); |
| 281 | read_event_file(pevent, sys, size); | 299 | ret = read_event_file(pevent, sys, size); |
| 300 | if (ret) | ||
| 301 | return ret; | ||
| 282 | } | 302 | } |
| 283 | } | 303 | } |
| 304 | return 0; | ||
| 284 | } | 305 | } |
| 285 | 306 | ||
| 286 | ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe) | 307 | ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe) |
| @@ -293,6 +314,7 @@ ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe) | |||
| 293 | int show_printk = 0; | 314 | int show_printk = 0; |
| 294 | ssize_t size = -1; | 315 | ssize_t size = -1; |
| 295 | struct pevent *pevent; | 316 | struct pevent *pevent; |
| 317 | int err; | ||
| 296 | 318 | ||
| 297 | *ppevent = NULL; | 319 | *ppevent = NULL; |
| 298 | 320 | ||
| @@ -310,6 +332,8 @@ ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe) | |||
| 310 | die("not a trace file (missing 'tracing' tag)"); | 332 | die("not a trace file (missing 'tracing' tag)"); |
| 311 | 333 | ||
| 312 | version = read_string(); | 334 | version = read_string(); |
| 335 | if (version == NULL) | ||
| 336 | return -1; | ||
| 313 | if (show_version) | 337 | if (show_version) |
| 314 | printf("version = %s\n", version); | 338 | printf("version = %s\n", version); |
| 315 | free(version); | 339 | free(version); |
| @@ -329,11 +353,21 @@ ssize_t trace_report(int fd, struct pevent **ppevent, bool __repipe) | |||
| 329 | 353 | ||
| 330 | page_size = read4(pevent); | 354 | page_size = read4(pevent); |
| 331 | 355 | ||
| 332 | read_header_files(pevent); | 356 | err = read_header_files(pevent); |
| 333 | read_ftrace_files(pevent); | 357 | if (err) |
| 334 | read_event_files(pevent); | 358 | goto out; |
| 335 | read_proc_kallsyms(pevent); | 359 | err = read_ftrace_files(pevent); |
| 336 | read_ftrace_printk(pevent); | 360 | if (err) |
| 361 | goto out; | ||
| 362 | err = read_event_files(pevent); | ||
| 363 | if (err) | ||
| 364 | goto out; | ||
| 365 | err = read_proc_kallsyms(pevent); | ||
| 366 | if (err) | ||
| 367 | goto out; | ||
| 368 | err = read_ftrace_printk(pevent); | ||
| 369 | if (err) | ||
| 370 | goto out; | ||
| 337 | 371 | ||
| 338 | size = calc_data_size - 1; | 372 | size = calc_data_size - 1; |
| 339 | calc_data_size = 0; | 373 | calc_data_size = 0; |
