glusterfs

Форк
0
442 строки · 20.2 Кб
1
/*
2
 * Copyright (c) 2003 Maxim Sobolev <sobomax@FreeBSD.org>
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 *
26
 * $Id: execinfo.c,v 1.3 2004/07/19 05:21:09 sobomax Exp $
27
 */
28

29
#ifndef _CONFIG_H
30
#define _CONFIG_H
31
#include "config.h"
32
#endif
33

34
#ifndef HAVE_BACKTRACE
35
#include <sys/types.h>
36
#include <sys/uio.h>
37
#include <dlfcn.h>
38
#include <math.h>
39
#include <stddef.h>
40
#include <stdio.h>
41
#include <stdlib.h>
42
#include <string.h>
43
#include <unistd.h>
44
#include <stddef.h>
45

46
#include "execinfo_compat.h"
47

48
#define D10(x) ceil(log10(((x) == 0) ? 2 : ((x) + 1)))
49

50
static void *
51
getreturnaddr(int level)
52
{
53
        switch (level) {
54
        case 0: return __builtin_return_address(1);
55
        case 1: return __builtin_return_address(2);
56
        case 2: return __builtin_return_address(3);
57
        case 3: return __builtin_return_address(4);
58
        case 4: return __builtin_return_address(5);
59
        case 5: return __builtin_return_address(6);
60
        case 6: return __builtin_return_address(7);
61
        case 7: return __builtin_return_address(8);
62
        case 8: return __builtin_return_address(9);
63
        case 9: return __builtin_return_address(10);
64
        case 10: return __builtin_return_address(11);
65
        case 11: return __builtin_return_address(12);
66
        case 12: return __builtin_return_address(13);
67
        case 13: return __builtin_return_address(14);
68
        case 14: return __builtin_return_address(15);
69
        case 15: return __builtin_return_address(16);
70
        case 16: return __builtin_return_address(17);
71
        case 17: return __builtin_return_address(18);
72
        case 18: return __builtin_return_address(19);
73
        case 19: return __builtin_return_address(20);
74
        case 20: return __builtin_return_address(21);
75
        case 21: return __builtin_return_address(22);
76
        case 22: return __builtin_return_address(23);
77
        case 23: return __builtin_return_address(24);
78
        case 24: return __builtin_return_address(25);
79
        case 25: return __builtin_return_address(26);
80
        case 26: return __builtin_return_address(27);
81
        case 27: return __builtin_return_address(28);
82
        case 28: return __builtin_return_address(29);
83
        case 29: return __builtin_return_address(30);
84
        case 30: return __builtin_return_address(31);
85
        case 31: return __builtin_return_address(32);
86
        case 32: return __builtin_return_address(33);
87
        case 33: return __builtin_return_address(34);
88
        case 34: return __builtin_return_address(35);
89
        case 35: return __builtin_return_address(36);
90
        case 36: return __builtin_return_address(37);
91
        case 37: return __builtin_return_address(38);
92
        case 38: return __builtin_return_address(39);
93
        case 39: return __builtin_return_address(40);
94
        case 40: return __builtin_return_address(41);
95
        case 41: return __builtin_return_address(42);
96
        case 42: return __builtin_return_address(43);
97
        case 43: return __builtin_return_address(44);
98
        case 44: return __builtin_return_address(45);
99
        case 45: return __builtin_return_address(46);
100
        case 46: return __builtin_return_address(47);
101
        case 47: return __builtin_return_address(48);
102
        case 48: return __builtin_return_address(49);
103
        case 49: return __builtin_return_address(50);
104
        case 50: return __builtin_return_address(51);
105
        case 51: return __builtin_return_address(52);
106
        case 52: return __builtin_return_address(53);
107
        case 53: return __builtin_return_address(54);
108
        case 54: return __builtin_return_address(55);
109
        case 55: return __builtin_return_address(56);
110
        case 56: return __builtin_return_address(57);
111
        case 57: return __builtin_return_address(58);
112
        case 58: return __builtin_return_address(59);
113
        case 59: return __builtin_return_address(60);
114
        case 60: return __builtin_return_address(61);
115
        case 61: return __builtin_return_address(62);
116
        case 62: return __builtin_return_address(63);
117
        case 63: return __builtin_return_address(64);
118
        case 64: return __builtin_return_address(65);
119
        case 65: return __builtin_return_address(66);
120
        case 66: return __builtin_return_address(67);
121
        case 67: return __builtin_return_address(68);
122
        case 68: return __builtin_return_address(69);
123
        case 69: return __builtin_return_address(70);
124
        case 70: return __builtin_return_address(71);
125
        case 71: return __builtin_return_address(72);
126
        case 72: return __builtin_return_address(73);
127
        case 73: return __builtin_return_address(74);
128
        case 74: return __builtin_return_address(75);
129
        case 75: return __builtin_return_address(76);
130
        case 76: return __builtin_return_address(77);
131
        case 77: return __builtin_return_address(78);
132
        case 78: return __builtin_return_address(79);
133
        case 79: return __builtin_return_address(80);
134
        case 80: return __builtin_return_address(81);
135
        case 81: return __builtin_return_address(82);
136
        case 82: return __builtin_return_address(83);
137
        case 83: return __builtin_return_address(84);
138
        case 84: return __builtin_return_address(85);
139
        case 85: return __builtin_return_address(86);
140
        case 86: return __builtin_return_address(87);
141
        case 87: return __builtin_return_address(88);
142
        case 88: return __builtin_return_address(89);
143
        case 89: return __builtin_return_address(90);
144
        case 90: return __builtin_return_address(91);
145
        case 91: return __builtin_return_address(92);
146
        case 92: return __builtin_return_address(93);
147
        case 93: return __builtin_return_address(94);
148
        case 94: return __builtin_return_address(95);
149
        case 95: return __builtin_return_address(96);
150
        case 96: return __builtin_return_address(97);
151
        case 97: return __builtin_return_address(98);
152
        case 98: return __builtin_return_address(99);
153
        case 99: return __builtin_return_address(100);
154
        case 100: return __builtin_return_address(101);
155
        case 101: return __builtin_return_address(102);
156
        case 102: return __builtin_return_address(103);
157
        case 103: return __builtin_return_address(104);
158
        case 104: return __builtin_return_address(105);
159
        case 105: return __builtin_return_address(106);
160
        case 106: return __builtin_return_address(107);
161
        case 107: return __builtin_return_address(108);
162
        case 108: return __builtin_return_address(109);
163
        case 109: return __builtin_return_address(110);
164
        case 110: return __builtin_return_address(111);
165
        case 111: return __builtin_return_address(112);
166
        case 112: return __builtin_return_address(113);
167
        case 113: return __builtin_return_address(114);
168
        case 114: return __builtin_return_address(115);
169
        case 115: return __builtin_return_address(116);
170
        case 116: return __builtin_return_address(117);
171
        case 117: return __builtin_return_address(118);
172
        case 118: return __builtin_return_address(119);
173
        case 119: return __builtin_return_address(120);
174
        case 120: return __builtin_return_address(121);
175
        case 121: return __builtin_return_address(122);
176
        case 122: return __builtin_return_address(123);
177
        case 123: return __builtin_return_address(124);
178
        case 124: return __builtin_return_address(125);
179
        case 125: return __builtin_return_address(126);
180
        case 126: return __builtin_return_address(127);
181
        case 127: return __builtin_return_address(128);
182
        default: return NULL;
183
        }
184
}
185

