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 #include "ftrace_field_parser.h"
16 #include "logging.h"
17 #include "printk_formats_parser.h"
18 #include "securec.h"
19
20 #include <cinttypes>
21 #include <cstring>
22 #include <fcntl.h>
23 #include <memory>
24 #include <unistd.h>
25
26 FTRACE_NS_BEGIN
27 namespace {
28 constexpr uint32_t DATALOC_OFFSET_MASK = 0xffff;
29 constexpr uint32_t DATALOC_LENGTH_MASK = 0xffff;
30 constexpr uint32_t DATALOC_LENGTH_SHIFT = 16;
31
ReadString(const uint8_t start[],const uint8_t end[],int size)32 std::string ReadString(const uint8_t start[], const uint8_t end[], int size)
33 {
34 std::string str;
35
36 if (end - start < static_cast<ptrdiff_t>(size)) {
37 return str;
38 }
39
40 const uint8_t* cursor = start;
41 const uint8_t* sectionEnd = start + size;
42 while (*cursor && cursor < sectionEnd) {
43 cursor++;
44 }
45
46 str.assign(start, cursor);
47 return str;
48 }
49 } // namespace
50
ReadData(const uint8_t start[],const uint8_t end[],void * out,size_t size)51 bool FtraceFieldParser::ReadData(const uint8_t start[], const uint8_t end[], void* out, size_t size)
52 {
53 ptrdiff_t memSize = end - start;
54 if (memSize < static_cast<ptrdiff_t>(size)) {
55 return false;
56 }
57
58 auto err = memcpy_s(out, size, start, size);
59 CHECK_TRUE(err == EOK, false, "copy %zu byte to memory region [%p, %p) FAILED!", size, start, end);
60 return true;
61 }
62
ParseStrField(const FieldFormat & format,uint8_t data[],size_t size)63 std::string FtraceFieldParser::ParseStrField(const FieldFormat& format, uint8_t data[], size_t size)
64 {
65 std::string retval;
66 if ((format.offset + format.size) > size) {
67 return retval;
68 }
69
70 uint8_t* start = data + format.offset;
71 uint8_t* end = data + size;
72 size_t strSize = 0;
73 uint64_t strPtr = 0;
74 uint32_t dataLoc = 0;
75 uint32_t dataOffset = 0;
76 uint32_t dataLength = 0;
77 errno_t errCode = EOK;
78 switch (format.filedType) {
79 case FIELD_TYPE_FIXEDCSTRING:
80 retval = ReadString(start, end, format.size);
81 break;
82 case FIELD_TYPE_CSTRING:
83 strSize = format.size;
84 if (strSize == 0) {
85 strSize = end - start;
86 retval = ReadString(start, end, strSize);
87 HILOG_WARN(LOG_CORE, "C string field %s, size: 0, parsed: %s", format.name.c_str(), retval.c_str());
88 } else {
89 retval = ReadString(start, end, strSize);
90 }
91 break;
92 case FIELD_TYPE_STRINGPTR:
93 strSize = std::min(static_cast<size_t>(format.size), sizeof(strPtr));
94 errCode = memcpy_s(&strPtr, sizeof(strPtr), start, strSize);
95 CHECK_TRUE(errCode == EOK, "", "parse STRINGPTR at %" PRIx64 " with %zu size FAILED!", strPtr, strSize);
96 retval = PrintkFormatsParser::GetInstance().GetSymbol(strPtr);
97 break;
98 case FIELD_TYPE_DATALOC:
99 dataLoc = 0;
100 ReadData(start, end, &dataLoc, sizeof(dataLoc));
101 dataOffset = dataLoc & DATALOC_OFFSET_MASK;
102 dataLength = (dataLoc >> DATALOC_LENGTH_SHIFT) & DATALOC_LENGTH_MASK;
103 if (dataOffset > 0 && start + dataOffset + dataLength <= end) {
104 retval = ReadString(start + dataOffset, end, dataLength);
105 }
106 break;
107 default:
108 break;
109 }
110 return retval;
111 }
112 FTRACE_NS_END
113