gdb: parse and set the inferior environment from core files

Extend the core file context parsing mechanism added in the previous
commit to also store the environment parsed from the core file.

This environment can then be injected into the inferior object.

The benefit of this is that when examining a core file in GDB, the
'show environment' command will now show the environment extracted
from a core file.

Consider this example:

  $ env -i GDB_TEST_VAR=FOO ./gen-core
  Segmentation fault (core dumped)
  $ gdb -c ./core.1669829
  ...
  [New LWP 1669829]
  Core was generated by `./gen-core'.
  Program terminated with signal SIGSEGV, Segmentation fault.
  #0  0x0000000000401111 in ?? ()
  (gdb) show environment
  GDB_TEST_VAR=foo
  (gdb)

There's a new test for this functionality.
This commit is contained in:
Andrew Burgess
2024-06-08 11:06:02 +01:00
parent d3d13bf876
commit 6004567165
5 changed files with 106 additions and 5 deletions

View File

@@ -21,6 +21,7 @@
#define GDB_ARCH_UTILS_H
#include "gdbarch.h"
#include "gdbsupport/environ.h"
class frame_info_ptr;
struct minimal_symbol;
@@ -88,9 +89,11 @@ struct core_file_exec_context
found but not ARGV then use the no-argument constructor to create an
empty context object. */
core_file_exec_context (gdb::unique_xmalloc_ptr<char> exec_name,
std::vector<gdb::unique_xmalloc_ptr<char>> argv)
std::vector<gdb::unique_xmalloc_ptr<char>> argv,
std::vector<gdb::unique_xmalloc_ptr<char>> envp)
: m_exec_name (std::move (exec_name)),
m_arguments (std::move (argv))
m_arguments (std::move (argv)),
m_environment (std::move (envp))
{
gdb_assert (m_exec_name != nullptr);
}
@@ -115,6 +118,9 @@ struct core_file_exec_context
const std::vector<gdb::unique_xmalloc_ptr<char>> &args () const
{ return m_arguments; }
/* Return the environment variables from this context. */
gdb_environ environment () const;
private:
/* The executable filename as reported in the core file. Can be nullptr
@@ -124,6 +130,9 @@ private:
/* List of arguments. Doesn't include argv[0] which is the executable
name, for this look at m_exec_name field. */
std::vector<gdb::unique_xmalloc_ptr<char>> m_arguments;
/* List of environment strings. */
std::vector<gdb::unique_xmalloc_ptr<char>> m_environment;
};
/* Default implementation of gdbarch_displaced_hw_singlestep. */