186
static void *
187
getframeaddr(int level)
188
{
189

190
        switch (level) {
191
        case 0: return __builtin_frame_address(1);
192
        case 1: return __builtin_frame_address(2);
193
        case 2: return __builtin_frame_address(3);
194
        case 3: return __builtin_frame_address(4);
195
        case 4: return __builtin_frame_address(5);
196
        case 5: return __builtin_frame_address(6);
197
        case 6: return __builtin_frame_address(7);
198
        case 7: return __builtin_frame_address(8);
199
        case 8: return __builtin_frame_address(9);
200
        case 9: return __builtin_frame_address(10);
201
        case 10: return __builtin_frame_address(11);
202
        case 11: return __builtin_frame_address(12);
203
        case 12: return __builtin_frame_address(13);
204
        case 13: return __builtin_frame_address(14);
205
        case 14: return __builtin_frame_address(15);
206
        case 15: return __builtin_frame_address(16);
207
        case 16: return __builtin_frame_address(17);
208
        case 17: return __builtin_frame_address(18);
209
        case 18: return __builtin_frame_address(19);
210
        case 19: return __builtin_frame_address(20);
211
        case 20: return __builtin_frame_address(21);
212
        case 21: return __builtin_frame_address(22);
213
        case 22: return __builtin_frame_address(23);
214
        case 23: return __builtin_frame_address(24);
215
        case 24: return __builtin_frame_address(25);
216
        case 25: return __builtin_frame_address(26);
217
        case 26: return __builtin_frame_address(27);
218
        case 27: return __builtin_frame_address(28);
219
        case 28: return __builtin_frame_address(29);
220
        case 29: return __builtin_frame_address(30);
221
        case 30: return __builtin_frame_address(31);
222
        case 31: return __builtin_frame_address(32);
223
        case 32: return __builtin_frame_address(33);
224
        case 33: return __builtin_frame_address(34);
225
        case 34: return __builtin_frame_address(35);
226
        case 35: return __builtin_frame_address(36);
227
        case 36: return __builtin_frame_address(37);
228
        case 37: return __builtin_frame_address(38);
229
        case 38: return __builtin_frame_address(39);
230
        case 39: return __builtin_frame_address(40);
231
        case 40: return __builtin_frame_address(41);
232
        case 41: return __builtin_frame_address(42);
233
        case 42: return __builtin_frame_address(43);
234
        case 43: return __builtin_frame_address(44);
235
        case 44: return __builtin_frame_address(45);
236
        case 45: return __builtin_frame_address(46);
237
        case 46: return __builtin_frame_address(47);
238
        case 47: return __builtin_frame_address(48);
239
        case 48: return __builtin_frame_address(49);
240
        case 49: return __builtin_frame_address(50);
241
        case 50: return __builtin_frame_address(51);
242
        case 51: return __builtin_frame_address(52);
243
        case 52: return __builtin_frame_address(53);
244
        case 53: return __builtin_frame_address(54);
245
        case 54: return __builtin_frame_address(55);
246
        case 55: return __builtin_frame_address(56);
247
        case 56: return __builtin_frame_address(57);
248
        case 57: return __builtin_frame_address(58);
249
        case 58: return __builtin_frame_address(59);
250
        case 59: return __builtin_frame_address(60);
251
        case 60: return __builtin_frame_address(61);
252
        case 61: return __builtin_frame_address(62);
253
        case 62: return __builtin_frame_address(63);
254
        case 63: return __builtin_frame_address(64);
255
        case 64: return __builtin_frame_address(65);
256
        case 65: return __builtin_frame_address(66);
257
        case 66: return __builtin_frame_address(67);
258
        case 67: return __builtin_frame_address(68);
259
        case 68: return __builtin_frame_address(69);
260
        case 69: return __builtin_frame_address(70);
261
        case 70: return __builtin_frame_address(71);
262
        case 71: return __builtin_frame_address(72);
263
        case 72: return __builtin_frame_address(73);
264
        case 73: return __builtin_frame_address(74);
265
        case 74: return __builtin_frame_address(75);
266
        case 75: return __builtin_frame_address(76);
267
        case 76: return __builtin_frame_address(77);
268
        case 77: return __builtin_frame_address(78);
269
        case 78: return __builtin_frame_address(79);
270
        case 79: return __builtin_frame_address(80);
271
        case 80: return __builtin_frame_address(81);
272
        case 81: return __builtin_frame_address(82);
273
        case 82: return __builtin_frame_address(83);
274
        case 83: return __builtin_frame_address(84);
275
        case 84: return __builtin_frame_address(85);
276
        case 85: return __builtin_frame_address(86);
277
        case 86: return __builtin_frame_address(87);
278
        case 87: return __builtin_frame_address(88);
279
        case 88: return __builtin_frame_address(89);
280
        case 89: return __builtin_frame_address(90);
281
        case 90: return __builtin_frame_address(91);
282
        case 91: return __builtin_frame_address(92);
283
        case 92: return __builtin_frame_address(93);
284
        case 93: return __builtin_frame_address(94);
285
        case 94: return __builtin_frame_address(95);
286
        case 95: return __builtin_frame_address(96);
287
        case 96: return __builtin_frame_address(97);
288
        case 97: return __builtin_frame_address(98);
289
        case 98: return __builtin_frame_address(99);
290
        case 99: return __builtin_frame_address(100);
291
        case 100: return __builtin_frame_address(101);
292
        case 101: return __builtin_frame_address(102);
293
        case 102: return __builtin_frame_address(103);
294
        case 103: return __builtin_frame_address(104);
295
        case 104: return __builtin_frame_address(105);
296
        case 105: return __builtin_frame_address(106);
297
        case 106: return __builtin_frame_address(107);
298
        case 107: return __builtin_frame_address(108);
299
        case 108: return __builtin_frame_address(109);
300
        case 109: return __builtin_frame_address(110);
301
        case 110: return __builtin_frame_address(111);
302
        case 111: return __builtin_frame_address(112);
303
        case 112: return __builtin_frame_address(113);
304
        case 113: return __builtin_frame_address(114);
305
        case 114: return __builtin_frame_address(115);
306
        case 115: return __builtin_frame_address(116);
307
        case 116: return __builtin_frame_address(117);
308
        case 117: return __builtin_frame_address(118);
309
        case 118: return __builtin_frame_address(119);
310
        case 119: return __builtin_frame_address(120);
311
        case 120: return __builtin_frame_address(121);
312
        case 121: return __builtin_frame_address(122);
313
        case 122: return __builtin_frame_address(123);
314
        case 123: return __builtin_frame_address(124);
315
        case 124: return __builtin_frame_address(125);
316
        case 125: return __builtin_frame_address(126);
317
        case 126: return __builtin_frame_address(127);
318
        case 127: return __builtin_frame_address(128);
319
        default: return NULL;
320
        }
321
}
322

