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 <set>
17 #include "dfx_buffer_writer.h"
18 #include "dfx_dump_request.h"
19 #include "dfx_process.h"
20 #include "dfx_signal.h"
21 #include "dfx_log.h"
22 #include "process_dump_config.h"
23 #include "unwinder.h"
24 namespace OHOS {
25 namespace HiviewDFX {
26 REGISTER_DUMP_INFO_CLASS(Maps);
27
Print(DfxProcess & process,const ProcessDumpRequest & request,Unwinder & unwinder)28 void Maps::Print(DfxProcess& process, const ProcessDumpRequest& request, Unwinder& unwinder)
29 {
30 DecorativeDumpInfo::Print(process, request, unwinder);
31 auto maps = unwinder.GetMaps();
32 if (maps == nullptr) {
33 DFXLOGE("maps is not init!");
34 return;
35 }
36 DfxBufferWriter::GetInstance().WriteMsg("\nMaps:\n");
37 if (ProcessDumpConfig::GetInstance().GetConfig().simplifyVmaPrinting ||
38 (process.GetCrashLogConfig().simplifyVmaPrinting)) {
39 std::set<DfxMap> simplifyMaps;
40 SimplifyVma(process, request, maps, simplifyMaps);
41 for (const auto& simplifyMap : simplifyMaps) {
42 DfxBufferWriter::GetInstance().WriteMsg(simplifyMap.ToString());
43 }
44 } else {
45 for (const auto& map : maps->GetMaps()) {
46 if (map != nullptr) {
47 DfxBufferWriter::GetInstance().WriteMsg(map->ToString());
48 }
49 }
50 }
51 }
52
SimplifyVma(DfxProcess & process,const ProcessDumpRequest & request,const std::shared_ptr<DfxMaps> & maps,std::set<DfxMap> & mapSet)53 void Maps::SimplifyVma(DfxProcess& process, const ProcessDumpRequest& request,
54 const std::shared_ptr<DfxMaps>& maps, std::set<DfxMap>& mapSet)
55 {
56 std::set<uintptr_t> interestedAddrs = process.GetMemoryValues();
57 auto regsData = process.GetFaultThreadRegisters()->GetRegsData();
58 interestedAddrs.insert(regsData.begin(), regsData.end());
59 DfxSignal dfxSignal(request.siginfo.si_signo);
60 if (dfxSignal.IsAddrAvailable()) {
61 interestedAddrs.insert(reinterpret_cast<uintptr_t>(request.siginfo.si_addr));
62 }
63 auto threads = process.GetOtherThreads();
64 threads.emplace_back(process.GetKeyThread());
65 std::set<std::string> mapNames;
66 for (const auto& thread : threads) {
67 for (const auto &frame : thread->GetFrames()) {
68 interestedAddrs.emplace(frame.pc);
69 }
70 }
71 for (const auto& addr : interestedAddrs) {
72 maps->FindMapGroupByAddr(addr, mapSet);
73 }
74 }
75 }
76 }