]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/jit-elf-fork-main.c
Automatic Copyright Year update after running gdb/copyright.py
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / jit-elf-fork-main.c
1 /* This test program is part of GDB, the GNU debugger.
2
3 Copyright 2011-2022 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 /* Simulate loading of JIT code. */
19
20 #include <elf.h>
21 #include <link.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/mman.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31
32 #include "jit-protocol.h"
33 #include "jit-elf-util.h"
34
35 static void
36 usage (void)
37 {
38 fprintf (stderr, "Usage: jit-elf-main libraries...\n");
39 exit (1);
40 }
41
42 /* Must be defined by .exp file when compiling to know
43 what address to map the ELF binary to. */
44 #ifndef LOAD_ADDRESS
45 #error "Must define LOAD_ADDRESS"
46 #endif
47 #ifndef LOAD_INCREMENT
48 #error "Must define LOAD_INCREMENT"
49 #endif
50
51 int
52 main (int argc, char *argv[])
53 {
54 int i;
55 alarm (300);
56 /* Used as backing storage for GDB to populate argv. */
57 char *fake_argv[10];
58
59 if (argc < 2)
60 {
61 usage ();
62 exit (1);
63 }
64
65 for (i = 1; i < argc; ++i)
66 {
67 size_t obj_size;
68 void *load_addr = (void *) (size_t) (LOAD_ADDRESS + (i - 1) * LOAD_INCREMENT);
69 printf ("Loading %s as JIT at %p\n", argv[i], load_addr);
70 void *addr = load_elf (argv[i], &obj_size, load_addr);
71
72 char name[32];
73 sprintf (name, "jit_function_%04d", i);
74 int (*jit_function) (void) = (int (*) (void)) load_symbol (addr, name);
75
76 /* Link entry at the end of the list. */
77 struct jit_code_entry *const entry = calloc (1, sizeof (*entry));
78 entry->symfile_addr = (const char *)addr;
79 entry->symfile_size = obj_size;
80 entry->prev_entry = __jit_debug_descriptor.relevant_entry;
81 __jit_debug_descriptor.relevant_entry = entry;
82
83 if (entry->prev_entry != NULL)
84 entry->prev_entry->next_entry = entry;
85 else
86 __jit_debug_descriptor.first_entry = entry;
87
88 /* Notify GDB. */
89 __jit_debug_descriptor.action_flag = JIT_REGISTER;
90 __jit_debug_register_code ();
91
92 if (jit_function () != 42)
93 {
94 fprintf (stderr, "unexpected return value\n");
95 exit (1);
96 }
97 }
98
99 i = 0; /* break before fork */
100
101 fork ();
102
103 i = 0; /* break after fork */
104
105 /* Now unregister them all in reverse order. */
106 while (__jit_debug_descriptor.relevant_entry != NULL)
107 {
108 struct jit_code_entry *const entry =
109 __jit_debug_descriptor.relevant_entry;
110 struct jit_code_entry *const prev_entry = entry->prev_entry;
111
112 if (prev_entry != NULL)
113 {
114 prev_entry->next_entry = NULL;
115 entry->prev_entry = NULL;
116 }
117 else
118 __jit_debug_descriptor.first_entry = NULL;
119
120 /* Notify GDB. */
121 __jit_debug_descriptor.action_flag = JIT_UNREGISTER;
122 __jit_debug_register_code ();
123
124 __jit_debug_descriptor.relevant_entry = prev_entry;
125 free (entry);
126 }
127
128 return 0; /* break before return */
129 }