Move gdb_argv to gdbsupport

This moves the gdb_argv class to a new header in gdbsupport.
This commit is contained in:
Tom Tromey 2020-02-25 18:14:19 -07:00
parent 5947982f1d
commit 7904e9613e
18 changed files with 220 additions and 185 deletions

View file

@ -27,6 +27,7 @@
#include "cli-script.h"
#include "completer.h"
#include "gdbsupport/intrusive_list.h"
#include "gdbsupport/buildargv.h"
/* Not a set/show command. Note that some commands which begin with
"set" or "show" might be in this category, if their syntax does

View file

@ -44,6 +44,7 @@
#include "gdbsupport/gdb_unlinker.h"
#include "gdbsupport/pathstuff.h"
#include "gdbsupport/scoped_ignore_signal.h"
#include "gdbsupport/buildargv.h"

View file

@ -49,6 +49,7 @@
#include <algorithm>
#include "gdbsupport/pathstuff.h"
#include "cli/cli-style.h"
#include "gdbsupport/buildargv.h"
void (*deprecated_file_changed_hook) (const char *);

View file

@ -23,6 +23,7 @@
#include "defs.h"
#include "charset.h"
#include "guile-internal.h"
#include "gdbsupport/buildargv.h"
/* Convert STRING to an int.
STRING must be a valid integer. */

View file

@ -35,6 +35,7 @@
#include "target-descriptions.h"
#include "readline/tilde.h"
#include "progspace-and-thread.h"
#include "gdbsupport/buildargv.h"
/* Keep a registry of per-inferior data-pointers required by other GDB
modules. */

View file

@ -72,6 +72,7 @@
#include "scoped-mock-context.h"
#include "test-target.h"
#include "gdbsupport/common-debug.h"
#include "gdbsupport/buildargv.h"
/* Prototypes for local functions */

View file

@ -38,6 +38,7 @@
#include "completer.h"
#include "top.h" /* For command_loop. */
#include "main.h"
#include "gdbsupport/buildargv.h"
/* Each UI has its own independent set of interpreters. */

View file

@ -36,6 +36,7 @@
#include "gdbcmd.h"
#include <algorithm>
#include <set>
#include "gdbsupport/buildargv.h"
static struct partial_symbol *lookup_partial_symbol (struct objfile *,
struct partial_symtab *,

View file

@ -37,6 +37,7 @@
#include "gdbsupport/gdb_optional.h"
#include <list>
#include "cli/cli-style.h"
#include "gdbsupport/buildargv.h"
/* True if we want to print debug printouts related to file/function
skipping. */

View file

@ -50,6 +50,7 @@
#include "observable.h"
#include "build-id.h"
#include "debuginfod-support.h"
#include "gdbsupport/buildargv.h"
#define OPEN_MODE (O_RDONLY | O_BINARY)
#define FDOPEN_MODE FOPEN_RB

View file

@ -55,6 +55,7 @@
#include "gdbsupport/def-vector.h"
#include "cli/cli-option.h"
#include "cli/cli-style.h"
#include "gdbsupport/buildargv.h"
/* The possible choices of "set print frame-arguments", and the value
of this setting. */

View file

@ -61,6 +61,7 @@
#include "gdbsupport/selftest.h"
#include "cli/cli-style.h"
#include "gdbsupport/forward-scope-exit.h"
#include "gdbsupport/buildargv.h"
#include <sys/types.h>
#include <fcntl.h>

View file

@ -38,6 +38,7 @@
#include "source.h"
#include "readline/tilde.h"
#include <cli/cli-style.h>
#include "gdbsupport/buildargv.h"
/* Prototypes for local functions */

View file

@ -24,6 +24,7 @@
#include "regcache.h"
#include "gdbsupport/byte-vector.h"
#include "gdbarch.h"
#include "gdbsupport/buildargv.h"
/* Helper macros. */

View file

@ -58,6 +58,7 @@
#include <algorithm>
#include "cli/cli-style.h"
#include "expop.h"
#include "gdbsupport/buildargv.h"
#include <unistd.h>

View file

@ -76,6 +76,7 @@
#include "cli-out.h"
#include "gdbsupport/gdb-safe-ctype.h"
#include "bt-utils.h"
#include "gdbsupport/buildargv.h"
void (*deprecated_error_begin_hook) (void);
@ -2890,17 +2891,6 @@ ldirname (const char *filename)
return dirname;
}
/* See utils.h. */
void
gdb_argv::reset (const char *s)
{
char **argv = buildargv (s);
freeargv (m_argv);
m_argv = argv;
}
/* Return ARGS parsed as a valid pid, or throw an error. */
int

