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