Optimized the performance of the printing C++ stack

issues:https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I9DRRI

Signed-off-by: rentangyu <rentangyu@huawei.com>
This commit is contained in:
rentangyu 2024-04-02 22:47:59 +08:00
parent bce7b3e87d
commit f1d75e0118
11 changed files with 16 additions and 57 deletions

View File

@ -254,10 +254,6 @@ JSHandle<EcmaString> ErrorHelper::BuildEcmaStackTrace(JSThread *thread, std::str
if (sourceMapcb != nullptr && !data.empty()) { if (sourceMapcb != nullptr && !data.empty()) {
data = sourceMapcb(data.c_str()); data = sourceMapcb(data.c_str());
} }
auto nativeStackcb = ecmaVm->GetNativeStackCallback();
if (nativeStackcb != nullptr && data.empty()) {
data = nativeStackcb();
}
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
return factory->NewFromStdString(data); return factory->NewFromStdString(data);

View File

@ -26,6 +26,9 @@
#if defined(PANDA_TARGET_OHOS) #if defined(PANDA_TARGET_OHOS)
#include "ecmascript/extractortool/src/extractor.h" #include "ecmascript/extractortool/src/extractor.h"
#endif #endif
#if defined(ENABLE_EXCEPTION_BACKTRACE)
#include "ecmascript/platform/backtrace.h"
#endif
namespace panda::ecmascript { namespace panda::ecmascript {
std::string JsStackInfo::BuildMethodTrace(Method *method, uint32_t pcOffset, bool enableStackSourceFile) std::string JsStackInfo::BuildMethodTrace(Method *method, uint32_t pcOffset, bool enableStackSourceFile)
@ -125,6 +128,13 @@ std::string JsStackInfo::BuildJsStackTrace(JSThread *thread, bool needNative)
data.append(" at native method (").append(strm.str()).append(")\n"); data.append(" at native method (").append(strm.str()).append(")\n");
} }
} }
if (data.empty()) {
#if defined(ENABLE_EXCEPTION_BACKTRACE)
std::ostringstream stack;
Backtrace(stack);
data = stack.str();
#endif
}
return data; return data;
} }

View File