323
static inline void *
324
realloc_safe(void *ptr, size_t size)
325
{
326
        void *nptr;
327

328
        nptr = realloc (ptr, size);
329
        if (nptr == NULL)
330
                free (ptr);
331
        return nptr;
332
}
333

334
int
335
backtrace(void **buffer, int size)
336
{
337
        int i;
338

339
        for (i = 1; getframeaddr(i + 1) != NULL && i != size + 1; i++) {
340
                buffer[i - 1] = getreturnaddr(i);
341
                if (buffer[i - 1] == NULL)
342
                        break;
343
        }
344
        return i - 1;
345
}
346

347
char **
348
backtrace_symbols(void *const *buffer, int size)
349
{
350
        size_t clen, alen;
351
        int i, offset;
352
        char **rval;
353
        Dl_info info;
354

355
        clen = size * sizeof(char *);
356
        rval = malloc(clen);
357
        if (rval == NULL)
358
                return NULL;
359
        for (i = 0; i < size; i++) {
360
                if (dladdr(buffer[i], &info) != 0) {
361
                        if (info.dli_sname == NULL)
362
                                info.dli_sname = "???";
363
                        if (info.dli_saddr == NULL)
364
                                info.dli_saddr = buffer[i];
365
                        offset = buffer[i] - info.dli_saddr;
366
                        /* "0x01234567 <function+offset> at filename" */
367
                        alen = 2 +                      /* "0x" */
368
                                (sizeof(void *) * 2) +   /* "01234567" */
369
                                2 +                      /* " <" */
370
                                strlen(info.dli_sname) + /* "function" */
371
                                1 +                      /* "+" */
372
                                10 +                     /* "offset */
373
                                5 +                      /* "> at " */
374
                                strlen(info.dli_fname) + /* "filename" */
375
                                1;                       /* "\0" */
376
                        rval = realloc_safe(rval, clen + alen);
377
                        if (rval == NULL)
378
                                return NULL;
379
                        snprintf((char *) rval + clen, alen, "%p <%s+%d> at %s",
380
                                 buffer[i], info.dli_sname, offset, info.dli_fname);
381
                } else {
382
                        alen = 2 +                      /* "0x" */
383
                                (sizeof(void *) * 2) +   /* "01234567" */
384
                                1;                       /* "\0" */
385
                        rval = realloc_safe(rval, clen + alen);
386
                        if (rval == NULL)
387
                                return NULL;
388
                        snprintf((char *) rval + clen, alen, "%p", buffer[i]);
389
                }
390
                rval[i] = (char *) clen;
391
                clen += alen;
392
        }
393

394
        for (i = 0; i < size; i++)
395
                rval[i] += (long) rval;
396

397
        return rval;
398
}
399

