diff --git a/bundle.json b/bundle.json index 9b8b4564928f9c534b217a64a930357890c3c3dc..20d87c593f544a70ef7e2eee22f5d60455c03120 100644 --- a/bundle.json +++ b/bundle.json @@ -34,6 +34,7 @@ "hitrace", "init", "ipc", + "jsoncpp", "libuv", "lzma", "samgr", diff --git a/example/BUILD.gn b/example/BUILD.gn index e759c02b501cda02a5c6eae2ae2335df70a47d01..46fc0c8793a88237db9719f7990c0b2c68744861 100644 --- a/example/BUILD.gn +++ b/example/BUILD.gn @@ -67,6 +67,7 @@ if (defined(ohos_lite)) { external_deps = [ "c_utils:utils", "hilog:libhilog", + "jsoncpp:jsoncpp", ] part_name = "faultloggerd" diff --git a/interfaces/innerkits/formatter/BUILD.gn b/interfaces/innerkits/formatter/BUILD.gn index 710e0230e9d9a6d48429d29184e67fb526f899bc..c7a8b71fd623a7d9f536227c4488e89f11f06c9f 100644 --- a/interfaces/innerkits/formatter/BUILD.gn +++ b/interfaces/innerkits/formatter/BUILD.gn @@ -36,7 +36,7 @@ if (defined(ohos_lite)) { version_script = "libjson_stack_formatter.map" external_deps = [ "bounds_checking_function:libsec_shared", - "cJSON:cjson", + "jsoncpp:jsoncpp", ] install_images = [ diff --git a/interfaces/innerkits/formatter/dfx_json_formatter.cpp b/interfaces/innerkits/formatter/dfx_json_formatter.cpp index 94d7db770a7c2ccc093f6b3d73fa493aff88840b..ac349891bac541a3055b61ea8456da37a51cb195 100644 --- a/interfaces/innerkits/formatter/dfx_json_formatter.cpp +++ b/interfaces/innerkits/formatter/dfx_json_formatter.cpp @@ -19,7 +19,7 @@ #include #include "dfx_kernel_stack.h" #ifndef is_ohos_lite -#include "cJSON.h" +#include "json/json.h" #endif namespace OHOS { @@ -27,39 +27,7 @@ namespace HiviewDFX { #ifndef is_ohos_lite namespace { const int FRAME_BUF_LEN = 1024; -static bool IsConvertToString(const cJSON *json) -{ - if (json == nullptr) { - return false; - } - return cJSON_IsString(json) || cJSON_IsNumber(json) || cJSON_IsBool(json) || cJSON_IsNull(json); -} - -static std::string GetStringValueFromItem(const cJSON *item) -{ - std::string ret{}; - if (cJSON_IsString(item)) { - ret = item->valuestring; - } else if (cJSON_IsNumber(item)) { - ret = std::to_string(item->valueint); - } else if (cJSON_IsBool(item)) { - ret = cJSON_IsTrue(item) ? "true" : "false"; - } else { - ret = ""; - } - return ret; -} - -static std::string GetStringValue(const cJSON *json, const std::string& key) -{ - if (!cJSON_IsObject(json)) { - return ""; - } - cJSON *item = cJSON_GetObjectItemCaseSensitive(json, key.c_str()); - return GetStringValueFromItem(item); -} - -static bool FormatJsFrame(const cJSON *frames, const uint32_t& frameIdx, std::string& outStr) +static bool FormatJsFrame(const Json::Value& frames, const uint32_t& frameIdx, std::string& outStr) { const int jsIdxLen = 10; char buf[jsIdxLen] = { 0 }; @@ -68,40 +36,32 @@ static bool FormatJsFrame(const cJSON *frames, const uint32_t& frameIdx, std::st return false; } outStr = std::string(buf); - cJSON *item = cJSON_GetArrayItem(frames, frameIdx); - if (item == nullptr) { - return false; - } - std::string symbol = GetStringValue(item, "symbol"); + std::string symbol = frames[frameIdx]["symbol"].asString(); if (!symbol.empty()) { outStr.append(" " + symbol); } - std::string packageName = GetStringValue(item, "packageName"); + std::string packageName = frames[frameIdx]["packageName"].asString(); if (!packageName.empty()) { outStr.append(" " + packageName); } - std::string file = GetStringValue(item, "file"); + std::string file = frames[frameIdx]["file"].asString(); if (!file.empty()) { - std::string line = GetStringValue(item, "line"); - std::string column = GetStringValue(item, "column"); + std::string line = frames[frameIdx]["line"].asString(); + std::string column = frames[frameIdx]["column"].asString(); outStr.append(" (" + file + ":" + line + ":" + column + ")"); } return true; } -static bool FormatNativeFrame(const cJSON *frames, const uint32_t& frameIdx, std::string& outStr) +static bool FormatNativeFrame(const Json::Value& frames, const uint32_t& frameIdx, std::string& outStr) { char buf[FRAME_BUF_LEN] = {0}; char format[] = "#%02u pc %s %s"; - cJSON *item = cJSON_GetArrayItem(frames, frameIdx); - if (item == nullptr) { - return false; - } - std::string buildId = GetStringValue(item, "buildId"); - std::string file = GetStringValue(item, "file"); - std::string offset = GetStringValue(item, "offset"); - std::string pc = GetStringValue(item, "pc"); - std::string symbol = GetStringValue(item, "symbol"); + std::string buildId = frames[frameIdx]["buildId"].asString(); + std::string file = frames[frameIdx]["file"].asString(); + std::string offset = frames[frameIdx]["offset"].asString(); + std::string pc = frames[frameIdx]["pc"].asString(); + std::string symbol = frames[frameIdx]["symbol"].asString(); if (snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format, frameIdx, pc.c_str(), file.empty() ? "Unknown" : file.c_str()) <= 0) { return false; @@ -119,28 +79,25 @@ static bool FormatNativeFrame(const cJSON *frames, const uint32_t& frameIdx, std bool DfxJsonFormatter::FormatJsonStack(const std::string& jsonStack, std::string& outStackStr) { - cJSON *root = cJSON_Parse(jsonStack.c_str()); - if (root == nullptr) { + Json::Reader reader; + Json::Value threads; + if (!(reader.parse(jsonStack, threads))) { outStackStr.append("Failed to parse json stack info."); return false; } - cJSON *item; - cJSON_ArrayForEach(item, root) { + for (uint32_t i = 0; i < threads.size(); ++i) { std::string ss; - cJSON *thread_tid = cJSON_GetObjectItemCaseSensitive(item, "tid"); - cJSON *thread_name = cJSON_GetObjectItemCaseSensitive(item, "thread_name"); - if ((IsConvertToString(thread_tid)) && (IsConvertToString(thread_name))) { - ss += "Tid:" + GetStringValueFromItem(thread_tid) + - ", Name:" + GetStringValueFromItem(thread_name) + "\n"; + Json::Value thread = threads[i]; + if (thread["tid"].isConvertibleTo(Json::stringValue) && + thread["thread_name"].isConvertibleTo(Json::stringValue)) { + ss += "Tid:" + thread["tid"].asString() + ", Name:" + thread["thread_name"].asString() + "\n"; } - const cJSON *frames = cJSON_GetObjectItemCaseSensitive(item, "frames"); - int frameSize = cJSON_GetArraySize(frames); - for (int j = 0; j < frameSize; ++j) { + const Json::Value frames = thread["frames"]; + for (uint32_t j = 0; j < frames.size(); ++j) { std::string frameStr = ""; bool formatStatus = false; - std::string line = GetStringValue(cJSON_GetArrayItem(frames, j), "line"); - if (line.empty()) { + if (frames[j]["line"].asString().empty()) { formatStatus = FormatNativeFrame(frames, j, frameStr); } else { formatStatus = FormatJsFrame(frames, j, frameStr); @@ -150,13 +107,11 @@ bool DfxJsonFormatter::FormatJsonStack(const std::string& jsonStack, std::string } else { // Shall we try to print more information? outStackStr.append("Frame info is illegal."); - cJSON_Delete(root); return false; } } outStackStr.append(ss); } - cJSON_Delete(root); return true; } @@ -184,58 +139,35 @@ static bool FormatKernelStackStr(const std::vector& processStack return true; } -static void AddItemToKernelStack(const DfxThreadStack& threadStack, cJSON *jsonInfo) -{ - cJSON *threadInfo = cJSON_CreateObject(); - if (threadInfo == nullptr) { - return; - } - cJSON_AddStringToObject(threadInfo, "thread_name", threadStack.threadName.c_str()); - cJSON_AddNumberToObject(threadInfo, "tid", threadStack.tid); - cJSON *frames = cJSON_CreateArray(); - if (frames == nullptr) { - cJSON_Delete(threadInfo); - return; - } - for (const auto& frame : threadStack.frames) { - cJSON *frameJson = cJSON_CreateObject(); - if (frameJson == nullptr) { - continue; - } - char buf[FRAME_BUF_LEN] = {0}; - char format[] = "%016" PRIx64; - if (snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format, frame.relPc) <= 0) { - cJSON_Delete(frameJson); - continue; - } - cJSON_AddStringToObject(frameJson, "pc", buf); - cJSON_AddStringToObject(frameJson, "symbol", ""); - cJSON_AddNumberToObject(frameJson, "offset", 0); - std::string file = frame.mapName.empty() ? "Unknown" : frame.mapName; - cJSON_AddStringToObject(frameJson, "file", file.c_str()); - cJSON_AddStringToObject(frameJson, "buildId", ""); - cJSON_AddItemToArray(frames, frameJson); - } - cJSON_AddItemToObject(threadInfo, "frames", frames); - cJSON_AddItemToArray(jsonInfo, threadInfo); -} - static bool FormatKernelStackJson(std::vector processStack, std::string& formattedStack) { if (processStack.empty()) { return false; } - cJSON *jsonInfo = cJSON_CreateArray(); - if (jsonInfo == nullptr) { - return false; - } + Json::Value jsonInfo; for (const auto &threadStack : processStack) { - AddItemToKernelStack(threadStack, jsonInfo); + Json::Value threadInfo; + threadInfo["thread_name"] = threadStack.threadName; + threadInfo["tid"] = threadStack.tid; + Json::Value frames(Json::arrayValue); + for (const auto& frame : threadStack.frames) { + Json::Value frameJson; + char buf[FRAME_BUF_LEN] = {0}; + char format[] = "%016" PRIx64; + if (snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, format, frame.relPc) <= 0) { + continue; + } + frameJson["pc"] = std::string(buf); + frameJson["symbol"] = ""; + frameJson["offset"] = 0; + frameJson["file"] = frame.mapName.empty() ? "Unknown" : frame.mapName; + frameJson["buildId"] = ""; + frames.append(frameJson); + } + threadInfo["frames"] = frames; + jsonInfo.append(threadInfo); } - auto itemStr = cJSON_PrintUnformatted(jsonInfo); - formattedStack = itemStr; - cJSON_free(itemStr); - cJSON_Delete(jsonInfo); + formattedStack = Json::FastWriter().write(jsonInfo); return true; } #endif diff --git a/test/fuzztest/faultloggerdserver_fuzzer/BUILD.gn b/test/fuzztest/faultloggerdserver_fuzzer/BUILD.gn index de088ca079abda8139449ca7187c212f1acacecc..f1ec0f304d79a135b91b130f58f3d64ae25dbf27 100644 --- a/test/fuzztest/faultloggerdserver_fuzzer/BUILD.gn +++ b/test/fuzztest/faultloggerdserver_fuzzer/BUILD.gn @@ -64,6 +64,7 @@ if (defined(ohos_lite)) { "hisysevent:libhisysevent", "hitrace:hitrace_meter", "init:libbegetutil", + "jsoncpp:jsoncpp", ] } diff --git a/test/unittest/dump_catcher/BUILD.gn b/test/unittest/dump_catcher/BUILD.gn index c7822e3b0d0091ccfe567b5c1f34c63da5c43ea5..e3886a8f864e5f9a3de19f8d6e77b305a6e6496b 100644 --- a/test/unittest/dump_catcher/BUILD.gn +++ b/test/unittest/dump_catcher/BUILD.gn @@ -101,6 +101,7 @@ if (defined(ohos_lite)) { "c_utils:utils", "googletest:gtest_main", "hilog:libhilog", + "jsoncpp:jsoncpp", ] } diff --git a/test/unittest/faultloggerd/BUILD.gn b/test/unittest/faultloggerd/BUILD.gn index 593838c8f3246463da74fd0724cfc76c08621289..2355a427bd926bb390b0b33d50c56537464339bc 100644 --- a/test/unittest/faultloggerd/BUILD.gn +++ b/test/unittest/faultloggerd/BUILD.gn @@ -143,6 +143,7 @@ if (defined(ohos_lite)) { "hisysevent:libhisysevent", "hitrace:hitrace_meter", "init:libbegetutil", + "jsoncpp:jsoncpp", ] if (build_selinux) { diff --git a/test/unittest/process_dump/BUILD.gn b/test/unittest/process_dump/BUILD.gn index 4a76ad7c687b18142d58e78d8a41c5f78f91e006..e3cc1ebeb270ea8803c17e9cc11ba1d486c76ddf 100644 --- a/test/unittest/process_dump/BUILD.gn +++ b/test/unittest/process_dump/BUILD.gn @@ -130,6 +130,7 @@ if (defined(ohos_lite)) { "hitrace:libhitracechain", "ipc:ipc_core", "samgr:samgr_proxy", + "jsoncpp:jsoncpp", ] if (support_jsapi) { deps += [ "$faultloggerd_path/test/resource:FaultloggerdJsTest" ] diff --git a/tools/process_dump/BUILD.gn b/tools/process_dump/BUILD.gn index 45b6dfdabf81805044410e994057a71ae2ba55bb..4d7b80083e088afd610cff426d03c456b7940470 100644 --- a/tools/process_dump/BUILD.gn +++ b/tools/process_dump/BUILD.gn @@ -164,6 +164,7 @@ if (defined(ohos_lite)) { "init:libbegetutil", "ipc:ipc_core", "samgr:samgr_proxy", + "jsoncpp:jsoncpp", ] part_name = "faultloggerd" @@ -215,6 +216,7 @@ if (defined(ohos_lite)) { "init:libbegetutil", "ipc:ipc_core", "samgr:samgr_proxy", + "jsoncpp:jsoncpp", ] version_script = "processdump.map" install_images = [ diff --git a/tools/process_dump/dump_info_json_formatter.cpp b/tools/process_dump/dump_info_json_formatter.cpp index 8cb2a04111b6596a8c9abca9a2fd854de3e9975f..df1df1fb534d21f4fed5e87b3fd124ac1b59fb49 100644 --- a/tools/process_dump/dump_info_json_formatter.cpp +++ b/tools/process_dump/dump_info_json_formatter.cpp @@ -35,7 +35,7 @@ bool DumpInfoJsonFormatter::GetJsonFormatInfo(const ProcessDumpRequest& request, std::string& jsonStringInfo) { #ifndef is_ohos_lite - cJSON *jsonInfo = nullptr; + Json::Value jsonInfo; switch (request.type) { case ProcessDumpType::DUMP_TYPE_CPP_CRASH: jsonInfo = cJSON_CreateObject(); @@ -56,105 +56,68 @@ bool DumpInfoJsonFormatter::GetJsonFormatInfo(const ProcessDumpRequest& request, default: return false; } - auto itemStr = cJSON_PrintUnformatted(jsonInfo); - jsonStringInfo = itemStr; - cJSON_free(itemStr); - cJSON_Delete(jsonInfo); + jsonStringInfo.append(Json::FastWriter().write(jsonInfo)); return true; #endif return false; } #ifndef is_ohos_lite -bool DumpInfoJsonFormatter::AddItemToJsonInfo(const ProcessDumpRequest& request, DfxProcess& process, - cJSON *jsonInfo) +void DumpInfoJsonFormatter::GetCrashJsonFormatInfo(const ProcessDumpRequest& request, DfxProcess& process, + Json::Value& jsonInfo) { + jsonInfo["time"] = request.timeStamp; + jsonInfo["uuid"] = ""; + jsonInfo["crash_type"] = "NativeCrash"; + jsonInfo["pid"] = process.GetProcessInfo().pid; + jsonInfo["uid"] = process.GetProcessInfo().uid; + jsonInfo["app_running_unique_id"] = request.appRunningId; + DfxSignal dfxSignal(request.siginfo.si_signo); - cJSON *signal = cJSON_CreateObject(); - if (!signal) { - DFXLOGE("Create cJson signal object failed."); - return false; - } - cJSON_AddNumberToObject(signal, "signo", request.siginfo.si_signo); - cJSON_AddNumberToObject(signal, "code", request.siginfo.si_code); - std::string address = dfxSignal.IsAddrAvailable() ? + Json::Value signal; + signal["signo"] = request.siginfo.si_signo; + signal["code"] = request.siginfo.si_code; + signal["address"] = dfxSignal.IsAddrAvailable() ? StringPrintf("%" PRIX64_ADDR, reinterpret_cast(request.siginfo.si_addr)) : ""; - cJSON_AddStringToObject(signal, "address", address.c_str()); - cJSON *exception = cJSON_CreateObject(); - if (!exception) { - DFXLOGE("Create cJson exception object failed."); - cJSON_Delete(signal); - return false; - } - cJSON_AddItemToObject(exception, "signal", signal); - cJSON_AddStringToObject(exception, "message", process.GetFatalMessage().c_str()); - - cJSON *frames = cJSON_CreateArray(); - if (!frames) { - DFXLOGE("Create cJson exception array failed."); - cJSON_Delete(exception); - return false; - } + Json::Value exception; + exception["signal"] = signal; + exception["message"] = process.GetFatalMessage(); + + Json::Value frames(Json::arrayValue); if (process.GetKeyThread() == nullptr) { - cJSON_AddStringToObject(exception, "thread_name", ""); - cJSON_AddNumberToObject(exception, "tid", 0); + exception["thread_name"] = ""; + exception["tid"] = 0; } else { - cJSON_AddStringToObject(exception, "thread_name", process.GetKeyThread()->GetThreadInfo().threadName.c_str()); - cJSON_AddNumberToObject(exception, "tid", process.GetKeyThread()->GetThreadInfo().tid); + exception["thread_name"] = process.GetKeyThread()->GetThreadInfo().threadName; + exception["tid"] = process.GetKeyThread()->GetThreadInfo().tid; FillFramesJson(process.GetKeyThread()->GetFrames(), frames); } - cJSON_AddItemToObject(exception, "frames", frames); - cJSON_AddItemToObject(jsonInfo, "exception", exception); - return true; -} - -void DumpInfoJsonFormatter::GetCrashJsonFormatInfo(const ProcessDumpRequest& request, DfxProcess& process, - cJSON *jsonInfo) -{ - cJSON_AddNumberToObject(jsonInfo, "time", request.timeStamp); - cJSON_AddStringToObject(jsonInfo, "uuid", ""); - cJSON_AddStringToObject(jsonInfo, "crash_type", "NativeCrash"); - cJSON_AddNumberToObject(jsonInfo, "pid", process.GetProcessInfo().pid); - cJSON_AddNumberToObject(jsonInfo, "uid", process.GetProcessInfo().uid); - cJSON_AddStringToObject(jsonInfo, "app_running_unique_id", request.appRunningId); - - if (!AddItemToJsonInfo(request, process, jsonInfo)) { - return; - } + exception["frames"] = frames; + jsonInfo["exception"] = exception; // fill other thread info const auto& otherThreads = process.GetOtherThreads(); if (otherThreads.size() > 0) { - cJSON *threadsJsonArray = cJSON_CreateArray(); - if (!threadsJsonArray) { - DFXLOGE("Create cJson threadsJsonArray array failed."); - return; - } + Json::Value threadsJsonArray(Json::arrayValue); AppendThreads(otherThreads, threadsJsonArray); - cJSON_AddItemToObject(jsonInfo, "threads", threadsJsonArray); + jsonInfo["threads"] = threadsJsonArray; } } -void DumpInfoJsonFormatter::GetDumpJsonFormatInfo(DfxProcess& process, cJSON *jsonInfo) +void DumpInfoJsonFormatter::GetDumpJsonFormatInfo(DfxProcess& process, Json::Value& jsonInfo) { - cJSON *thread = cJSON_CreateObject(); - cJSON *frames = cJSON_CreateArray(); - if (!thread || !frames) { - DFXLOGE("Create cJson thread or frames failed."); - cJSON_Delete(thread); - cJSON_Delete(frames); - return; - } + Json::Value thread; + Json::Value frames(Json::arrayValue); if (process.GetKeyThread() == nullptr) { - cJSON_AddStringToObject(thread, "thread_name", ""); - cJSON_AddNumberToObject(thread, "tid", 0); + thread["thread_name"] = ""; + thread["tid"] = 0; } else { - cJSON_AddStringToObject(thread, "thread_name", process.GetKeyThread()->GetThreadInfo().threadName.c_str()); - cJSON_AddNumberToObject(thread, "tid", process.GetKeyThread()->GetThreadInfo().tid); + thread["thread_name"] = process.GetKeyThread()->GetThreadInfo().threadName; + thread["tid"] = process.GetKeyThread()->GetThreadInfo().tid; FillFramesJson(process.GetKeyThread()->GetFrames(), frames); } - cJSON_AddItemToObject(thread, "frames", frames); - cJSON_AddItemToArray(jsonInfo, thread); + thread["frames"] = frames; + jsonInfo.append(thread); // fill other thread info const auto& otherThreads = process.GetOtherThreads(); @@ -164,31 +127,22 @@ void DumpInfoJsonFormatter::GetDumpJsonFormatInfo(DfxProcess& process, cJSON *js } void DumpInfoJsonFormatter::AppendThreads(const std::vector>& threads, - cJSON *jsonInfo) const + Json::Value& jsonInfo) const { for (auto const& oneThread : threads) { if (oneThread != nullptr) { - cJSON *threadJson = cJSON_CreateObject(); - if (!threadJson) { - DFXLOGE("Create cJson threadJson object failed, continue."); - continue; - } - cJSON_AddStringToObject(threadJson, "thread_name", oneThread->GetThreadInfo().threadName.c_str()); - cJSON_AddNumberToObject(threadJson, "tid", oneThread->GetThreadInfo().tid); - cJSON *framesJson = cJSON_CreateArray(); - if (!framesJson) { - DFXLOGE("Create cJson framesJson array failed, continue."); - cJSON_Delete(threadJson); - continue; - } + Json::Value threadJson; + threadJson["thread_name"] = oneThread->GetThreadInfo().threadName; + threadJson["tid"] = oneThread->GetThreadInfo().tid; + Json::Value framesJson(Json::arrayValue); FillFramesJson(oneThread->GetFrames(), framesJson); - cJSON_AddItemToObject(threadJson, "frames", framesJson); - cJSON_AddItemToArray(jsonInfo, threadJson); + threadJson["frames"] = framesJson; + jsonInfo.append(threadJson); } } } -bool DumpInfoJsonFormatter::FillFramesJson(const std::vector& frames, cJSON *jsonInfo) const +bool DumpInfoJsonFormatter::FillFramesJson(const std::vector& frames, Json::Value& jsonInfo) const { for (const auto& frame : frames) { if (frame.isJsFrame) { @@ -205,45 +159,36 @@ bool DumpInfoJsonFormatter::FillFramesJson(const std::vector& frames, return true; } -void DumpInfoJsonFormatter::FillJsFrameJson(const DfxFrame& frame, cJSON *jsonInfo) const +void DumpInfoJsonFormatter::FillJsFrameJson(const DfxFrame& frame, Json::Value& jsonInfo) const { - cJSON *frameJson = cJSON_CreateObject(); - if (!frameJson) { - DFXLOGE("Create cJson frameJson object failed."); - return; - } - cJSON_AddStringToObject(frameJson, "file", frame.mapName.c_str()); - cJSON_AddStringToObject(frameJson, "packageName", frame.packageName.c_str()); - cJSON_AddStringToObject(frameJson, "symbol", frame.funcName.c_str()); - cJSON_AddNumberToObject(frameJson, "line", frame.line); - cJSON_AddNumberToObject(frameJson, "column", frame.column); - cJSON_AddItemToArray(jsonInfo, frameJson); + Json::Value frameJson; + frameJson["file"] = frame.mapName; + frameJson["packageName"] = frame.packageName; + frameJson["symbol"] = frame.funcName; + frameJson["line"] = frame.line; + frameJson["column"] = frame.column; + jsonInfo.append(frameJson); } -void DumpInfoJsonFormatter::FillNativeFrameJson(const DfxFrame& frame, cJSON *jsonInfo) const +void DumpInfoJsonFormatter::FillNativeFrameJson(const DfxFrame& frame, Json::Value& jsonInfo) const { - cJSON *frameJson = cJSON_CreateObject(); - if (!frameJson) { - DFXLOGE("Create cJson frameJson object failed."); - return; - } + Json::Value frameJson; #ifdef __LP64__ - std::string pc = StringPrintf("%016lx", frame.relPc); + frameJson["pc"] = StringPrintf("%016lx", frame.relPc); #else - std::string pc = StringPrintf("%08llx", frame.relPc); + frameJson["pc"] = StringPrintf("%08llx", frame.relPc); #endif - cJSON_AddStringToObject(frameJson, "pc", pc.c_str()); if (frame.funcName.length() > MAX_FUNC_NAME_LEN) { DFXLOGD("length of funcName greater than 256 byte, do not report it"); - cJSON_AddStringToObject(frameJson, "symbol", ""); + frameJson["symbol"] = ""; } else { - cJSON_AddStringToObject(frameJson, "symbol", frame.funcName.c_str()); + frameJson["symbol"] = frame.funcName; } - cJSON_AddNumberToObject(frameJson, "offset", frame.funcOffset); + frameJson["offset"] = frame.funcOffset; std::string strippedMapName = DfxMap::UnFormatMapName(frame.mapName); - cJSON_AddStringToObject(frameJson, "file", strippedMapName.c_str()); - cJSON_AddStringToObject(frameJson, "buildId", frame.buildId.c_str()); - cJSON_AddItemToArray(jsonInfo, frameJson); + frameJson["file"] = strippedMapName; + frameJson["buildId"] = frame.buildId; + jsonInfo.append(frameJson); } #endif } // namespace HiviewDFX diff --git a/tools/process_dump/dump_info_json_formatter.h b/tools/process_dump/dump_info_json_formatter.h index 71a7cdf89c993287be1e9c495e5bed7d2e62ca6f..1aecbdb889db20836f7a6e851b016907ac60ad4c 100644 --- a/tools/process_dump/dump_info_json_formatter.h +++ b/tools/process_dump/dump_info_json_formatter.h @@ -22,7 +22,7 @@ #include "dfx_dump_request.h" #include "dfx_process.h" #ifndef is_ohos_lite -#include "cJSON.h" +#include "json/json.h" #endif namespace OHOS { @@ -35,13 +35,12 @@ public: private: #ifndef is_ohos_lite - bool AddItemToJsonInfo(const ProcessDumpRequest& request, DfxProcess& process, cJSON *jsonInfo); - void GetCrashJsonFormatInfo(const ProcessDumpRequest& request, DfxProcess& process, cJSON *jsonInfo); - void GetDumpJsonFormatInfo(DfxProcess& process, cJSON *jsonInfo); - void AppendThreads(const std::vector>& threads, cJSON *jsonInfo) const; - bool FillFramesJson(const std::vector& frames, cJSON *jsonInfo) const; - void FillJsFrameJson(const DfxFrame& frame, cJSON *jsonInfo) const; - void FillNativeFrameJson(const DfxFrame& frame, cJSON *jsonInfo) const; + void GetCrashJsonFormatInfo(const ProcessDumpRequest& request, DfxProcess& process, Json::Value& jsonInfo); + void GetDumpJsonFormatInfo(DfxProcess& process, Json::Value& jsonInfo); + void AppendThreads(const std::vector>& threads, Json::Value& jsonInfo) const; + bool FillFramesJson(const std::vector& frames, Json::Value& jsonInfo) const; + void FillJsFrameJson(const DfxFrame& frame, Json::Value& jsonInfo) const; + void FillNativeFrameJson(const DfxFrame& frame, Json::Value& jsonInfo) const; #endif }; } // namespace HiviewDFX