• 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 
16 #include "hidebug/hidebug.h"
17 
18 #include "dfx_map.h"
19 #include "fp_backtrace.h"
20 
OH_HiDebug_BacktraceFromFp(HiDebug_Backtrace_Object object,void * startFp,void ** pcArray,int size)21 int OH_HiDebug_BacktraceFromFp(HiDebug_Backtrace_Object object, void* startFp, void** pcArray, int size)
22 {
23     if (object == nullptr || startFp == nullptr || pcArray == nullptr || size <= 0) {
24         return 0;
25     }
26     const auto fpBacktrace = reinterpret_cast<OHOS::HiviewDFX::FpBacktrace*>(object);
27     return static_cast<int>(fpBacktrace->BacktraceFromFp(startFp, pcArray, static_cast<uint32_t>(size)));
28 }
29 
OH_HiDebug_SymbolicAddress(HiDebug_Backtrace_Object object,void * pc,void * arg,OH_HiDebug_SymbolicAddressCallback callback)30 HiDebug_ErrorCode OH_HiDebug_SymbolicAddress(HiDebug_Backtrace_Object object, void* pc, void* arg,
31     OH_HiDebug_SymbolicAddressCallback callback)
32 {
33     if (object == nullptr || callback == nullptr || pc == nullptr) {
34         return HIDEBUG_INVALID_ARGUMENT;
35     }
36     auto fpBacktrace = reinterpret_cast<OHOS::HiviewDFX::FpBacktrace*>(object);
37     const auto* frame = fpBacktrace->SymbolicAddress(pc);
38     if (frame == nullptr) {
39         return HIDEBUG_INVALID_SYMBOLIC_PC_ADDRESS;
40     }
41     HiDebug_StackFrame stackFrame;
42     if (frame->isJsFrame) {
43         stackFrame.type = HiDebug_StackFrameType::HIDEBUG_STACK_FRAME_TYPE_JS;
44         stackFrame.frame.js.column = frame->column;
45         stackFrame.frame.js.functionName = frame->funcName.c_str();
46         stackFrame.frame.js.mapName = frame->map->name.c_str();
47         stackFrame.frame.js.packageName = frame->packageName.c_str();
48         stackFrame.frame.js.url = frame->mapName.c_str();
49         stackFrame.frame.js.relativePc = frame->relPc;
50         stackFrame.frame.js.line = frame->line;
51     } else {
52         stackFrame.type = HiDebug_StackFrameType::HIDEBUG_STACK_FRAME_TYPE_NATIVE;
53         stackFrame.frame.native.buildId = frame->buildId.c_str();
54         stackFrame.frame.native.funcOffset = frame->funcOffset;
55         stackFrame.frame.native.functionName = frame->funcName.c_str();
56         stackFrame.frame.native.mapName = frame->mapName.c_str();
57         stackFrame.frame.native.relativePc = frame->relPc;
58         stackFrame.frame.native.reserved = nullptr;
59     }
60     callback(pc, arg, &stackFrame);
61     return HIDEBUG_SUCCESS;
62 }
63 
OH_HiDebug_CreateBacktraceObject(void)64 HiDebug_Backtrace_Object OH_HiDebug_CreateBacktraceObject(void)
65 {
66     return reinterpret_cast<HiDebug_Backtrace_Object>(OHOS::HiviewDFX::FpBacktrace::CreateInstance());
67 }
68 
OH_HiDebug_DestroyBacktraceObject(HiDebug_Backtrace_Object object)69 void OH_HiDebug_DestroyBacktraceObject(HiDebug_Backtrace_Object object)
70 {
71     delete reinterpret_cast<OHOS::HiviewDFX::FpBacktrace*>(object);
72 }