更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_hilog.c 文件参考

浏览源代码.

结构体

struct  HiLogEntry
 
struct  HiLogCharDevice
 

函数

ssize_t HilogRead (struct file *filep, char __user *buf, size_t count)
 
ssize_t HilogWrite (struct file *filep, const char __user *buf, size_t count)
 
int HiLogOpen (struct file *filep)
 为支持VFS,作打开状 更多...
 
int HiLogClose (struct file *filep)
 为支持VFS,作关闭状 更多...
 
static ssize_t HiLogWrite (struct file *filep, const char *buffer, size_t bufLen)
 写hilog,外部以VFS方式写入 更多...
 
static ssize_t HiLogRead (struct file *filep, char *buffer, size_t bufLen)
 
static unsigned char * HiLogBufferHead (void)
 
static void HiLogBufferInc (size_t sz)
 读写对冲,对hilog的写操作,更新相关变量内容 更多...
 
static void HiLogBufferDec (size_t sz)
 读写对冲,对hilog的读操作,更新相关变量内容 更多...
 
static int HiLogBufferCopy (unsigned char *dst, unsigned dstLen, const unsigned char *src, size_t srcLen)
 
static int HiLogReadRingBuffer (unsigned char *buffer, size_t bufLen)
 读取ring buffer 更多...
 
static int HiLogWriteRingBuffer (unsigned char *buffer, size_t bufLen)
 写入 RingBuffer环形缓冲,也叫 circleBuffer 更多...
 
static void HiLogHeadInit (struct HiLogEntry *header, size_t len)
 hilog实体初始化 更多...
 
static void HiLogCoverOldLog (size_t bufLen)
 
int HiLogWriteInternal (const char *buffer, size_t bufLen)
 将外部buf写入hilog设备分两步完成 更多...
 
static void HiLogDeviceInit (void)
 初始化全局变量g_hiLogDev 更多...
 
int OsHiLogDriverInit (VOID)
 初始化hilog驱动 更多...
 
 LOS_MODULE_INIT (OsHiLogDriverInit, LOS_INIT_LEVEL_KMOD_EXTENDED)
 

变量

STATIC struct file_operations_vfs g_hilogFops
 
struct HiLogCharDevice g_hiLogDev
 

函数说明

◆ HiLogBufferCopy()

static int HiLogBufferCopy ( unsigned char *  dst,
unsigned  dstLen,
const unsigned char *  src,
size_t  srcLen 
)
static

在文件 los_hilog.c129 行定义.

130{
131 int retval = -1;
132 size_t minLen = (dstLen > srcLen) ? srcLen : dstLen;
133
134 if (LOS_IsUserAddressRange((VADDR_T)(UINTPTR)dst, minLen) &&
135 LOS_IsUserAddressRange((VADDR_T)(UINTPTR)src, minLen)) {
136 return retval;
137 }
138
139 if (LOS_IsUserAddressRange((VADDR_T)(UINTPTR)dst, minLen)) {
140 retval = LOS_ArchCopyToUser(dst, src, minLen);
141 } else if (LOS_IsUserAddressRange((VADDR_T)(UINTPTR)src, minLen)) {
142 retval = LOS_ArchCopyFromUser(dst, src, minLen);
143 } else {
144 retval = memcpy_s(dst, dstLen, src, srcLen);
145 }
146 return retval;
147}
unsigned long VADDR_T
Definition: los_typedef.h:208
unsigned long UINTPTR
Definition: los_typedef.h:68
STATIC INLINE BOOL LOS_IsUserAddressRange(VADDR_T vaddr, size_t len)
虚拟地址[vaddr,vaddr + len]是否在用户空间
Definition: los_vm_map.h:281
size_t LOS_ArchCopyToUser(void *dst, const void *src, size_t len)
从内核空间拷贝到用户空间
Definition: user_copy.c:79
size_t LOS_ArchCopyFromUser(void *dst, const void *src, size_t len)
Definition: user_copy.c:58
函数调用图:
这是这个函数的调用关系图:

◆ HiLogBufferDec()

static void HiLogBufferDec ( size_t  sz)
static

