Giter Site home page Giter Site logo

gdb-ruby's Introduction

Build Status Gem Version Code Climate Issue Count Test Coverage Inline docs MIT License

GDB-Ruby

It's time for Ruby lovers to use Ruby in gdb and gdb in Ruby!

Achieve two things in one gem:

  1. Launching Ruby interactive shell (pry) in gdb.
  2. gdb Ruby-binding, i.e. communicate with gdb in Ruby scripts.

Use Ruby in gdb

We provide a binary gdb-ruby (a Ruby script actually) with usage exactly the same as a normal gdb, while has two extra commands: ruby and pry!

See examples below:

$ gdb-ruby -q bash
Reading symbols from bash...(no debugging symbols found)...done.
(gdb) help ruby
Evaluate a Ruby command.
There's an instance 'gdb' for you. See examples.

Syntax: ruby <ruby code>

Examples:
    ruby p 'abcd'
    # "abcd"

Use gdb:
    ruby puts gdb.break('main')
    # Breakpoint 1 at 0x41eed0

Method defined will remain in context:
    ruby def a(b); b * b; end
    ruby p a(9)
    # 81
(gdb) help pry
Enter Ruby interactive shell.
Everything works like a charm!

Syntax: pry

Example:
    pry
    # [1] pry(#<GDB::EvalContext>)>

Integrate with other gdb extensions

Completely NO effort if you want to use gdb-ruby with other gdb extensions.

For example, I usually use the plugin gef with gdb. Everything works as usual when integrated with gdb-ruby:

Launching with $ gdb-ruby -q bash

ruby-in-gef

Use gdb in Ruby

Communicate with gdb in your Ruby script.

Useful methods

Basic usage is use execute to do anything you want to execute inside gdb, while gdb-ruby provides some useful methods listed as following:

  • break: Set break points. Alias: b
  • run: Run. Alias: r
  • register: Get value by register's name. Alias: reg
  • text_base: Get current running program's text base, useful for a PIE binary.
  • pid: Get the process id of running process.
  • read_memory: Read process's memory, with friendly type casting. Alias: readm
  • write_memory: Write process's memory, useful for dynamic analysis. Alias: writem
  • interact: Back to normal gdb interactive mode.

All of these methods are fully documented at online doc, go for it!

Examples

Play with argv using gdb-ruby.

This script does:

  1. Set a break point at main.
  2. Get argv using register and read_memory.
  3. Change argv using write_memory.
require 'gdb'

# launch a gdb instance
gdb = GDB::GDB.new('bash')

# 1. set breakpoint
gdb.break('main')
#=> "Breakpoint 1 at 0x41eed0"
gdb.run('-c "echo cat"')

# 2. get argv pointers
rdi = gdb.reg(:rdi)
#=> 3
rsi = gdb.reg(:rsi)
argv = gdb.readm(rsi, rdi, as: :u64)
argv.map { |c| '0x%x' % c }
#=> ['0x7fffffffe61b', '0x7fffffffe625', '0x7fffffffe628']

# 3. overwrite argv[2]'s 'cat' to 'FAT'
gdb.writem(argv[2] + 5, 'FAT') # echo FAT

puts gdb.execute('continue')
# Continuing.
# FAT
# [Inferior 1 (process 32217) exited normally]

Set a break point, run it, and back to gdb interactive mode.

require 'gdb'

# launch a gdb instance
gdb = GDB::GDB.new('bash')
# set breakpoints
gdb.break('main')
gdb.run
# to show the process do stop at the breakpoint
gdb.execute('info reg rip')
#=> "rip            0x41eed0\t0x41eed0 <main>"

# interaction like normal gdb!
gdb.interact

Installation

Available on RubyGems.org!

$ gem install gdb

Development

git clone https://github.com/david942j/gdb-ruby
cd gdb-ruby
bundle
bundle exec rake

Bugs & Feedback

Feel free to file an issue if you find any bugs. Any feature requests and suggestions are welcome! 😬

Growing up

gdb-ruby is under developing, give it a star and watch for latest updates!

gdb-ruby's People

Contributors

david942j avatar dependabot-preview[bot] avatar dependabot[bot] 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

Watchers

 avatar  avatar  avatar  avatar  avatar

gdb-ruby's Issues

Dependabot can't resolve your Ruby dependency files

Dependabot can't resolve your Ruby dependency files.

As a result, Dependabot couldn't update your dependencies.

The error Dependabot encountered was:

