1*6881a400Schristos /* Common code for targets with the none ABI (bare-metal), but where the 2*6881a400Schristos BFD library is build with ELF support. 3*6881a400Schristos 4*6881a400Schristos Copyright (C) 2020-2023 Free Software Foundation, Inc. 5*6881a400Schristos 6*6881a400Schristos This file is part of GDB. 7*6881a400Schristos 8*6881a400Schristos This program is free software; you can redistribute it and/or modify 9*6881a400Schristos it under the terms of the GNU General Public License as published by 10*6881a400Schristos the Free Software Foundation; either version 3 of the License, or 11*6881a400Schristos (at your option) any later version. 12*6881a400Schristos 13*6881a400Schristos This program is distributed in the hope that it will be useful, 14*6881a400Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 15*6881a400Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*6881a400Schristos GNU General Public License for more details. 17*6881a400Schristos 18*6881a400Schristos You should have received a copy of the GNU General Public License 19*6881a400Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 20*6881a400Schristos 21*6881a400Schristos #include "defs.h" 22*6881a400Schristos #include "elf-none-tdep.h" 23*6881a400Schristos #include "regset.h" 24*6881a400Schristos #include "elf-bfd.h" /* for elfcore_write_* */ 25*6881a400Schristos #include "inferior.h" 26*6881a400Schristos #include "regcache.h" 27*6881a400Schristos #include "gdbarch.h" 28*6881a400Schristos #include "gcore.h" 29*6881a400Schristos #include "gcore-elf.h" 30*6881a400Schristos 31*6881a400Schristos /* Build the note section for a corefile, and return it in a malloc 32*6881a400Schristos buffer. Currently this just dumps all available registers for each 33*6881a400Schristos thread. */ 34*6881a400Schristos 35*6881a400Schristos static gdb::unique_xmalloc_ptr<char> 36*6881a400Schristos elf_none_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, 37*6881a400Schristos int *note_size) 38*6881a400Schristos { 39*6881a400Schristos gdb::unique_xmalloc_ptr<char> note_data; 40*6881a400Schristos 41*6881a400Schristos /* Add note information about the executable and its arguments. */ 42*6881a400Schristos std::string fname; 43*6881a400Schristos std::string psargs; 44*6881a400Schristos static const size_t fname_len = 16; 45*6881a400Schristos static const size_t psargs_len = 80; 46*6881a400Schristos if (get_exec_file (0)) 47*6881a400Schristos { 48*6881a400Schristos const char *exe = get_exec_file (0); 49*6881a400Schristos fname = lbasename (exe); 50*6881a400Schristos psargs = std::string (exe); 51*6881a400Schristos 52*6881a400Schristos const std::string &infargs = current_inferior ()->args (); 53*6881a400Schristos if (!infargs.empty ()) 54*6881a400Schristos psargs += ' ' + infargs; 55*6881a400Schristos 56*6881a400Schristos /* All existing targets that handle writing out prpsinfo expect the 57*6881a400Schristos fname and psargs strings to be at least 16 and 80 characters long 58*6881a400Schristos respectively, including a null terminator at the end. Resize to 59*6881a400Schristos the expected length minus one to ensure there is a null within the 60*6881a400Schristos required length. */ 61*6881a400Schristos fname.resize (fname_len - 1); 62*6881a400Schristos psargs.resize (psargs_len - 1); 63*6881a400Schristos } 64*6881a400Schristos 65*6881a400Schristos /* Resize the buffers up to their required lengths. This will fill any 66*6881a400Schristos remaining space with the null character. */ 67*6881a400Schristos fname.resize (fname_len); 68*6881a400Schristos psargs.resize (psargs_len); 69*6881a400Schristos 70*6881a400Schristos /* Now write out the prpsinfo structure. */ 71*6881a400Schristos note_data.reset (elfcore_write_prpsinfo (obfd, note_data.release (), 72*6881a400Schristos note_size, fname.c_str (), 73*6881a400Schristos psargs.c_str ())); 74*6881a400Schristos if (note_data == nullptr) 75*6881a400Schristos return nullptr; 76*6881a400Schristos 77*6881a400Schristos /* Thread register information. */ 78*6881a400Schristos try 79*6881a400Schristos { 80*6881a400Schristos update_thread_list (); 81*6881a400Schristos } 82*6881a400Schristos catch (const gdb_exception_error &e) 83*6881a400Schristos { 84*6881a400Schristos exception_print (gdb_stderr, e); 85*6881a400Schristos } 86*6881a400Schristos 87*6881a400Schristos /* Like the Linux kernel, prefer dumping the signalled thread first. 88*6881a400Schristos "First thread" is what tools use to infer the signalled thread. */ 89*6881a400Schristos thread_info *signalled_thr = gcore_find_signalled_thread (); 90*6881a400Schristos 91*6881a400Schristos /* All threads are reported as having been stopped by the same signal 92*6881a400Schristos that stopped SIGNALLED_THR. */ 93*6881a400Schristos gdb_signal stop_signal; 94*6881a400Schristos if (signalled_thr != nullptr) 95*6881a400Schristos stop_signal = signalled_thr->stop_signal (); 96*6881a400Schristos else 97*6881a400Schristos stop_signal = GDB_SIGNAL_0; 98*6881a400Schristos 99*6881a400Schristos if (signalled_thr != nullptr) 100*6881a400Schristos gcore_elf_build_thread_register_notes (gdbarch, signalled_thr, 101*6881a400Schristos stop_signal, obfd, ¬e_data, 102*6881a400Schristos note_size); 103*6881a400Schristos for (thread_info *thr : current_inferior ()->non_exited_threads ()) 104*6881a400Schristos { 105*6881a400Schristos if (thr == signalled_thr) 106*6881a400Schristos continue; 107*6881a400Schristos 108*6881a400Schristos gcore_elf_build_thread_register_notes (gdbarch, thr, stop_signal, obfd, 109*6881a400Schristos ¬e_data, note_size); 110*6881a400Schristos } 111*6881a400Schristos 112*6881a400Schristos 113*6881a400Schristos /* Target description. */ 114*6881a400Schristos gcore_elf_make_tdesc_note (obfd, ¬e_data, note_size); 115*6881a400Schristos 116*6881a400Schristos return note_data; 117*6881a400Schristos } 118*6881a400Schristos 119*6881a400Schristos /* See none-tdep.h. */ 120*6881a400Schristos 121*6881a400Schristos void 122*6881a400Schristos elf_none_init_abi (struct gdbarch *gdbarch) 123*6881a400Schristos { 124*6881a400Schristos /* Default core file support. */ 125*6881a400Schristos set_gdbarch_make_corefile_notes (gdbarch, elf_none_make_corefile_notes); 126*6881a400Schristos } 127