gdbdump 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ea50c60dac2189b7cea3222ddcf45ef8a351d203
4
+ data.tar.gz: df893c9a3c07006e1864358def10125388660f4d
5
+ SHA512:
6
+ metadata.gz: 209d5cd21074e35b9b02a14a29b6ba396b8dfb03f66aaf76f34bc1e556f3f31fb7ffce4df202cbfd5321c2e4ac0b913356a89a6b7d140ea23d47a56217758c89
7
+ data.tar.gz: 4999fe00d9d43b77b4a330eeb57a7185a1dcb4b870d712ea8799ac9737da3dbf5cbff91addbcc5fdf6f7c2604a8e2d3a649705a545484d8646db408234476ea4
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at sonots@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in gdbdump.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 sonots
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,79 @@
1
+ # Gdbdump
2
+
3
+ Print C level and Ruby level backtrace of living process using gdb
4
+
5
+ ## Installation
6
+
7
+ $ gem install gdbdump
8
+
9
+ ## Requirements
10
+
11
+ * run with sudo.
12
+ * on Linux.
13
+
14
+ I verified that this gem works with ruby executables built by [rbenv/ruby-build](https://github.com/rbenv/ruby-build).
15
+
16
+ ## Example
17
+
18
+ With living ruby process of pid 1897,
19
+
20
+ ```
21
+ $ sudo gdbdump 1897
22
+ ```
23
+
24
+ You will see C and Ruby level backtrace on STDERR of **the target process** of pid 1897 as:
25
+
26
+ ```
27
+ == c backtrace ==
28
+ /home/ubuntu/.rbenv/versions/2.4.1/bin/ruby(rb_print_backtrace+0x15) [0x7fd23062c115] vm_dump.c:684
29
+ [0x7ffc98c378af]
30
+ == ruby backtrace ==
31
+ from loop.rb:3:in `<main>'
32
+ from loop.rb:3:in `loop'
33
+ from loop.rb:5:in `block in <main>'
34
+ from loop.rb:5:in `sleep'
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ ```
40
+ Usage: gdbdump [options] [pid|prog pid]
41
+ -d, --[no-]debug print debug log (default: false)
42
+ ```
43
+
44
+ ## How this work
45
+
46
+ Attach to the ruby process with gdb, and call `rb_print_backtrace()` (C level backtrace) and `rb_backtrace()` (Ruby level backtrace). That's it.
47
+
48
+ The path of ruby executable is found by `/proc/[PID]/exe` as default.
49
+
50
+ ## ToDo
51
+
52
+ * Want to print backtrace on STDOUT of gdbdump process.
53
+ * To do it, we need another version of `rb_print_backtrace` and `rb_backtrace` to print results into a file in CRuby itself.
54
+ * If they are available, gdbdump can dump to a file and, then read and print into STDOUT of gdbdump process.
55
+
56
+ ## Comparisons
57
+
58
+ * gdb
59
+ * You can get C level backtrace with raw gdb, of course.
60
+ * [sigdump](https://github.com/frsyuki/sigdump)
61
+ * You can get ruby level backtrace with sigdump gem, but your ruby application must pre-install sigdump and `require 'sigdump/setup'`. `gdbdump` does not require it.
62
+
63
+ ## Development
64
+
65
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
66
+
67
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sonots/gdbdump. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
76
+
77
+ ## Code of Conduct
78
+
79
+ Everyone interacting in the Gdbdump project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/gdbdump/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gdbdump"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/gdbdump/cli'
4
+ Gdbdump::CLI.new.run
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "gdbdump/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gdbdump"
8
+ spec.version = Gdbdump::VERSION
9
+ spec.authors = ["Naotoshi Seo"]
10
+ spec.email = ["sonots@gmail.com"]
11
+
12
+ spec.summary = %q{Print C level and Ruby level backtrace of living process using gdb.}
13
+ spec.description = %q{Print C level and Ruby level backtrace of living process using gdb.}
14
+ spec.homepage = "https://github.com/sonots/gdbdump-ruby"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "test-unit"
27
+ end
@@ -0,0 +1,3 @@
1
+ require_relative "gdbdump/version"
2
+ require_relative "gdbdump/gdb"
3
+ require_relative "gdbdump/procfs"
@@ -0,0 +1,49 @@
1
+ require 'optparse'
2
+ require_relative '../gdbdump'
3
+
4
+ class Gdbdump
5
+ class CLI
6
+ def parse_options(argv = ARGV)
7
+ op = OptionParser.new
8
+
9
+ self.class.module_eval do
10
+ define_method(:usage) do |msg = nil|
11
+ puts op.to_s
12
+ puts "error: #{msg}" if msg
13
+ exit 1
14
+ end
15
+ end
16
+
17
+ opts = {
18
+ debug: false,
19
+ }
20
+
21
+ op.on('-d', '--[no-]debug', "print debug log (default: #{opts[:debug]})") {|v|
22
+ opts[:debug] = v
23
+ }
24
+
25
+ op.banner += ' [pid|prog pid]'
26
+ begin
27
+ args = op.parse(argv)
28
+ rescue OptionParser::InvalidOption => e
29
+ usage e.message
30
+ end
31
+
32
+ if args.size == 1
33
+ @pid = args.first
34
+ @prog = Gdbdump::Procfs.new(@pid).exe
35
+ elsif args.size == 2
36
+ @prog, @pid = args
37
+ else
38
+ usage 'number of arguments must be 1 or 2'
39
+ end
40
+
41
+ @opts = opts
42
+ end
43
+
44
+ def run
45
+ parse_options
46
+ GDB.new(pid: @pid, prog: @prog, debug: @opts[:debug]).print_backtrace
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,102 @@
1
+ require 'open3'
2
+
3
+ class Gdbdump
4
+ # GDB.new(pid: 999, prog: '/path/to/ruby', debug: true).run do |gdb|
5
+ # puts gdb.cmd_exec('bt')
6
+ # end
7
+ class GDB
8
+ COMMAND_READ_BUFFER_SIZE = 1024
9
+
10
+ def initialize(pid:, prog:, debug: false)
11
+ @prog = prog
12
+ @pid = pid.to_s
13
+ @debug = debug
14
+ @exec_options = ['gdb', '-silent', '-nw', @prog, @pid]
15
+ end
16
+
17
+ def print_backtrace
18
+ run do |gdb|
19
+ gdb.cmd_exec('call write(2, "== c backtrace ==\n", 18)')
20
+ gdb.cmd_exec('call rb_print_backtrace()')
21
+ gdb.cmd_exec('call write(2, "== ruby backtrace ==\n", 21)')
22
+ gdb.cmd_exec('call rb_backtrace()')
23
+ end
24
+ end
25
+
26
+ def run
27
+ @stdin, @stdout, @stderr = Open3.popen3(*@exec_options)
28
+ if get_response =~ /ptrace: Operation not permitted./
29
+ raise 'Must run gdbdump with sudo'
30
+ end
31
+ prepare
32
+ begin
33
+ yield(self)
34
+ detach
35
+ ensure
36
+ Process.kill('CONT', @pid.to_i)
37
+ @stdin.close
38
+ @stdout.close
39
+ @stderr.close
40
+ end
41
+ end
42
+
43
+ def cmd_exec(cmd)
44
+ log('C', cmd)
45
+ if cmd
46
+ send_cmd = cmd.empty? ? cmd : "#{cmd}\n"
47
+ r = @stdin.syswrite(send_cmd)
48
+ raise "failed to send: [#{cmd}]" if r < send_cmd.length
49
+ end
50
+
51
+ get_response
52
+ end
53
+
54
+ def get_response
55
+ response = +''
56
+
57
+ loop do
58
+ begin
59
+ buf = @stdout.sysread(COMMAND_READ_BUFFER_SIZE)
60
+ rescue EOFError
61
+ break
62
+ end
63
+ response << buf
64
+ break if buf =~ /\(gdb\) $/
65
+ end
66
+
67
+ loop do
68
+ begin
69
+ buf = @stderr.read_nonblock(COMMAND_READ_BUFFER_SIZE)
70
+ rescue Errno::EAGAIN, Errno::EWOULDBLOCK
71
+ break
72
+ rescue EOFError
73
+ break
74
+ end
75
+ response << buf if buf
76
+ end
77
+
78
+ log('R', response)
79
+ response
80
+ end
81
+
82
+ private
83
+
84
+ def prepare
85
+ cmd_exec('')
86
+ cmd_exec('set pagination off')
87
+ end
88
+
89
+ def detach
90
+ cmd_exec('detach')
91
+ cmd_exec('quit')
92
+ end
93
+
94
+ def log(pre, message)
95
+ return unless @debug
96
+ message.each_line do |line|
97
+ puts "#{pre}: #{line}"
98
+ end
99
+ end
100
+ end
101
+ end
102
+
@@ -0,0 +1,17 @@
1
+ require_relative 'version'
2
+
3
+ class Gdbdump
4
+ class Procfs
5
+ def initialize(pid)
6
+ @pid = pid
7
+ end
8
+
9
+ def exe
10
+ begin
11
+ File.readlink("/proc/#{@pid}/exe")
12
+ rescue Errno::ENOENT
13
+ raise "/proc/#{@pid}/exe does not exist, it seems pid #{@pid} does not exit"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ class Gdbdump
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gdbdump
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Naotoshi Seo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: test-unit
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Print C level and Ruby level backtrace of living process using gdb.
56
+ email:
57
+ - sonots@gmail.com
58
+ executables:
59
+ - gdbdump
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - exe/gdbdump
73
+ - gdbdump.gemspec
74
+ - lib/gdbdump.rb
75
+ - lib/gdbdump/cli.rb
76
+ - lib/gdbdump/gdb.rb
77
+ - lib/gdbdump/procfs.rb
78
+ - lib/gdbdump/version.rb
79
+ homepage: https://github.com/sonots/gdbdump-ruby
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.11
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Print C level and Ruby level backtrace of living process using gdb.
103
+ test_files: []