gdb freezes in malloc

前端 未结 2 922
眼角桃花
眼角桃花 2020-12-21 08:04

Suppose I have some C program like this:

#include 
#include 

int main()
{
    while (true) {
        void *p = malloc(1000)         


        
相关标签:
2条回答
  • 2020-12-21 08:37

    If the 'finish' solution doesn't work for you. Here is another idea.

    You can check if you are in malloc when you break the program. Based on the boolean in/out you skip calling the print commands. Here is a working example.

    # gdb script: pygdb-logg.gdb
    # easier interface for pygdb-logg.py stuff
    # from within gdb: (gdb) source -v pygdb-logg.gdb
    # from cdmline: gdb -x pygdb-logg.gdb -se test.exe
    
    # first, "include" the python file:
    source -v pygdb-logg.py
    
    # define shorthand for inMalloc():
    define inMalloc
      python inMalloc()
    end
    

    The associated python file:

    # gdb will 'recognize' this as python
    #  upon 'source pygdb-logg.py'
    # however, from gdb functions still have
    #  to be called like:
    #  (gdb) python print logExecCapture("bt")
    
    import sys
    import gdb
    import os
    
    def logExecCapture(instr):
      # /dev/shm - save file in RAM
      ltxname="/dev/shm/c.log"
    
      gdb.execute("set logging file "+ltxname) # lpfname
      gdb.execute("set logging redirect on")
      gdb.execute("set logging overwrite on")
      gdb.execute("set logging on")
      gdb.execute("bt")
      gdb.execute("set logging off")
    
      replyContents = open(ltxname, 'r').read() # read entire file
      return replyContents
    
    # in malloc?
    def inMalloc():
      isInMalloc = -1;
      # as long as we don't find "Breakpoint" in report:
      while isInMalloc == -1:
        REP=logExecCapture("n")
    #Look for calls that have '_malloc' in them 
        isInMalloc = REP.find("_malloc")
        if(isInMalloc != -1):
    #       print ("Malloc:: ", isInMalloc, "\n", REP)
           gdb.execute("set $inMalloc=1")
           return True
        else:
    #       print ("No Malloc:: ", isInMalloc, "\n", REP)
           gdb.execute("set $inMalloc=0")
           return False
    
    gdb -x pygdb-logg.gdb -se test.exe
    

    From the command line or script,

    (gdb) inMalloc
    (gdb) print $inMalloc
    

    From an actual test program:

    Program received signal SIGINT, Interrupt.
    0x00007ffff7a94dba in _int_malloc (av=<optimized out>, bytes=1) at malloc.c:3806
    3806    malloc.c: No such file or directory.
    (gdb) inMalloc
    (gdb) if $inMalloc
     >print $inMalloc
     >end
    $1 = 1
    

    I believe your script can use a similar 'if' structure to do/not do printf

    Most of this was knocked off from here

    0 讨论(0)
  • 2020-12-21 08:50

    The reason you're froze is probably a lock that's being held by your program, and is also required by printf. When you try to aquire it twice - you fail.

    A possible WA is when breaking your program to call printf, just before you make the call, type finish - it will cause the current function to complete and return to the main frame. This will ensure the lock is free before you call printf.

    0 讨论(0)
提交回复
热议问题