diff --git a/src/js_native_api_v8.cpp b/src/js_native_api_v8.cpp index 70c5422addf408999ff9cbcd436748374ddbbcf6..d7a235f55e6758c554caad6eda70beaece779292 100644 --- a/src/js_native_api_v8.cpp +++ b/src/js_native_api_v8.cpp @@ -6000,3 +6000,51 @@ JSVM_Status OH_JSVM_PromiseRegisterHandler(JSVM_Env env, return ClearLastError(env); } + +__attribute__((visibility("default"))) int step_jsvm(void *ctx, ReadMemFunc readMem, JsvmStepParam *frame) +{ + uintptr_t preFp = 0; + readMem(ctx, *(frame->fp), &preFp); + uintptr_t prePc = 0; + readMem(ctx, *(frame->fp) + sizeof(void*), &prePc); + + *(frame->fp) = preFp; + *(frame->pc) = prePc; + *(frame->sp) = preFp; + return 0; +} + + +__attribute__((visibility("default"))) int create_jsvm_extractor(uintptr_t* extractorPptr, uint32_t pid) +{ + uintptr_t extractorPtr = 0; + if (v8::V8::CreateJSVMExtractor(extractorPtr, pid) == 0) { + *extractorPptr = extractorPtr; + return 0; + } else { + return -1; + } +} + +__attribute_((visibility)("default")) int destory_jsvm_extractor(uintptr_t extractorPtr) +{ + v8::V8::DeleteJSVMExtractor(extractorPtr); + return 0; +} + +__attribute_((visibility)("default")) int jsvm_parse_js_frame_info(uintptr_t pc, + uintptr_t jsvmExtractorPtr, + JsvmFunction* jsvmFunction) +{ + std::string codeName; + int ret = v8::V8::GetJSVMCodeName(jsvmExtractorPtr, pc, codeName); + if (ret == 0) { + size_t nameSize = codeName.size() + 1; + if (strcpy_s(jsvmFunction->functionName, nameSize, codeName.c_str()) != EOK) { + return -1; + } + return 0; + } else { + return -1; + } +} diff --git a/src/jsvm_dfx.h b/src/jsvm_dfx.h index 7aca9b4c49277f6a084a21460b4ccd32fbe5964e..f0c139efb710341f9ad42710dd2f3dfbf0b1d1c2 100644 --- a/src/jsvm_dfx.h +++ b/src/jsvm_dfx.h @@ -122,6 +122,33 @@ private: } // namespace jsvm +constexpr uint16_t FUNCTIONNAME_MAX = 1024; + +struct JsvmStepParam { + uintptr_t *fp; + uintptr_t *sp; + uintptr_t *pc; + bool *isJsvmFrame; + + JsvmStepParam(uintptr_t *fp, uintptr_t *sp, uintptr_t *pc, bool *isJsvmFrame) + : fp(fp), sp(sp), pc(pc), isJsvmFrame(isJsvmFrame) {} +} +struct JsvmFunction { + char functionName[FUNCTIONNAME_MAX]; +} + +typedef bool (*ReadMemFunc)(void *ctx, uintptr_t addr, uintptr_t *val); + +extern "C" int step_jsvm(void *ctx, ReadMemFunc readMem, JsvmStepParam *frame); + +extern "C" int create_jsvm_extractor(uintptr_t *extractorPptr, uint32_t pid); + +extern "C" int destory_jsvm_extractor(uintptr_t extractorPtr); + +extern "C" int jsvm_parse_js_frame_info(uintptr_t pc, + uintptr_t jsvmExtractorPtr, + JsvmFunction *jsvmFunction); + #define UNREACHABLE(...) JSVM_FATAL("Unreachable code reached" __VA_OPT__(": ") __VA_ARGS__)