sigcdump 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ /**********************************************************************
2
+
3
+ addr2line.h -
4
+
5
+ $Author$
6
+
7
+ Copyright (C) 2010 Shinichiro Hamaji
8
+
9
+ **********************************************************************/
10
+
11
+ #ifndef RUBY_ADDR2LINE_H
12
+ #define RUBY_ADDR2LINE_H
13
+
14
+ #if (defined(USE_ELF) || defined(HAVE_MACH_O_LOADER_H))
15
+
16
+ void
17
+ rb_dump_backtrace_with_lines(int num_traces, void **traces);
18
+
19
+ #endif /* USE_ELF */
20
+
21
+ #endif /* RUBY_ADDR2LINE_H */
@@ -0,0 +1,70 @@
1
+ #include <errno.h>
2
+ #include <execinfo.h>
3
+ #include <fcntl.h>
4
+ #include "sigcdump.h"
5
+
6
+ extern void rb_dump_backtrace_with_lines(int num_traces, void **traces);
7
+ static VALUE rb_mSigcdump;
8
+
9
+ static void
10
+ dump_to_stderr(void)
11
+ {
12
+ #define MAX_NATIVE_TRACE 1024
13
+ static void *trace[MAX_NATIVE_TRACE];
14
+ int n = (int)backtrace(trace, MAX_NATIVE_TRACE);
15
+ #if (defined(USE_ELF) || defined(HAVE_MACH_O_LOADER_H)) && defined(HAVE_DLADDR) && !defined(__sparc)
16
+ rb_dump_backtrace_with_lines(n, trace);
17
+ #else
18
+ char **syms = backtrace_symbols(trace, n);
19
+ if (syms) {
20
+ int i;
21
+ for (i=0; i<n; i++) {
22
+ fprintf(stderr, "%s\n", syms[i]);
23
+ }
24
+ free(syms);
25
+ }
26
+ #endif
27
+ fprintf(stderr, "\n");
28
+ }
29
+
30
+ /*
31
+ * When path is:
32
+ * "-" => stdout
33
+ * "+" => stderr
34
+ * other String => dump to the path
35
+ *
36
+ * IO object is not supported yet.
37
+ */
38
+ static VALUE
39
+ rb_sigcdump_dump_internal(VALUE klass, VALUE path)
40
+ {
41
+ int err = dup(STDERR_FILENO);
42
+ if (strcmp("-", RSTRING_PTR(path)) == 0) {
43
+ dup2(STDOUT_FILENO, STDERR_FILENO);
44
+ }
45
+ else if (strcmp("+", RSTRING_PTR(path)) == 0) {
46
+ // none
47
+ }
48
+ else {
49
+ int fd = open(RSTRING_PTR(path), O_CREAT|O_WRONLY|O_APPEND, 0644);
50
+ if (fd < 0) {
51
+ fprintf(stderr, "Failed to open %s: %s\n", "/tmp/hello", strerror(errno));
52
+ return Qfalse;
53
+ }
54
+ dup2(fd, STDERR_FILENO);
55
+ close(fd);
56
+ }
57
+
58
+ dump_to_stderr();
59
+
60
+ dup2(err, STDERR_FILENO);
61
+ close(err);
62
+ return Qtrue;
63
+ }
64
+
65
+ void
66
+ Init_sigcdump(void)
67
+ {
68
+ rb_mSigcdump = rb_define_module("Sigcdump");
69
+ rb_define_singleton_method(rb_mSigcdump, "dump_internal", rb_sigcdump_dump_internal, 1);
70
+ }
@@ -0,0 +1,6 @@
1
+ #ifndef SIGCDUMP_H
2
+ #define SIGCDUMP_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ #endif /* SIGCDUMP_H */
@@ -0,0 +1,41 @@
1
+ require 'sigcdump/version'
2
+ require 'sigcdump/sigcdump'
3
+
4
+ class << Sigcdump
5
+ def setup(signal = ENV['SIGCDUMP_SIGNAL'] || 'SIGCONT', path = ENV['SIGCDUMP_PATH'])
6
+ Kernel.trap(signal) do
7
+ begin
8
+ dump(path)
9
+ rescue
10
+ end
11
+ end
12
+ end
13
+
14
+ def dump(arg = ENV['SIGCDUMP_PATH'])
15
+ with_path_and_io(arg) do |path, io|
16
+ io.write "Sigcdump at #{Time.now} process #{Process.pid} (#{$0})\n"
17
+ io.flush
18
+ dump_internal(path)
19
+ end
20
+ end
21
+
22
+ private def with_path_and_io(path, &block)
23
+ case path
24
+ when IO
25
+ raise NotImplementedError, 'specifying IO in Sigcdump.dump is not implemented yet'
26
+ when '-'
27
+ block.call(path, STDOUT)
28
+ when '+'
29
+ block.call(path, STDERR)
30
+ else
31
+ if path == nil || path == ''
32
+ path = "/tmp/sigcdump-#{Process.pid}.log"
33
+ end
34
+ File.open(path, 'a') do |io|
35
+ block.call(path, io)
36
+ end
37
+ end
38
+ end
39
+
40
+ private :dump_internal
41
+ end
@@ -0,0 +1,2 @@
1
+ require_relative '../sigcdump'
2
+ Sigcdump.setup
@@ -0,0 +1,3 @@
1
+ module Sigcdump
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'sigcdump/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'sigcdump'
7
+ spec.version = Sigcdump::VERSION
8
+ spec.authors = ['Takashi Kokubun']
9
+ spec.email = ['takashikkbn@gmail.com']
10
+
11
+ spec.summary = %q{Sigdump for C backtrace}
12
+ spec.description = %q{Sigdump for C backtrace}
13
+ spec.homepage = 'https://github.com/k0kubun/sigcdump'
14
+ spec.license = 'Ruby License'
15
+
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end + %w[
19
+ addr2line.h
20
+ addr2line.c
21
+ ].map { |f| File.join('ext/sigcdump/ruby', f) }
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+ spec.extensions = ['ext/sigcdump/extconf.rb']
26
+
27
+ spec.add_development_dependency 'bundler'
28
+ spec.add_development_dependency 'rake'
29
+ spec.add_development_dependency 'rake-compiler'
30
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sigcdump
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takashi Kokubun
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-10 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-compiler
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: Sigdump for C backtrace
56
+ email:
57
+ - takashikkbn@gmail.com
58
+ executables: []
59
+ extensions:
60
+ - ext/sigcdump/extconf.rb
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".gitmodules"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - ext/sigcdump/extconf.rb
73
+ - ext/sigcdump/ruby/addr2line.c
74
+ - ext/sigcdump/ruby/addr2line.h
75
+ - ext/sigcdump/sigcdump.c
76
+ - ext/sigcdump/sigcdump.h
77
+ - lib/sigcdump.rb
78
+ - lib/sigcdump/setup.rb
79
+ - lib/sigcdump/version.rb
80
+ - sigcdump.gemspec
81
+ homepage: https://github.com/k0kubun/sigcdump
82
+ licenses:
83
+ - Ruby License
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.0.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Sigdump for C backtrace
104
+ test_files: []