blob: 7e2df0c3673e07b9ac4a811d313bde23b7497c57 [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
2 * Copyright (C) 2015 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
29#ifndef DEBUG_MALLOC_DEBUGDATA_H
30#define DEBUG_MALLOC_DEBUGDATA_H
31
32#include <stdint.h>
33
34#include <memory>
35#include <vector>
36
37#include <private/bionic_macros.h>
38
39#include "BacktraceData.h"
40#include "Config.h"
41#include "FreeTrackData.h"
42#include "GuardData.h"
43#include "malloc_debug.h"
Christopher Ferris7bd01782016-04-20 12:30:58 -070044#include "RecordData.h"
Christopher Ferris63860cb2015-11-16 17:30:32 -080045#include "TrackData.h"
46
47class DebugData {
48 public:
49 DebugData() = default;
50 ~DebugData() = default;
51
52 bool Initialize();
53
54 static bool Disabled();
55
56 inline void* GetPointer(const Header* header) {
57 uintptr_t value = reinterpret_cast<uintptr_t>(header);
58 return reinterpret_cast<void*>(value + pointer_offset_);
59 }
60
61 Header* GetHeader(const void* pointer) {
62 uintptr_t value = reinterpret_cast<uintptr_t>(pointer);
63 return reinterpret_cast<Header*>(value - pointer_offset_);
64 }
65
66 BacktraceHeader* GetAllocBacktrace(const Header* header) {
67 uintptr_t value = reinterpret_cast<uintptr_t>(header);
68 return reinterpret_cast<BacktraceHeader*>(value + backtrace->alloc_offset());
69 }
70
Christopher Ferris63860cb2015-11-16 17:30:32 -080071 uint8_t* GetFrontGuard(const Header* header) {
72 uintptr_t value = reinterpret_cast<uintptr_t>(header);
73 return reinterpret_cast<uint8_t*>(value + front_guard->offset());
74 }
75
76 uint8_t* GetRearGuard(const Header* header) {
77 uintptr_t value = reinterpret_cast<uintptr_t>(GetPointer(header));
78 return reinterpret_cast<uint8_t*>(value + header->real_size());
79 }
80
81 const Config& config() { return config_; }
82 size_t pointer_offset() { return pointer_offset_; }
83 bool need_header() { return need_header_; }
84 size_t extra_bytes() { return extra_bytes_; }
85
Colin Cross7a28a3c2016-02-07 22:51:15 -080086 void PrepareFork();
87 void PostForkParent();
88 void PostForkChild();
89
Christopher Ferris63860cb2015-11-16 17:30:32 -080090 std::unique_ptr<BacktraceData> backtrace;
91 std::unique_ptr<TrackData> track;
92 std::unique_ptr<FrontGuardData> front_guard;
93 std::unique_ptr<RearGuardData> rear_guard;
94 std::unique_ptr<FreeTrackData> free_track;
Christopher Ferris7bd01782016-04-20 12:30:58 -070095 std::unique_ptr<RecordData> record;
Christopher Ferris63860cb2015-11-16 17:30:32 -080096
97 private:
98 size_t extra_bytes_ = 0;
99
100 size_t pointer_offset_ = 0;
101 bool need_header_ = false;
102
103 Config config_;
104
105 DISALLOW_COPY_AND_ASSIGN(DebugData);
106};
107
Christopher Ferris55a89a42016-04-07 17:14:53 -0700108extern DebugData* g_debug;
109
Christopher Ferris63860cb2015-11-16 17:30:32 -0800110#endif // MALLOC_DEBUG_DEBUGDATA_H