Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / elf-none-tdep.c
blob00d46992b24842d4156504f9e07e6772a33cbcb0
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-2024 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 "elf-none-tdep.h"
22 #include "regset.h"
23 #include "elf-bfd.h"
24 #include "inferior.h"
25 #include "regcache.h"
26 #include "gdbarch.h"
27 #include "gcore.h"
28 #include "gcore-elf.h"
30 /* Build the note section for a corefile, and return it in a malloc
31 buffer. Currently this just dumps all available registers for each
32 thread. */
34 static gdb::unique_xmalloc_ptr<char>
35 elf_none_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd,
36 int *note_size)
38 gdb::unique_xmalloc_ptr<char> note_data;
40 /* Add note information about the executable and its arguments. */
41 std::string fname;
42 std::string psargs;
43 static const size_t fname_len = 16;
44 static const size_t psargs_len = 80;
45 if (get_exec_file (0))
47 const char *exe = get_exec_file (0);
48 fname = lbasename (exe);
49 psargs = std::string (exe);
51 const std::string &infargs = current_inferior ()->args ();
52 if (!infargs.empty ())
53 psargs += ' ' + infargs;
55 /* All existing targets that handle writing out prpsinfo expect the
56 fname and psargs strings to be at least 16 and 80 characters long
57 respectively, including a null terminator at the end. Resize to
58 the expected length minus one to ensure there is a null within the
59 required length. */
60 fname.resize (fname_len - 1);
61 psargs.resize (psargs_len - 1);
64 /* Resize the buffers up to their required lengths. This will fill any
65 remaining space with the null character. */
66 fname.resize (fname_len);
67 psargs.resize (psargs_len);
69 /* Now write out the prpsinfo structure. */
70 note_data.reset (elfcore_write_prpsinfo (obfd, note_data.release (),
71 note_size, fname.c_str (),
72 psargs.c_str ()));
73 if (note_data == nullptr)
74 return nullptr;
76 /* Thread register information. */
77 try
79 update_thread_list ();
81 catch (const gdb_exception_error &e)
83 exception_print (gdb_stderr, e);
86 /* Like the Linux kernel, prefer dumping the signalled thread first.
87 "First thread" is what tools use to infer the signalled thread. */
88 thread_info *signalled_thr = gcore_find_signalled_thread ();
90 /* All threads are reported as having been stopped by the same signal
91 that stopped SIGNALLED_THR. */
92 gdb_signal stop_signal;
93 if (signalled_thr != nullptr)
94 stop_signal = signalled_thr->stop_signal ();
95 else
96 stop_signal = GDB_SIGNAL_0;
98 if (signalled_thr != nullptr)
99 gcore_elf_build_thread_register_notes (gdbarch, signalled_thr,
100 stop_signal, obfd, &note_data,
101 note_size);
102 for (thread_info *thr : current_inferior ()->non_exited_threads ())
104 if (thr == signalled_thr)
105 continue;
107 gcore_elf_build_thread_register_notes (gdbarch, thr, stop_signal, obfd,
108 &note_data, note_size);
112 /* Include the target description when possible. Some architectures
113 allow for per-thread gdbarch so we should really be emitting a tdesc
114 per-thread, however, we don't currently support reading in a
115 per-thread tdesc, so just emit the tdesc for the signalled thread. */
116 gdbarch = target_thread_architecture (signalled_thr->ptid);
117 gcore_elf_make_tdesc_note (gdbarch, obfd, &note_data, note_size);
119 return note_data;
122 /* See none-tdep.h. */
124 void
125 elf_none_init_abi (struct gdbarch *gdbarch)
127 /* Default core file support. */
128 set_gdbarch_make_corefile_notes (gdbarch, elf_none_make_corefile_notes);