Bundler::VersionConflict with message: Bundler found conflicting requirements for the Ruby version:
  In Gemfile:
    Ruby (~> 2.3.8.0)

    rubocop (~> 1) was resolved to 1.12.1, which depends on
      rubocop-ast (>= 1.2.0, < 2.0) was resolved to 1.4.1, which depends on
        Ruby (>= 2.4)

If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

View the update logs.

Question: global variables not visible

I'm doing a test via rails console to see if gdb-ruby can read a global variable I've assigned in it:

~/P/retirement ❯❯❯ nice rails c                                                                                                                                                                             
Running via Spring preloader in process 2394046
Loading development environment (Rails 5.1.4)
[1] pry(main)> $abc = 5
=> 5

But then on gdb this variable isn't set, what stupid thing am I doing?

~/P/retirement ❯❯❯ gdb-ruby 2394046                                                                                                                                                                         
(gdb) attach 2394046
Attaching to process 2394046
[New LWP 2394048]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
0x00007fd56b23b79c in read () from /usr/lib/libpthread.so.0
(gdb) pry
[1] pry(#<GDB::EvalContext>)> $abc
=> nil

Hanging with Long Params to gdb.run and gdb.execute

When I put a paramater to gdb.run that is longer than 175 characters the command hangs.

A Starting program: program AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
When the string of A's reaches 176 it freezes before "Starting program"

Inappropriate error message when no GDB installed

Trying to install and run as documented results in this error:

$ rbenv shell 2.5.3                                                                                                       
febeling:~/ $ gem install gdb                                                                                                                   [13:32:30]
Fetching: dentaku-3.3.0.gem (100%)
Successfully installed dentaku-3.3.0
Fetching: memory_io-0.1.1.gem (100%)
Successfully installed memory_io-0.1.1
Fetching: gdb-0.3.0.gem (100%)
Successfully installed gdb-0.3.0
3 gems installed
febeling:~/ $ gdb-ruby -q bash                                                                                                                  [13:32:39]
Traceback (most recent call last):
	6: from /Users/febeling/.rbenv/versions/2.5.3/bin/gdb-ruby:23:in `<main>'
	5: from /Users/febeling/.rbenv/versions/2.5.3/bin/gdb-ruby:23:in `load'
	4: from /Users/febeling/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/gdb-0.3.0/bin/gdb-ruby:9:in `<top (required)>'
	3: from /Users/febeling/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/gdb-0.3.0/bin/gdb-ruby:9:in `new'
	2: from /Users/febeling/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/gdb-0.3.0/lib/gdb/gdb.rb:29:in `initialize'
	1: from /Users/febeling/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/gdb-0.3.0/lib/gdb/gdb.rb:284:in `spawn'
/Users/febeling/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/gdb-0.3.0/lib/gdb/gdb.rb:284:in `spawn': No such file or directory - fork failed (Errno::ENOENT)

Support for process attachment?

Hi David,

I'm really interested in the project! Does GDB::Ruby allow for attaching to a running process (as gdb does), and if not, are there plans for that? This would be incredibly powerful with regards to debugging running processes, such as those in a production environment.

macOS support

As #32 mentioned, gdb-ruby on macOS raises an error:

/Users/febeling/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/gdb-0.3.0/lib/gdb/gdb.rb:285:in `ioctl': Inappropriate ioctl for device @ rb_ioctl - /dev/ttys009 (Errno::ENOTTY)

`winsize=': no implicit conversion of false into Integer (TypeError)

I hate to be the bearer of bad news.

Here is my script

#!/usr/bin/ruby

require 'gdb'
gdb = GDB::GDB.new('bash')

and here is the exception it throws:

ruby sploit.rb
/var/lib/gems/2.3.0/gems/gdb-1.0.0/lib/gdb/gdb.rb:332:in `winsize=': no implicit conversion of false into Integer (TypeError)
	from /var/lib/gems/2.3.0/gems/gdb-1.0.0/lib/gdb/gdb.rb:332:in `spawn'
	from /var/lib/gems/2.3.0/gems/gdb-1.0.0/lib/gdb/gdb.rb:37:in `initialize'
	from sploit.rb:4:in `new'
	from sploit.rb:4:in `<main>'

Its throwing on

gdb = GDB::GDB.new('bash')

Thanks in advance.

Can't change gdb's prompt

It seems that "set prompt" command is ignored in gdb-ruby.

On gdb:

# gdb -q
(gdb) set prompt gdb> 
gdb> show prompt
Gdb's prompt is "gdb> ".
gdb> quit

On gdb-ruby (0.2.0):

# gdb-ruby -q
(gdb) set prompt gdb> 
(gdb) show prompt
Gdb's prompt is "(gdb) ".
(gdb) quit

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.