Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1 | /* |
| 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 | #include <stdint.h> |
| 30 | |
| 31 | #include "BacktraceData.h" |
| 32 | #include "Config.h" |
| 33 | #include "DebugData.h" |
| 34 | #include "debug_disable.h" |
| 35 | #include "FreeTrackData.h" |
| 36 | #include "GuardData.h" |
| 37 | #include "malloc_debug.h" |
| 38 | #include "TrackData.h" |
| 39 | |
| 40 | bool DebugData::Initialize() { |
| 41 | if (!config_.SetFromProperties()) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | // Check to see if the options that require a header are enabled. |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 46 | if (config_.options & HEADER_OPTIONS) { |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 47 | need_header_ = true; |
| 48 | |
| 49 | // Initialize all of the static header offsets. |
Christopher Ferris | 72df670 | 2016-02-11 15:51:31 -0800 | [diff] [blame] | 50 | pointer_offset_ = BIONIC_ALIGN(sizeof(Header), MINIMUM_ALIGNMENT_BYTES); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 51 | |
| 52 | if (config_.options & BACKTRACE) { |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 53 | backtrace.reset(new BacktraceData(this, config_, &pointer_offset_)); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 54 | if (!backtrace->Initialize(config_)) { |
| 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (config_.options & FRONT_GUARD) { |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 60 | front_guard.reset(new FrontGuardData(this, config_, &pointer_offset_)); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | extra_bytes_ = pointer_offset_; |
| 64 | |
| 65 | // Initialize all of the non-header data. |
| 66 | if (config_.options & REAR_GUARD) { |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 67 | rear_guard.reset(new RearGuardData(this, config_)); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 68 | extra_bytes_ += config_.rear_guard_bytes; |
| 69 | } |
| 70 | |
| 71 | if (config_.options & FREE_TRACK) { |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 72 | free_track.reset(new FreeTrackData(this, config_)); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | if (config_.options & TRACK_ALLOCS) { |
Christopher Ferris | 55a89a4 | 2016-04-07 17:14:53 -0700 | [diff] [blame] | 76 | track.reset(new TrackData(this)); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
Christopher Ferris | 7bd0178 | 2016-04-20 12:30:58 -0700 | [diff] [blame^] | 80 | if (config_.options & RECORD_ALLOCS) { |
| 81 | record.reset(new RecordData()); |
| 82 | if (!record->Initialize(config_)) { |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 87 | if (config_.options & EXPAND_ALLOC) { |
| 88 | extra_bytes_ += config_.expand_alloc_bytes; |
| 89 | } |
| 90 | return true; |
| 91 | } |
Colin Cross | 7a28a3c | 2016-02-07 22:51:15 -0800 | [diff] [blame] | 92 | |
| 93 | void DebugData::PrepareFork() { |
| 94 | if (track != nullptr) { |
| 95 | track->PrepareFork(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void DebugData::PostForkParent() { |
| 100 | if (track != nullptr) { |
| 101 | track->PostForkParent(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void DebugData::PostForkChild() { |
| 106 | if (track != nullptr) { |
| 107 | track->PostForkChild(); |
| 108 | } |
| 109 | } |