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 #include "thread_sampler_api.h"
16
17 #include <cstring>
18
19 namespace OHOS {
20 namespace HiviewDFX {
ThreadSamplerInit(int collectStackCount)21 int ThreadSamplerInit(int collectStackCount)
22 {
23 return ThreadSampler::GetInstance().Init(collectStackCount) ? SUCCESS : FAIL;
24 }
25
ThreadSamplerSample()26 int32_t ThreadSamplerSample()
27 {
28 return ThreadSampler::GetInstance().Sample();
29 }
30
ThreadSamplerCollect(char * stack,size_t size,int treeFormat)31 int ThreadSamplerCollect(char* stack, size_t size, int treeFormat)
32 {
33 bool enableTreeFormat = (treeFormat == 1);
34 std::string stk;
35 int success = (ThreadSampler::GetInstance().CollectStack(stk, enableTreeFormat) ? SUCCESS : FAIL);
36 size_t len = stk.size();
37 if (strncpy_s(stack, size, stk.c_str(), len + 1) != EOK) {
38 return FAIL;
39 }
40 return success;
41 }
42
ThreadSamplerDeinit()43 int ThreadSamplerDeinit()
44 {
45 return ThreadSampler::GetInstance().Deinit() ? SUCCESS : FAIL;
46 }
47
ThreadSamplerSigHandler(int sig,siginfo_t * si,void * context)48 void ThreadSamplerSigHandler(int sig, siginfo_t* si, void* context)
49 {
50 ThreadSampler::ThreadSamplerSignalHandler(sig, si, context);
51 }
52 }
53 }
54