Coder Social home page Coder Social logo

gdb-heap's Introduction

gdb-heap

Original fork derived from https://fedorahosted.org/gdb-heap/. This repo is now considered the official repository for the gdb-heap library.

Installation instructions

  1. To get this module working with Ubuntu 16.04, make sure you have the following packages installed:
sudo apt-get install libc6-dev libc6-dbg python-gi libglib2.0-0-dbg python-ply

The original forked version assumes an "import gdb" module, which resides in "/usr/share/glib-2.0/gdb" as part of the libglib2.0-0-dbg package. Earlier versions of Ubuntu have this library is located in the ibglib2.0-dev package.

There is also a conflict with the python-gobject-2 library, which are deprecated Python bindings for the GObject library. This package installs a glib/ directory inside /usr/lib/python2.7/dist-packages/glib/option.py, which many Gtk-related modules depend. You will therefore need to make sure the sys.path for /usr/share/glib-2.0/gdb is declared first for this reason (see code example).

You'll also want to install python-dbg since the package comes with the debugging symbols for the stock Python 2.7, as well as a python-dbg binary compiled with the --with-pydebug option that will only work with C extensions modules compiled with the /usr/include/python2.7_d headers.

NOTE: The Python binary that accompanies Ubuntu distributions uses link-time optimization compilation. As a result, many of the Python data structures are optimized out and prevent gdb-heap from being able to properly categorize the various data structures. To take advantage of this capability, you will need to download the Python source and recompile without using the -flto option in the CFLAGS/LDFLAGS configuration option. Normally this capability is not used in standard configure so simply compiling it should do the trick. (If you want to have SSL support in this binary, make sure to edit Modules/Setup.dist).

The python-dbg binary is compiled with the Py_TRACE_REFS conditional via the --pydebug which modifies the internal Python data structures and adds two pointers into every base PyObject, preventing previously compiled C extensions to be used. Using your own compiled version of Python is therefore the way to go if you want to take advantage of the categorize features of gdb-heap and/or inspecting the internal memory structures of Python.

  1. Create a file that will help automate the loading of the gdbheap library:

gdb-heap-commands:

python
import sys
sys.path.insert(0, "/usr/share/glib-2.0/gdb")
sys.path.append("/usr/share/glib-2.0/gdb")
sys.path.append("/home/rhu/projects/gdb-heap")
import gdbheap
end

To attach to an existing process, you can execute as follows:

sudo gdb -p 7458 -x ~/gdb-heap-commands

To take a core dump of a process, you can do the following:

1) sudo gdb -p <pid>
2) Type "generate-core-file" at the GDB prompt.
3) Wait awhile (and be careful not to hit enter again, since it will repeat the same command)
4) Copy the core.<pid> file somewhere.

You can then use gdb to attach to this core file:

sudo gdb python <core file> -x ~/gdb-heap-commands

Commands to run

heap - print a report on memory usage, by category
heap sizes - print a report on memory usage, by sizes
heap used - print used heap chunks
heap free - print free heap chunks
heap all - print all heap chunks
heap log - print a log of recorded heap states
heap label - record the current state of the heap for later comparison
heap diff - compare two states of the heap
heap select - query used heap chunks
hexdump <addr> [-c] - print a hexdump, stating at the specific region of memory (expose hex characters with -c option)
heap arenas - print glibs arenas
heap arena <arena> - select glibc arena number

Useful resources

gdb-heap's People

Contributors

davidmalcolm avatar rogerhu avatar spaceone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gdb-heap's Issues

First impression: No module named 'glib', TypeError: must use keyword argument for key function

I just started trying to use this project and stumble on:

(gdb) heap                                                                                                                                                                                                                                                                       
Blocks retrieved 10000                                                                                                                                                                                                                                                           
Blocks retrieved 20000                                                                                                                                                                                                                                                           
Blocks retrieved 30000                                                                        
…                                                                             
Blocks retrieved 460000                                                                            
Blocks retrieved 470000                                                                                  
Blocks retrieved 480000                                                                              
Python Exception <class 'ImportError'> No module named 'glib':                                          
Error occurred in Python command: No module named 'glib'                                                
(gdb) heap sizes                                                                          
Python Exception <class 'TypeError'> must use keyword argument for key function:                           
Error occurred in Python command: must use keyword argument for key function  

Are there missing dependencies? Where do I get glib from?
Can I let me show the tracebacks?

Change README to stop saying "forked from..."

