asmrepl 1.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d89ea7a3b6af16e72444221e66fa9c2af2ceaebaae8c4e48d7a30d4b419283f5
4
+ data.tar.gz: 7b9e86750882fe36a9fdf5ee311e7cdb483c19faf3ffa8a46bdb85808a93e455
5
+ SHA512:
6
+ metadata.gz: c583255650ca956401117259c068faa6cae8f8df435b88d447186a883a8c32b45a6e6e8d16aae0fc082f4b6b979156ee88aeea6c27bf4296f73fe0c15b7b85f0
7
+ data.tar.gz: 349bcb373b93a9dec96421be445005dba162325acea07eb3e20850fd7078550d67576059ea81c33be9d99fe9f6147e8e3bde1d2945989372671e1d5418b3b141
@@ -0,0 +1,78 @@
1
+
2
+ # Contributor Covenant Code of Conduct
3
+
4
+ ## Our Pledge
5
+
6
+ In the interest of fostering an open and welcoming environment, we as
7
+ contributors and maintainers pledge to make participation in our project and
8
+ our community a harassment-free experience for everyone, regardless of age, body
9
+ size, disability, ethnicity, sex characteristics, gender identity and expression,
10
+ level of experience, education, socio-economic status, nationality, personal
11
+ appearance, race, religion, or sexual identity and orientation.
12
+
13
+ ## Our Standards
14
+
15
+ Examples of behavior that contributes to creating a positive environment
16
+ include:
17
+
18
+ * Using welcoming and inclusive language
19
+ * Being respectful of differing viewpoints and experiences
20
+ * Gracefully accepting constructive criticism
21
+ * Focusing on what is best for the community
22
+ * Showing empathy towards other community members
23
+
24
+ Examples of unacceptable behavior by participants include:
25
+
26
+ * The use of sexualized language or imagery and unwelcome sexual attention or
27
+ advances
28
+ * Trolling, insulting/derogatory comments, and personal or political attacks
29
+ * Public or private harassment
30
+ * Publishing others' private information, such as a physical or electronic
31
+ address, without explicit permission
32
+ * Other conduct which could reasonably be considered inappropriate in a
33
+ professional setting
34
+
35
+ ## Our Responsibilities
36
+
37
+ Project maintainers are responsible for clarifying the standards of acceptable
38
+ behavior and are expected to take appropriate and fair corrective action in
39
+ response to any instances of unacceptable behavior.
40
+
41
+ Project maintainers have the right and responsibility to remove, edit, or
42
+ reject comments, commits, code, wiki edits, issues, and other contributions
43
+ that are not aligned to this Code of Conduct, or to ban temporarily or
44
+ permanently any contributor for other behaviors that they deem inappropriate,
45
+ threatening, offensive, or harmful.
46
+
47
+ ## Scope
48
+
49
+ This Code of Conduct applies within all project spaces, and it also applies when
50
+ an individual is representing the project or its community in public spaces.
51
+ Examples of representing a project or community include using an official
52
+ project e-mail address, posting via an official social media account, or acting
53
+ as an appointed representative at an online or offline event. Representation of
54
+ a project may be further defined and clarified by project maintainers.
55
+
56
+ ## Enforcement
57
+
58
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
59
+ reported by contacting the project team at aaron.patterson at gmail.com. All
60
+ complaints will be reviewed and investigated and will result in a response that
61
+ is deemed necessary and appropriate to the circumstances. The project team is
62
+ obligated to maintain confidentiality with regard to the reporter of an incident.
63
+ Further details of specific enforcement policies may be posted separately.
64
+
65
+ Project maintainers who do not follow or enforce the Code of Conduct in good
66
+ faith may face temporary or permanent repercussions as determined by other
67
+ members of the project's leadership.
68
+
69
+ ## Attribution
70
+
71
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
72
+ available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
73
+
74
+ [homepage]: https://www.contributor-covenant.org
75
+
76
+ For answers to common questions about this code of conduct, see
77
+ https://www.contributor-covenant.org/faq
78
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2021 Aaron Patterson
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # ASMREPL
2
+
3
+ This is a REPL for assembly language.
4
+
5
+ ## Usage
6
+
7
+ I haven't made a gem yet, so use it like this:
8
+
9
+ ```
10
+ $ bundle install
11
+ $ sudo ruby -I lib:test bin/asmrepl
12
+ ```
13
+
14
+ When the REPL starts, it will display all register values and flags:
15
+
16
+ ```
17
+ ================== CPU STATE ===================
18
+ rax 000000000000000000 r8 0x0000600001782be0
19
+ rbx 000000000000000000 r9 0x00007fbf9b0068c0
20
+ rcx 0x0000000109dae951 r10 000000000000000000
21
+ rdx 0x000000000000000c r11 0x000000010999c000
22
+ rdi 0x00007ff7b6b2bbf0 r12 000000000000000000
23
+ rsi 0x00000001096315fd r13 0x00007ff7b6b2bdc0
24
+ rbp 0x00007ff7b6b2bc40 r14 000000000000000000
25
+ rsp 0x00007ff7b6b2bc38 r15 000000000000000000
26
+
27
+ rip 0x000000010999c001
28
+ rflags 0x0000000000000246
29
+ cs 0x000000000000002b
30
+ fs 000000000000000000
31
+ gs 000000000000000000
32
+
33
+ FLAGS: ["PF", "ZF", "IF"]
34
+
35
+ >>
36
+ ```
37
+
38
+ Then you can issue commands and inspect register values. Let's write to the
39
+ `rax` register and inspect its value:
40
+
41
+ ```
42
+ >> mov rax, 5
43
+ >> rax
44
+ 0x0000000000000005
45
+ >>
46
+ ```
47
+
48
+ Now let's write to the `rbx` register and add the two values:
49
+
50
+ ```
51
+ >> mov rbx, 3
52
+ >> add rax, rbx
53
+ >> rax
54
+ 0x0000000000000008
55
+ >> rbx
56
+ 0x0000000000000003
57
+ >>
58
+ ```
59
+
60
+ Finally, lets check all values in the CPU:
61
+
62
+ ```
63
+ >> cpu
64
+ ================== CPU STATE ===================
65
+ rax 0x0000000000000008 r8 0x0000600001d848a0
66
+ rbx 0x0000000000000003 r9 0x00007fced316f850
67
+ rcx 0x00000001017da951 r10 000000000000000000
68
+ rdx 0x000000000000000c r11 0x00000001013cc000
69
+ rdi 0x00007ff7bf0fdbf0 r12 000000000000000000
70
+ rsi 0x000000010105f5fd r13 0x00007ff7bf0fddc0
71
+ rbp 0x00007ff7bf0fdc40 r14 000000000000000000
72
+ rsp 0x00007ff7bf0fdc38 r15 000000000000000000
73
+
74
+ rip 0x00000001013cc029
75
+ rflags 0x0000000000000202
76
+ cs 0x000000000000002b
77
+ fs 000000000000000000
78
+ gs 000000000000000000
79
+
80
+ FLAGS: ["IF"]
81
+
82
+ >>
83
+ ```
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require "rake/testtask"
2
+ require "rake/clean"
3
+
4
+ ENV["MT_NO_PLUGINS"] = "1"
5
+
6
+ file "lib/asmrepl/parser.tab.rb" => "lib/asmrepl/parser.y" do |t|
7
+ sh "racc -l -o #{t.name} #{t.prerequisites.first}"
8
+ end
9
+
10
+ task :compile => "lib/asmrepl/parser.tab.rb"
11
+
12
+ Rake::TestTask.new do |t|
13
+ t.libs << "test"
14
+ t.test_files = FileList['test/*_test.rb']
15
+ t.verbose = true
16
+ t.warning = true
17
+ end
18
+
19
+ task :autotest do
20
+ sh "fswatch -o lib test | xargs -n1 -I{} bundle exec rake test"
21
+ end
22
+
23
+ task :test => :compile
data/asmrepl.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ $: << File.expand_path("lib")
2
+
3
+ require "asmrepl/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "asmrepl"
7
+ s.version = ASMREPL::VERSION
8
+ s.summary = "Write assembly in a REPL!"
9
+ s.description = "Tired of writing assembly and them assembling it? Now you can write assembly and evaluate it!"
10
+ s.authors = ["Aaron Patterson"]
11
+ s.email = "tenderlove@ruby-lang.org"
12
+ s.files = `git ls-files -z`.split("\x0")
13
+ s.test_files = s.files.grep(%r{^test/})
14
+ s.homepage = "https://github.com/tenderlove/asmrepl"
15
+ s.license = "Apache-2.0"
16
+ s.bindir = "bin"
17
+
18
+ s.executables << "asmrepl"
19
+
20
+ s.add_development_dependency 'minitest', '~> 5.14'
21
+ s.add_development_dependency 'crabstone', '~> 4.0'
22
+ s.add_development_dependency 'rake', '~> 13.0'
23
+ s.add_dependency 'fisk', '~> 2'
24
+ end
data/bin/asmrepl ADDED
@@ -0,0 +1,5 @@
1
+ #!/bin/env ruby
2
+
3
+ require "asmrepl"
4
+
5
+ ASMREPL::REPL.new.start
@@ -0,0 +1,49 @@
1
+ module ASMREPL
2
+ class Assembler
3
+ def assemble ast
4
+ fisk = Fisk.new
5
+
6
+ case ast
7
+ in [:command, [:instruction, insn], [:register, r], [:int, n]]
8
+ possibles = insn.forms.find_all do |form|
9
+ form.operands.first.type == r.type
10
+ end
11
+ l = if possibles.any? { |form| form.operands[1].type == n.to_s }
12
+ fisk.lit(n)
13
+ else
14
+ fisk.imm(n)
15
+ end
16
+ fisk.gen_with_insn insn, [r, l]
17
+ in [:command, [:instruction, insn], [:register, r], [:register, r2]]
18
+ fisk.gen_with_insn insn, [r, r2]
19
+ in [:command, [:instruction, insn], [:register, r], [:memory, mem]]
20
+ fisk.gen_with_insn insn, [r, mem]
21
+ in [:command, [:instruction, insn], [:memory, a], [:register, b]]
22
+ fisk.gen_with_insn insn, [a, b]
23
+ in [:command, [:instruction, insn], [:int, n]]
24
+ forms = insn.forms
25
+
26
+ l = if forms.any? { |form| form.operands[0].type == n.to_s }
27
+ fisk.lit(n)
28
+ else
29
+ fisk.imm(n)
30
+ end
31
+ fisk.gen_with_insn insn, [l]
32
+
33
+ in [:command, [:instruction, insn], [:register, n]]
34
+ fisk.gen_with_insn insn, [n]
35
+ in [:command, [:instruction, insn], [:memory, n]]
36
+ fisk.gen_with_insn insn, [n]
37
+ in [:command, [:instruction, insn], [:memory, n], [:int, b]]
38
+ fisk.gen_with_insn insn, [n, fisk.imm(b)]
39
+ in [:command, [:instruction, insn]]
40
+ fisk.gen_with_insn insn, []
41
+ else
42
+ p ast
43
+ raise "Unknown"
44
+ end
45
+
46
+ fisk.to_binary
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,186 @@
1
+ require "fisk/helpers"
2
+
3
+ module ASMREPL
4
+ module Linux
5
+ include Fiddle
6
+
7
+ def self.make_function name, args, ret
8
+ ptr = Handle::DEFAULT[name]
9
+ func = Function.new ptr, args, ret, name: name
10
+ define_singleton_method name, &func.to_proc
11
+ end
12
+
13
+ # from sys/mman.h on macOS
14
+ PROT_READ = 0x01
15
+ PROT_WRITE = 0x02
16
+ PROT_EXEC = 0x04
17
+ MAP_PRIVATE = 0x0002
18
+ MAP_SHARED = 0x0001
19
+ MAP_ANON = 0x20
20
+
21
+ make_function "ptrace", [TYPE_INT, TYPE_INT, TYPE_VOIDP, TYPE_VOIDP], TYPE_INT
22
+ make_function "memset", [TYPE_VOIDP, TYPE_INT, TYPE_SIZE_T], TYPE_VOID
23
+
24
+ make_function "mmap", [TYPE_VOIDP,
25
+ TYPE_SIZE_T,
26
+ TYPE_INT,
27
+ TYPE_INT,
28
+ TYPE_INT,
29
+ TYPE_INT], TYPE_VOIDP
30
+
31
+ def self.mmap_jit size
32
+ ptr = mmap 0, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED | MAP_ANON, -1, 0
33
+ ptr.size = size
34
+ ptr
35
+ end
36
+
37
+ def self.jitbuffer size
38
+ Fisk::Helpers::JITBuffer.new mmap_jit(size), size
39
+ end
40
+
41
+ PTRACE_TRACEME = 0
42
+
43
+ PTRACE_CONT = 7
44
+
45
+ # x86_64-linux-gnu/sys/ptrace.h
46
+ PTRACE_GETREGS = 12
47
+
48
+ def self.traceme
49
+ raise unless ptrace(PTRACE_TRACEME, 0, 0, 0).zero?
50
+ end
51
+
52
+ class ThreadState
53
+ fields = (<<-eostruct).scan(/int ([^;]*);/).flatten
54
+ struct user_regs_struct
55
+ {
56
+ __extension__ unsigned long long int r15;
57
+ __extension__ unsigned long long int r14;
58
+ __extension__ unsigned long long int r13;
59
+ __extension__ unsigned long long int r12;
60
+ __extension__ unsigned long long int rbp;
61
+ __extension__ unsigned long long int rbx;
62
+ __extension__ unsigned long long int r11;
63
+ __extension__ unsigned long long int r10;
64
+ __extension__ unsigned long long int r9;
65
+ __extension__ unsigned long long int r8;
66
+ __extension__ unsigned long long int rax;
67
+ __extension__ unsigned long long int rcx;
68
+ __extension__ unsigned long long int rdx;
69
+ __extension__ unsigned long long int rsi;
70
+ __extension__ unsigned long long int rdi;
71
+ __extension__ unsigned long long int orig_rax;
72
+ __extension__ unsigned long long int rip;
73
+ __extension__ unsigned long long int cs;
74
+ __extension__ unsigned long long int eflags;
75
+ __extension__ unsigned long long int rsp;
76
+ __extension__ unsigned long long int ss;
77
+ __extension__ unsigned long long int fs_base;
78
+ __extension__ unsigned long long int gs_base;
79
+ __extension__ unsigned long long int ds;
80
+ __extension__ unsigned long long int es;
81
+ __extension__ unsigned long long int fs;
82
+ __extension__ unsigned long long int gs;
83
+ };
84
+ eostruct
85
+ fields.each_with_index do |field, i|
86
+ define_method(field) do
87
+ to_ptr[Fiddle::SIZEOF_INT64_T * i, Fiddle::SIZEOF_INT64_T].unpack1("l!")
88
+ end
89
+ end
90
+
91
+ define_singleton_method(:sizeof) do
92
+ fields.length * Fiddle::SIZEOF_INT64_T
93
+ end
94
+
95
+ def [] name
96
+ idx = fields.index(name)
97
+ return unless idx
98
+ to_ptr[Fiddle::SIZEOF_INT64_T * idx, Fiddle::SIZEOF_INT64_T].unpack1("l!")
99
+ end
100
+
101
+ def self.malloc
102
+ new Fiddle::Pointer.malloc sizeof
103
+ end
104
+
105
+ attr_reader :to_ptr
106
+
107
+ def initialize buffer
108
+ @to_ptr = buffer
109
+ end
110
+
111
+ define_method(:fields) do
112
+ fields
113
+ end
114
+
115
+ def to_s
116
+ buf = ""
117
+ fields.first(8).zip(fields.drop(8).first(8)).each do |l, r|
118
+ buf << "#{l.ljust(3)} #{sprintf("%#018x", send(l))}"
119
+ buf << " "
120
+ buf << "#{r.ljust(3)} #{sprintf("%#018x", send(r))}\n"
121
+ end
122
+
123
+ buf << "\n"
124
+
125
+ fields.drop(16).each do |reg|
126
+ buf << "#{reg.ljust(8)} #{sprintf("%#018x", send(reg))}\n"
127
+ end
128
+ buf
129
+ end
130
+
131
+ FLAGS = [
132
+ ['CF', 'Carry Flag'],
133
+ [nil, 'Reserved'],
134
+ ['PF', 'Parity Flag'],
135
+ [nil, 'Reserved'],
136
+ ['AF', 'Adjust Flag'],
137
+ [nil, 'Reserved'],
138
+ ['ZF', 'Zero Flag'],
139
+ ['SF', 'Sign Flag'],
140
+ ['TF', 'Trap Flag'],
141
+ ['IF', 'Interrupt Enable Flag'],
142
+ ['DF', 'Direction Flag'],
143
+ ['OF', 'Overflow Flag'],
144
+ ['IOPL_H', 'I/O privilege level High bit'],
145
+ ['IOPL_L', 'I/O privilege level Low bit'],
146
+ ['NT', 'Nested Task Flag'],
147
+ [nil, 'Reserved'],
148
+ ]
149
+
150
+ def flags
151
+ flags = eflags
152
+ f = []
153
+ FLAGS.each do |abbrv, _|
154
+ if abbrv && flags & 1 == 1
155
+ f << abbrv
156
+ end
157
+ flags >>= 1
158
+ end
159
+ f
160
+ end
161
+ end
162
+
163
+ class Tracer
164
+ def initialize pid
165
+ @pid = pid
166
+ end
167
+
168
+ def wait
169
+ Process.waitpid @pid
170
+ end
171
+
172
+ def state
173
+ state = ThreadState.malloc
174
+ raise unless Linux.ptrace(PTRACE_GETREGS, @pid, 0, state).zero?
175
+
176
+ state
177
+ end
178
+
179
+ def continue
180
+ unless Linux.ptrace(Linux::PTRACE_CONT, @pid, 1, 0).zero?
181
+ raise
182
+ end
183
+ end
184
+ end
185
+ end
186
+ end