读写对冲,对hilog的读操作,更新相关变量内容

在文件 los_hilog.c119 行定义.

120{
121 if (g_hiLogDev.size >= sz) {
122 g_hiLogDev.size -= sz;//维持可使用buf size
124 g_hiLogDev.headOffset %= HILOG_BUFFER;
125 g_hiLogDev.count--;//读写对冲
126 }
127}
struct HiLogCharDevice g_hiLogDev
size_t count
Definition: los_hilog.c:89
size_t headOffset
Definition: los_hilog.c:87
这是这个函数的调用关系图:

◆ HiLogBufferHead()

static unsigned char * HiLogBufferHead ( void  )
inlinestatic

在文件 los_hilog.c92 行定义.

93{
95}
unsigned char * buffer
Definition: los_hilog.c:84
这是这个函数的调用关系图:

◆ HiLogBufferInc()

static void HiLogBufferInc ( size_t  sz)
static

读写对冲,对hilog的写操作,更新相关变量内容

在文件 los_hilog.c109 行定义.

110{
111 if (g_hiLogDev.size + sz <= HILOG_BUFFER) {
112 g_hiLogDev.size += sz; //已使用buf的size
113 g_hiLogDev.writeOffset += sz;//大小是不断累加
114 g_hiLogDev.writeOffset %= HILOG_BUFFER;//ring buf
115 g_hiLogDev.count++;//读写对冲
116 }
117}
size_t writeOffset
Definition: los_hilog.c:86
这是这个函数的调用关系图:

◆ HiLogClose()

int HiLogClose ( struct file filep)

为支持VFS,作关闭状

在文件 los_hilog.c103 行定义.

104{
105 (void)filep;
106 return 0;
107}
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 void

◆ HiLogCoverOldLog()

static void HiLogCoverOldLog ( size_t  bufLen)
static

在文件 los_hilog.c254 行定义.

255{
256 int retval;
257 struct HiLogEntry header;
258 size_t totalSize = bufLen + sizeof(struct HiLogEntry);
259 static int dropLogLines = 0;
260 static int isLastTimeFull = 0;
261 int isThisTimeFull = 0;
262
263 while (totalSize + g_hiLogDev.size > HILOG_BUFFER) {
264 retval = HiLogReadRingBuffer((unsigned char *)&header, sizeof(header));
265 if (retval < 0) {
266 break;
267 }
268
269 dropLogLines++;
270 isThisTimeFull = 1;
271 isLastTimeFull = 1;
272 HiLogBufferDec(sizeof(header));
273 HiLogBufferDec(header.len);
274 }
275 if (isLastTimeFull == 1 && isThisTimeFull == 0) {
276 /* so we can only print one log if hilog ring buffer is full in a short time */
277 if (dropLogLines > 0) {
278 PRINTK("hilog ringbuffer full, drop %d line(s) log\n", dropLogLines);
279 }
280 isLastTimeFull = 0;
281 dropLogLines = 0;
282 }
283}
static void HiLogBufferDec(size_t sz)
读写对冲,对hilog的读操作,更新相关变量内容
Definition: los_hilog.c:119
static int HiLogReadRingBuffer(unsigned char *buffer, size_t bufLen)
读取ring buffer
Definition: los_hilog.c:149
Definition: los_hilog.c:48
函数调用图:
这是这个函数的调用关系图:

◆ HiLogDeviceInit()

static void HiLogDeviceInit ( void  )
static

初始化全局变量g_hiLogDev

在文件 los_hilog.c339 行定义.

