The 10th chapter debugging technology of Embedded Linux

Source: Internet
Author: User

One, prevent function PRINTK reduce Linux performance:

Take advantage of the compiler directives in the C language (#if, #else, #endif等).

Now modify the Printk_demo driver code, define a Pr_debug macro by compiling the instructions, and control whether the PRINTK function is called by modifying the conditional value of the compilation directive. As follows:

# if 1//here is 1, use PRINTK function, 0, ignore PRINTK function

#define PR_DEBUG (x,...) Do {} while (0)

#endif

In addition to this, we need to know two knowledge points: 1. Macro with variable parameters: The difference between a variable-parameter macro and a fixed-parameter macro: A variable parameter macro needs to get variable parameters for a variable parameter's macro by _VA_ARGS_ the macro "does not support the case of variable parameter number 0." Defining a mutable parameter macro is the same as defining a variadic function, using 3 points (...). ) to represent a mutable parameter, the variable parameter must be the last parameter of the macro and function.

Ii. data interaction through a virtual file system (/PROC):

Necessity: In Linux file systems,/proc is often used as a tool for data interaction in kernel space. /proc is a virtual file system, which means that/proc is not a real file system, but a memory map. All read and write/PROC operations Military to memory read and write operations. So reading and writing/proc file systems is much faster than reading/writing/dev. As a result, the/proc file system can also serve as a tool for Linux to interact with user-space programs.

A lot of information is provided through the/proc file system by the kernel space program to the outside session. For example: The current system memory resource is read through the/proc/meminfo file, the reader can use the following command: View the contents of the/proc/meminfo file: cat/proc/ Meminfo. We can see if the displayed information matches some of the content in the Meminfo file by executing the free command.

In Linux drivers, you can use kernel functions to create and delete virtual files in the/proc directory, as well as to establish and delete virtual directories.

    1. 1. Create_proc_entry create proc file
      struct Proc_dir_entry *create_proc_entry (const char *name, mode_t mode,structproc_dir_entry *parent);

Name: The filename to be created;

Mode: The protection mask for the file;

Parent: Determines the directory in which the file resides and, if NULL, the location is/proc.

    1. 2. Proc_mkdir Create a directory

/* The function creates a directory under parent directory name* */

struct Proc_dir_entry * PROC_MKDIR (const char *name,structproc_dir_entry *parent);

@name: The name of the directory to create

@parent: The parent directory of this directory

3.remove_proc_entry Deleting a file or directory

/* This function deletes a file or directory from the proc file system.

* Note: 1. Is removed by the parameter name, rather than by the pointer returned at creation time.

* 2. This function does not recursively delete files under the directory.

* 3. The data variable holds the allocated memory, freeing the corresponding memory before deleting the file.

* */

void Remove_proc_entry (const char *name,struct proc_dir_entry*parent);

@name: The name of the file or directory to be deleted

@parent: The parent directory where

4.create_proc_read_entry Create a read-only proc file

struct Proc_dir_entry * create_proc_read_entry (const char

*name,mode_t mode,struct proc_dir_entry *parent,read_proc_t*

Read_proc,void *data);

@name: The file name to create

@mode: The properties of the file to be created default 0755

@parent: The parent directory of this file

@read_proc: When the user reads this file, the kernel calls the function

@data: Parameters passed to Read_proc

Note: Before deleting the virtual file directory, delete the virtual files in the directory.

Executing the build.sh script file installs the Proc_demo driver on Ubuntu Linux, the Development Board, or the Android virtual machine. Then execute the following command to view the contents of the/proc/proc_demo directory.

Ls–al/proc/proc_demo/bin2dec

Cat/proc/proc_demo/bin2dec

Cat/proc_demo/readonly

Three, Debugging Tools:

    1. To debug a user-space program with GDB:

GDB can track programs that debug user space.

For a test executable (GDB_DEBUG.C), we can run the build.sh script file directly, but note that with the command parameter-G, the complete compile command is as follows:

#gcc –static–g–o gdb_debug/root/drivers/debug/gdb_debug.c

Now use the following command to debug:

Gdb Gdb_debug

GDB contains the commands:

1> quit: Used to exit the GDB debug interface

2> list: Used to list code in a program. There are three invocation formats: list: Displays the 10 rows following the last line that was called the output of the list command. list-: Displays the last 10 lines preceding the first line of the list command output, and the first call to the List command will not show anything. List N: Displays 10 rows near the nth row, typically showing the first 5 rows and 4 rows, plus the nth row, which is exactly 10 rows.

3> BREAKN: Sets the specified line to a breakpoint, n indicates the line number

4> Clearn: Clears the breakpoint for the specified line.

5> TBREAKN: Sets the specified line as a breakpoint, the breakpoint can only be used once, and automatically zeroing out after use is complete.

6> cont/continue: Skips the current breakpoint to continue execution. [cont: Skips the current breakpoint to continue execution; Cont N: Skips the N-times breakpoint and continues]

7> Next: Continue with the following statement, but skip this program. Equivalent to step over. [Ibid. there are two formats next and Nextn]

8> Nexti: Step into the statement, and next difference: it will be traced to the inside of the subroutine, but does not print out the statement inside the subroutine.

9> printvar_name: View variable values.

    1. To debug a user-space program remotely with Gdbserver:

1. The difference from GDB: GDB is used for testing on the PC, while the Gdbserver test runs on the Development Board, mobile phone, Android emulator.

2. Open the test program on the Development Board using Gdbserver and then the serial, wired, wireless network can be tested on the PC.

3. First step: Enter the Android emulator terminal, then enter the Data/local directory, and execute the following command: Gdbserver:4321./gdb_debug start the Gdbserver listener. 4321 means listening using the 4321 port number of this machine.

Step two: Turn on another Linux terminal and forward the 4321 port packet of the external access simulator to the 4321 port inside the Android emulator: Adb–s emulator-5544 ForWord tcp:4321[ When you have multiple Android devices, specify the specific Android device by adding the-s command line parameter.

The second method [maps Port]:1. Enter Telent:telentlocalhost 5554 2. Map port: Redir add tcp:4321:4321

Step three: Enter the GDB console: Arm-none-linux-guneabi-gdb gdb_debug

Fourth step: Connect the Android Emulator: (GDB) Target remotelocalhost:4321

Finally: Enter the GDB command to debug.

Connect the Gdbserver:1.gdbserver Localhost:4321./gdb_debug on the Development Board via IP 2. The GDB console on the Linux endpoint links the development board to the Gdbsrever: (GDB) target remote192.168.17.103./gdb_debug.

Through the serial port mode: Corresponding should be: 1.gdbserver/dev/s3c2410_serial0./gdb_debug 2. (GDB) Target remote/dev/ttyusb0

    1. To debug kernel programs remotely with KGDB:

In addition to providing log output functions like the PRINTK function, KGDB allows developers to link target devices directly on the PC via GDB.

The KGDB consists of two parts: the KGDB core and a set of link interfaces "currently supports serial TTY device links and Ethernet connections." The serial port connection needs to specify the serial port TTY device to be linked via the kernel parameter KGDBOC: The Ethernet connection specifies the IP and port number via the kernel parameter Kgdboc.

To debug the kernel with KGDB, you first need to configure the Linux kernel. Use the Make Menuconfig command to enter the configuration menu for the Linux kernel. "Kernel Hacking--àkgdb:kernel Debugger"

When configuring kernel parameters, these parameters inform the Linux kernel how to debug. If you want to debug via USB to COM port data cable, you need to fold the Kgdboc parameter value to ttyUSB0, transfer efficiency is 115200, kdbwait is usually specified. These parameters need to press ENTER into Uboot mode during the S3C board process, then use the setenv command to set the startup parameters of the Linux kernel, and then use the saveenv and Rest commands to save and restart the Linux kernel.

After the setup is complete, the host can use the GDB command to debug the Linux kernel like a normal embedded application, executing the following commands:

#gdb./vmlinux

When you are finished, use the following command to set the transfer rate and connect the Linux kernel to debug as well.

(GDB) Set Remoteband 115200

(GDB) Target remote/dev/ttyusb0

Finally, a variety of GDB commands are used for Linux kernel debugging.

The 10th chapter debugging technology of Embedded Linux

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.