lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #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 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 11 | #define DEBUG_INFO_MAX_TOTAL_LEN (140) |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 12 | //#define DEBUG_INFO_MAX_DATA_LEN (128) |
| 13 | //#define DEBUG_INFO_MEM_HEAD_LEN (8) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 14 | |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 15 | ssize_t safe_write(int fd, const void *buf, size_t count) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 16 | { |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 17 | ssize_t n; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 18 | |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 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 | } |
| 33 | int sc_debug_info_record(char *id, const char *format, ...) |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 34 | { |
| 35 | int fd = -1; |
| 36 | ssize_t writelen; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 37 | int len; |
| 38 | va_list args; |
| 39 | char str_buf[DEBUG_INFO_MAX_TOTAL_LEN] __attribute__((aligned(4))); |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 40 | char *ptmpstr = str_buf; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 41 | |
| 42 | /* args是一个char*类型指针,指向format之后的第一个参数*/ |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 43 | if( id == NULL) |
| 44 | return -1; |
| 45 | |
| 46 | len = snprintf((char *)ptmpstr, DEBUG_INFO_MAX_TOTAL_LEN, "[%s]",id); |
| 47 | ptmpstr += len; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 48 | va_start(args, format); |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 49 | len += vsnprintf(ptmpstr, DEBUG_INFO_MAX_TOTAL_LEN - len, format, args); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 | |
xf.li | df7f8ba | 2024-09-12 23:53:34 -0700 | [diff] [blame] | 64 | writelen = safe_write(fd, (char *)str_buf, len); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 | } |