340{
341 g_hiLogDev.buffer = LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, HILOG_BUFFER);//分配内核空间
342 if (g_hiLogDev.buffer == NULL) {
343 PRINTK("In %s line %d,LOS_MemAlloc fail\n", __FUNCTION__, __LINE__);
344 }
345 //初始化waitqueue头,请确保输入参数wait有效,否则系统将崩溃。
346 init_waitqueue_head(&g_hiLogDev.wq);//见于..\third_party\FreeBSD\sys\compat\linuxkpi\common\src\linux_semaphore.c
347 LOS_MuxInit(&g_hiLogDev.mtx, NULL);//初始化hilog互斥量
348
349 g_hiLogDev.writeOffset = 0;//写g_hiLogDev.buffer偏移地址
351 g_hiLogDev.size = 0; //
352 g_hiLogDev.count = 0;
353}
VOID * LOS_MemAlloc(VOID *pool, UINT32 size)
从指定内存池中申请size长度的内存,注意这可不是从内核堆空间中申请内存
Definition: los_memory.c:1123
LITE_OS_SEC_TEXT UINT32 LOS_MuxInit(LosMux *mutex, const LosMuxAttr *attr)
初始化互斥锁
Definition: los_mux.c:262
wait_queue_head_t wq
Definition: los_hilog.c:85
函数调用图:
这是这个函数的调用关系图:

◆ HiLogHeadInit()

static void HiLogHeadInit ( struct HiLogEntry header,
size_t  len 
)
static

hilog实体初始化

在文件 los_hilog.c235 行定义.

236{
237 struct timespec now;//标准C库函数,时间格式,包含秒数和纳秒数
238 int ret;
239
240 ret = clock_gettime(CLOCK_REALTIME, &now);//获取系统实时时间
241 if (ret != 0) {
242 PRINTK("In %s line %d,clock_gettime fail\n", __FUNCTION__, __LINE__);
243 return;
244 }
245
246 header->len = len;//写入buffer的内容长度
247 header->pid = LOS_GetCurrProcessID();//当前进程ID
248 header->taskId = LOS_CurTaskIDGet(); //当前任务ID
249 header->sec = now.tv_sec; //秒级记录
250 header->nsec = now.tv_nsec; //纳秒级记录
251 header->hdrSize = sizeof(struct HiLogEntry);
252}
LITE_OS_SEC_TEXT UINT32 LOS_CurTaskIDGet(VOID)
Obtain current running task ID.
Definition: los_task.c:331
LITE_OS_SEC_TEXT UINT32 LOS_GetCurrProcessID(VOID)
获取当前进程的进程ID
Definition: los_process.c:2161
unsigned int taskId
Definition: los_hilog.c:52
unsigned int nsec
Definition: los_hilog.c:54
unsigned int hdrSize
Definition: los_hilog.c:50
unsigned int pid
Definition: los_hilog.c:51
unsigned int sec
Definition: los_hilog.c:53
unsigned int len
Definition: los_hilog.c:49
int clock_gettime(clockid_t clockID, struct timespec *tp)
当用户程序进行特定系统调用时(例如clock_gettime(CLOCK_REALTIME_COARSE, &ts)),VDSO代码页会将其拦截;
Definition: time.c:614
函数调用图:
这是这个函数的调用关系图:

◆ HiLogOpen()

int HiLogOpen ( struct file filep)

为支持VFS,作打开状

在文件 los_hilog.c97 行定义.

98{
99 (void)filep;
100 return 0;
101}

◆ HiLogRead()

static ssize_t HiLogRead ( struct file filep,
char *  buffer,
size_t  bufLen 
)
static

在文件 los_hilog.c166 行定义.

