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