blob: 03fe2b8866de7ce310b8d6416a1085b3b574ae01 [file] [log] [blame]
xf.libfc6e712025-02-07 01:54:34 -08001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <string.h>
5#include <errno.h>
6#include <fcntl.h>
7#include <stdarg.h>
8
9#include "pub_debug_info.h"
10
11#define DEBUG_INFO_MAX_TOTAL_LEN (140)
12//#define DEBUG_INFO_MAX_DATA_LEN (128)
13//#define DEBUG_INFO_MEM_HEAD_LEN (8)
14
15ssize_t safe_write(int fd, const void *buf, size_t count)
16{
17 ssize_t n;
18
19 for (;;) {
20 n = write(fd, buf, count);
21 if (n >= 0 || errno != EINTR)
22 break;
23 /* Some callers set errno=0, are upset when they see EINTR.
24 * Returning EINTR is wrong since we retry write(),
25 * the "error" was transient.
26 */
27 errno = 0;
28 /* repeat the write() */
29 }
30
31 return n;
32}
33int sc_debug_info_record(char *id, const char *format, ...)
34{
35 int fd = -1;
36 ssize_t writelen;
37 int len;
38 va_list args;
39 char str_buf[DEBUG_INFO_MAX_TOTAL_LEN] __attribute__((aligned(4)));
40 char *ptmpstr = str_buf;
41
42 /* args是一个char*类型指针,指向format之后的第一个参数*/
43 if( id == NULL)
44 return -1;
45
46 len = snprintf((char *)ptmpstr, DEBUG_INFO_MAX_TOTAL_LEN, "[%s]",id);
47 ptmpstr += len;
48 va_start(args, format);
49 len += vsnprintf(ptmpstr, DEBUG_INFO_MAX_TOTAL_LEN - len, format, args);
50 va_end(args);
51 if (len < 0)
52 {
53 printf("[libdebug_info]: vsnprintf format error, %s.\n", strerror(errno));
54 return -1;
55 }
56
57 fd = open(DEBUG_INFO_DEV_PATH, O_WRONLY);
58 if (fd < 0)
59 {
60 printf("[libdebug_info]: sc_debug_info_record, open debug_info error, %s\n", strerror(errno));
61 return -1;
62 }
63
64 writelen = safe_write(fd, (char *)str_buf, len);
65 if (writelen < 0)
66 {
67 printf("[libdebug_info]: sc_debug_info_record, write debug_info error, %s\n", strerror(errno));
68 return -1;
69 }
70
71 if (fd >= 0)
72 {
73 close(fd);
74 }
75
76 return writelen;
77}