FILE * fopen_with_cleanup(const char *filename, const char *mode) { FILE *file = fopen(filename, mode); if (file == NULL) perror_with_name(filename); make_cleanup_fclose(file); return file; }
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; struct cleanup *cleanup; mapfilename = xstrprintf ("/proc/%ld/map", (long) pid); cleanup = make_cleanup (xfree, mapfilename); mapfile = fopen (mapfilename, "r"); if (mapfile == NULL) error (_("Couldn't open %s."), mapfilename); make_cleanup_fclose (mapfile); if (info_verbose) fprintf_filtered (gdb_stdout, "Reading memory regions from %s\n", mapfilename); /* Now iterate until end-of-file. */ while (fbsd_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' : '-'); } /* Invoke the callback function to create the corefile segment. */ func (start, size, read, write, exec, obfd); } do_cleanups (cleanup); return 0; }