View file

@ -120,180 +120,6 @@ extern int parse_pid_to_attach (const char *args);
extern int parse_escape (struct gdbarch *, const char **);
/* A wrapper for an array of char* that was allocated in the way that
'buildargv' does, and should be freed with 'freeargv'. */
class gdb_argv
{
public:
/* A constructor that initializes to NULL. */
gdb_argv ()
: m_argv (NULL)
{
}
/* A constructor that calls buildargv on STR. STR may be NULL, in
which case this object is initialized with a NULL array. */
explicit gdb_argv (const char *str)
: m_argv (NULL)
{
reset (str);
}
/* A constructor that takes ownership of an existing array. */
explicit gdb_argv (char **array)
: m_argv (array)
{
}
gdb_argv (const gdb_argv &) = delete;
gdb_argv &operator= (const gdb_argv &) = delete;
gdb_argv &operator= (gdb_argv &&other)
{
freeargv (m_argv);
m_argv = other.m_argv;
other.m_argv = nullptr;
return *this;
}
gdb_argv (gdb_argv &&other)
{
m_argv = other.m_argv;
other.m_argv = nullptr;
}
~gdb_argv ()
{
freeargv (m_argv);
}
/* Call buildargv on STR, storing the result in this object. Any
previous state is freed. STR may be NULL, in which case this
object is reset with a NULL array. If buildargv fails due to
out-of-memory, call malloc_failure. Therefore, the value is
guaranteed to be non-NULL, unless the parameter itself is
NULL. */
void reset (const char *str);
/* Return the underlying array. */
char **get ()
{
return m_argv;
}
const char * const * get () const
{
return m_argv;
}
/* Return the underlying array, transferring ownership to the
caller. */
ATTRIBUTE_UNUSED_RESULT char **release ()
{
char **result = m_argv;
m_argv = NULL;
return result;
}
/* Return the number of items in the array. */
int count () const
{
return countargv (m_argv);
}
/* Index into the array. */
char *operator[] (int arg)
{
gdb_assert (m_argv != NULL);
return m_argv[arg];
}
/* Return the arguments array as an array view. */
gdb::array_view<char *> as_array_view ()
{
return gdb::array_view<char *> (this->get (), this->count ());
}
gdb::array_view<const char * const> as_array_view () const
{
return gdb::array_view<const char * const> (this->get (), this->count ());
}
/* Append arguments to this array. */
void append (gdb_argv &&other)
{
int size = count ();
int argc = other.count ();
m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1));
for (int argi = 0; argi < argc; argi++)
{
/* Transfer ownership of the string. */
m_argv[size++] = other.m_argv[argi];
/* Ensure that destruction of OTHER works correctly. */
other.m_argv[argi] = nullptr;
}
m_argv[size] = nullptr;
}
/* Append arguments to this array. */
void append (const gdb_argv &other)
{
int size = count ();
int argc = other.count ();
m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1));
for (int argi = 0; argi < argc; argi++)
m_argv[size++] = xstrdup (other.m_argv[argi]);
m_argv[size] = nullptr;
}
/* The iterator type. */
typedef char **iterator;
/* Return an iterator pointing to the start of the array. */
iterator begin ()
{
return m_argv;
}
/* Return an iterator pointing to the end of the array. */
iterator end ()
{
return m_argv + count ();
}
bool operator!= (std::nullptr_t)
{
return m_argv != NULL;
}
bool operator== (std::nullptr_t)
{
return m_argv == NULL;
}
private:
/* The wrapped array. */
char **m_argv;
};
/* Cleanup utilities. */

