1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <err.h>
18 #include <inttypes.h>
19 #include <stdio.h>
20
21 #include "AllocParser.h"
22
23 #include <iostream>
24
AllocGetData(const std::string & line,AllocEntry * entry)25 void AllocGetData(const std::string& line, AllocEntry* entry) {
26 int op_prefix_pos = 0;
27 char name[128];
28 // All lines have this format:
29 // TID: ALLOCATION_TYPE POINTER
30 // where
31 // TID is the thread id of the thread doing the operation.
32 // ALLOCATION_TYPE is one of malloc, calloc, memalign, realloc, free, thread_done
33 // POINTER is the hex value of the actual pointer
34 if (sscanf(line.c_str(), "%d: %127s %" SCNx64 " %n", &entry->tid, name, &entry->ptr,
35 &op_prefix_pos) != 3) {
36 errx(1, "File Error: Failed to process %s", line.c_str());
37 }
38 std::string type(name);
39 if (type == "thread_done") {
40 entry->type = THREAD_DONE;
41 } else {
42 int args_offset = 0;
43 const char* args_beg = &line[op_prefix_pos];
44 if (type == "malloc") {
45 // Format:
46 // TID: malloc POINTER SIZE_OF_ALLOCATION
47 if (sscanf(args_beg, "%zu%n", &entry->size, &args_offset) != 1) {
48 errx(1, "File Error: Failed to read malloc data %s", line.c_str());
49 }
50 entry->type = MALLOC;
51 } else if (type == "free") {
52 // Format:
53 // TID: free POINTER
54 entry->type = FREE;
55 } else if (type == "calloc") {
56 // Format:
57 // TID: calloc POINTER ITEM_COUNT ITEM_SIZE
58 if (sscanf(args_beg, "%" SCNd64 " %zu%n", &entry->u.n_elements, &entry->size,
59 &args_offset) != 2) {
60 errx(1, "File Error: Failed to read calloc data %s", line.c_str());
61 }
62 entry->type = CALLOC;
63 } else if (type == "realloc") {
64 // Format:
65 // TID: realloc POINTER OLD_POINTER NEW_SIZE
66 if (sscanf(args_beg, "%" SCNx64 " %zu%n", &entry->u.old_ptr, &entry->size,
67 &args_offset) != 2) {
68 errx(1, "File Error: Failed to read realloc data %s", line.c_str());
69 }
70 entry->type = REALLOC;
71 } else if (type == "memalign") {
72 // Format:
73 // TID: memalign POINTER ALIGNMENT SIZE
74 if (sscanf(args_beg, "%" SCNd64 " %zu%n", &entry->u.align, &entry->size,
75 &args_offset) != 2) {
76 errx(1, "File Error: Failed to read memalign data %s", line.c_str());
77 }
78 entry->type = MEMALIGN;
79 } else {
80 errx(1, "File Error: Unknown type %s", type.c_str());
81 }
82
83 const char* timestamps_beg = &args_beg[args_offset];
84
85 // Timestamps come after the alloc args if present, for example,
86 // TID: malloc POINTER SIZE_OF_ALLOCATION START_TIME END_TIME
87 int n_match = sscanf(timestamps_beg, "%" SCNd64 " %" SCNd64, &entry->st, &entry->et);
88 if (n_match != EOF && n_match != 2) {
89 errx(1, "File Error: Failed to read timestamps %s", line.c_str());
90 }
91 }
92 }
93