1 /*
2 * Copyright (c) 2024 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 "thread_sampler_utils.h"
16
17 #include <cstdio>
18 #include <ctime>
19
20 namespace OHOS {
21 namespace HiviewDFX {
22 constexpr uint64_t SEC_TO_NANOSEC = 1000000000;
23
GetCurrentTimeNanoseconds()24 uint64_t GetCurrentTimeNanoseconds()
25 {
26 struct timespec t;
27 t.tv_sec = 0;
28 t.tv_nsec = 0;
29 clock_gettime(CLOCK_REALTIME, &t);
30 return static_cast<uint64_t>(t.tv_sec) * SEC_TO_NANOSEC + static_cast<uint64_t>(t.tv_nsec);
31 }
32
DoUnwind(const std::shared_ptr<Unwinder> & unwinder,UnwindInfo & unwindInfo)33 void DoUnwind(const std::shared_ptr<Unwinder>& unwinder, UnwindInfo& unwindInfo)
34 {
35 #if defined(__aarch64__)
36 static std::shared_ptr<DfxRegs> regs = std::make_shared<DfxRegsArm64>();
37 regs->SetSp(unwindInfo.context->sp);
38 regs->SetPc(unwindInfo.context->pc);
39 regs->SetFp(unwindInfo.context->fp);
40 regs->SetReg(REG_LR, &(unwindInfo.context->lr));
41 unwinder->SetRegs(regs);
42 unwinder->Unwind(&unwindInfo);
43 #endif // #if defined(__aarch64__)
44 #if defined(__loongarch_lp64)
45 static std::shared_ptr<DfxRegs> regs = std::make_shared<DfxRegsLoongArch64>();
46 regs->SetSp(unwindInfo.context->sp);
47 regs->SetPc(unwindInfo.context->pc);
48 regs->SetReg(REG_LOONGARCH64_R22, &(unwindInfo.context->fp));
49 regs->SetReg(REG_LOONGARCH64_R1, &(unwindInfo.context->lr));
50 unwinder->SetRegs(regs);
51 unwinder->Unwind(&unwindInfo);
52 #endif // #if defined(__loongarch_lp64)
53 }
54 } // end of namespace HiviewDFX
55 } // end of namespace OHOS
56