当前位置:网站首页>Printf segment error (core dump): a problem caused by formatted output

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

2022-06-12 23:23:00 Overtime ape

1、printf Segment error (core dump): A problem caused by formatted output

Post a simple example :

#include <stdio.h>

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

`[email protected]:test#gcc test.c
test.c: In function ‘main’:
test.c:5:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("%s\n",len);
^`
`
[email protected]:test# ./a.out
Segmentation fault (core dumped)
`

Because the project file is relatively large , use Makefile The compiler did not pay attention to some important compilation warnings , And also upgraded the version , It took two or three days to find it gdb After debugging, I found that it was really printf Cause your own segment error .
Post a project gdb Running error : The thread is called CoreThread Received signal segment error occurs

(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>, [email protected]=0x7fffed7f9cf8) at vfprintf.c:1632
1632    vfprintf.c: No such file or directory.

2、 use gdb Of “bt” Command to display the current call stack

(gdb) bt
#0  0x00007ffff6d95cd0 in _IO_vfprintf_internal (s=0x7ffff710c620 <_IO_2_1_stdout_>, 
    format=<optimized out>, [email protected]=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 ([email protected]=0, [email protected]=0x7fffed7f9ea0, 
    [email protected]=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*) ()
#7  0x00007ffff71186ba in start_thread (arg=0x7fffed7fa700) at pthread_create.c:333
#8  0x00007ffff6e4e51d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
(gdb) 

3、 Find the cause of the error

according to gdb The current call stack information is located. #2 This line found startPushStream In this function printf, New version found session_id The type of int, And here printing is used %s, Caused a segment error , Because of the old version of session_id The type is char * So there are no mistakes .

Be flexible when your program has segment errors gdb It can speed up the speed of locating errors and save time , Instead of adding printing, which is a cumbersome debugging and positioning method .

4、gdb Some commonly used methods are

1、 Run the command

run: Short for r , Its function is to run the program , When a breakpoint is encountered , The program stops at the breakpoint , Wait for the user to enter the next command .
continue ( Abbreviation c ): Carry on , To the next breakpoint ( Or end of operation )
next:( Abbreviation n), Single step tracker , When a function call is encountered , Also does not enter the body of this function ; This command is the same as step The main difference is ,step User defined function encountered , Step into the function to run , and next Call the function directly , Does not enter the function body .
step ( Abbreviation s): Single step debugging if there is a function call , Then enter the function ; And command n Different ,n Is a function that does not enter the call
until: When you're tired of single step tracking in a loop , This command runs the program until it exits the loop .
until+ Line number : Run to a line , Not just to jump out of the loop
finish: Run the program , Until the current function returns , And print the stack address, return value and parameter value when the function returns .
call function ( Parameters ): Calling functions visible in the program , And transmission “ Parameters ”, Such as :call gdb_test(55)
quit: Short for q , sign out gdb

2、 To set breakpoints

break n ( Abbreviation b n): In the n Set breakpoint at line
( You can bring the code path and code name : b OAGUPDATE.cpp:578)
b fn1 if a>b: Conditional breakpoint settings
break func(break Abbreviation for b): In function func() Set breakpoints at the entrance of , Such as :break cb_button
delete Breakpoint number n: Delete the first n A breakpoint
disable Breakpoint number n: Suspend the second n A breakpoint
enable Breakpoint number n: Open the n A breakpoint
clear Line number n: Clear section n The breakpoint of the line
info b (info breakpoints) : Display the breakpoint setting of the current program
delete breakpoints: Clear all breakpoints :

3、 View source code

list : Short for l , Its function is to list the source code of the program , Default per display 10 That's ok .
list Line number : The current file is displayed to “ Line number ” Before and after the center 10 Line code , Such as :list 12
list Function name : Will be displayed “ Function name ” Source code of the function , Such as :list main
list : With no arguments , Will follow the last time list Ordered , Output the contents below .

4、 Print expressions

print expression : Short for p , among “ expression ” Can be any valid expression for the program currently being tested , For example, it is currently being debugged C Language program , that “ expression ” It could be anything C Valid expressions for language , Including digital , Variables and even function calls .
print a: The integer is displayed a Value
print ++a: Will put a Add... To the value in 1, And show it
print name: The string is displayed name Value
print gdb_test(22): Will be expressed as an integer 22 Call... As a parameter gdb_test() function
print gdb_test(a): Will be in variable a Call... As a parameter gdb_test() function
display expression : It will be very useful when running in a single step , Use display Command after setting an expression , It will be executed after each single step instruction , Then output the set expression and value . Such as : display a
watch expression : Set up a watch point , Once you're under surveillance “ expression ” The value of the change ,gdb The program being debugged will be forcibly terminated . Such as : watch a
whatis : Query variable or function
info function: Query function
Expand info locals: Show all variables for the current stack page

5、 View the run information

where/bt : List of currently running stacks ;
bt backtrace Display the current call stack
up/down Change the depth of the stack display
set args Parameters : Specify runtime parameters
show args: View the set parameters
info program: To see if the program is running , Process number , Reason for suspension .

6、 Split window

layout: Used to split windows , You can view the code , While testing :
layout src: Show source window
layout asm: Show Disassembly window
layout regs: Show source code / Disassembly and CPU register window
layout split: Display the source code and Disassembly window
Ctrl + L: Refresh the window

原网站

版权声明
本文为[Overtime ape]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280955508965.html