400
void
401
backtrace_symbols_fd(void *const *buffer, int size, int fd)
402
{
403
        int i, len, offset;
404
        char *buf;
405
        Dl_info info;
406

407
        for (i = 0; i < size; i++) {
408
                if (dladdr(buffer[i], &info) != 0) {
409
                        if (info.dli_sname == NULL)
410
                                info.dli_sname = "???";
411
                        if (info.dli_saddr == NULL)
412
                                info.dli_saddr = buffer[i];
413
                        offset = buffer[i] - info.dli_saddr;
414
                        /* "0x01234567 <function+offset> at filename" */
415
                        len = 2 +                      /* "0x" */
416
                                (sizeof(void *) * 2) +   /* "01234567" */
417
                                2 +                      /* " <" */
418
                                strlen(info.dli_sname) + /* "function" */
419
                                1 +                      /* "+" */
420
                                D10(offset) +            /* "offset */
421
                                5 +                      /* "> at " */
422
                                strlen(info.dli_fname) + /* "filename" */
423
                                2;                       /* "\n\0" */
424
                        buf = alloca(len);
425
                        if (buf == NULL)
426
                                return;
427
                        snprintf(buf, len, "%p <%s+%d> at %s\n",
428
                                 buffer[i], info.dli_sname, offset, info.dli_fname);
429
                } else {
430
                        len = 2 +                      /* "0x" */
431
                                (sizeof(void *) * 2) +   /* "01234567" */
432
                                2;                       /* "\n\0" */
433
                        buf = alloca(len);
434
                        if (buf == NULL)
435
                                return;
436
                        snprintf(buf, len, "%p\n", buffer[i]);
437
                }
438
                if (write(fd, buf, strlen(buf)) == -1)
439
                        return;
440
        }
441
}
442
#endif
443

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.