elf-none-tdep.c 3.9 KB

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