• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 DFX_ASYNC_KERNEL_STACK_H
17 #define DFX_ASYNC_KERNEL_STACK_H
18 
19 #include <atomic>
20 #include <cinttypes>
21 #include <future>
22 #include <string>
23 #include <utility>
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 
28 class KernelStackAsyncCollector {
29 public:
30     enum ErrorCode : int32_t {
31         STACK_SUCCESS = 0,
32         STACK_ECREATE,
33         STACK_EOPEN,
34         STACK_EIOCTL,
35         STACK_TIMEOUT,
36         STACK_DEFERRED,
37         STACK_OVER_LIMIT,
38         STACK_NO_PROCESS,
39         STACK_UNKNOWN,
40     };
41     struct KernelResult {
42         ErrorCode errorCode = STACK_UNKNOWN;
43         std::string msg = "";
44         uint32_t threadCount = 0;
45         KernelResult() = default;
KernelResultKernelResult46         explicit KernelResult(ErrorCode errorCode) : errorCode(errorCode) {}
KernelResultKernelResult47         KernelResult(ErrorCode errorCode, uint32_t threadCount) : errorCode(errorCode), threadCount(threadCount) {}
KernelResultKernelResult48         KernelResult(ErrorCode errorCode, std::string msg, uint32_t threadCount)
49             : errorCode(errorCode), msg(msg), threadCount(threadCount) {}
50     };
51 
52     KernelResult GetProcessStackWithTimeout(int pid, uint32_t timeoutMs) const;
53 
54     bool NotifyStartCollect(int pid);
55     KernelResult GetCollectedStackResult();
56 private:
57     static void CollectKernelStackTask(int pid, std::promise<KernelResult> result);
58     static bool CheckProcessValid(int pid);
59     static ErrorCode ToErrCode(int kernelErr);
60 
61     static constexpr int maxAsyncTaskNum_ = 2;
62     static std::atomic<int> asyncCount_;
63 
64     std::future<KernelResult> stackFuture_;
65 };
66 
67 } // namespace HiviewDFX
68 } // namespace OHOS
69 #endif