• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "dfx_kernel_stack.h"
17 
18 #include <dlfcn.h>
19 #include <fcntl.h>
20 #include <regex>
21 #include <string_ex.h>
22 #include <sys/ioctl.h>
23 #include <unistd.h>
24 
25 #include "dfx_log.h"
26 
27 #define LOGGER_GET_STACK    _IO(0xAB, 9)
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace {
31 // keep sync with defines in kernel/hicollie
32 const char* const BBOX_PATH = "/dev/bbox";
33 const int BUFF_STACK_SIZE = 20 * 1024;
34 const uint32_t MAGIC_NUM = 0x9517;
35 typedef struct HstackVal {
36     uint32_t magic {0};
37     pid_t pid {0};
38     char hstackLogBuff[BUFF_STACK_SIZE] {0};
39 } HstackVal;
40 }
DfxGetKernelStack(int32_t pid,std::string & kernelStack)41 int32_t DfxGetKernelStack(int32_t pid, std::string& kernelStack)
42 {
43     auto kstackBuf = std::make_shared<HstackVal>();
44     if (kstackBuf == nullptr) {
45         DFXLOGW("Failed create HstackVal, pid:%{public}d, errno:%{public}d", pid, errno);
46         return KERNELSTACK_ECREATE;
47     }
48     kstackBuf->pid = pid;
49     kstackBuf->magic = MAGIC_NUM;
50     int fd = open(BBOX_PATH, O_WRONLY | O_CLOEXEC);
51     if (fd < 0) {
52         DFXLOGW("Failed to open bbox, pid:%{public}d, errno:%{public}d", pid, errno);
53         return KERNELSTACK_EOPEN;
54     }
55 
56     int ret = ioctl(fd, LOGGER_GET_STACK, kstackBuf.get());
57     int32_t res = KERNELSTACK_ESUCCESS;
58     if (ret != 0) {
59         DFXLOGW("Failed to get pid(%{public}d) kernel stack, errno:%{public}d", pid, errno);
60         res = KERNELSTACK_EIOCTL;
61     } else {
62         kernelStack = std::string(kstackBuf->hstackLogBuff);
63     }
64     close(fd);
65     return res;
66 }
67 
FormatThreadKernelStack(const std::string & kernelStack,DfxThreadStack & threadStack)68 bool FormatThreadKernelStack(const std::string& kernelStack, DfxThreadStack& threadStack)
69 {
70 #ifdef __aarch64__
71     std::regex headerPattern(R"(name=(.{1,20}), tid=(\d{1,10}), ([\w\=\.]{1,256}, ){3}pid=(\d{1,10}))");
72     std::smatch result;
73     if (!regex_search(kernelStack, result, headerPattern)) {
74         DFXLOGI("search thread name failed");
75         return false;
76     }
77     threadStack.threadName = result[1].str();
78     int base {10};
79     threadStack.tid = strtol(result[2].str().c_str(), nullptr, base); // 2 : second of searched element
80     auto pos = kernelStack.rfind("pid=" + result[result.size() - 1].str());
81     if (pos == std::string::npos) {
82         return false;
83     }
84     size_t index = 0;
85     std::regex framePattern(R"(\[(\w{16})\]\<[\w\?+/]{1,1024}\> \(([\w\-./]{1,1024})\))");
86     for (std::sregex_iterator it = std::sregex_iterator(kernelStack.begin() + pos, kernelStack.end(), framePattern);
87         it != std::sregex_iterator(); ++it) {
88         if ((*it)[2].str().rfind(".elf") != std::string::npos) { // 2 : second of searched element is map name
89             continue;
90         }
91         DfxFrame frame;
92         frame.index = index++;
93         base = 16; // 16 : Hexadecimal
94         frame.relPc = strtoull((*it)[1].str().c_str(), nullptr, base);
95         frame.mapName = (*it)[2].str(); // 2 : second of searched element is map name
96         threadStack.frames.emplace_back(frame);
97     }
98     return true;
99 #else
100     return false;
101 #endif
102 }
103 
FormatProcessKernelStack(const std::string & kernelStack,std::vector<DfxThreadStack> & processStack)104 bool FormatProcessKernelStack(const std::string& kernelStack, std::vector<DfxThreadStack>& processStack)
105 {
106 #if !defined(is_ohos_lite) && defined(__aarch64__)
107     std::vector<std::string> threadKernelStackVec;
108     OHOS::SplitStr(kernelStack, "Thread info:", threadKernelStackVec);
109     if (threadKernelStackVec.size() == 1) {
110         return false;
111     }
112     for (const std::string& threadKernelStack : threadKernelStackVec) {
113         DfxThreadStack threadStack;
114         if (FormatThreadKernelStack(threadKernelStack, threadStack)) {
115             processStack.emplace_back(threadStack);
116         }
117     }
118     return true;
119 #else
120     return false;
121 #endif
122 }
123 }
124 }