-
Notifications
You must be signed in to change notification settings - Fork 18
/
utils.c
150 lines (123 loc) · 2.9 KB
/
utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* utils.c - Common utilities provided to fs via libzus
*
* Copyright (c) 2018 NetApp, Inc. All rights reserved.
*
* See module.c for LICENSE details.
*
* Authors:
* Shachar Sharon <[email protected]>
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/resource.h>
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#include <zus.h>
#define BACKTRACE_MAX 128
static int _dump_backtrace(FILE *fp, bool warn)
{
int err, lim = BACKTRACE_MAX;
char sym[256] = "";
unw_word_t ip, sp, off;
unw_context_t context;
unw_cursor_t cursor;
const char *prefix = warn ? LOG_STR(LOG_WARNING) "zus_warn: " :
LOG_STR(LOG_NOTICE) " ";
err = unw_getcontext(&context);
if (err != UNW_ESUCCESS)
return err;
err = unw_init_local(&cursor, &context);
if (err != UNW_ESUCCESS)
return err;
while (lim-- > 0) {
ip = sp = off = 0;
err = unw_step(&cursor);
if (err <= 0)
return err;
err = unw_get_reg(&cursor, UNW_REG_IP, &ip);
if (err)
return err;
err = unw_get_reg(&cursor, UNW_REG_SP, &sp);
if (err)
return err;
err = unw_get_proc_name(&cursor, sym, sizeof(sym) - 1, &off);
if (err)
return err;
fprintf(fp, "%s[<%p>] 0x%lx %s+0x%lx\n",
prefix, (void *)ip, (long)sp, sym, (long)off);
}
return 0;
}
static void _dump_addr2line(FILE *fp)
{
int i, len;
void *arr[BACKTRACE_MAX];
char ptrS[2048];
char *m = ptrS;
int s = sizeof(ptrS);
len = unw_backtrace(arr, BACKTRACE_MAX);
for (i = 0; i < len - 3; ++i) {
int l;
if (!(i % 5)) {
l = snprintf(m, s,
"\\\n ");
s -= l; m += l;
}
l = snprintf(m, s, "%p ", arr[i + 1]);
s -= l; m += l;
}
fprintf(fp, LOG_STR(LOG_WARNING)
"zus_warn: addr2line -a -C -e %s -f -p -s %s\n",
program_invocation_name, ptrS);
}
void zus_dump_stack(FILE *fp, bool warn, const char *fmt, ...)
{
va_list args;
flockfile(fp);
va_start(args, fmt);
vfprintf(fp, fmt, args);
va_end(args);
_dump_backtrace(fp, warn);
if (warn)
_dump_addr2line(fp);
funlockfile(fp);
}
void zus_warn(const char *cond, const char *file, int line)
{
zus_dump_stack(stderr, true, LOG_STR(LOG_WARNING) "%s: %s (%s:%d)\n",
__func__, cond, file, line);
}
void zus_bug(const char *cond, const char *file, int line)
{
zus_dump_stack(stderr, true, LOG_STR(LOG_ERR) "%s: %s (%s:%d)\n",
__func__, cond, file, line);
abort();
}
#define ZUS_MAX_FILES 65536
int zus_increase_max_files(void)
{
struct rlimit rl;
int err;
err = getrlimit(RLIMIT_NOFILE, &rl);
if (err) {
ERROR("getrlimit failed => %d", -errno);
return -errno;
}
if (rl.rlim_cur < ZUS_MAX_FILES)
rl.rlim_cur = ZUS_MAX_FILES;
if (rl.rlim_max < ZUS_MAX_FILES)
rl.rlim_max = ZUS_MAX_FILES;
err = setrlimit(RLIMIT_NOFILE, &rl);
if (err) {
ERROR("setrlimit %lu/%lu failed => %d\n", rl.rlim_cur,
rl.rlim_max, -errno);
return -errno;
}
return 0;
}