UBUNTU:QEMU+GDB Debugging Linux Kernel learning notes

Source: Internet
Author: User
Tags what debugging

Statement:

The content of this note is not my original, 90% from the integration of network information. At the same time, because they are just contact with QEMU & gdbserver Remote Debug, this article is not even a tutorial, only for the reference of the people.

------------------------------------------------------------------------------------------------Split Line---------------------- -----------------------------------------------------

Step 1:kernel Compilation Environment installation

  

Apt-cache search build-essentialsudo apt-get install build-essential-yapt-cache Search libncurses-devsudo Apt-get install Libncurses-dev-y

Of course, there may be other tools, if the GCC g++ make tools, after all, build-essential is a tool box, if there is a neat, it may be a bit of a conflict. And Ncurses-dev, this is a must have, I remember in Fedora is the direct yum install Ncurses-dev can,. The Deb series seems to have added a prefix.

Installation of Step 2:gdb

What needs to be told is that Build-essential should contain a gdb & Gdbsever tool, but unfortunately it is not available and this error will occur:

Remote'g'Packet reply isTooLong: 000000000000000020000000000000004000000000000000001006000000000000F009000000000028AECE81FFFFFFFF981FC081FFFFFFFF901FC08 1ffffffff0030c1010000000000000000000000000000000000000000f0b926020000000020f1d281ffffffffb01fc081ffffffff00e0e681ffffffff 0010e781ffffffff02fbd281ffffffff96000000100000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000007f03000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000801f0000 

So, we need to download a relatively new GDB source down (I use 7.8), the URL is: http://ftp.gnu.org/gnu/gdb/

  

http://ftp.gnu.org/gnu/gdb/

Then according to the information that people share before the network, modify the source of gdb: static void Process_g_packet in gdb-7.8/gdb/remote.c file (struct Regcache *regcache) The function modifies some of the contents as follows:

