From 20796f181d4b6bfa9e8d11186882167da774a262 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 ++++++++++++++--------- 2 files changed, 45 insertions(+), 31 deletions(-) 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; } -- Gitee