167{
168 int retval;
169 struct HiLogEntry header;
170
171 (void)filep;
172
173 wait_event_interruptible(g_hiLogDev.wq, (g_hiLogDev.size > 0));
174
175 (VOID)LOS_MuxAcquire(&g_hiLogDev.mtx);//临界区操作开始
176 retval = HiLogReadRingBuffer((unsigned char *)&header, sizeof(header));
177 if (retval < 0) {
178 retval = -EINVAL;
179 goto out;
180 }
181
182 if (bufLen < header.len + sizeof(header)) {
183 PRINTK("buffer too small,bufLen=%d, header.len=%d,%d\n", bufLen, header.len, header.hdrSize);
184 retval = -ENOMEM;
185 goto out;
186 }
187
188 HiLogBufferDec(sizeof(header));
189
190 retval = HiLogBufferCopy((unsigned char *)buffer, bufLen, (unsigned char *)&header, sizeof(header));
191 if (retval < 0) {
192 retval = -EINVAL;
193 goto out;
194 }
195
196 retval = HiLogReadRingBuffer((unsigned char *)(buffer + sizeof(header)), header.len);
197 if (retval < 0) {
198 retval = -EINVAL;
199 goto out;
200 }
201
202 HiLogBufferDec(header.len);
203 retval = header.len + sizeof(header);
204out:
205 if (retval == -ENOMEM) {
206 // clean ring buffer
209 g_hiLogDev.size = 0;
210 g_hiLogDev.count = 0;
211 }
212 (VOID)LOS_MuxRelease(&g_hiLogDev.mtx);//临界区操作结束
213 return (ssize_t)retval;
214}
static int HiLogBufferCopy(unsigned char *dst, unsigned dstLen, const unsigned char *src, size_t srcLen)
Definition: los_hilog.c:129
INT64 ssize_t
Definition: los_typedef.h:79
STATIC INLINE STATUS_T LOS_MuxAcquire(LosMux *m)
Definition: los_vm_lock.h:48
STATIC INLINE STATUS_T LOS_MuxRelease(LosMux *m)
Definition: los_vm_lock.h:53
函数调用图:

◆ HilogRead()

ssize_t HilogRead ( struct file filep,
char __user *  buf,
size_t  count 
)

◆ HiLogReadRingBuffer()

static int HiLogReadRingBuffer ( unsigned char *  buffer,
size_t  bufLen 
)
static

读取ring buffer

在文件 los_hilog.c149 行定义.

150{
151 int retval;
152 size_t bufLeft = HILOG_BUFFER - g_hiLogDev.headOffset;
153 if (bufLeft > bufLen) {//计算出读取方向
154 retval = HiLogBufferCopy(buffer, bufLen, HiLogBufferHead(), bufLen);
155 } else {
156 retval = HiLogBufferCopy(buffer, bufLen, HiLogBufferHead(), bufLeft);
157 if (retval < 0) {
158 return retval;
159 }
160
161 retval = HiLogBufferCopy(buffer + bufLeft, bufLen - bufLeft, g_hiLogDev.buffer, bufLen - bufLeft);
162 }
163 return retval;
164}
static unsigned char * HiLogBufferHead(void)
Definition: los_hilog.c:92
函数调用图:
这是这个函数的调用关系图:

◆ HiLogWrite()

static ssize_t HiLogWrite ( struct file filep,
const char *  buffer,
size_t  bufLen 
)
static

写hilog,外部以VFS方式写入

在文件 los_hilog.c328 行定义.

329{
330 (void)filep;
331 if (bufLen + sizeof(struct HiLogEntry) > HILOG_BUFFER) {
332 PRINTK("input too large\n");
333 return -ENOMEM;
334 }
335
336 return HiLogWriteInternal(buffer, bufLen);
337}
int HiLogWriteInternal(const char *buffer, size_t bufLen)
将外部buf写入hilog设备分两步完成
Definition: los_hilog.c:285
函数调用图:

◆ HilogWrite()

ssize_t HilogWrite ( struct file filep,
const char __user *  buf,
size_t  count 
)

◆ HiLogWriteInternal()

int HiLogWriteInternal ( const char *  buffer,
size_t  bufLen 
)

将外部buf写入hilog设备分两步完成

在文件 los_hilog.c285 行定义.

