blob: 03fe2b8866de7ce310b8d6416a1085b3b574ae01 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#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
lh9ed821d2023-04-07 01:36:19 -070011#define DEBUG_INFO_MAX_TOTAL_LEN (140)
xf.lidf7f8ba2024-09-12 23:53:34 -070012//#define DEBUG_INFO_MAX_DATA_LEN (128)
13//#define DEBUG_INFO_MEM_HEAD_LEN (8)
lh9ed821d2023-04-07 01:36:19 -070014
xf.lidf7f8ba2024-09-12 23:53:34 -070015ssize_t safe_write(int fd, const void *buf, size_t count)
lh9ed821d2023-04-07 01:36:19 -070016{
xf.lidf7f8ba2024-09-12 23:53:34 -070017 ssize_t n;
lh9ed821d2023-04-07 01:36:19 -070018
xf.lidf7f8ba2024-09-12 23:53:34 -070019 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, ...)
lh9ed821d2023-04-07 01:36:19 -070034{
35 int fd = -1;
36 ssize_t writelen;
lh9ed821d2023-04-07 01:36:19 -070037 int len;
38 va_list args;
39 char str_buf[DEBUG_INFO_MAX_TOTAL_LEN] __attribute__((aligned(4)));
xf.lidf7f8ba2024-09-12 23:53:34 -070040 char *ptmpstr = str_buf;
lh9ed821d2023-04-07 01:36:19 -070041
42 /* args是一个char*类型指针,指向format之后的第一个参数*/
xf.lidf7f8ba2024-09-12 23:53:34 -070043 if( id == NULL)
44 return -1;
45
46 len = snprintf((char *)ptmpstr, DEBUG_INFO_MAX_TOTAL_LEN, "[%s]",id);
47 ptmpstr += len;
lh9ed821d2023-04-07 01:36:19 -070048 va_start(args, format);
xf.lidf7f8ba2024-09-12 23:53:34 -070049 len += vsnprintf(ptmpstr, DEBUG_INFO_MAX_TOTAL_LEN - len, format, args);
lh9ed821d2023-04-07 01:36:19 -070050 va_end(args);
51 if (len < 0)
52 {
53 printf("[libdebug_info]: vsnprintf format error, %s.\n", strerror(errno));
54 return -1;
55 }
56
lh9ed821d2023-04-07 01:36:19 -070057 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
xf.lidf7f8ba2024-09-12 23:53:34 -070064 writelen = safe_write(fd, (char *)str_buf, len);
lh9ed821d2023-04-07 01:36:19 -070065 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}