OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "snapshot/mac/mach_o_image_annotations_reader.h" |
| 16 |
| 17 #include <mach-o/loader.h> |
| 18 #include <mach/mach.h> |
| 19 |
| 20 #include "base/logging.h" |
| 21 #include "client/crashpad_info.h" |
| 22 #include "client/simple_string_dictionary.h" |
| 23 #include "snapshot/mac/mach_o_image_reader.h" |
| 24 #include "snapshot/mac/process_reader.h" |
| 25 #include "snapshot/mac/process_types.h" |
| 26 #include "util/mach/task_memory.h" |
| 27 #include "util/stdlib/strnlen.h" |
| 28 |
| 29 namespace crashpad { |
| 30 |
| 31 MachOImageAnnotationsReader::MachOImageAnnotationsReader( |
| 32 ProcessReader* process_reader, |
| 33 const MachOImageReader* image_reader, |
| 34 const std::string& name) |
| 35 : name_(name), |
| 36 process_reader_(process_reader), |
| 37 image_reader_(image_reader) { |
| 38 } |
| 39 |
| 40 std::vector<std::string> MachOImageAnnotationsReader::Vector() const { |
| 41 std::vector<std::string> vector_annotations; |
| 42 |
| 43 ReadCrashReporterClientAnnotations(&vector_annotations); |
| 44 ReadDyldErrorStringAnnotation(&vector_annotations); |
| 45 |
| 46 return vector_annotations; |
| 47 } |
| 48 |
| 49 std::map<std::string, std::string> MachOImageAnnotationsReader::SimpleMap() |
| 50 const { |
| 51 std::map<std::string, std::string> simple_map_annotations; |
| 52 |
| 53 ReadCrashpadSimpleAnnotations(&simple_map_annotations); |
| 54 |
| 55 return simple_map_annotations; |
| 56 } |
| 57 |
| 58 void MachOImageAnnotationsReader::ReadCrashReporterClientAnnotations( |
| 59 std::vector<std::string>* vector_annotations) const { |
| 60 mach_vm_address_t crash_info_address; |
| 61 const process_types::section* crash_info_section = |
| 62 image_reader_->GetSectionByName( |
| 63 SEG_DATA, "__crash_info", &crash_info_address); |
| 64 if (!crash_info_section) { |
| 65 return; |
| 66 } |
| 67 |
| 68 process_types::crashreporter_annotations_t crash_info; |
| 69 if (crash_info_section->size < crash_info.ExpectedSize(process_reader_)) { |
| 70 LOG(WARNING) << "small crash info section size " << crash_info_section->size |
| 71 << " in " << name_; |
| 72 return; |
| 73 } |
| 74 |
| 75 if (!crash_info.Read(process_reader_, crash_info_address)) { |
| 76 LOG(WARNING) << "could not read crash info from " << name_; |
| 77 return; |
| 78 } |
| 79 |
| 80 if (crash_info.version != 4) { |
| 81 LOG(WARNING) << "unexpected crash info version " << crash_info.version |
| 82 << " in " << name_; |
| 83 return; |
| 84 } |
| 85 |
| 86 // This number was totally made up out of nowhere, but it seems prudent to |
| 87 // enforce some limit. |
| 88 const size_t kMaxMessageSize = 1024; |
| 89 if (crash_info.message) { |
| 90 std::string message; |
| 91 if (process_reader_->Memory()-> |
| 92 ReadCStringSizeLimited( |
| 93 crash_info.message, kMaxMessageSize, &message)) { |
| 94 vector_annotations->push_back(message); |
| 95 } else { |
| 96 LOG(WARNING) << "could not read crash message in " << name_; |
| 97 } |
| 98 } |
| 99 |
| 100 if (crash_info.message2) { |
| 101 std::string message; |
| 102 if (process_reader_->Memory()-> |
| 103 ReadCStringSizeLimited( |
| 104 crash_info.message2, kMaxMessageSize, &message)) { |
| 105 vector_annotations->push_back(message); |
| 106 } else { |
| 107 LOG(WARNING) << "could not read crash message 2 in " << name_; |
| 108 } |
| 109 } |
| 110 } |
| 111 |
| 112 void MachOImageAnnotationsReader::ReadDyldErrorStringAnnotation( |
| 113 std::vector<std::string>* vector_annotations) const { |
| 114 // dyld stores its error string at the external symbol for |const char |
| 115 // error_string[1024]|. See 10.9.5 dyld-239.4/src/dyld.cpp error_string. |
| 116 if (image_reader_->FileType() != MH_DYLINKER) { |
| 117 return; |
| 118 } |
| 119 |
| 120 mach_vm_address_t error_string_address; |
| 121 if (!image_reader_->LookUpExternalDefinedSymbol("_error_string", |
| 122 &error_string_address)) { |
| 123 return; |
| 124 } |
| 125 |
| 126 std::string message; |
| 127 // 1024 here is distinct from kMaxMessageSize above, because it refers to a |
| 128 // precisely-sized buffer inside dyld. |
| 129 if (process_reader_->Memory()-> |
| 130 ReadCStringSizeLimited(error_string_address, 1024, &message)) { |
| 131 if (!message.empty()) { |
| 132 vector_annotations->push_back(message); |
| 133 } |
| 134 } else { |
| 135 LOG(WARNING) << "could not read dylinker error string from " << name_; |
| 136 } |
| 137 } |
| 138 |
| 139 void MachOImageAnnotationsReader::ReadCrashpadSimpleAnnotations( |
| 140 std::map<std::string, std::string>* simple_map_annotations) const { |
| 141 mach_vm_address_t crashpad_info_address; |
| 142 const process_types::section* crashpad_info_section = |
| 143 image_reader_->GetSectionByName( |
| 144 SEG_DATA, "__crashpad_info", &crashpad_info_address); |
| 145 if (!crashpad_info_section) { |
| 146 return; |
| 147 } |
| 148 |
| 149 process_types::CrashpadInfo crashpad_info; |
| 150 if (crashpad_info_section->size < |
| 151 crashpad_info.ExpectedSize(process_reader_)) { |
| 152 LOG(WARNING) << "small crashpad info section size " |
| 153 << crashpad_info_section->size << " in " << name_; |
| 154 return; |
| 155 } |
| 156 |
| 157 if (!crashpad_info.Read(process_reader_, crashpad_info_address)) { |
| 158 LOG(WARNING) << "could not read crashpad info from " << name_; |
| 159 return; |
| 160 } |
| 161 |
| 162 if (crashpad_info.signature != CrashpadInfo::kSignature || |
| 163 crashpad_info.size != crashpad_info_section->size || |
| 164 crashpad_info.version < 1) { |
| 165 LOG(WARNING) << "unexpected crashpad info data in " << name_; |
| 166 return; |
| 167 } |
| 168 |
| 169 if (!crashpad_info.simple_annotations) { |
| 170 return; |
| 171 } |
| 172 |
| 173 std::vector<SimpleStringDictionary::Entry> |
| 174 simple_annotations(SimpleStringDictionary::num_entries); |
| 175 if (!process_reader_->Memory() |
| 176 ->Read(crashpad_info.simple_annotations, |
| 177 simple_annotations.size() * sizeof(simple_annotations[0]), |
| 178 &simple_annotations[0])) { |
| 179 LOG(WARNING) << "could not read simple annotations from " << name_; |
| 180 return; |
| 181 } |
| 182 |
| 183 for (const auto& entry : simple_annotations) { |
| 184 size_t key_length = strnlen(entry.key, sizeof(entry.key)); |
| 185 if (key_length) { |
| 186 std::string key(entry.key, key_length); |
| 187 std::string value(entry.value, strnlen(entry.value, sizeof(entry.value))); |
| 188 if (!simple_map_annotations->count(key)) { |
| 189 simple_map_annotations->insert( |
| 190 std::pair<std::string, std::string>(key, value)); |
| 191 } else { |
| 192 LOG(INFO) << "duplicate simple annotation " << key << " in " << name_; |
| 193 } |
| 194 } |
| 195 } |
| 196 } |
| 197 |
| 198 } // namespace crashpad |
OLD | NEW |