author | Jan Vrany <jan.vrany@labware.com> |
Tue, 22 Aug 2023 11:38:01 +0100 | |
changeset 307 | 17a6a8329a8f |
parent 259 | 651864c2aa29 |
child 308 | 2f4c59d35518 |
permissions | -rw-r--r-- |
139 | 1 |
# libgdbs |
2 |
||
152
fab425b52c21
Refactor `GDBProcess` hierarchy to improve portability
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
139
diff
changeset
|
3 |
*libgdbs* is a library providing smalltalk interface for GDB. |
139 | 4 |
|
5 |
## Features |
|
6 |
||
7 |
* event-based interface |
|
8 |
* convenient API to inspect and query current state of a debugee |
|
307 | 9 |
* support for custom disassemblers allowing for custom code analysis (not part |
216 | 10 |
of *libgdbs*) |
11 |
||
12 |
## Example Use |
|
152
fab425b52c21
Refactor `GDBProcess` hierarchy to improve portability
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
139
diff
changeset
|
13 |
|
307 | 14 |
``` |
15 |
| b0 thread frame i r | |
|
16 |
||
17 |
"Create new debugger object - `gdb`." |
|
18 |
gdb := GDBDebugger new. |
|
19 |
||
20 |
"Load an executable to debug, the parameter is a String |
|
21 |
with path to the executable." |
|
22 |
gdb executable: GDBDebuggeesResource current binaryFactorial1. |
|
23 |
||
24 |
"Set breakpoint on function `factorial(int)`, see`factorial.c`(*). |
|
25 |
Here we use `GDBDebugger >> #send:` to send a standard CLI command |
|
26 |
to `gdb`. The method blocks and returns once the command is executed. |
|
27 |
See class `GDBDebugger`, protocol 'commands' for more ways to send a |
|
28 |
command to GDB. |
|
29 |
||
30 |
(*) https://jan.vrany.io/hg/jv-libgdbs/file/tip/tests/c/factorial1.c |
|
31 |
" |
|
32 |
gdb send: 'b factorial'. |
|
33 |
||
34 |
"You may ask `gdb` for list of breakpoints, ..." |
|
35 |
gdb breakpoints. |
|
36 |
||
37 |
"...retrieve a breakpoint as an object..." |
|
38 |
b0 := gdb breakpoints first. |
|
39 |
||
40 |
"...and then use breakpoint object to get details |
|
41 |
for example on what file and line the breakpoint was |
|
42 |
put:" |
|
43 |
'location is %1 : %2' bindWith: b0 file with: b0 line. |
|
44 |
||
45 |
"Breakpoint objects can also be used to manipulate it, |
|
46 |
for example to set a condition when the breakpoint should |
|
47 |
hit. See class `GDBBreakpoint`. " |
|
48 |
b0 condition: 'i == 1'. |
|
216 | 49 |
|
307 | 50 |
"Now let's run debuggee util it hits the breakpoint using |
51 |
GDB's `r` (or `run`) command. Here we (have to) use a different |
|
52 |
API than plain `#send: - `#send:` executes the command and return |
|
53 |
as soon as the command is executed. This is not what we want now, |
|
54 |
here we want to run the debuggee and wait until breakpoint hits |
|
55 |
(or until some error occur). In theory, this could be hours after |
|
56 |
the `r` command is executed. |
|
57 |
||
58 |
For cases like this, libgdb's provides another API: |
|
59 |
`GDBDebugger >> send:andWaitFor:` to wait for 'stopped' event:" |
|
60 |
gdb send: 'r' andWaitFor: GDBStoppedEvent. |
|
61 |
||
62 |
"Now let's see where we have stopped. Since GDB allows |
|
63 |
to debug multiple processes (called inferiors in GDB parlance) |
|
64 |
and each process may have multiple threads, we have to first get |
|
65 |
a the thread and then ask the thread for first (newest) frame of |
|
66 |
threads callstack. In this case, there' only one process with |
|
67 |
only one thread, so getting the thread is easy. |
|
139 | 68 |
|
307 | 69 |
Note, that there's always a callstack with at least one frame." |
70 |
thread := gdb selectedInferior threads first. |
|
71 |
frame := thread stack first. |
|
72 |
||
73 |
"Again, thread and frame are objects and can be used to access |
|
74 |
program state" |
|
75 |
frame func. " -> 'factorial'" |
|
76 |
i := frame variables first. |
|
77 |
i name. " -> 'i' " |
|
78 |
i value. " -> '1' " |
|
79 |
||
80 |
r := frame registers first. |
|
81 |
r name. " -> 'rax' (depending on target architecture, of course) " |
|
82 |
r value. |
|
83 |
||
84 |
"Finally, remove all breakpoints and let the program continue and finish..." |
|
85 |
gdb send: 'del'. |
|
86 |
gdb send: 'c' andWaitFor: GDBThreadGroupExitedEvent. |
|
87 |
``` |
|
216 | 88 |
|
89 |
For more examples, load `jv:libgdbs/tests` and see `GDBDebuggerExamples`. You may also |
|
90 |
want to have a look at tests `GDBDebuggerTestsR`. |
|
91 |
||
92 |
## Getting Started... |
|
93 |
||
94 |
### ...on Linux |
|
95 |
||
96 |
1. Create a directory where to download and install all components - in following |
|
97 |
steps we use `/where/you/want/it` but naturally, choice is yours: |
|
98 |
||
99 |
mkdir -p /where/you/want/it |
|
100 |
cd /where/you/want/it |
|
101 |
||
307 | 102 |
2. Get suitable GDB. Stock GDB 13 and later should be fine. |
139 | 103 |
|
216 | 104 |
Alternatively, you may want to build suitable GDB from sources, see |
105 |
[doc/GDB.md][9] for details. |
|
106 |
||
307 | 107 |
3. Get [Smalltalk/X jv-branch][3]. Pre-build "toy" binaries for Linux |
108 |
can be found at [https://jan.vrany.io/download/smalltalkx/devel][5]: |
|
216 | 109 |
|
307 | 110 |
wget https://dl.vrany.io/public/smalltalkx/devel//YYYY-MM-DD_NNN/smalltalkx-jv-branch-8.0.99_buildNNN_x86_64-pc-linux-gnu.tar.bz2 |
216 | 111 |
tar xf smalltalkx-jv-branch-8.0.99_buildNNN_x86_64-pc-linux-gnu.tar.bz2 |
112 |
rm smalltalkx-jv-branch-8.0.99_buildNNN_x86_64-pc-linux-gnu.tar.bz2 |
|
113 |
||
114 |
Alternatively, you may want to build Smalltalk/X from sources, see |
|
115 |
[build instructions][12]. This is actually preferred way to work with |
|
116 |
Smalltalk/X. |
|
152
fab425b52c21
Refactor `GDBProcess` hierarchy to improve portability
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
139
diff
changeset
|
117 |
|
216 | 118 |
4. Get *jv:libgdbs*: |
119 |
||
120 |
mkdir -p jv |
|
307 | 121 |
hg clone https://jan.vrany.io/hg/jv-libgdbs jv/libgdbs |
216 | 122 |
|
123 |
5. Start Smalltalk/X: |
|
124 |
||
125 |
./smalltalkx-jv-branch-8.0.99_buildNNN_x86_64-pc-linux-gnu/bin/stx --package-path /where/you/want/it |
|
126 |
||
127 |
Please note, that `/where/you/want/it` should be the path to where you |
|
128 |
created `jv` directory in which libgdbs has been cloned in step 3. |
|
129 |
||
130 |
6. In Smalltalk/X workspace, load `jv:libgdbs` and set path to suitable GDB: |
|
131 |
||
132 |
Smalltalk loadPackage: 'jv:libgdbs'. |
|
133 |
UserPreferences current gdbCommand: '/where/you/want/it/gdb_x86_64-pc-linux-gnu_YYYY-MM-DD_XXXXXXXX_NN/bin/gdb'. |
|
134 |
||
135 |
7. Now you may create an instance of GDB to play with - in Smalltalk/X |
|
136 |
workspace execute: |
|
137 |
||
138 |
gdb := GDBDebugger new. |
|
139 |
||
140 |
### ...on Windows |
|
141 |
||
142 |
For installing on Windows, see [doc/GettingStartedOnWindows.md][13] |
|
152
fab425b52c21
Refactor `GDBProcess` hierarchy to improve portability
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
139
diff
changeset
|
143 |
|
161 | 144 |
## Documentation |
145 |
||
146 |
Some documentation can be found in [doc][10] directory, see [doc/README.md][10] |
|
147 |
||
139 | 148 |
## Reporting issues |
149 |
||
150 |
Use [Smalltalk/X jv-branch issue tracker][6] to [report issues][7] (you may need |
|
151 |
to login using your Google account in order to submit an issue). Alternatively, |
|
152
fab425b52c21
Refactor `GDBProcess` hierarchy to improve portability
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
139
diff
changeset
|
152 |
send a message to [Smalltalk/X mailing list][8]. |
139 | 153 |
|
154 |
## Author |
|
155 |
||
307 | 156 |
Jan Vrany `<jan [at] vrany [.] io>` |
139 | 157 |
|
158 |
## License |
|
159 |
||
259
651864c2aa29
Relicense under MIT license.
Jan Vrany <jan.vrany@labware.com>
parents:
216
diff
changeset
|
160 |
This software is licensed under *MIT license*. |
139 | 161 |
You may find a full license text in `LICENSE.txt`. |
162 |
||
163 |
[2]: https://www.gnu.org/software/gdb/ |
|
307 | 164 |
[3]: https://jan.vrany.io/stx |
139 | 165 |
[4]: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git |
307 | 166 |
[5]: https://dl.vrany.io/public/smalltalkx/devel |
167 |
[6]: https://jan.vrany.io/stx/report/9 |
|
168 |
[7]: https://jan.vrany.io/stx/newticket |
|
152
fab425b52c21
Refactor `GDBProcess` hierarchy to improve portability
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
139
diff
changeset
|
169 |
[8]: https://groups.google.com/forum/#!forum/stx-jv |
fab425b52c21
Refactor `GDBProcess` hierarchy to improve portability
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
139
diff
changeset
|
170 |
[9]: doc/GDB.md |
216 | 171 |
[10]: doc/README.md |
307 | 172 |
[11]: https://jan.vrany.io/public/gdb |
173 |
[12]: https://jan.vrany.io/stx/wiki/Documentation/BuildingStXWithRakefiles |
|
216 | 174 |
[13]: doc/GettingStartedOnWindows.md |