Skip to content

Commit 15ab0a2

Browse files
committed
[GR-41207] Add missing code snippet in Debug Native Executables with GDB guide.
PullRequest: graal/12709
2 parents 4820bdd + 6bd93da commit 15ab0a2

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

docs/reference-manual/native-image/guides/debug-native-executables-with-gdb.md

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,64 @@ This will enable source-level debugging, and the debugger (GDB) then correlates
3030
- Linux AMD64
3131
- GDB 10.1 or higher
3232

33-
Follow the steps to test debugging a native executable with GDB. The below workflow is know to work on Linux OS with GDB 10.1.
34-
35-
1. Save the following code to the file named _GDBDemo.java_.
33+
Follow the steps to test debugging a native executable with GDB. The below workflow is known to work on Linux with GDB 10.1.
34+
35+
1. Save the following code to the file named _GDBDemo.java_.
36+
37+
```java
38+
public class GDBDemo {
39+
static long fieldUsed = 1000;
40+
41+
public static void main(String[] args) {
42+
if (args.length > 0) {
43+
int n = -1;
44+
try {
45+
n = Integer.parseInt(args[0]);
46+
} catch (NumberFormatException ex) {
47+
System.out.println(args[0] + " is not a number!");
48+
}
49+
if (n < 0) {
50+
System.out.println(args[0] + " is negative.");
51+
}
52+
double f = factorial(n);
53+
System.out.println(n + "! = " + f);
54+
}
55+
56+
if (false)
57+
neverCalledMethod();
58+
59+
StringBuilder text = new StringBuilder();
60+
text.append("Hello World from GraalVM Native Image and GDB in Java.\n");
61+
System.out.println(text.toString());
62+
}
63+
64+
static void neverCalledMethod() {
65+
System.out.println("This method is unreachable and will not be included in the native executable.");
66+
}
67+
68+
static double factorial(int n) {
69+
if (n == 0) {
70+
return 1;
71+
}
72+
if (n >= fieldUsed) {
73+
return Double.POSITIVE_INFINITY;
74+
}
75+
double f = 1;
76+
while (n > 1) {
77+
f *= n--;
78+
}
79+
return f;
80+
}
81+
}
82+
```
3683

3784
2. Compile it and generate a native executable with debug information:
3885

3986
```shell
40-
$JAVA_HOME/bin/javac JFRDemo.java
87+
$JAVA_HOME/bin/javac GDBDemo.java
4188
```
4289
```shell
43-
native-image -g -O0 JFRDemo
90+
native-image -g -O0 GDBDemo
4491
```
4592
The `-g` option instructs `native-image` to generate debug information. The resulting native executable will contain debug records in a format GDB understands.
4693

@@ -49,7 +96,7 @@ Follow the steps to test debugging a native executable with GDB. The below workf
4996
3. Launch the debugger and run your native executable:
5097

5198
```shell
52-
gdb ./jfrdemo
99+
gdb ./gdbdemo
53100
```
54101
The `gdb` prompt will open.
55102

0 commit comments

Comments
 (0)