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 #include "kernel_snapshot_kernel_frame.h"
17
18 namespace OHOS {
19 namespace HiviewDFX {
Parse(const std::string & line)20 void KernelFrame::Parse(const std::string& line)
21 {
22 size_t pos = 0;
23 pc = ExtractContent(line, pos, '[', ']');
24 fp = ExtractContent(line, ++pos, '[', ']');
25 funcNameOffset = ExtractContent(line, ++pos, '<', '>');
26 elf = ExtractContent(line, ++pos, '(', ')');
27 }
28
ToString(int count) const29 std::string KernelFrame::ToString(int count) const
30 {
31 return std::string("#") + (count < 10 ? "0" : "") + // 10 : add leading 0 if less than 10
32 std::to_string(count) + " pc " + pc + " " + elf;
33 }
34
ExtractContent(const std::string & line,size_t & pos,char startChar,char endChar)35 std::string KernelFrame::ExtractContent(const std::string& line, size_t& pos, char startChar, char endChar)
36 {
37 size_t start = line.find(startChar, pos);
38 if (start == std::string::npos) {
39 return "";
40 }
41 size_t end = line.find(endChar, start + 1);
42 if (end == std::string::npos) {
43 return "";
44 }
45 return line.substr(start + 1, end - start - 1);
46 }
47 } // namespace HiviewDFX
48 } // namespace OHOS
49