David Malcolm told me on irc that this is the canonical repository for gdb-heap now. I think the README should change to stop saying that it is a fork of the fedora hosted repository (which isn't available any more anyway...).

heap command returns "unorderable types: int() <= NoneType()" error

Hello,

I'm not sure if this is a bug or just wrong usage, but I'm getting the error from the title when trying to use the heap command.

Here you have the whole output, including the commands used and the error (last two rows):

root@host ~ # cat gdb-heap-commands 
python
import sys
sys.path.insert(0, "/usr/share/glib-2.0/gdb")
sys.path.append("/usr/share/glib-2.0/gdb")
sys.path.append("/root/gdb-heap")
import gdbheap
end
root@host ~ # gdb python core.9795 -x gdb-heap-commands
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from python...Reading symbols from /usr/lib/debug//usr/bin/python2.7...done.
done.

warning: core file may not match specified executable file.
[New LWP 9795]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `gunicorn: worker [gunicorn]'.
#0  0x00007f08d4eb2af3 in __epoll_wait_nocancel () at ../sysdeps/unix/syscall-template.S:81
81  ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) heap
Blocks retrieved 10000
Blocks retrieved 20000
Blocks retrieved 30000
Python Exception <class 'TypeError'> unorderable types: int() <= NoneType(): 
Error occurred in Python command: unorderable types: int() <= NoneType()

As you can see, I'm also obtaining an other error about a syscall file not being found, though I cannot say if it has any relation (probably not).

Failed to run heap on Fedora:31

I've tried top setup gdb-heap on my Fedora:31 machine. It feels to me gdb-heap initializes correctly and I'm able to attach to an existing process, gdb-heap commands are registered and most of them work. The issue is when I'm trying to obtain memory information using heap command:

$ gdb -p 127637 -x gdb-heap-commands
GNU gdb (GDB) Fedora 8.3.50.20190824-30.fc31
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
Attaching to process 127637
[New LWP 127648]
[New LWP 127649]
[New LWP 127650]
[New LWP 127651]
[New LWP 127652]
[New LWP 127653]
[New LWP 127654]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Missing separate debuginfo for /home/fpokorny/.local/share/virtualenvs/adviser-1eaKppR3/lib64/python3.6/site-packages/numpy/core/../../numpy.libs/libgfortran-ed201abd.so.3.0.0
Missing separate debuginfo for /home/fpokorny/.local/share/virtualenvs/adviser-1eaKppR3/lib64/python3.6/site-packages/psycopg2/../psycopg2_binary.libs/./libgssapi_krb5-174f8956.so.2.2
Missing separate debuginfo for /home/fpokorny/.local/share/virtualenvs/adviser-1eaKppR3/lib64/python3.6/site-packages/psycopg2/../psycopg2_binary.libs/./libz-a147dcb0.so.1.2.3
Missing separate debuginfo for /home/fpokorny/.local/share/virtualenvs/adviser-1eaKppR3/lib64/python3.6/site-packages/psycopg2/../psycopg2_binary.libs/./libkrb5-fb0d2caa.so.3.3
Missing separate debuginfo for /home/fpokorny/.local/share/virtualenvs/adviser-1eaKppR3/lib64/python3.6/site-packages/psycopg2/../psycopg2_binary.libs/./libk5crypto-622ef25b.so.3.1
Missing separate debuginfo for /home/fpokorny/.local/share/virtualenvs/adviser-1eaKppR3/lib64/python3.6/site-packages/psycopg2/../psycopg2_binary.libs/./libcom_err-beb60336.so.2.1
Missing separate debuginfo for /home/fpokorny/.local/share/virtualenvs/adviser-1eaKppR3/lib64/python3.6/site-packages/psycopg2/../psycopg2_binary.libs/./libkrb5support-d7ce89d4.so.0.1
Missing separate debuginfo for /home/fpokorny/.local/share/virtualenvs/adviser-1eaKppR3/lib64/python3.6/site-packages/psycopg2/../psycopg2_binary.libs/./libkeyutils-1-ff31573b.2.so
Missing separate debuginfo for /home/fpokorny/.local/share/virtualenvs/adviser-1eaKppR3/lib64/python3.6/site-packages/psycopg2/../psycopg2_binary.libs/./libselinux-cf8f9094.so.1
Missing separate debuginfo for /home/fpokorny/.local/share/virtualenvs/adviser-1eaKppR3/lib64/python3.6/site-packages/psycopg2/../psycopg2_binary.libs/./libsepol-b4f5b513.so.1
0x00007f65a169da3f in __GI___poll (fds=0x7ffffe9223f0, nfds=1, timeout=-1)
    at ../sysdeps/unix/sysv/linux/poll.c:29
29	  return SYSCALL_CANCEL (poll, fds, nfds, timeout);
(gdb) heap sizes
Chunk size  Num chunks  Allocated size
----------  ----------  --------------
       944        6093       5,751,792
     1,120        3013       3,374,560
       592        4443       2,630,256
     2,224         682       1,516,768
     4,640         316       1,466,240
       544        1591         865,504
    36,912          14         516,768
     9,264          49         453,936
     4,288          79         338,752
    18,464          18         332,352
     4,400          75         330,000
       576         503         289,728
     4,384          64         280,576
       976         234         228,384
     4,272          52         222,144
    73,760           3         221,280
     4,304          50         215,200
     2,064         100         206,400
       624         327         204,048
     8,208          24         196,992
    32,832           6         196,992
       560         305         170,800
       720         230         165,600
       896         169         151,424
       608         249         151,392
       672         223         149,856
       528         281         148,368
     4,416          33         145,728
       640         213         136,320
     1,344          97         130,368
       688         180         123,840
     1,744          70         122,080
     1,024         118         120,832
     1,280          92         117,760
     1,728          67         115,776
    16,400           7         114,800
       656         174         114,144
        32        3544         113,408
       704         156         109,824
     2,256          48         108,288
     1,104          97         107,088
     1,392          76         105,792
       784         130         101,920
     1,056          95         100,320
       800         125         100,000
     4,320          23          99,360
--Type <RET> for more, q to quit, c to continue without paging--q
Quit
(gdb) heap
Blocks retrieved 10000
Blocks retrieved 20000
Blocks retrieved 30000
Python Exception <class 'gdb.error'> There is no member named str.: 
Error occurred in Python: There is no member named str.
(gdb) 

Installed packages:

gdb-heap-0.5-36.20191013gitf3dcc53.fc31.x86_64
glib2-2.62.6-1.fc31.x86_64
glib2-devel-2.62.6-1.fc31.x86_64

Running Python3.6.

Thanks for any pointers.

heap command is giving error

Environment: CentOS-7.5
Python - python27
Core - C++ core file
[root@nodea14 gdb-heap]# gdb ~/contrail-vrouter-agent.debug ~/core.contrail-vroute.3812.compute-2zgo.dus1.cloud.ecg.so.1591439024 -x gdb-heap-commands

(gdb) heap
Python Exception <type 'exceptions.TypeError'> iter() returned non-iterator of type 'ProgressNotifier':
Error occurred in Python command: iter() returned non-iterator of type 'ProgressNotifier'

Could you please help me how to fix the above error ?

Error on using heap command.

I am trying to use the gdb-heap for getting the heap memory allocation of my program. I am doing this, "gdb -p 3108 -x ~/Documents/gdb-heap-commands",
after following the instructions out here. Then I did heap, and I got this, "Python Exception <type 'exceptions.TypeError'> iter() returned non-iterator of type 'ProgressNotifier':
Error occurred in Python command: iter() returned non-iterator of type 'ProgressNotifier'"

No symbol "main_arena" in current context.

I currently have a docker setup with GDB, like this

I'm trying to debug a python3 app, I have python3-dbg installed on the docker container with GDB.

[Current thread is 1 (LWP 9335)]
Traceback (most recent call last):
  File "<string>", line 5, in <module>
  File "/home/gdb-heap/gdbheap.py", line 17, in <module>
    from heap.commands import register_commands
  File "/home/gdb-heap/heap/commands.py", line 21, in <module>
    from heap.glibc import glibc_arenas
  File "/home/gdb-heap/heap/glibc.py", line 445, in <module>
    glibc_arenas = GlibcArenas()
  File "/home/gdb-heap/heap/glibc.py", line 415, in __init__
    self.main_arena = self.get_main_arena()
  File "/home/gdb-heap/heap/glibc.py", line 420, in get_main_arena
    return gdb.parse_and_eval("main_arena")
gdb.error: No symbol "main_arena" in current context.
/home/gdb-heap-commands:7: Error in sourced command file:

Seems similar to hugsy/gef#81

There is no member named size

Hi, not sure if this project is being maintained anymore, but looking for some tips (I'm not that experiences with GDB but trying to diagnose a memory leak). Getting this error when running the program:

(gdb) set python print-stack full
(gdb) heap
Traceback (most recent call last):
  File "/app/gdb-heap/heap/commands.py", line 34, in g
    return f(self, args, from_tty)
  File "/app/gdb-heap/heap/commands.py", line 56, in invoke
    usage_list = list(lazily_get_usage_list())
  File "/app/gdb-heap/heap/__init__.py", line 502, in lazily_get_usage_list
    usage_list = list(iter_usage_with_progress())
  File "/app/gdb-heap/heap/__init__.py", line 612, in __next__
    return self.inner.__next__()
  File "/app/gdb-heap/heap/__init__.py", line 677, in iter_usage
    chunksize = chunk.chunksize()
  File "/app/gdb-heap/heap/glibc.py", line 75, in chunksize
    return self.size() & ~(self.SIZE_BITS)
  File "/app/gdb-heap/heap/glibc.py", line 71, in size
    self._cached_size = int(self.field('size'))
  File "/app/gdb-heap/heap/__init__.py", line 117, in field
    return self._gdbval[attr]
gdb.error: There is no member named size.
Error occurred in Python command: There is no member named size.

If it helps, using python3.7-dbg for all of this, running inside a Docker container.

No module named 'cpython'

This is probably also related to #18 and the migration of gdb to python 3 stated by @rogerhu.

The question is that I tried the same commands sequence as in #18 in a different machine, and this is what I got:

(gdb) heap
Blocks retrieved 10000
Blocks retrieved 20000
Blocks retrieved 30000
Blocks retrieved 40000
Blocks retrieved 50000
Blocks analyzed 10000
Blocks analyzed 20000
Blocks analyzed 30000
Blocks analyzed 40000
Blocks analyzed 50000
Traceback (most recent call last):
  File "gdb-heap/heap/commands.py", line 34, in g
    return f(self, args, from_tty)
  File "gdb-heap/heap/commands.py", line 56, in invoke
    usage_list = list(lazily_get_usage_list())
  File "gdb-heap/heap/__init__.py", line 503, in lazily_get_usage_list
    categorize_usage_list(usage_list)
  File "gdb-heap/heap/__init__.py", line 536, in categorize_usage_list
    from cpython import python_categorization
ImportError: No module named 'cpython'
Error occurred in Python command: No module named 'cpython'

Unable to install & use 'gdb-heap' on Ubuntu for post-debugging with coredump

I have a coredump generated on other device for my C code. I am trying to debug that core on my system, considering memory corruption as probable cause.
Please provide steps to install & use 'gdb-heap' on my system. Please help me.

/home/user1$ cat gdb-heap-commands
python
import sys
sys.path.insert(0, "/usr/share/glib-2.0/gdb")
sys.path.append("/usr/share/glib-2.0/gdb")
sys.path.append("/home/user1/gdb-heap")
import gdbheap
end

/home/user1$ gdb -x gdb-heap-commands
GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
For bug reporting instructions, please see:
http://bugs.launchpad.net/gdb-linaro/.
Traceback (most recent call last):
File "", line 5, in
ImportError: No module named gdbheap
gdb-heap-commands:7: Error in sourced command file:
Error while executing Python code.
(gdb)

SyntaxError:

Ubuntu: 13:10
Run command
./run-gdb-heap python

GNU gdb (GDB) 7.6.1-ubuntu
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
http://www.gnu.org/software/gdb/bugs/...
Reading symbols from /usr/bin/python2.7...Reading symbols from /usr/lib/debug/usr/bin/python2.7...done.
done.
File "/usr/lib/debug/usr/bin/python2.7-gdb.py", line 54
Py_TPFLAGS_HEAPTYPE = (1L << 9)

SyntaxError: invalid syntax
File "/usr/lib/debug/usr/bin/python2.7-gdb.py", line 54
Py_TPFLAGS_HEAPTYPE = (1L << 9)

SyntaxError: invalid syntax
Traceback (most recent call last):
File "", line 1, in
File "/tmp/gdb-heap/gdbheap.py", line 17, in
from heap.commands import register_commands
File "/tmp/gdb-heap/heap/init.py", line 63
print 'type cache miss: %r' % typename

SyntaxError: invalid syntax
Error while executing Python code.
(gdb)

Don't read /proc/$PID/maps for core files

iter_mmap_heap_chunks() tries to read /proc/$PID/maps which only works if the process is currently running. If you are using a core file, it will fail with an exceptions.IOError.

Having iter_mmap_chunks() return immediately if we are processing a core file rather than having an attached process solves it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.