redis调试---backtrace

[yangpeng@yangpeng src]>make
[yangpeng@yangpeng src]>sudo make install
#include <execinfo.h>

int backtrace(void **buffer, int size);

char **backtrace_symbols(void *const *buffer, int size);

void backtrace_symbols_fd(void *const *buffer, int size, int fd);

backtrace_symbols的实现需要符号名称的支持,在gcc编译过程中需要加入-rdynamic参数;
 1 //dump.c  //TEST: gcc -DDEBUG_TEST -rdynamic dump.c
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <stddef.h>
 5 #include <stdarg.h>
 6 #include <string.h>
 7 #include <assert.h>
 8 #include <hiredis/hiredis.h>
 9 
10 #include <unistd.h>
11 #include <signal.h>        /* for signal */
12 #include <execinfo.h>     /* for backtrace() */
13  
14 #define BACKTRACE_SIZE   16
15  
16 void dump(void)
17 {
18     int j, nptrs;
19     void *buffer[BACKTRACE_SIZE];
20     char **strings;
21     
22     nptrs = backtrace(buffer, BACKTRACE_SIZE);
23     
24     printf("backtrace() returned %d addresses\n", nptrs);
25  
26     strings = backtrace_symbols(buffer, nptrs);
27     if (strings == NULL) {
28         perror("backtrace_symbols");
29         exit(EXIT_FAILURE);
30     }
31  
32     for (j = 0; j < nptrs; j++)
33         printf("  [%02d] %s\n", j, strings[j]);
34  
35     free(strings);
36 }
37 
38 int add1(int num)
39 {
40     int ret = 0x00;
41     int *pTemp = &ret;
42     
43     *pTemp = 0x01;  /* line 40 set pTemp = NULL这将导致一个段错误,致使程序崩溃退出 */
44     dump();
45     ret = num + *pTemp;
46     
47     return ret;
48 }
49  
50 int add(int num)
51 {
52     int ret = 0x00;
53  
54     ret = add1(num);
55     
56     return ret;
57 }
58 #ifdef DEBUG_TEST
59 int main(int argc, char *argv[])
60 {
61     int sum = 0x00;
62     
63     sum = add(sum);
64     
65     printf(" sum = %d \n", sum);
66     
67     return 0x00;
68 }
69 #endif
dump.c //TEST: gcc -DDEBUG_TEST -rdynamic dump.c
Makefile:

FINAL_CFLAGS=$(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS) $(REDIS_CFLAGS) -rdynamic

REDIS_SERVER_OBJ=dump.o adlist.o ae.o anet.o dict.o redis.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o

REDIS_CLI_OBJ=anet.o sds.o adlist.o redis-cli.o zmalloc.o release.o anet.o ae.o crc64.o dump.o

REDIS_BENCHMARK_OBJ=dump.o ae.o anet.o redis-benchmark.o sds.o adlist.o zmalloc.o redis-benchmark.o
run:
[yangpeng@yangpeng redis-3.0]>./src/redis-server redis.conf
----------------------------
----------------------------
-----------yangpeng---------
----------------------------
----------------------------
4640:M 19 Dec 12:20:42.628 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.7 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 4640
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

4640:M 19 Dec 12:20:42.629 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4640:M 19 Dec 12:20:42.629 # Server started, Redis version 3.0.7
4640:M 19 Dec 12:20:42.629 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
4640:M 19 Dec 12:20:42.629 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
/MOM/codeBuild02/yangpeng/UTFT/test/redis_src/redis-3.0
rdbLoad:1139 dump.rdb
4640:M 19 Dec 12:20:42.632 * DB loaded from disk: 0.003 seconds
4640:M 19 Dec 12:20:42.632 * The server is now ready to accept connections on port 6379


[yangpeng@yangpeng redis-3.0]>src/redis-cli
backtrace() returned 7 addresses
[00] src/redis-cli(dump+0x2b) [0x40debb]
[01] src/redis-cli() [0x41196c]
[02] src/redis-cli(redisConnect+0x5a) [0x4102ba]
[03] src/redis-cli() [0x40b9a2]
[04] src/redis-cli(main+0x477) [0x404ed7]
[05] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0) [0x7f2a6cf3f830]
[06] src/redis-cli(_start+0x29) [0x4064e9]
127.0.0.1:6379> 

猜你喜欢

转载自www.cnblogs.com/cjyp/p/10136416.html
今日推荐