1 /* 2 * Copyright (c) 2021 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 PANDA_LIBPANDABASE_OS_STACKTRACE_H_ 17 #define PANDA_LIBPANDABASE_OS_STACKTRACE_H_ 18 19 #include <vector> 20 #include <iostream> 21 #include "macros.h" 22 23 namespace panda { 24 25 class StackPrinterImpl; 26 27 /* 28 * Return stack trace as a vector of PCs 29 * 30 * Use std::vector instead of PandaVector to have ability 31 * to print stack traces in internal allocator. 32 * Since PandaVector uses internal allocator it leads to 33 * infinite recursion. 34 */ 35 std::vector<uintptr_t> GetStacktrace(); 36 37 /* 38 * Print stack trace provided into 'Print' function. 39 * The class caches information. So it is aimed to be used 40 * to print multiple stack traces. 41 */ 42 std::ostream &PrintStack(const std::vector<uintptr_t> &stacktrace, std::ostream &out); 43 44 /* 45 * Print stack trace 46 */ 47 // NOLINTNEXTLINE(misc-definitions-in-headers) PrintStack(std::ostream & out)48inline std::ostream &PrintStack(std::ostream &out) 49 { 50 return PrintStack(GetStacktrace(), out); 51 } 52 53 } // namespace panda 54 55 #endif // PANDA_LIBPANDABASE_OS_STACKTRACE_H_ 56