@ -49,12 +49,6 @@ public:
JSThread *thread {nullptr}; JSThread *thread {nullptr};
}; };
HWTEST_F_L0(JsStackInfoTest, BuildJsStackTrace)
{
std::string stack = JsStackInfo::BuildJsStackTrace(thread, false);
ASSERT_TRUE(!stack.empty());
}
HWTEST_F_L0(JsStackInfoTest, FrameCheckTest) HWTEST_F_L0(JsStackInfoTest, FrameCheckTest)
{ {
uintptr_t frame[22]; uintptr_t frame[22];

View File

@ -95,7 +95,6 @@ class Jit;
using NativePtrGetter = void* (*)(void* info); using NativePtrGetter = void* (*)(void* info);
using SourceMapCallback = std::function<std::string(const std::string& rawStack)>; using SourceMapCallback = std::function<std::string(const std::string& rawStack)>;
using SourceMapTranslateCallback = std::function<bool(std::string& url, int& line, int& column)>; using SourceMapTranslateCallback = std::function<bool(std::string& url, int& line, int& column)>;
using NativeStackCallback = std::function<std::string()>;
using ResolveBufferCallback = std::function<bool(std::string dirPath, uint8_t **buff, size_t *buffSize)>; using ResolveBufferCallback = std::function<bool(std::string dirPath, uint8_t **buff, size_t *buffSize)>;
using UnloadNativeModuleCallback = std::function<bool(const std::string &moduleKey)>; using UnloadNativeModuleCallback = std::function<bool(const std::string &moduleKey)>;
using RequestAotCallback = using RequestAotCallback =
@ -311,16 +310,6 @@ public:
return sourceMapTranslateCallback_; return sourceMapTranslateCallback_;
} }
void SetNativeStackCallback(NativeStackCallback cb)
{
nativeStackCallback_ = cb;
}
NativeStackCallback GetNativeStackCallback() const
{
return nativeStackCallback_;
}
size_t GetNativePointerListSize() size_t GetNativePointerListSize()
{ {
return nativePointerList_.size(); return nativePointerList_.size();
@ -694,7 +683,6 @@ private:
NativePtrGetter nativePtrGetter_ {nullptr}; NativePtrGetter nativePtrGetter_ {nullptr};
SourceMapCallback sourceMapCallback_ {nullptr}; SourceMapCallback sourceMapCallback_ {nullptr};
SourceMapTranslateCallback sourceMapTranslateCallback_ {nullptr}; SourceMapTranslateCallback sourceMapTranslateCallback_ {nullptr};
NativeStackCallback nativeStackCallback_ {nullptr};
void *loop_ {nullptr}; void *loop_ {nullptr};
// resolve path to get abc's buffer // resolve path to get abc's buffer

View File

@ -96,7 +96,6 @@ using JSTaggedType = uint64_t;
using ConcurrentCallback = void (*)(Local<JSValueRef> result, bool success, void *taskInfo, void *data); using ConcurrentCallback = void (*)(Local<JSValueRef> result, bool success, void *taskInfo, void *data);
using SourceMapCallback = std::function<std::string(const std::string& rawStack)>; using SourceMapCallback = std::function<std::string(const std::string& rawStack)>;
using SourceMapTranslateCallback = std::function<bool(std::string& url, int& line, int& column)>; using SourceMapTranslateCallback = std::function<bool(std::string& url, int& line, int& column)>;
using NativeStackCallback = std::function<std::string()>;
using DeviceDisconnectCallback = std::function<bool()>; using DeviceDisconnectCallback = std::function<bool()>;
#define ECMA_DISALLOW_COPY(className) \ #define ECMA_DISALLOW_COPY(className) \
@ -1327,7 +1326,6 @@ public:
static void SetNativePtrGetter(EcmaVM *vm, void* cb); static void SetNativePtrGetter(EcmaVM *vm, void* cb);
static void SetSourceMapCallback(EcmaVM *vm, SourceMapCallback cb); static void SetSourceMapCallback(EcmaVM *vm, SourceMapCallback cb);
static void SetSourceMapTranslateCallback(EcmaVM *vm, SourceMapTranslateCallback cb); static void SetSourceMapTranslateCallback(EcmaVM *vm, SourceMapTranslateCallback cb);
static void SetNativeStackCallback(EcmaVM *vm, NativeStackCallback cb);
static void SetHostEnqueueJob(const EcmaVM* vm, Local<JSValueRef> cb); static void SetHostEnqueueJob(const EcmaVM* vm, Local<JSValueRef> cb);
static EcmaVM* CreateEcmaVM(const ecmascript::JSRuntimeOptions &options); static EcmaVM* CreateEcmaVM(const ecmascript::JSRuntimeOptions &options);
static void PreFork(EcmaVM *vm); static void PreFork(EcmaVM *vm);

View File

@ -2991,11 +2991,6 @@ void JSNApi::SetSourceMapTranslateCallback(EcmaVM *vm, SourceMapTranslateCallbac
vm->SetSourceMapTranslateCallback(callback); vm->SetSourceMapTranslateCallback(callback);
} }
void JSNApi::SetNativeStackCallback(EcmaVM *vm, NativeStackCallback callback)
{
vm->SetNativeStackCallback(callback);
}
void JSNApi::SetSourceMapCallback(EcmaVM *vm, SourceMapCallback callback) void JSNApi::SetSourceMapCallback(EcmaVM *vm, SourceMapCallback callback)
{ {
vm->SetSourceMapCallback(callback); vm->SetSourceMapCallback(callback);

View File

@ -20,6 +20,6 @@
#include <string> #include <string>
namespace panda::ecmascript { namespace panda::ecmascript {
void Backtrace(std::ostringstream &stack, bool enableCache = false, bool jsStack = false); void Backtrace(std::ostringstream &stack, bool enableCache = false);
} // namespace panda::ecmascript } // namespace panda::ecmascript
#endif // ECMASCRIPT_PLATFORM_BACKTRACE_H #endif // ECMASCRIPT_PLATFORM_BACKTRACE_H

View File

@ -24,8 +24,7 @@ namespace panda::ecmascript {
static const int MAX_STACK_SIZE = 256; static const int MAX_STACK_SIZE = 256;
static const int FRAMES_LEN = 16; static const int FRAMES_LEN = 16;
void Backtrace(std::ostringstream &stack, [[maybe_unused]] bool enableCache, void Backtrace(std::ostringstream &stack, [[maybe_unused]] bool enableCache)
[[maybe_unused]] bool jsStack)
{ {
void *buffer[MAX_STACK_SIZE]; void *buffer[MAX_STACK_SIZE];
char **stackList = nullptr; char **stackList = nullptr;

View File

@ -17,8 +17,7 @@
#include "ecmascript/log_wrapper.h" #include "ecmascript/log_wrapper.h"
namespace panda::ecmascript { namespace panda::ecmascript {
void Backtrace([[maybe_unused]] std::ostringstream &stack, [[maybe_unused]] bool enableCache, void Backtrace([[maybe_unused]] std::ostringstream &stack, [[maybe_unused]] bool enableCache)
[[maybe_unused]] bool jsStack)
{ {
LOG_ECMA(INFO) << "Print backtrace in macos not support"; LOG_ECMA(INFO) << "Print backtrace in macos not support";
} }

View File

@ -29,8 +29,6 @@
namespace panda::ecmascript { namespace panda::ecmascript {
static const std::string LIB_UNWIND_SO_NAME = "libunwind.so"; static const std::string LIB_UNWIND_SO_NAME = "libunwind.so";
static const std::string LIB_UNWIND_Z_SO_NAME = "libunwind.z.so"; static const std::string LIB_UNWIND_Z_SO_NAME = "libunwind.z.so";
static const std::string LIB_ARK_JSRUNTIME_SO_NAME = "libark_jsruntime.so";
static const std::string LIB_ACE_NAPI_Z_SO_NAME = "libace_napi.z.so";
static const int MAX_STACK_SIZE = 16; static const int MAX_STACK_SIZE = 16;
static const int LOG_BUF_LEN = 1024; static const int LOG_BUF_LEN = 1024;
@ -38,7 +36,7 @@ using UnwBackTraceFunc = int (*)(void**, int);
static std::map<void *, Dl_info> stackInfoCache; static std::map<void *, Dl_info> stackInfoCache;
void Backtrace(std::ostringstream &stack, bool enableCache, bool jsStack) void Backtrace(std::ostringstream &stack, bool enableCache)
{ {
static UnwBackTraceFunc unwBackTrace = nullptr; static UnwBackTraceFunc unwBackTrace = nullptr;
if (!unwBackTrace) { if (!unwBackTrace) {
@ -60,10 +58,7 @@ void Backtrace(std::ostringstream &stack, bool enableCache, bool jsStack)
void *buffer[MAX_STACK_SIZE] = { nullptr }; void *buffer[MAX_STACK_SIZE] = { nullptr };
int level = unwBackTrace(reinterpret_cast<void**>(&buffer), MAX_STACK_SIZE); int level = unwBackTrace(reinterpret_cast<void**>(&buffer), MAX_STACK_SIZE);
stack << "=====================Backtrace========================"; stack << "=====================Backtrace========================";
bool flag = true;
int index = 0;
for (int i = 1; i < level; i++) { for (int i = 1; i < level; i++) {
index++;
Dl_info info; Dl_info info;
auto iter = stackInfoCache.find(buffer[i]); auto iter = stackInfoCache.find(buffer[i]);
if (enableCache && iter != stackInfoCache.end()) { if (enableCache && iter != stackInfoCache.end()) {
@ -77,26 +72,12 @@ void Backtrace(std::ostringstream &stack, bool enableCache, bool jsStack)
} }
} }
const char *file = info.dli_fname ? info.dli_fname : ""; const char *file = info.dli_fname ? info.dli_fname : "";
if (jsStack) {
std::string str = file;
auto splitPos = str.rfind("/");
if (splitPos != std::string::npos) {
str = str.substr(splitPos + 1);
}
if ((str.compare(LIB_ARK_JSRUNTIME_SO_NAME) == 0 ||
str.compare(LIB_ACE_NAPI_Z_SO_NAME) == 0) && flag) {
index--;
continue;
} else {
flag = false;
}
}
uint64_t offset = info.dli_fbase ? ToUintPtr(buffer[i]) - ToUintPtr(info.dli_fbase) : 0; uint64_t offset = info.dli_fbase ? ToUintPtr(buffer[i]) - ToUintPtr(info.dli_fbase) : 0;
char buf[LOG_BUF_LEN] = {0}; char buf[LOG_BUF_LEN] = {0};
char frameFormatWithMapName[] = "#%02zu pc %016" PRIx64 " %s"; char frameFormatWithMapName[] = "#%02zu pc %016" PRIx64 " %s";
int ret = 0; int ret = 0;
ret = static_cast<int>(snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, frameFormatWithMapName, \ ret = static_cast<int>(snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, frameFormatWithMapName, \
index, offset, file)); i, offset, file));
if (ret <= 0) { if (ret <= 0) {
LOG_ECMA(ERROR) << "Backtrace snprintf_s failed"; LOG_ECMA(ERROR) << "Backtrace snprintf_s failed";
return; return;

View File

@ -17,8 +17,7 @@
#include "ecmascript/log_wrapper.h" #include "ecmascript/log_wrapper.h"
namespace panda::ecmascript { namespace panda::ecmascript {
void Backtrace([[maybe_unused]] std::ostringstream &stack, [[maybe_unused]] bool enableCache, void Backtrace([[maybe_unused]] std::ostringstream &stack, [[maybe_unused]] bool enableCache)
[[maybe_unused]] bool jsStack)
{ {
LOG_ECMA(INFO) << "Print backtrace in windows not support"; LOG_ECMA(INFO) << "Print backtrace in windows not support";
} }