1 Static void2Process_g_packet (structRegcache *Regcache)3 {4   structGdbarch *gdbarch =Get_regcache_arch (regcache);5   structRemote_state *rs =get_remote_state ();6   structRemote_arch_state *rsa =get_remote_arch_state ();7   intI, Buf_len;8   Char*p;9   Char*regs;Ten  OneBuf_len = strlen (rs->buf); A  -   /*further sanity checks, with knowledge of the architecture. */ - /*if (Buf_len > 2 * rsa->sizeof_g_packet) the Error (_ ("Remote ' G ' packet reply is too long:%s"), rs->buf); - */ -   /*Modify by XX*/ -   if(Buf_len >2* rsa->sizeof_g_packet) { +Rsa->sizeof_g_packet =Buf_len; -      for(i =0; I < Gdbarch_num_regs (Gdbarch); i++) { +         if(Rsa->regs[i].pnum = =-1)     A             Continue; at         if(Rsa->regs[i].offset >= rsa->sizeof_g_packet) -Rsa->regs[i].in_g_packet =0; -         Else -Rsa->regs[i].in_g_packet =1; -     } -   } in   //....... -     } to     } +}

The above 14-15 lines is the original file, and 18-27 lines is re-added on the < in order to save the layout, I did not post, so, notice "{}" may lead to grammatical errors, and so the specific principle of the modification, I am not clear, but did solve the problem.

Once the changes are complete, start compiling gdb. Under Gdb-7.8/, execute the following command:

1 ./configure--prefix=. /.. /tools/23 make install

Note that there is no makefile file in the gdb-7.8/directory and you need to use./configure to produce it. In the configuration, if you want to specify the GDB installation path (directory), then you need to keep up with the--prefix= $PATH parameters, generally this situation may be for the system already has a gdb but can not be used, but also not deleted, then the newly compiled GDB may need to install in another directory. Of course my own is installed in the. /.. The/tools/directory.

Step 3: Compiling Linux kernel

Go to www.kernel.org Download the version you need, and after that, build Bzimage & vmlinux files for kernel compilation. If you are just getting started with me, you can refer to the following commands & steps:

  

CD  linux-3.12.  //boot/config-3.13. 0-generic-configmake menuconfig<save> makebzimage

Need to explain is, specifically to see what debugging information, should be in make menuconfig to configure, go to choose, and then save the good, then compile. After compiling, bzimage this is compressed, for the use of QEMU virtual machine, vmlinux with some information, there is no compression, for GDB to use.

When the compilation is finished, you can copy the Vmlinux bzimage file to a clean directory---this with their own habits, not copy also does not matter.

The above forgot to prepare the most important things: QEMU

Use of step 4:qemu

Simply put: QEMU is a virtual machine, can simulate x86 & arm and so on hardware platform < seems to simulate a lot of hardware platform ...; and QEMU has a gdbserver embedded in it. This gdbserver then can and GDB form a remote partner, through the Ip:port network mode or through the serial port/dev/ttys* to work, one in this head, one in the head.

As for the installation of the QEMU virtual machine, it can be compiled by source code, makes & make install, which can be downloaded here: http://wiki.qemu.org/Download. You can also apt-get install QEMU-KVM directly in the Ubuntu software package. Not in detail here. When installed, the possible files are these:

1 qemu-system-i38623 qemu-system-x86_6445 qemu- img 6 7 qemu-io8 ....

What does this mean? The first line represents the QEMU virtual machine used on the i386 machine, and the second line represents the virtual machine used on the x86_64. The other has not been used. For details, please refer to the official website document: Http://wiki.qemu.org/Main_Page. Of course, my own system is x86_64, using the second one.

Step 4: Let kernel take a moment for you

  

1 2 -gdb tcp::1234-  s

Start qemu with the command first.

qemu-system-x86_64 parameters are more, here is simple:

-kernel is specifying a large kernel file, which is bzimage.

-INITRD is to specify a initrd.img file, which can be copied from/boot/initrd.img-3.13.0-43-generic, what is it about? You can refer to this: http://www.linuxfly.org/post/94/, or this http://blog.csdn.net/chrisniu1984/article/details/3907874.

-SMP can guess from the name, it is to give qemu to specify a few processors, or a few threads < well, probably the meaning of thread >.

-gdb is the embedded gdbserver that initiates qemu, listening to the local TCP port 1234---If this is the case:-gdb tcp:192.168.1.100:1234, it seems to be fine.

-S is the hang up gdbserver, let GDB remote connect it. There is also a-s, that is another situation to use.

If you have a problem with the command, < Although that is cool, you can save the command to a file using the following methods, such as Qemu.start:

  

1 #!/bin/bash22 -gdb tcp::1234  -S3 <save> 4 chmod  +x qemu.start56 ./qemu.start

So you can start QEMU. < Note the path to your bzimage & initrd.img file >

Tip: Man qemu-system-x86_64, you'll get some help.

Step 5: Use GDB to connect to the QEMU that is already started:

  

1.. /tools/gdb/bin/gdb Vmlinux2 3-----4GNU gdb (GDB)7.85Copyright (C) theFree Software Foundation, Inc.6License gplv3+: GNU GPL version3or later //gnu.org/licenses/gpl.html>7This isFree software:you is free-to-change and redistribute it.8There isNO WARRANTY, to the extent permitted by law. Type"Show Copying"9and"Show Warranty"  fordetails.TenThis GDB is configured as "X86_64-unknown-linux-gnu". OneType"Show Configuration"  forconfiguration details. A for bugs reporting instructions, please see: -//www.gnu.org/software/gdb/bugs/>. - Find the GDB manual and other documentation resources online at: the//www.gnu.org/software/gdb/documentation/>. -For help, type" Help". -Type"Apropos Word"To search forCommands related to"Word"... -Reading symbols fromVmlinux...done. +----- -  +(GDB) Target remote:1234 ARemote debuggingusing:1234 at 0x0000000000000000 inchirq_stack_union () -  - (gdb) b start_kernel -Breakpoint1At0XFFFFFFFF81D2FB02: File Init/main.c, line476. -  - (GDB) C in continuing. -  toBreakpoint1, Start_kernel () at INIT/MAIN.C:476 + 476    { -  the  * (GDB) n $ 485smp_setup_processor_id ();Panax Notoginseng (GDB) n - 491boot_init_stack_canary (); the (GDB) n + 493Cgroup_init_early ();

The first line indicates that I started my own compiled GDB, which is two ways: gdb filename starts, or gdb starts, and then uses file FileName to start

Line 21st indicates the connection to the remote Gdbserver, because this is on the same laptop, there is no IP address specified, only the port number is specified. ----Of course, if it is connected to the UART port, it is OK.

Line 25th, is break a breakpoint at the entrance of a function.

Line 28th, should be sent a command, let QEMU continue to run the meaning, this time, the screen on the QEMU side will flash out: "Booting from ROM ..."

Back, that's what the next step means: Next ... next ...  Of course, you can also choose step step s .... Where do you print messages on QEMU? To be in Console_init (); This line of code will not be.

Make a slightly important note: I do not have the file system enabled here, if necessary, you can try to do one with BusyBox, and then refer to the QEMU kernel debugging manual, or network resources to join the debugging.

--------------------------------------------------------------------------------------RIP Split Line------------------------------ --------------------------------------------------------------

Postscript:

As for the GDB command, there are many, if it is new to kernel, please click here: http://www.sourceware.org/gdb/the best is to slowly bite the official website documents, and then look at others understanding, should be almost. Or look at this: http://www.yolinux.com/TUTORIALS/GDB-Commands.html

These days have been searching for kernel debug method, Qemu+gdb is a kind of, of course there are other methods. But in summary, the price is QEMU+GDB minimum. If you are a big screen pc, try to incorporate qemu+gdb+eclipse into the IDE environment. And you are a notebook, you do not make the IDE, under the vim shell enough.

Slightly mixed with a little idea: The front is also read some operating system books +linux kernel primer, but have never had the chance to try to run the following kernel, see how it goes < the way the construction of the book costs relatively large: or two computers, Or the kind of > that will be given up in half. Over time, also slack, let alone go to detailed look at the code. Linux kernel is really great, but there's no need to myth about it. If you understand the operating system principle and program design to a certain level, you can also complete an OS---although it may be very rough. Therefore, it is necessary to know the unity of the line.

Finally, if you really interested in kernel, then at least have to engage in English, and then early attention to kernel maillist----Although it is original aim, but the world is also the development of change. There is a time limit on how much the book is.

Keyword: qemu kernel gdb gdbserver Debug

UBUNTU:QEMU+GDB Debugging Linux Kernel learning notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.