1800b99b8Sopenharmony_ci/* 2800b99b8Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License. 5800b99b8Sopenharmony_ci * You may obtain a copy of the License at 6800b99b8Sopenharmony_ci * 7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8800b99b8Sopenharmony_ci * 9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and 13800b99b8Sopenharmony_ci * limitations under the License. 14800b99b8Sopenharmony_ci */ 15800b99b8Sopenharmony_ci 16800b99b8Sopenharmony_ci#include "dfx_hook_utils.h" 17800b99b8Sopenharmony_ci 18800b99b8Sopenharmony_ci#include <securec.h> 19800b99b8Sopenharmony_ci#include <stdio.h> 20800b99b8Sopenharmony_ci#include <unistd.h> 21800b99b8Sopenharmony_ci#include <stdbool.h> 22800b99b8Sopenharmony_ci 23800b99b8Sopenharmony_ci#include <libunwind_i.h> 24800b99b8Sopenharmony_ci 25800b99b8Sopenharmony_ci#include "dfx_define.h" 26800b99b8Sopenharmony_ci#include "dfx_log.h" 27800b99b8Sopenharmony_ci 28800b99b8Sopenharmony_ci#ifdef LOG_DOMAIN 29800b99b8Sopenharmony_ci#undef LOG_DOMAIN 30800b99b8Sopenharmony_ci#define LOG_DOMAIN 0xD002D11 31800b99b8Sopenharmony_ci#endif 32800b99b8Sopenharmony_ci 33800b99b8Sopenharmony_ci#ifdef LOG_TAG 34800b99b8Sopenharmony_ci#undef LOG_TAG 35800b99b8Sopenharmony_ci#define LOG_TAG "DfxFuncHook" 36800b99b8Sopenharmony_ci#endif 37800b99b8Sopenharmony_ci 38800b99b8Sopenharmony_ci#define MIN_FRAME 4 39800b99b8Sopenharmony_ci#define MAX_FRAME 64 40800b99b8Sopenharmony_ci#define BUF_SZ 512 41800b99b8Sopenharmony_ci 42800b99b8Sopenharmony_cistatic pthread_mutex_t g_backtraceLock = PTHREAD_MUTEX_INITIALIZER; 43800b99b8Sopenharmony_ci 44800b99b8Sopenharmony_civoid LogBacktrace(void) 45800b99b8Sopenharmony_ci{ 46800b99b8Sopenharmony_ci pthread_mutex_lock(&g_backtraceLock); 47800b99b8Sopenharmony_ci unw_context_t context; 48800b99b8Sopenharmony_ci unw_getcontext(&context); 49800b99b8Sopenharmony_ci 50800b99b8Sopenharmony_ci unw_cursor_t cursor; 51800b99b8Sopenharmony_ci unw_init_local(&cursor, &context); 52800b99b8Sopenharmony_ci 53800b99b8Sopenharmony_ci int index = 0; 54800b99b8Sopenharmony_ci unw_word_t pc; 55800b99b8Sopenharmony_ci unw_word_t prevPc; 56800b99b8Sopenharmony_ci bool shouldContinue = true; 57800b99b8Sopenharmony_ci while (true) { 58800b99b8Sopenharmony_ci if (index > MAX_FRAME) { 59800b99b8Sopenharmony_ci break; 60800b99b8Sopenharmony_ci } 61800b99b8Sopenharmony_ci 62800b99b8Sopenharmony_ci if (unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t*)(&pc))) { 63800b99b8Sopenharmony_ci break; 64800b99b8Sopenharmony_ci } 65800b99b8Sopenharmony_ci 66800b99b8Sopenharmony_ci if (index > MIN_FRAME && prevPc == pc) { 67800b99b8Sopenharmony_ci break; 68800b99b8Sopenharmony_ci } 69800b99b8Sopenharmony_ci prevPc = pc; 70800b99b8Sopenharmony_ci index++; 71800b99b8Sopenharmony_ci 72800b99b8Sopenharmony_ci if (!shouldContinue) { 73800b99b8Sopenharmony_ci break; 74800b99b8Sopenharmony_ci } 75800b99b8Sopenharmony_ci 76800b99b8Sopenharmony_ci int ret = unw_step(&cursor); 77800b99b8Sopenharmony_ci if (ret == 0) { 78800b99b8Sopenharmony_ci shouldContinue = false; 79800b99b8Sopenharmony_ci } else if (ret < 0) { 80800b99b8Sopenharmony_ci break; 81800b99b8Sopenharmony_ci } 82800b99b8Sopenharmony_ci } 83800b99b8Sopenharmony_ci pthread_mutex_unlock(&g_backtraceLock); 84800b99b8Sopenharmony_ci}