blob: 3d37529d6c9e6ffbd7376aaa294e55588b9fda77 [file] [log] [blame]
Christopher Ferris7bd01782016-04-20 12:30:58 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughescbc80ba2018-02-13 14:26:29 -080029#pragma once
Christopher Ferris7bd01782016-04-20 12:30:58 -070030
Christopher Ferris7bd01782016-04-20 12:30:58 -070031#include <pthread.h>
Christopher Ferris4da25032018-03-07 13:38:48 -080032#include <stdint.h>
Christopher Ferris7bd01782016-04-20 12:30:58 -070033#include <unistd.h>
34
35#include <atomic>
36#include <mutex>
37#include <string>
38
Josh Gao4956c372019-12-19 16:35:51 -080039#include <platform/bionic/macros.h>
Christopher Ferris7bd01782016-04-20 12:30:58 -070040
41class RecordEntry {
42 public:
43 RecordEntry();
44 virtual ~RecordEntry() = default;
45
46 virtual std::string GetString() const = 0;
47
48 protected:
49 pid_t tid_;
50
51 private:
Elliott Hughes5e62b342018-10-25 11:00:00 -070052 BIONIC_DISALLOW_COPY_AND_ASSIGN(RecordEntry);
Christopher Ferris7bd01782016-04-20 12:30:58 -070053};
54
55class ThreadCompleteEntry : public RecordEntry {
56 public:
57 ThreadCompleteEntry() = default;
58 virtual ~ThreadCompleteEntry() = default;
59
60 std::string GetString() const override;
61
62 private:
Elliott Hughes5e62b342018-10-25 11:00:00 -070063 BIONIC_DISALLOW_COPY_AND_ASSIGN(ThreadCompleteEntry);
Christopher Ferris7bd01782016-04-20 12:30:58 -070064};
65
66class AllocEntry : public RecordEntry {
67 public:
Elliott Hughes5cec3772018-01-19 15:45:23 -080068 explicit AllocEntry(void* pointer);
Christopher Ferris7bd01782016-04-20 12:30:58 -070069 virtual ~AllocEntry() = default;
70
71 protected:
72 void* pointer_;
73
74 private:
Elliott Hughes5e62b342018-10-25 11:00:00 -070075 BIONIC_DISALLOW_COPY_AND_ASSIGN(AllocEntry);
Christopher Ferris7bd01782016-04-20 12:30:58 -070076};
77
78class MallocEntry : public AllocEntry {
79 public:
80 MallocEntry(void* pointer, size_t size);
81 virtual ~MallocEntry() = default;
82
83 std::string GetString() const override;
84
85 protected:
86 size_t size_;
87
88 private:
Elliott Hughes5e62b342018-10-25 11:00:00 -070089 BIONIC_DISALLOW_COPY_AND_ASSIGN(MallocEntry);
Christopher Ferris7bd01782016-04-20 12:30:58 -070090};
91
92class FreeEntry : public AllocEntry {
93 public:
Elliott Hughes5cec3772018-01-19 15:45:23 -080094 explicit FreeEntry(void* pointer);
Christopher Ferris7bd01782016-04-20 12:30:58 -070095 virtual ~FreeEntry() = default;
96
97 std::string GetString() const override;
98
99 private:
Elliott Hughes5e62b342018-10-25 11:00:00 -0700100 BIONIC_DISALLOW_COPY_AND_ASSIGN(FreeEntry);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700101};
102
103class CallocEntry : public MallocEntry {
104 public:
105 CallocEntry(void* pointer, size_t size, size_t nmemb);
106 virtual ~CallocEntry() = default;
107
108 std::string GetString() const override;
109
110 protected:
111 size_t nmemb_;
112
113 private:
Elliott Hughes5e62b342018-10-25 11:00:00 -0700114 BIONIC_DISALLOW_COPY_AND_ASSIGN(CallocEntry);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700115};
116
117class ReallocEntry : public MallocEntry {
118 public:
119 ReallocEntry(void* pointer, size_t size, void* old_pointer);
120 virtual ~ReallocEntry() = default;
121
122 std::string GetString() const override;
123
124 protected:
125 void* old_pointer_;
126
127 private:
Elliott Hughes5e62b342018-10-25 11:00:00 -0700128 BIONIC_DISALLOW_COPY_AND_ASSIGN(ReallocEntry);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700129};
130
Christopher Ferriscae21a92018-02-05 18:14:55 -0800131// aligned_alloc, posix_memalign, memalign, pvalloc, valloc all recorded with this class.
Christopher Ferris7bd01782016-04-20 12:30:58 -0700132class MemalignEntry : public MallocEntry {
133 public:
134 MemalignEntry(void* pointer, size_t size, size_t alignment);
135 virtual ~MemalignEntry() = default;
136
137 std::string GetString() const override;
138
139 protected:
140 size_t alignment_;
141
142 private:
Elliott Hughes5e62b342018-10-25 11:00:00 -0700143 BIONIC_DISALLOW_COPY_AND_ASSIGN(MemalignEntry);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700144};
145
Christopher Ferris2b2b25b2017-04-05 19:13:03 -0700146class Config;
Christopher Ferris7bd01782016-04-20 12:30:58 -0700147
148class RecordData {
149 public:
150 RecordData();
151 virtual ~RecordData();
152
153 bool Initialize(const Config& config);
154
155 void AddEntry(const RecordEntry* entry);
156 void AddEntryOnly(const RecordEntry* entry);
157
158 void SetToDump() { dump_ = true; }
159
160 pthread_key_t key() { return key_; }
161
162 private:
163 void Dump();
164
165 std::mutex dump_lock_;
166 pthread_key_t key_;
167 const RecordEntry** entries_ = nullptr;
168 size_t num_entries_ = 0;
169 std::atomic_uint cur_index_;
170 std::atomic_bool dump_;
171 std::string dump_file_;
172
Elliott Hughes5e62b342018-10-25 11:00:00 -0700173 BIONIC_DISALLOW_COPY_AND_ASSIGN(RecordData);
Christopher Ferris7bd01782016-04-20 12:30:58 -0700174};