• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2024-2025. All rights reserved.
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 <cstdio>
16 #include <cstring>
17 #include <dirent.h>
18 #include <string>
19 #include <unistd.h>
20 #include "test.h"
21 
22 constexpr int MAX_BUFFER_SIZE = 128;
23 constexpr int LARGE_LOG_LINE_AMOUNT = 5000;
24 constexpr const char *FAULTLOG_DIR = "/data/log/faultlog/faultlogger/";
25 constexpr const char *LOG_TAG = "ohos_dfx_log";
26 constexpr const char *DEFAULT_LOGPATH = "faultlogger";
27 
28 extern "C" __attribute__((weak)) int ohos_dfx_log(const char *s, const char *p);
29 
ClearDfxLogs()30 static void ClearDfxLogs()
31 {
32     DIR *dir;
33     struct dirent *ptr;
34     dir = opendir(FAULTLOG_DIR);
35     if (dir == NULL) {
36         return;
37     }
38     while ((ptr = readdir(dir)) != NULL) {
39         if (strstr(ptr->d_name, LOG_TAG) != NULL) {
40             char tmp[MAX_BUFFER_SIZE];
41             snprintf(tmp, MAX_BUFFER_SIZE, "%s/%s", FAULTLOG_DIR, ptr->d_name);
42             remove(tmp);
43         }
44     }
45     closedir(dir);
46 }
47 
CheckLogContent(char * file,const char * content)48 static void CheckLogContent(char *file, const char *content)
49 {
50     FILE *fp = fopen(file, "r");
51     if (!fp) {
52         return;
53     }
54     if (fseek(fp, 0, SEEK_END) == -1) {
55         return;
56     }
57     int size = ftell(fp);
58     if (size <= 0) {
59         fclose(fp);
60         t_error("FAIL %s size is <=0!\n", file);
61     }
62 
63     std::string buffer;
64     buffer.resize(size);
65 
66     if (fseek(fp, 0, SEEK_SET) == -1) {
67         fclose(fp);
68         return;
69     }
70     int rsize = fread(&buffer[0], 1, size, fp);
71     if (rsize == 0) {
72         fclose(fp);
73         return;
74     }
75 
76     if (buffer.find(content) != std::string::npos) {
77         printf("[ohos_dfx_log] check pass:\n%s\n", content);
78     } else {
79         t_error("[ohos_dfx_log] failed:\n%s\n", content);
80     }
81 
82     fclose(fp);
83 }
84 
CheckLog(const char * content)85 static void CheckLog(const char *content)
86 {
87     sleep(1);
88     DIR *faultlogDir = opendir(FAULTLOG_DIR);
89     if (faultlogDir == NULL) {
90         return;
91     }
92     struct dirent *ptr;
93     while ((ptr = readdir(faultlogDir)) != NULL) {
94         if (strstr(ptr->d_name, LOG_TAG) != NULL) {
95             char tmp[MAX_BUFFER_SIZE];
96             snprintf(tmp, MAX_BUFFER_SIZE, "%s/%s", FAULTLOG_DIR, ptr->d_name);
97             CheckLogContent(tmp, content);
98             break;
99         }
100     }
101     closedir(faultlogDir);
102 }
103 
TestAsanLog()104 static void TestAsanLog()
105 {
106     ClearDfxLogs();
107     if (&ohos_dfx_log) {
108         ohos_dfx_log("[ohos_dfx_log] output something to the log path.\n", DEFAULT_LOGPATH);
109         ohos_dfx_log("End Asan report", DEFAULT_LOGPATH);
110         CheckLog("[ohos_dfx_log] output something to the log path.\nEnd Asan report");
111     } else {
112         t_error("[ohos_dfx_log] cannot find ohos_dfx_log");
113     }
114 }
115 
TestHWAsanLog()116 static void TestHWAsanLog()
117 {
118     ClearDfxLogs();
119     if (&ohos_dfx_log) {
120         ohos_dfx_log("[ohos_dfx_log] output something to the log path.\n", DEFAULT_LOGPATH);
121         ohos_dfx_log("End Hwasan report", DEFAULT_LOGPATH);
122         CheckLog("[ohos_dfx_log] output something to the log path.\nEnd Hwasan report");
123     } else {
124         t_error("[ohos_dfx_log] cannot find ohos_dfx_log");
125     }
126 }
127 
TestTsanLog()128 static void TestTsanLog()
129 {
130     ClearDfxLogs();
131     if (&ohos_dfx_log) {
132         ohos_dfx_log("[ohos_dfx_log] output something to the log path.\n", DEFAULT_LOGPATH);
133         ohos_dfx_log("End Tsan report", DEFAULT_LOGPATH);
134         CheckLog("[ohos_dfx_log] output something to the log path.\nEnd Tsan report");
135     } else {
136         t_error("[ohos_dfx_log] cannot find ohos_dfx_log");
137     }
138 }
139 
TestUbsanLog()140 static void TestUbsanLog()
141 {
142     ClearDfxLogs();
143     if (&ohos_dfx_log) {
144         ohos_dfx_log("[ohos_dfx_log] output something to the log path.\n", DEFAULT_LOGPATH);
145         ohos_dfx_log("End Ubsan report", DEFAULT_LOGPATH);
146         CheckLog("[ohos_dfx_log] output something to the log path.\nEnd Ubsan report");
147     } else {
148         t_error("[ohos_dfx_log] cannot find ohos_dfx_log");
149     }
150 }
151 
TestCfiLog()152 static void TestCfiLog()
153 {
154     ClearDfxLogs();
155     if (&ohos_dfx_log) {
156         ohos_dfx_log("[ohos_dfx_log] output something to the log path.\n", DEFAULT_LOGPATH);
157         ohos_dfx_log("End CFI report", DEFAULT_LOGPATH);
158         CheckLog("[ohos_dfx_log] output something to the log path.\nEnd CFI report");
159     } else {
160         t_error("[ohos_dfx_log] cannot find ohos_dfx_log");
161     }
162 }
163 
TestBufferExpand()164 static void TestBufferExpand()
165 {
166     ClearDfxLogs();
167     if (&ohos_dfx_log) {
168         for (int i = 0; i < LARGE_LOG_LINE_AMOUNT; ++i) {
169             ohos_dfx_log(
170                 "ohos_dfx_log output something to the log path ohos_dfx_log output something to the log path.\n",
171                 DEFAULT_LOGPATH);
172         }
173         ohos_dfx_log("End Asan report", DEFAULT_LOGPATH);
174         CheckLog("End Asan report");
175     } else {
176         t_error("[ohos_dfx_log] cannot find ohos_dfx_log");
177     }
178 }
179 
main()180 int main()
181 {
182     TestAsanLog();
183     TestHWAsanLog();
184     TestTsanLog();
185     TestUbsanLog();
186     TestCfiLog();
187     TestBufferExpand();
188     return t_status;
189 }