204
gdbsupport/buildargv.h Normal file
View file

@ -0,0 +1,204 @@
/* RAII wrapper for buildargv
Copyright (C) 2021 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef GDBSUPPORT_BUILDARGV_H
#define GDBSUPPORT_BUILDARGV_H
#include "libiberty.h"
/* A wrapper for an array of char* that was allocated in the way that
'buildargv' does, and should be freed with 'freeargv'. */
class gdb_argv
{
public:
/* A constructor that initializes to NULL. */
gdb_argv ()
: m_argv (NULL)
{
}
/* A constructor that calls buildargv on STR. STR may be NULL, in
which case this object is initialized with a NULL array. */
explicit gdb_argv (const char *str)
: m_argv (NULL)
{
reset (str);
}
/* A constructor that takes ownership of an existing array. */
explicit gdb_argv (char **array)
: m_argv (array)
{
}
gdb_argv (const gdb_argv &) = delete;
gdb_argv &operator= (const gdb_argv &) = delete;
gdb_argv &operator= (gdb_argv &&other)
{
freeargv (m_argv);
m_argv = other.m_argv;
other.m_argv = nullptr;
return *this;
}
gdb_argv (gdb_argv &&other)
{
m_argv = other.m_argv;
other.m_argv = nullptr;
}
~gdb_argv ()
{
freeargv (m_argv);
}
/* Call buildargv on STR, storing the result in this object. Any
previous state is freed. STR may be NULL, in which case this
object is reset with a NULL array. If buildargv fails due to
out-of-memory, call malloc_failure. Therefore, the value is
guaranteed to be non-NULL, unless the parameter itself is
NULL. */
void reset (const char *str)
{
char **argv = buildargv (str);
freeargv (m_argv);
m_argv = argv;
}
/* Return the underlying array. */
char **get ()
{
return m_argv;
}
const char * const * get () const
{
return m_argv;
}
/* Return the underlying array, transferring ownership to the
caller. */
ATTRIBUTE_UNUSED_RESULT char **release ()
{
char **result = m_argv;
m_argv = NULL;
return result;
}
/* Return the number of items in the array. */
int count () const
{
return countargv (m_argv);
}
/* Index into the array. */
char *operator[] (int arg)
{
gdb_assert (m_argv != NULL);
return m_argv[arg];
}
/* Return the arguments array as an array view. */
gdb::array_view<char *> as_array_view ()
{
return gdb::array_view<char *> (this->get (), this->count ());
}
gdb::array_view<const char * const> as_array_view () const
{
return gdb::array_view<const char * const> (this->get (), this->count ());
}
/* Append arguments to this array. */
void append (gdb_argv &&other)
{
int size = count ();
int argc = other.count ();
m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1));
for (int argi = 0; argi < argc; argi++)
{
/* Transfer ownership of the string. */
m_argv[size++] = other.m_argv[argi];
/* Ensure that destruction of OTHER works correctly. */
other.m_argv[argi] = nullptr;
}
m_argv[size] = nullptr;
}
/* Append arguments to this array. */
void append (const gdb_argv &other)
{
int size = count ();
int argc = other.count ();
m_argv = XRESIZEVEC (char *, m_argv, (size + argc + 1));
for (int argi = 0; argi < argc; argi++)
m_argv[size++] = xstrdup (other.m_argv[argi]);
m_argv[size] = nullptr;
}
/* The iterator type. */
typedef char **iterator;
/* Return an iterator pointing to the start of the array. */
iterator begin ()
{
return m_argv;
}
/* Return an iterator pointing to the end of the array. */
iterator end ()
{
return m_argv + count ();
}
bool operator!= (std::nullptr_t)
{
return m_argv != NULL;
}
bool operator== (std::nullptr_t)
{
return m_argv == NULL;
}
private:
/* The wrapped array. */
char **m_argv;
};
#endif /* GDBSUPPORT_BUILDARGV_H */