Printf segment error (core dump) : a problem caused by formatting output

Post a simple example:

#include <stdio.h>

int main(a){
	int len = sizeof(int);
	printf("%s\n",len);
	return 0;
}

Copy the code

root@ubuntu:test# GCC test.c test.c: In function ‘main’ : test.c:5:2: warning: Format ‘%s’ kripke argument of type’ char * ‘, but argument 2 has type ‘int’ [-wformat =] printf(“%s\n”,len); ^ root@ubuntu:test# ./a.out Segmentation fault (core dumped)

Because of the size of the project file, I didn’t notice some important compilation warnings when I compiled with the Makefile, and I upgraded the version. After two or three days of debugging with GDB, I realized that printf actually caused my segment error. GDB runtime error: thread name CoreThread received signal segment error

(gdb) bt
Thread 12 "CoreThread" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffed7fa700 (LWP 21396)]
0x00007ffff6d95cd0 in _IO_vfprintf_internal (s=0x7ffff710c620 <_IO_2_1_stdout_>, 
    format=<optimized out>, ap=ap@entry=0x7fffed7f9cf8) at vfprintf.c:1632
1632	vfprintf.c: No such file or directory.
Copy the code

2, use GDB”bt“Displays the current call stack

(gdb) bt
#0  0x00007ffff6d95cd0 in _IO_vfprintf_internal (s=0x7ffff710c620 <_IO_2_1_stdout_>, 
    format=<optimized out>, ap=ap@entry=0x7fffed7f9cf8) at vfprintf.c:1632
#1  0x00007ffff6e5daef in ___printf_chk (flag=1, format=<optimized out>)
    at printf_chk.c:35
#2  0x000000000041f10c in printf (
    __fmt=0x5b3bc0 "%s, startPushStream success, channel: %d, session_id:%s; lv_stream_type_e:%d, bastime: %d, offset: %d, worktype: %d\n")
    at /usr/include/x86_64-linux-gnu/bits/stdio2.h:104
#3  StartPushStream (channelId=channelId@entry=0, session=session@entry=0x7fffed7f9ea0, 
    type=type@entry=SDK_LV_STREAM_CMD_LIVE) at nvr_demo/nvrCallback.cpp:235
#4  0x000000000042004c in NvrStartPushStreamingCb (channelId=0, 
    session=0x7fffed7f9ea0, param=0x7fffd400be20) at nvr_demo/nvrCallback.cpp:584
#5  0x0000000000417140 in StartNvrPushStreamingCb (auth=0x1407f80, param=0x7fffd400be20)at .. /src/core_cb/cb_to_cloud.c:885 #6 0x0000000000462619 in linkvisual::JobPool::jobProcess(void*) (a)
#7  0x00007ffff71186ba in start_thread (arg=0x7fffed7fa700) at pthread_create.c:333
#8  0x00007ffff6e4e51d in clone (a)at .. /sysdeps/unix/sysv/linux/x86_64/clone.S:109(gdb) 

Copy the code

3. Find the cause of the error

According to the current call stack information we locate the GDB line # 2 * * * * this find startPushStream inside this function printf, found the new version of the type of session_id for int, and print it with % s here, led to the error, There is no error because the old version of session_id is char *.

The flexible use of GDB can speed up the location of errors and save time when their own programs appear segment errors, rather than printing this cumbersome debugging location method.

4. Some common GDB uses methods

1. Run commands

Run: r. It runs the program. When a breakpoint is encountered, the program stops running at the breakpoint and waits for the user to enter the next command.

Continue – C: Execution continues until the next breakpoint (or the end of the run)

Next :(short n), a single step tracer that does not enter the function body when a function call is encountered; The main difference between this command and step is that step is used

To a user-defined function, you step into the function, while next calls the function directly without going into the function body.

Step – S: Step into a function if there is a function call; Unlike the command n, n does not enter the called function

Until: When you get tired of stepping through a loop, this command will run the program until you exit the loop.

Until + Line number: Run to a line, not just to break out of a loop

Finish: Runs the program until the current function returns, printing the stack address and return values and parameter values when the function returns.

Call function (arguments) : Calls functions visible in the program and passes “arguments”, such as call gdb_test(55)

Quit: q is used to exit GDB

2. Set breakpoints

Break n (short b n) : Sets a breakpoint at line n

(You can bring the code path and the code name: boagupdate.cpp :578)

B fn1 if A > B: Conditional breakpoint setting

Break func(break shortened to b) : Set a breakpoint at the entry of func(), for example, break cb_button

Delete Breakpoint number n: deletes the NTH breakpoint

Disable Breakpoint number n: breaks the NTH breakpoint

Enable Breakpoint number n: enables the NTH breakpoint

Clear Line n: Clears the breakpoint on line n

Info breakpoints (INFO B) : displays the breakpoint Settings of the current program

Delete breakpoints: clear all breakpoints:

3, view the source code

List: lists the source code of the program. By default, 10 lines are displayed at a time.

List Line number: Displays the first and last 10 lines of the current file centered on Line number, for example, list 12

List function name: Displays the source code for the function name, such as List main

List: no arguments, will continue the last list command, output the following content.

4. Print the expression

Print expression: short for p, where “expression” can be a valid expression of any program currently being tested, such as the program currently debugging C, so “expression” can be a valid expression of any C language, including numbers, variables and even function calls.

Print a: will display the value of integer a

Print ++a: Increases the value of a by 1 and displays it

Print name: displays the value of the string name

Print gdb_test(22) : call gdb_test() with the integer 22

Print gdb_test(a) : call gdb_test() with variable A as argument

Display expressions: This is very useful when running in a single step. After setting an expression with the display command, it will output the set expression and value immediately after each step. Such as: display a

Watch expression: Sets up a monitoring point where GDB forcibly terminates the program being debugged if the value of the monitored “expression” changes. Such as: watch a

Whatis: Query variables or functions

Info function: query function

Extension Info locals: Displays all variables of the current stack page

5. View the running information

Where /bt: currently running stack list;

Bt Backtrace displays the current call stack

Up/Down changes the depth of the stack display

Set args parameter: Specifies the run-time parameter

Show args: View the configured parameters

Info program: to check whether the program is running, the process number, and the reason for the suspension.

6. Split Windows

Layout: Used to split Windows and test while viewing code:

Layout SRC: Displays the source code window

Layout ASM: Displays the disassembly window

Layout Regs: Displays source code/disassembly and CPU register Windows

Layout split: Displays the source code and disassembly window

Ctrl + L: Refreshes the window