1 /* 2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_ABILITY_RUNTIME_MIX_STACK_DUMPER_H 17 #define OHOS_ABILITY_RUNTIME_MIX_STACK_DUMPER_H 18 19 #include <signal.h> 20 #include <unistd.h> 21 #include <vector> 22 23 #include "catchframe_local.h" 24 #include "event_handler.h" 25 #include "ohos_application.h" 26 #include "runtime.h" 27 28 namespace OHOS { 29 namespace AppExecFwk { 30 class MixStackDumper { 31 public: 32 MixStackDumper() = default; 33 ~MixStackDumper() = default; 34 void InstallDumpHandler(std::shared_ptr<OHOSApplication> application, 35 std::shared_ptr<EventHandler> handler); 36 static std::string GetMixStack(bool onlyMainThread); 37 static bool IsInstalled(); 38 static bool Dump_SignalHandler(int sig, siginfo_t *si, void *context); 39 40 private: 41 void Init(pid_t pid); 42 void Destroy(); 43 bool DumpMixFrame(int fd, pid_t nstid, pid_t tid); 44 bool IsJsNativePcEqual(uintptr_t *jsNativePointer, uint64_t nativePc, uint64_t nativeOffset); 45 void BuildJsNativeMixStack(int fd, std::vector<JsFrames>& jsFrames, 46 std::vector<OHOS::HiviewDFX::DfxFrame>& nativeFrames); 47 std::string GetThreadStackTraceLabel(pid_t tid); 48 void PrintProcessHeader(int fd, pid_t pid, uid_t uid); 49 void Write(int fd, const std::string& outStr); 50 std::string DumpMixStackLocked(int fd, pid_t tid); 51 52 static void HandleMixDumpRequest(); 53 54 private: 55 static std::weak_ptr<EventHandler> signalHandler_; 56 static std::weak_ptr<OHOSApplication> application_; 57 std::unique_ptr<OHOS::HiviewDFX::DfxCatchFrameLocal> catcher_{nullptr}; 58 std::string outputStr_; 59 }; 60 } // AppExecFwk 61 } // OHOS 62 #endif // OHOS_ABILITY_RUNTIME_MIX_STACK_DUMPER_H 63