第07课:【实战】调试Redis准备工作

7.1 Redis源码下载与编译

  Redis源码下载与编译在前面已经说过了,同学们可以去第04课:GDB常用命令详解(上)学习。

  编译成功后,会在src目录下生成多个可执行程序,其中redis-server和redis-cli使我们即将调试的程序,进入src目录,使用GDB启动redis-server这个程序。

wzq@wzq-PC:~/Desktop/redis-5.0.3/src$ gdb redis-server 
GNU gdb (Debian 7.12-6+b2) 7.12.0.20161007-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from redis-server...done.
(gdb) r
Starting program: /home/wzq/Desktop/redis-5.0.3/src/redis-server 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
8073:C 14 Jan 2019 10:30:29.039 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8073:C 14 Jan 2019 10:30:29.039 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=8073, just started
8073:C 14 Jan 2019 10:30:29.040 # Warning: no config file specified, using the default config. In order to specify a config file use /home/wzq/Desktop/redis-5.0.3/src/redis-server /path/to/redis.conf
8073:M 14 Jan 2019 10:30:29.040 * Increased maximum number of open files to 10032 (it was originally set to 1024).
[New Thread 0x7ffff67ff700 (LWP 8077)]
[New Thread 0x7ffff5ffe700 (LWP 8078)]
[New Thread 0x7ffff57fd700 (LWP 8079)]
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.3 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 8073
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

8073:M 14 Jan 2019 10:30:29.042 # Server initialized
8073:M 14 Jan 2019 10:30:29.042 # 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.
8073:M 14 Jan 2019 10:30:29.042 # 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.
8073:M 14 Jan 2019 10:30:29.042 * Ready to accept connections

  以上是redis-server启动成功后的画面。

  我们再开一个session,再次进入Redis源码所在的src目录,然后使用GDB启动Redis客户端redis-cli:

wzq@wzq-PC:~/Desktop/redis-5.0.3/src$ gdb redis-cli 
GNU gdb (Debian 7.12-6+b2) 7.12.0.20161007-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from redis-cli...done.
(gdb) r
Starting program: /home/wzq/Desktop/redis-5.0.3/src/redis-cli 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
127.0.0.1:6379> 

  以上是redis-cli启动成功后的画面。

7.2 通信示例

  本课程的学习目的是研究Redis的网络通信模块,为了说明问题方便,我们使用一个简单的通信实例,即通过redis-cli产生一个可以为“hello”,值为“world”的key-value数据,然后得到redis-server的响应。

127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> 

  读者需要注意的是,我这里说是一个“简单”的实例,其实并不简单。有两个原因:

  -我们是在redis-cli(Redis客户端)输入的命令,这个命令经redis-cli处理后封装成网络通信包,通过客户端的网络通信模块发给redis-server,然后redis-server网络通信模块收到后解析出命令,执行命令后得到结果再封装成相关的网络数据包,返回给redis-cli。这个过程中涉及到两端的网络通信模块使我们研究和学习的重点。

  -redis-server基本的数据类型都是可以通过类似的命令产生,因此这个例子是一个典型的研究redis的典范。

7.3小结

  这节课介绍了我们利用调试Redis源码来学习GDB的一些准备工作和实例代码,有兴趣的读者可以根据本节课中介绍的内容准备一些学习材料,以备后面的进一步学习,从下一课开始我们正式利用GDB来调试Redis。

  

原文地址:https://www.cnblogs.com/wzqstudy/p/10265722.html