• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "crash_exception.h"
17 #include "dfx_log.h"
18 #include "dump_utils.h"
19 #include "dfx_trace.h"
20 #include "process_dump_config.h"
21 #include "dfx_buffer_writer.h"
22 namespace OHOS {
23 namespace HiviewDFX {
24 REGISTER_DUMP_INFO_CLASS(OtherThreadDumpInfo);
25 
UnwindStack(DfxProcess & process,const ProcessDumpRequest & request,Unwinder & unwinder)26 int OtherThreadDumpInfo::UnwindStack(DfxProcess& process, const ProcessDumpRequest& request, Unwinder& unwinder)
27 {
28     int unwindSuccessCnt = DecorativeDumpInfo::UnwindStack(process, request, unwinder);
29     DFXLOGI("unwind other thread dump start");
30     auto pid = process.GetVmPid();
31     if (pid == 0) {
32         DFXLOGW("vm pid is null, can not unwind other thread!");
33         return unwindSuccessCnt;
34     }
35     for (const auto &thread : process.GetOtherThreads()) {
36         auto regs = thread->GetThreadRegs();
37         unwinder.SetRegs(regs);
38         bool withRegs = regs != nullptr;
39         pid_t tid = thread->GetThreadInfo().nsTid;
40         if (!withRegs) {
41             DFXLOGI("not with regs");
42             DumpUtils::GetThreadKernelStack(*thread);
43             continue;
44         }
45         DFX_TRACE_START("OtherThreadUnwindRemote:%d", tid);
46         if (unwinder.UnwindRemote(pid, withRegs, ProcessDumpConfig::GetInstance().GetConfig().maxFrameNums)) {
47             unwindSuccessCnt++;
48         } else {
49             DFXLOGW("%{public}s, unwind tid(%{public}d) failed.", __func__, tid);
50         }
51         DFX_TRACE_FINISH();
52         DFX_TRACE_START("OtherThreadGetFrames:%d", tid);
53         thread->SetFrames(unwinder.GetFrames());
54         DFX_TRACE_FINISH();
55 #ifdef PARSE_LOCK_OWNER
56         DumpUtils::ParseLockInfo(unwinder, pid, tid);
57 #endif
58         ReportUnwinderException(unwinder.GetLastErrorCode());
59     }
60     return unwindSuccessCnt;
61 }
62 
Print(DfxProcess & process,const ProcessDumpRequest & request,Unwinder & unwinder)63 void OtherThreadDumpInfo::Print(DfxProcess& process, const ProcessDumpRequest& request, Unwinder& unwinder)
64 {
65     DecorativeDumpInfo::Print(process, request, unwinder);
66     std::string dumpInfo;
67     if (request.type != ProcessDumpType::DUMP_TYPE_DUMP_CATCH && !process.GetOtherThreads().empty()) {
68         dumpInfo += "Other thread info:\n";
69     }
70     for (const auto &thread : process.GetOtherThreads()) {
71         dumpInfo += thread->ToString();
72     }
73     DfxBufferWriter::GetInstance().WriteMsg(dumpInfo);
74 }
75 
Symbolize(DfxProcess & process,Unwinder & unwinder)76 void OtherThreadDumpInfo::Symbolize(DfxProcess& process, Unwinder& unwinder)
77 {
78     DecorativeDumpInfo::Symbolize(process, unwinder);
79     for (const auto &thread : process.GetOtherThreads()) {
80         DFX_TRACE_START("ParseSymbol otherThread:%d", thread->GetThreadInfo().nsTid);
81         thread->ParseSymbol(unwinder);
82         DFX_TRACE_FINISH();
83     }
84 }
85 }
86 }