binutils-gdb/gdb/common
Pedro Alves 503b1c39dc gdb: Replace operator new / operator new[]
If xmalloc fails allocating memory, usually because something tried a
huge allocation, like xmalloc(-1) or some such, GDB asks the user what
to do:

  .../src/gdb/utils.c:1079: internal-error: virtual memory exhausted.
  A problem internal to GDB has been detected,
  further debugging may prove unreliable.
  Quit this debugging session? (y or n)

If the user says "n", that throws a QUIT exception, which is caught by
one of the multiple CATCH(RETURN_MASK_ALL) blocks somewhere up the
stack.

The default implementations of operator new / operator new[] call
malloc directly, and on memory allocation failure throw
std::bad_alloc.  Currently, if that happens, since nothing catches it,
the exception escapes out of main, and GDB aborts from unhandled
exception.

This patch replaces the default operator new variants with versions
that, just like xmalloc:

 #1 - Raise an internal-error on memory allocation failure.

 #2 - Throw a QUIT gdb_exception, so that the exact same CATCH blocks
      continue handling memory allocation problems.

A minor complication of #2 is that operator new can _only_ throw
std::bad_alloc, or something that extends it:

  void* operator new (std::size_t size) throw (std::bad_alloc);

That means that if we let a gdb QUIT exception escape from within
operator new, the C++ runtime aborts due to unexpected exception
thrown.

So to bridge the gap, this patch adds a new gdb_quit_bad_alloc
exception type that inherits both std::bad_alloc and gdb_exception,
and throws _that_.

If we decide that we should be catching memory allocation errors in
fewer places than all the places we currently catch them (everywhere
we use RETURN_MASK_ALL currently), then we could change operator new
to throw plain std::bad_alloc then.  But I'm considering such a change
as separate matter from this one -- it'd make sense to do the same to
xmalloc at the same time, for instance.

Meanwhile, this allows using new/new[] instead of xmalloc/XNEW/etc.
without losing the "virtual memory exhausted" internal-error
safeguard.

Tested on x86_64 Fedora 23.

gdb/ChangeLog:
2016-09-23  Pedro Alves  <palves@redhat.com>

	* Makefile.in (SFILES): Add common/new-op.c.
	(COMMON_OBS): Add common/new-op.o.
	(new-op.o): New rule.
	* common/common-exceptions.h: Include <new>.
	(struct gdb_quit_bad_alloc): New type.
	* common/new-op.c: New file.

gdb/gdbserver/ChangeLog:
2016-09-23  Pedro Alves  <palves@redhat.com>

	* Makefile.in (SFILES): Add common/new-op.c.
	(OBS): Add common/new-op.o.
	(new-op.o): New rule.
2016-09-23 16:42:24 +01:00
..
agent.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
agent.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
ax.def GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
break-common.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
btrace-common.c Add method/format information to =record-started 2016-06-06 17:10:18 -04:00
btrace-common.h Add method/format information to =record-started 2016-06-06 17:10:18 -04:00
buffer.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
buffer.h Use struct buffer in gdb_readline_no_editing 2016-03-09 18:25:00 +00:00
cleanups.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
cleanups.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
common-debug.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
common-debug.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
common-defs.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
common-exceptions.c Propagate GDB/C++ exceptions across readline using sj/lj-based TRY/CATCH 2016-04-22 16:20:04 +01:00
common-exceptions.h gdb: Replace operator new / operator new[] 2016-09-23 16:42:24 +01:00
common-regcache.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
common-regcache.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
common-types.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
common-utils.c Add xmalloc_failed() function to common-utils.c in to avoid the need to link in libiberty's xmalloc code. 2016-06-01 11:44:08 +01:00
common-utils.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
common.host GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
common.m4 Fix PR gdb/18653: gdb disturbs inferior's inherited signal dispositions 2016-08-09 20:16:20 +01:00
create-version.sh GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
enum-flags.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
errors.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
errors.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
fileio.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
fileio.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
filestuff.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
filestuff.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
format.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
format.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
gdb_assert.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
gdb_locale.h gdb: Fix build breakage with GCC 4.1 and --disable-nls 2016-09-19 16:55:35 +01:00
gdb_setjmp.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
gdb_signals.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
gdb_sys_time.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
gdb_vecs.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
gdb_vecs.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
gdb_wait.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
host-defs.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
mingw-strerror.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
new-op.c gdb: Replace operator new / operator new[] 2016-09-23 16:42:24 +01:00
posix-strerror.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
print-utils.c Centralize thread ID printing 2016-01-13 10:59:14 +00:00
print-utils.h Centralize thread ID printing 2016-01-13 10:59:14 +00:00
ptid.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
ptid.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
queue.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
rsp-low.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
rsp-low.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
signals-state-save-restore.c Quiet ARI gettext checks 2016-08-10 16:06:57 +01:00
signals-state-save-restore.h Fix PR gdb/18653: gdb disturbs inferior's inherited signal dispositions 2016-08-09 20:16:20 +01:00
signals.c Pass SIGLIBRT directly to child processes. 2016-07-15 06:35:37 -07:00
symbol.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
vec.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
vec.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
version.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
x86-xstate.h Add target descriptions for AVX + MPX 2016-04-19 15:44:32 +02:00
xml-utils.c GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00
xml-utils.h GDB copyright headers update after running GDB's copyright.py script. 2016-01-01 08:43:22 +04:00