#include "defs.h"
#include "gdbcore.h"
#include "inferior.h"
#include "gdb_string.h"
#include <sys/procfs.h>
#include <sys/types.h>
#include "elf-bfd.h"
#include "gregset.h"
char *
child_pid_to_exec_file (int pid)
{
char *path;
char *buf;
xasprintf (&path, "/proc/%d/file", pid);
buf = xcalloc (MAXPATHLEN, sizeof (char));
make_cleanup (xfree, path);
make_cleanup (xfree, buf);
if (readlink (path, buf, MAXPATHLEN) > 0)
return buf;
return NULL;
}
static int
read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end,
char *protection)
{
char buf[256];
int resident, privateresident;
unsigned long obj;
int ret = EOF;
if (fgets (buf, sizeof buf, mapfile) != NULL)
ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end,
&resident, &privateresident, &obj, protection);
return (ret != 0 && ret != EOF);
}
static int
fbsd_find_memory_regions (int (*func) (CORE_ADDR, unsigned long,
int, int, int, void *),
void *obfd)
{
pid_t pid = ptid_get_pid (inferior_ptid);
char *mapfilename;
FILE *mapfile;
unsigned long start, end, size;
char protection[4];
int read, write, exec;
xasprintf (&mapfilename, "/proc/%ld/map", (long) pid);
mapfile = fopen (mapfilename, "r");
if (mapfile == NULL)
error ("Couldn't open %s\n", mapfilename);
if (info_verbose)
fprintf_filtered (gdb_stdout,
"Reading memory regions from %s\n", mapfilename);
while (read_mapping (mapfile, &start, &end, &protection[0]))
{
size = end - start;
read = (strchr (protection, 'r') != 0);
write = (strchr (protection, 'w') != 0);
exec = (strchr (protection, 'x') != 0);
if (info_verbose)
{
fprintf_filtered (gdb_stdout,
"Save segment, %ld bytes at 0x%s (%c%c%c)\n",
size, paddr_nz (start),
read ? 'r' : '-',
write ? 'w' : '-',
exec ? 'x' : '-');
}
func (start, size, read, write, exec, obfd);
}
fclose (mapfile);
return 0;
}
static char *
fbsd_make_corefile_notes (bfd *obfd, int *note_size)
{
gregset_t gregs;
fpregset_t fpregs;
char *note_data = NULL;
Elf_Internal_Ehdr *i_ehdrp;
i_ehdrp = elf_elfheader (obfd);
i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
fill_gregset (&gregs, -1);
note_data = elfcore_write_prstatus (obfd, note_data, note_size,
ptid_get_pid (inferior_ptid),
stop_signal, &gregs);
fill_fpregset (&fpregs, -1);
note_data = elfcore_write_prfpreg (obfd, note_data, note_size,
&fpregs, sizeof (fpregs));
if (get_exec_file (0))
{
char *fname = strrchr (get_exec_file (0), '/') + 1;
char *psargs = xstrdup (fname);
if (get_inferior_args ())
psargs = reconcat (psargs, psargs, " ", get_inferior_args (), NULL);
note_data = elfcore_write_prpsinfo (obfd, note_data, note_size,
fname, psargs);
}
make_cleanup (xfree, note_data);
return note_data;
}
void
_initialize_fbsd_proc (void)
{
extern void inftarg_set_find_memory_regions ();
extern void inftarg_set_make_corefile_notes ();
inftarg_set_find_memory_regions (fbsd_find_memory_regions);
inftarg_set_make_corefile_notes (fbsd_make_corefile_notes);
}