1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
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 "decorative_dump_info.h"
16 #include "dfx_buffer_writer.h"
17 #include "dfx_dump_request.h"
18 #include "dfx_util.h"
19 #include "dfx_process.h"
20 #include "dfx_log.h"
21 #include "dump_utils.h"
22 #include "unwinder.h"
23 namespace OHOS {
24 namespace HiviewDFX {
25 REGISTER_DUMP_INFO_CLASS(ExtraCrashInfo);
26
Print(DfxProcess & process,const ProcessDumpRequest & request,Unwinder & unwinder)27 void ExtraCrashInfo::Print(DfxProcess& process, const ProcessDumpRequest& request, Unwinder& unwinder)
28 {
29 DecorativeDumpInfo::Print(process, request, unwinder);
30 DfxBufferWriter::GetInstance().WriteMsg(GetCrashObjContent(process, request));
31 }
32
ReadCrashObjMemory(pid_t tid,uintptr_t addr,size_t length) const33 std::string ExtraCrashInfo::ReadCrashObjMemory(pid_t tid, uintptr_t addr, size_t length) const
34 {
35 DFXLOGI("Start read memory type of crashObj, memory length(%zu).", length);
36 constexpr size_t step = sizeof(uintptr_t);
37 std::string memoryContent = StringPrintf("ExtraCrashInfo(Memory start address %018" PRIx64 "):",
38 static_cast<uint64_t>(addr));
39 size_t size = (length + step - 1) / step;
40 std::vector<uintptr_t> memory(size, 0);
41 if (ReadProcMemByPid(tid, addr, memory.data(), length) != length) {
42 DFXLOGE("[%{public}d]: read target mem error %{public}s", __LINE__, strerror(errno));
43 memoryContent += "\n";
44 return memoryContent;
45 }
46 for (size_t index = 0; index < size; index++) {
47 size_t offset = index * step;
48 if (offset % 0x20 == 0) { // Print offset every 32 bytes
49 memoryContent += StringPrintf("\n+0x%03" PRIx64 ":", static_cast<uint64_t>(offset));
50 }
51 memoryContent += StringPrintf(" %018" PRIx64, static_cast<uint64_t>(memory[index]));
52 }
53 memoryContent += "\n";
54 return memoryContent;
55 }
56
GetCrashObjContent(DfxProcess & process,const ProcessDumpRequest & request)57 std::string ExtraCrashInfo::GetCrashObjContent(DfxProcess& process, const ProcessDumpRequest& request)
58 {
59 std::string content = "";
60 #ifdef __LP64__
61 if (request.type != ProcessDumpType::DUMP_TYPE_CPP_CRASH || request.crashObj == 0) {
62 DFXLOGI("crash obj not int.");
63 return content;
64 }
65 uintptr_t type = request.crashObj >> 56; // 56 :: Move 56 bit to the right
66 uintptr_t addr = request.crashObj & 0xffffffffffffff;
67 std::vector<size_t> memorylengthTable = {0, 64, 256, 1024, 2048, 4096};
68 if (type != 0 && type < memorylengthTable.size()) {
69 content = ReadCrashObjMemory(request.nsPid, addr, memorylengthTable[type]);
70 }
71 #endif
72 return content;
73 }
74 }
75 }