From 9fcccf3f895078bec3fe28adb06818a89394a383 Mon Sep 17 00:00:00 2001 From: liu-yaxue Date: Fri, 11 Jul 2025 11:49:19 +0800 Subject: [PATCH] optimize performance of data copy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue: 【lldb】optimize performance of data copy between arkts and native in lldb https://gitee.com/openharmony/third_party_llvm-project/issues/ICLMZ0 Test: use log to show the performance before and after modification Signed-off-by: liu-yaxue --- lldb/source/Target/MixedArkTSDebugger.cpp | 9 +-- lldb/source/Target/MixedDebugger.cpp | 67 +++++++++++-------- .../mixed_debugger/arkts/Makefile | 14 +++- .../mixed_debugger/arkts/arkshim.c | 28 ++++++++ .../mixed_debugger/arkts/main.c | 35 +++++++--- 5 files changed, 110 insertions(+), 43 deletions(-) create mode 100644 lldb/test/API/functionalities/mixed_debugger/arkts/arkshim.c diff --git a/lldb/source/Target/MixedArkTSDebugger.cpp b/lldb/source/Target/MixedArkTSDebugger.cpp index 40a110ecca9c..6e1137a992a5 100644 --- a/lldb/source/Target/MixedArkTSDebugger.cpp +++ b/lldb/source/Target/MixedArkTSDebugger.cpp @@ -20,9 +20,8 @@ using namespace lldb; using namespace lldb_private; - -static const char* BackTrace = "(const char*)GetJsBacktrace()"; -static const char* DebugMessage = "(const char*)OperateJsDebugMessage(\"{0}\")"; +static const char* BackTrace = "struct DebugInput { size_t size; char *data; }; (DebugInput)GetJsBacktrace()"; +static const char* DebugMessage = "(DebugInput)OperateJsDebugMessage(\"{0}\")"; MixedArkTSDebugger::MixedArkTSDebugger(const TargetSP &target_sp) : MixedDebugger(target_sp) {} @@ -38,7 +37,9 @@ DataExtractorSP MixedArkTSDebugger::GetCurrentThreadBackTrace(Status &error) { } DataExtractorSP MixedArkTSDebugger::GetCurrentThreadOperateDebugMessageResult(const char *message, Status &error) { - std::string operateMessage = llvm::formatv(DebugMessage, message).str(); + std::string operateMessage = + "struct DebugInput {size_t size; char *data; };" + + llvm::formatv(DebugMessage, message).str(); DataExtractorSP result = ExecuteAction(operateMessage.c_str(), error); if (!error.Success()) { Log *log = GetLog(LLDBLog::MixedDebugger); diff --git a/lldb/source/Target/MixedDebugger.cpp b/lldb/source/Target/MixedDebugger.cpp index 3c21ac062592..fb76ba3cfd3c 100644 --- a/lldb/source/Target/MixedDebugger.cpp +++ b/lldb/source/Target/MixedDebugger.cpp @@ -53,35 +53,47 @@ DataExtractorSP MixedDebugger::ExecuteAction(const char* expr, Status &error) { return result; } - const size_t k_max_buf_size = 64; - size_t cstr_len = UINT32_MAX; - size_t offset = 0; - size_t bytes_read = 0; + ValueObjectSP size_vo = expr_value_sp->GetChildAtIndex(0, true); + ValueObjectSP data_ptr_vo = expr_value_sp->GetChildAtIndex(1, true); + + if (!data_ptr_vo || !size_vo) { + error.SetErrorString( + "[ExecuteAction] Failed to get DebugInput members"); + LLDB_LOGF(log, + "[ExecuteAction] struct members missing: data=%p, size=%p", + data_ptr_vo.get(), size_vo.get()); + return result; + } + + size_t payload_len = size_vo->GetValueAsUnsigned(0); + if (payload_len == 0 || payload_len >= UINT32_MAX) { + error.SetErrorString("[ExecuteAction] Invalid payload size"); + LLDB_LOGF(log, "[ExecuteAction] Invalid payload size: %zu", + payload_len); + return result; + } + DataExtractor data; - while ((bytes_read = expr_value_sp->GetPointeeData(data, offset, k_max_buf_size)) > 0) { - const char *cstr = data.PeekCStr(0); - size_t len = strnlen(cstr, k_max_buf_size); - if (len >= cstr_len) { - error.SetErrorString("[MixedDebugger::ExecuteAction] result over size"); - result->Clear(); - return result; - } - if (!result->Append(const_cast(cstr), len)) { - error.SetErrorString("[MixedDebugger::ExecuteAction] result append data failed"); - result->Clear(); - return result; - } - if (len < k_max_buf_size) { - break; - } - cstr_len -= len; - offset += len; + size_t bytes_read = data_ptr_vo->GetPointeeData(data, 0, payload_len); + if (bytes_read < payload_len) { + error.SetErrorString( + "[ExecuteAction] Failed to read data from pointer"); + LLDB_LOGF(log, "[ExecuteAction] Failed to read expected payload_len"); + return result; + } + + if (!result->Append(const_cast(data.GetDataStart()), + payload_len)) { + error.SetErrorString("[ExecuteAction] Failed to append result data"); + LLDB_LOGF(log, "[ExecuteAction] Failed to append result data"); + result->Clear(); + return result; } - // Add terminator to result data - char te = '\0'; - if (!result->Append(&te, 1)) { - error.SetErrorString("[MixedDebugger::ExecuteAction] result append terminator failed"); + char terminator = '\0'; + if (!result->Append(&terminator, 1)) { + error.SetErrorString( + "[MixedDebugger::ExecuteAction] result append terminator failed"); result->Clear(); return result; } @@ -89,6 +101,7 @@ DataExtractorSP MixedDebugger::ExecuteAction(const char* expr, Status &error) { } LLDB_LOGF(log, "[MixedDebugger::ExecuteAction] result is " - "%s", result->PeekCStr(0)); + "%s", + result->PeekCStr(0)); return result; } diff --git a/lldb/test/API/functionalities/mixed_debugger/arkts/Makefile b/lldb/test/API/functionalities/mixed_debugger/arkts/Makefile index 651ab4422319..3ba2879eb39e 100644 --- a/lldb/test/API/functionalities/mixed_debugger/arkts/Makefile +++ b/lldb/test/API/functionalities/mixed_debugger/arkts/Makefile @@ -11,6 +11,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -C_SOURCES = main.c +LD_EXTRAS := -ldl -Wl,-rpath,'$$ORIGIN' + +C_SOURCES := main.c include Makefile.rules + +all: libarkshim.so + +libarkshim.so: $(SRCDIR)/arkshim.c + $(CC) $(CFLAGS) -fPIC -fvisibility=hidden -shared -Wl,-soname,libarkshim.so \ + -o $@ $< + - strip -g $@ # 没有 strip 也不致命,加个 '-' 忽略报错 + +clean:: + $(RM) libarkshim.so diff --git a/lldb/test/API/functionalities/mixed_debugger/arkts/arkshim.c b/lldb/test/API/functionalities/mixed_debugger/arkts/arkshim.c new file mode 100644 index 000000000000..29178e874ffe --- /dev/null +++ b/lldb/test/API/functionalities/mixed_debugger/arkts/arkshim.c @@ -0,0 +1,28 @@ +// arkshim.c +#include +#include + +struct DebugInput { + size_t size; + char *data; +}; + +static const char kBt[] = "This is a ArkTS backtrace"; +static const char kMsg[] = "This is a ArkTS operate debug message result"; + +__attribute__((visibility("default"))) +struct DebugInput GetJsBacktrace(void) { + struct DebugInput out; + out.size = strlen(kBt) + 1; + out.data = (char *)kBt; + return out; +} + +__attribute__((visibility("default"))) +struct DebugInput OperateJsDebugMessage(const char *msg) { + (void)msg; + struct DebugInput out; + out.size = strlen(kMsg) + 1; + out.data = (char *)kMsg; + return out; +} diff --git a/lldb/test/API/functionalities/mixed_debugger/arkts/main.c b/lldb/test/API/functionalities/mixed_debugger/arkts/main.c index 52ebaef7fc45..c0ddbb81e0a1 100644 --- a/lldb/test/API/functionalities/mixed_debugger/arkts/main.c +++ b/lldb/test/API/functionalities/mixed_debugger/arkts/main.c @@ -12,21 +12,34 @@ // limitations under the License. #include +#include +#include +#include +#include static int var = 5; -const char *bt = "This is a ArkTS backtrace"; -const char *msg = "This is a ArkTS operate debug message result"; -const char *GetJsBacktrace() { - return bt; -} +int main(void) { + printf("%p is %d\n", (void*)&var, var); -const char *OperateJsDebugMessage(const char *message) { - return msg; -} + char exe[PATH_MAX] = {0}; + ssize_t n = readlink("/proc/self/exe", exe, sizeof(exe)-1); + if (n > 0) exe[n] = '\0'; else return 1; + + char tmp[PATH_MAX] = {0}; + snprintf(tmp, sizeof(tmp), "%s", exe); + char *dir = dirname(tmp); + + char so_path[PATH_MAX] = {0}; + snprintf(so_path, sizeof(so_path), "%s/libarkshim.so", dir); + + void *h = dlopen(so_path, RTLD_LAZY); + if (!h) { + fprintf(stderr, "dlopen error: %s\n", dlerror()); + } else { + fprintf(stderr, "[DBG] dlopen OK\n"); // break on this line + } -int main () -{ - printf ("%p is %d\n", &var, var); // break on this line + for (volatile int i = 0; i < 1; ++i) {} return ++var; } -- Gitee