From 9e8ffc3d0a5472cc6e1c48ebf90641f79157fc8a Mon Sep 17 00:00:00 2001
From: Michael Eager <meager@cisco.com>
Date: Fri, 10 Apr 2015 11:55:02 -0700
Subject: [PATCH] GDB support compressed exec and core files.
Add support to automatically unzip compressed executable and core files.
Files will be uncompressed into temporary directory (/tmp or $TMPDIR)
and are deleted when GDB exits. This should be transparent to users,
except for disk space requirements. The name of the uncompressed file is
mentioned, but all references to the file in GDB messages is to the file
which the user specified.
gdb/ChangeLog:
* utils.c (struct compressed_file_cache_search, eq_compressed_file,
is_gzip, decompress_gzip, do_compressed_cleanup, identify_compression,
uncompress_to_temporar, gdb_uncompress): New.
* utils.h (gdb_uncompress): Declare.
* corelow.c (core_open): Uncompress core file.
* exec.c (exec_file_attach): Uncompress exec file.
* symfile.c (symfile_bfd_open): Uncompress sym (exec) file.
* NEWS: Mention new functionality.
gdb/doc:
* gdb.texinfo (Files): Mention gzipped exec and core files.
---
gdb/NEWS | 3 +
gdb/corelow.c | 11 ++-
gdb/doc/gdb.texinfo | 5 ++
gdb/exec.c | 14 +++-
gdb/symfile.c | 14 +++-
gdb/utils.c | 211 ++++++++++++++++++++++++++++++++++++++++++++++++++++
gdb/utils.h | 4 +
7 files changed, 259 insertions(+), 3 deletions(-)
@@ -139,6 +139,9 @@ qXfer:exec-file:read
* Removed targets and native configurations
+* GDB will automatically uncompress executable and core files
+ which have been compressed using gzip.
+
HP/PA running HP-UX hppa*-*-hpux*
Itanium running HP-UX ia64-*-hpux*
@@ -279,6 +279,7 @@ core_open (const char *arg, int from_tty)
int scratch_chan;
int flags;
char *filename;
+ char *uncompressed_filename;
target_preopen (from_tty);
if (!arg)
@@ -310,12 +311,20 @@ core_open (const char *arg, int from_tty)
if (scratch_chan < 0)
perror_with_name (filename);
- temp_bfd = gdb_bfd_fopen (filename, gnutarget,
+ temp_bfd = gdb_bfd_fopen (filename, gnutarget,
write_files ? FOPEN_RUB : FOPEN_RB,
scratch_chan);
if (temp_bfd == NULL)
perror_with_name (filename);
+ if (!write_files && gdb_uncompress (filename, &uncompressed_filename))
+ {
+ close (scratch_chan);
+ scratch_chan = gdb_open_cloexec (uncompressed_filename, flags, 0);
+ temp_bfd = gdb_bfd_fopen (uncompressed_filename, gnutarget,
+ FOPEN_RB, scratch_chan);
+ }
+
if (!bfd_check_format (temp_bfd, bfd_core)
&& !gdb_check_format (temp_bfd))
{
@@ -17504,6 +17504,11 @@ via @code{gdbserver} (@pxref{Server, file, Using the @code{gdbserver}
Program}). In these situations the @value{GDBN} commands to specify
new files are useful.
+Executable and core files may be compressed using @command{gzip}. These
+files will be uncompressed into temporary files in the system-wide
+temporary directory (e.g., @file{/tmp} on some systems). The files will
+have a unique name and will be deleted when @value{GDBN} terminates.
+
@table @code
@cindex executable file
@kindex file
@@ -196,6 +196,7 @@ void
exec_file_attach (const char *filename, int from_tty)
{
struct cleanup *cleanups;
+ char *uncompressed_filename = NULL;
/* First, acquire a reference to the current exec_bfd. We release
this at the end of the function; but acquiring it now lets the
@@ -280,7 +281,18 @@ exec_file_attach (const char *filename, int from_tty)
exec_bfd = gdb_bfd_fopen (canonical_pathname, gnutarget,
FOPEN_RUB, scratch_chan);
else
- exec_bfd = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
+ {
+ if (!gdb_uncompress (canonical_pathname, &uncompressed_filename))
+ exec_bfd = gdb_bfd_open (canonical_pathname, gnutarget, scratch_chan);
+ else
+ {
+ close (scratch_chan);
+ scratch_chan = openp ("", 0, uncompressed_filename,
+ O_RDONLY | O_BINARY, &scratch_pathname);
+
+ exec_bfd = gdb_bfd_open (uncompressed_filename, gnutarget, scratch_chan);
+ }
+ }
if (!exec_bfd)
{
@@ -56,6 +56,7 @@
#include "stack.h"
#include "gdb_bfd.h"
#include "cli/cli-utils.h"
+#include "utils.h"
#include <sys/types.h>
#include <fcntl.h>
@@ -1723,6 +1724,8 @@ symfile_bfd_open (const char *name)
bfd *sym_bfd;
int desc = -1;
struct cleanup *back_to = make_cleanup (null_cleanup, 0);
+ char *absolute_name;
+ char *uncompressed_filename;
if (!is_target_filename (name))
{
@@ -1756,7 +1759,16 @@ symfile_bfd_open (const char *name)
name = absolute_name;
}
- sym_bfd = gdb_bfd_open (name, gnutarget, desc);
+ if (!gdb_uncompress (name, &uncompressed_filename))
+ sym_bfd = gdb_bfd_open (name, gnutarget, desc);
+ else
+ {
+ close (desc);
+ desc = openp ("", 0, uncompressed_filename, O_RDONLY | O_BINARY, &absolute_name);
+
+ sym_bfd = gdb_bfd_open (uncompressed_filename, gnutarget, desc);
+ }
+
if (!sym_bfd)
error (_("`%s': can't open to read symbols: %s."), name,
bfd_errmsg (bfd_get_error ()));
@@ -29,6 +29,8 @@
#include <sys/resource.h>
#endif /* HAVE_SYS_RESOURCE_H */
+#include <zlib.h>
+
#ifdef TUI
#include "tui/tui.h" /* For tui_get_command_dimension. */
#endif
@@ -126,6 +128,10 @@ int immediate_quit;
as octal escapes. Zero means just print the value (e.g. it's an
international character, and the terminal or window can cope.) */
+/* Type of file compression. */
+
+enum file_compression_t {NONE, GZIP, BZIP2};
+
int sevenbit_strings = 0;
static void
show_sevenbit_strings (struct ui_file *file, int from_tty,
@@ -3479,6 +3485,211 @@ gdb_filename_fnmatch (const char *pattern, const char *string, int flags)
return fnmatch (pattern, string, flags);
}
+
+/* Hash table of compressed files. */
+
+static htab_t compressed_file_cache;
+
+struct compressed_file_cache_search
+{
+ char *filename;
+ char *uncompressed_filename;
+ time_t mtime;
+};
+
+static int
+eq_compressed_file (const void *a, const void *b)
+{
+ const struct compressed_file_cache_search *entry = a;
+ const struct compressed_file_cache_search *search = b;
+
+ return (strcmp (entry->filename, search->filename) == 0);
+}
+
+/* Test if file is compressed with gzip. */
+
+static inline int
+is_gzip (unsigned char *buf)
+{
+ return (buf[0] == 037 && buf[1] == 0213); /* From /usr/share/magic. */
+}
+
+/* Alloc 1Mb buffer to uncompress data. */
+#define COMPRESS_BUF_SIZE (1024 * 1024)
+static int
+decompress_gzip (const char *filename, FILE *tmp)
+{
+ char *buf = xmalloc (COMPRESS_BUF_SIZE);
+ gzFile compressed = gzopen (filename, "r");
+ int count, res;
+
+ if (buf == NULL || compressed == NULL)
+ {
+ fprintf_filtered (gdb_stderr, _("error copying gzip file\n"));
+ free (buf);
+ return 0;
+ }
+
+ while ((count = gzread (compressed, buf, COMPRESS_BUF_SIZE)))
+ {
+ res = fwrite (buf, 1, count, tmp);
+ if (res != count)
+ {
+ fprintf_filtered (gdb_stderr, _("error decompressing gzip file\n"));
+ free (buf);
+ return 0;
+ }
+ }
+
+ gzclose (compressed);
+ free (buf);
+ return 1;
+}
+
+/* Delete uncompressed temp file when terminating. */
+static void
+do_compressed_cleanup (void *filename)
+{
+ unlink (filename);
+ xfree (filename);
+}
+
+/* Identify type of file compression used. */
+static enum file_compression_t
+identify_compression (const char *filename)
+{
+ FILE *handle;
+ enum file_compression_t file_compression = NONE;
+ unsigned char buffer[1024];
+ size_t count;
+
+ handle = fopen (filename, FOPEN_RB);
+ if (handle == NULL)
+ return 0;
+
+ count = fread (buffer, 1, sizeof buffer, handle);
+ if (count > 0)
+ {
+ if (is_gzip (buffer))
+ file_compression = GZIP;
+ }
+
+ fclose (handle);
+
+ return file_compression;
+}
+
+/* Uncompress into temporary file. Return temp file name. */
+static char *
+uncompress_to_temporary (const char *filename)
+{
+ enum file_compression_t file_compression;
+ char *template;
+ int ret = 0;
+ int decomp_fd;
+ FILE *decomp_file;
+
+ file_compression = identify_compression (filename);
+ if (file_compression != GZIP)
+ /* Only gzip supported at this time. */
+ return NULL;
+
+ /* Create temporary file name for uncompressed file. */
+ if (!asprintf (&template, "%s%s-XXXXXX", choose_tmpdir (),
+ basename (filename)))
+ return NULL;
+
+ decomp_fd = mkstemp (template);
+ if (decomp_fd == -1)
+ return NULL;
+
+ decomp_file = fdopen (decomp_fd, FOPEN_WUB);
+
+ if (file_compression == GZIP)
+ {
+ printf (_("Decompressing %s to %s\n"), filename, template);
+ ret = decompress_gzip (filename, decomp_file);
+ }
+
+ if (!ret)
+ {
+ xfree (template);
+ template = NULL;
+ }
+
+ fclose (decomp_file);
+ return template;
+}
+
+/* If file is compressed, uncompress it into a temporary. */
+int
+gdb_uncompress (const char *filename, char **uncompressed_filename)
+{
+ struct compressed_file_cache_search search, *found;
+ struct stat st;
+ hashval_t hash;
+ void **slot;
+ char *temp_filename;
+
+ if (compressed_file_cache == NULL)
+ compressed_file_cache = htab_create_alloc (1, htab_hash_string,
+ eq_compressed_file,
+ NULL, xcalloc, xfree);
+ if (stat (filename, &st) < 0)
+ return 0;
+
+ search.filename = (char *) filename;
+ search.uncompressed_filename = NULL;
+
+ hash = htab_hash_string (filename);
+ found = htab_find_with_hash (compressed_file_cache, &search, hash);
+
+ if (found)
+ {
+ /* We previously uncompressed the file. */
+ if (found->mtime == st.st_mtime)
+ {
+ /* Return file if compressed file not changed. */
+ *uncompressed_filename = found->uncompressed_filename;
+ return 1;
+ }
+ else
+ {
+ /* Delete old uncompressed file. */
+ unlink (found->uncompressed_filename);
+ xfree (found->filename);
+ xfree (found->uncompressed_filename);
+ }
+ }
+
+ temp_filename = uncompress_to_temporary (filename);
+
+ if (!temp_filename)
+ /* Not uncompressed. */
+ return 0;
+
+ if (!found)
+ {
+ slot = htab_find_slot_with_hash (compressed_file_cache,
+ &search, hash, INSERT);
+ gdb_assert (slot && !*slot);
+ found = xmalloc (sizeof (struct compressed_file_cache_search));
+ *slot = found;
+ }
+
+ found->filename = strdup (filename);
+ found->mtime = st.st_mtime;
+ found->uncompressed_filename = temp_filename;
+
+ /* Tell caller name of uncompressed file. */
+ *uncompressed_filename = temp_filename;
+
+ /* Schedule delete of temp file when gdb ends. */
+ make_final_cleanup (do_compressed_cleanup, xstrdup (temp_filename));
+
+ return 1;
+}
+
/* Provide a prototype to silence -Wmissing-prototypes. */
extern initialize_file_ftype _initialize_utils;
@@ -371,4 +371,8 @@ extern void dump_core (void);
extern char *make_hex_string (const gdb_byte *data, size_t length);
+/* Uncompress file if compressed. */
+
+int gdb_uncompress (const char *filename, char **uncompressed_filename);
+
#endif /* UTILS_H */
--
2.2.1