286{
287 struct HiLogEntry header;
288 int retval;
289 LosTaskCB *runTask = (LosTaskCB *)OsCurrTaskGet();
290
291 if ((g_hiLogDev.buffer == NULL) || (OS_INT_ACTIVE) || (runTask->taskStatus & OS_TASK_FLAG_SYSTEM_TASK)) {
292 PRINTK("%s\n", buffer);
293 return -EAGAIN;
294 }
295
297 HiLogCoverOldLog(bufLen);
298 HiLogHeadInit(&header, bufLen);
299
300 retval = HiLogWriteRingBuffer((unsigned char *)&header, sizeof(header));//1.先写入头部内容
301 if (retval) {
302 retval = -ENODATA;
303 goto out;
304 }
305 HiLogBufferInc(sizeof(header));//
306
307 retval = HiLogWriteRingBuffer((unsigned char *)(buffer), header.len);//2.再写入实际buf内容
308 if (retval) {
309 retval = -ENODATA;
310 goto out;
311 }
312
313 HiLogBufferInc(header.len);
314
315 retval = header.len;
316
317out:
319 if (retval > 0) {
320 wake_up_interruptible(&g_hiLogDev.wq);
321 }
322 if (retval < 0) {
323 PRINTK("write fail retval=%d\n", retval);
324 }
325 return retval;
326}
static void HiLogBufferInc(size_t sz)
读写对冲,对hilog的写操作,更新相关变量内容
Definition: los_hilog.c:109
static void HiLogCoverOldLog(size_t bufLen)
Definition: los_hilog.c:254
static int HiLogWriteRingBuffer(unsigned char *buffer, size_t bufLen)
写入 RingBuffer环形缓冲,也叫 circleBuffer
Definition: los_hilog.c:216
static void HiLogHeadInit(struct HiLogEntry *header, size_t len)
hilog实体初始化
Definition: los_hilog.c:235
STATIC INLINE LosTaskCB * OsCurrTaskGet(VOID)
UINT16 taskStatus
函数调用图:
这是这个函数的调用关系图:

◆ HiLogWriteRingBuffer()

static int HiLogWriteRingBuffer ( unsigned char *  buffer,
size_t  bufLen 
)
static

写入 RingBuffer环形缓冲,也叫 circleBuffer

在文件 los_hilog.c216 行定义.

217{
218 int retval;
219 size_t bufLeft = HILOG_BUFFER - g_hiLogDev.writeOffset;
220 if (bufLen > bufLeft) {
221 retval = HiLogBufferCopy(g_hiLogDev.buffer + g_hiLogDev.writeOffset, bufLeft, buffer, bufLeft);
222 if (retval) {
223 return -1;
224 }
225 retval = HiLogBufferCopy(g_hiLogDev.buffer, HILOG_BUFFER, buffer + bufLeft, bufLen - bufLeft);
226 } else {
227 retval = HiLogBufferCopy(g_hiLogDev.buffer + g_hiLogDev.writeOffset, bufLeft, buffer, bufLen);
228 }
229 if (retval < 0) {
230 return -1;
231 }
232 return 0;
233}
函数调用图:
这是这个函数的调用关系图:

◆ LOS_MODULE_INIT()

LOS_MODULE_INIT ( OsHiLogDriverInit  ,
LOS_INIT_LEVEL_KMOD_EXTENDED   
)

◆ OsHiLogDriverInit()

int OsHiLogDriverInit ( VOID  )

初始化hilog驱动

在文件 los_hilog.c355 行定义.

356{
357 HiLogDeviceInit();//初始化全局变量g_hiLogDev
358 return register_driver(HILOG_DRIVER, &g_hilogFops, DRIVER_MODE, NULL);//注册字符设备驱动程序,生成inode节点
359}
static void HiLogDeviceInit(void)
初始化全局变量g_hiLogDev
Definition: los_hilog.c:339
STATIC struct file_operations_vfs g_hilogFops
Definition: los_hilog.c:67
函数调用图:

变量说明

◆ g_hiLogDev

struct HiLogCharDevice g_hiLogDev

◆ g_hilogFops

STATIC struct file_operations_vfs g_hilogFops
初始值:
= {
NULL,
NULL,
NULL,
#ifndef CONFIG_DISABLE_POLL
NULL,
#endif
NULL,
}
static ssize_t HiLogRead(struct file *filep, char *buffer, size_t bufLen)
Definition: los_hilog.c:166
int HiLogClose(struct file *filep)
为支持VFS,作关闭状
Definition: los_hilog.c:103
static ssize_t HiLogWrite(struct file *filep, const char *buffer, size_t bufLen)
写hilog,外部以VFS方式写入
Definition: los_hilog.c:328
int HiLogOpen(struct file *filep)
为支持VFS,作打开状
Definition: los_hilog.c:97

在文件 los_hilog.c67 行定义.