1 /* <proc_service.h> implementation.
3 Copyright (C) 1999-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "gdbthread.h"
30 #include "gdb_proc_service.h"
32 #include <sys/procfs.h>
34 /* Prototypes for supply_gregset etc. */
38 /* Helper functions. */
40 /* Convert a psaddr_t to a CORE_ADDR. */
43 ps_addr_to_core_addr (psaddr_t addr
)
45 if (current_program_space
->exec_bfd ()
46 && bfd_get_sign_extend_vma (current_program_space
->exec_bfd ()))
47 return (intptr_t) addr
;
49 return (uintptr_t) addr
;
52 /* Convert a CORE_ADDR to a psaddr_t. */
55 core_addr_to_ps_addr (CORE_ADDR addr
)
57 if (current_program_space
->exec_bfd ()
58 && bfd_get_sign_extend_vma (current_program_space
->exec_bfd ()))
59 return (psaddr_t
) (intptr_t) addr
;
61 return (psaddr_t
) (uintptr_t) addr
;
64 /* Transfer LEN bytes of memory between BUF and address ADDR in the
65 process specified by PH. If WRITE, transfer them to the process,
66 else transfer them from the process. Returns PS_OK for success,
69 This is a helper function for ps_pdread and ps_pdwrite. */
72 ps_xfer_memory (const struct ps_prochandle
*ph
, psaddr_t addr
,
73 gdb_byte
*buf
, size_t len
, int write
)
75 scoped_restore_current_inferior_for_memory
save_inferior (ph
->thread
->inf
);
77 CORE_ADDR core_addr
= ps_addr_to_core_addr (addr
);
81 ret
= target_write_memory (core_addr
, buf
, len
);
83 ret
= target_read_memory (core_addr
, buf
, len
);
84 return (ret
== 0 ? PS_OK
: PS_ERR
);
88 /* Search for the symbol named NAME within the object named OBJ within
89 the target process PH. If the symbol is found the address of the
90 symbol is stored in SYM_ADDR. */
93 ps_pglobal_lookup (struct ps_prochandle
*ph
, const char *obj
,
94 const char *name
, psaddr_t
*sym_addr
)
96 inferior
*inf
= ph
->thread
->inf
;
98 scoped_restore_current_program_space restore_pspace
;
100 set_current_program_space (inf
->pspace
);
102 /* FIXME: kettenis/2000-09-03: What should we do with OBJ? */
103 bound_minimal_symbol ms
= lookup_minimal_symbol (name
, NULL
, NULL
);
104 if (ms
.minsym
== NULL
)
107 *sym_addr
= core_addr_to_ps_addr (ms
.value_address ());
111 /* Read SIZE bytes from the target process PH at address ADDR and copy
115 ps_pdread (struct ps_prochandle
*ph
, psaddr_t addr
, void *buf
, size_t size
)
117 return ps_xfer_memory (ph
, addr
, (gdb_byte
*) buf
, size
, 0);
120 /* Write SIZE bytes from BUF into the target process PH at address ADDR. */
123 ps_pdwrite (struct ps_prochandle
*ph
, psaddr_t addr
,
124 const void *buf
, size_t size
)
126 return ps_xfer_memory (ph
, addr
, (gdb_byte
*) buf
, size
, 1);
129 /* Get a regcache for LWPID using its inferior's "main" architecture,
130 which is the register set libthread_db expects to be using. In
131 multi-arch debugging scenarios, the thread's architecture may
132 differ from the inferior's "main" architecture. */
134 static struct regcache
*
135 get_ps_regcache (struct ps_prochandle
*ph
, lwpid_t lwpid
)
137 inferior
*inf
= ph
->thread
->inf
;
138 return get_thread_arch_regcache (inf
->process_target (),
139 ptid_t (inf
->pid
, lwpid
),
143 /* Get the general registers of LWP LWPID within the target process PH
144 and store them in GREGSET. */
147 ps_lgetregs (struct ps_prochandle
*ph
, lwpid_t lwpid
, prgregset_t gregset
)
149 struct regcache
*regcache
= get_ps_regcache (ph
, lwpid
);
151 target_fetch_registers (regcache
, -1);
152 fill_gregset (regcache
, (gdb_gregset_t
*) gregset
, -1);
157 /* Set the general registers of LWP LWPID within the target process PH
161 ps_lsetregs (struct ps_prochandle
*ph
, lwpid_t lwpid
, const prgregset_t gregset
)
163 struct regcache
*regcache
= get_ps_regcache (ph
, lwpid
);
165 supply_gregset (regcache
, (const gdb_gregset_t
*) gregset
);
166 target_store_registers (regcache
, -1);
171 /* Get the floating-point registers of LWP LWPID within the target
172 process PH and store them in FPREGSET. */
175 ps_lgetfpregs (struct ps_prochandle
*ph
, lwpid_t lwpid
,
176 prfpregset_t
*fpregset
)
178 struct regcache
*regcache
= get_ps_regcache (ph
, lwpid
);
180 target_fetch_registers (regcache
, -1);
181 fill_fpregset (regcache
, (gdb_fpregset_t
*) fpregset
, -1);
186 /* Set the floating-point registers of LWP LWPID within the target
187 process PH from FPREGSET. */
190 ps_lsetfpregs (struct ps_prochandle
*ph
, lwpid_t lwpid
,
191 const prfpregset_t
*fpregset
)
193 struct regcache
*regcache
= get_ps_regcache (ph
, lwpid
);
195 supply_fpregset (regcache
, (const gdb_fpregset_t
*) fpregset
);
196 target_store_registers (regcache
, -1);
201 /* Return overall process id of the target PH. Special for GNU/Linux
202 -- not used on Solaris. */
205 ps_getpid (struct ps_prochandle
*ph
)
207 return ph
->thread
->ptid
.pid ();
210 void _initialize_proc_service ();
212 _initialize_proc_service ()
214 /* This function solely exists to make sure this module is linked
215 into the final binary. */