From patchwork Tue Apr 11 23:51:11 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 19978 Received: (qmail 43817 invoked by alias); 11 Apr 2017 23:51:24 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 43622 invoked by uid 89); 11 Apr 2017 23:51:21 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=quiet, announce X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 11 Apr 2017 23:51:19 +0000 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6C677770C for ; Tue, 11 Apr 2017 23:51:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6C677770C Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=palves@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 6C677770C Received: from cascais.lan (ovpn04.gateway.prod.ext.phx2.redhat.com [10.5.9.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id E003017C3C for ; Tue, 11 Apr 2017 23:51:18 +0000 (UTC) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 6/8] Make inferior a class with cdtors, and use new/delete Date: Wed, 12 Apr 2017 00:51:11 +0100 Message-Id: <1491954673-29172-7-git-send-email-palves@redhat.com> In-Reply-To: <1491954673-29172-1-git-send-email-palves@redhat.com> References: <1491954673-29172-1-git-send-email-palves@redhat.com> A following patch in this series will add a private field to struct inferior, making it a non-POD, which requires allocating/destroying inferiors with new/delete, etc. Note: this commit makes all boolean fields of inferior be "bool", except the "detaching" field. That'll require more work, so I split it to a separate patch. gdb/ChangeLog: yyyy-mm-dd Pedro Alves * inferior.c (free_inferior): Convert to ... (inferior::~inferior): ... this dtor. (inferior::inferior): New ctor, factored out from ... (add_inferior_silent): ... here. Allocate the inferior with a new expression. (delete_inferior): Call delete instead of free_inferior. * inferior.h (gdb_environ, continuation): Forward declare. (inferior): Now a class. Add in-class initialization to all members. Make boolean fields bool, except 'detaching'. (inferior::inferior): New explicit ctor. (inferior::~inferior): New. --- gdb/inferior.c | 35 ++++++++++++++---------------- gdb/inferior.h | 67 +++++++++++++++++++++++++++++++--------------------------- 2 files changed, 52 insertions(+), 50 deletions(-) diff --git a/gdb/inferior.c b/gdb/inferior.c index 54e9967..327590a 100644 --- a/gdb/inferior.c +++ b/gdb/inferior.c @@ -92,9 +92,10 @@ save_current_inferior (void) return old_chain; } -static void -free_inferior (struct inferior *inf) +inferior::~inferior () { + inferior *inf = this; + discard_all_inferior_continuations (inf); inferior_free_data (inf); xfree (inf->args); @@ -102,38 +103,34 @@ free_inferior (struct inferior *inf) free_environ (inf->environment); target_desc_info_free (inf->tdesc_info); xfree (inf->priv); - xfree (inf); +} + +inferior::inferior (int pid_) + : num (++highest_inferior_num), + pid (pid_), + environment (make_environ ()), + registry_data () +{ + init_environ (this->environment); + inferior_alloc_data (this); } struct inferior * add_inferior_silent (int pid) { - struct inferior *inf; - - inf = XNEW (struct inferior); - memset (inf, 0, sizeof (*inf)); - inf->pid = pid; - - inf->control.stop_soon = NO_STOP_QUIETLY; - - inf->num = ++highest_inferior_num; + inferior *inf = new inferior (pid); if (inferior_list == NULL) inferior_list = inf; else { - struct inferior *last; + inferior *last; for (last = inferior_list; last->next != NULL; last = last->next) ; last->next = inf; } - inf->environment = make_environ (); - init_environ (inf->environment); - - inferior_alloc_data (inf); - observer_notify_inferior_added (inf); if (pid != 0) @@ -207,7 +204,7 @@ delete_inferior (struct inferior *todel) if (program_space_empty_p (inf->pspace)) delete_program_space (inf->pspace); - free_inferior (inf); + delete inf; } /* If SILENT then be quiet -- don't announce a inferior exit, or the diff --git a/gdb/inferior.h b/gdb/inferior.h index 29ec7be..2638ac7 100644 --- a/gdb/inferior.h +++ b/gdb/inferior.h @@ -30,6 +30,8 @@ struct regcache; struct ui_out; struct terminal_info; struct target_desc_info; +struct gdb_environ; +struct continuation; /* For bpstat. */ #include "breakpoint.h" @@ -305,111 +307,114 @@ struct inferior_control_state target process ids. Each inferior may in turn have multiple threads running in it. */ -struct inferior +class inferior { +public: + explicit inferior (int pid); + ~inferior (); + /* Pointer to next inferior in singly-linked list of inferiors. */ - struct inferior *next; + struct inferior *next = NULL; /* Convenient handle (GDB inferior id). Unique across all inferiors. */ - int num; + int num = 0; /* Actual target inferior id, usually, a process id. This matches the ptid_t.pid member of threads of this inferior. */ - int pid; + int pid = 0; /* True if the PID was actually faked by GDB. */ - int fake_pid_p; + bool fake_pid_p = false; /* The highest thread number this inferior ever had. */ - int highest_thread_num; + int highest_thread_num = 0; /* State of GDB control of inferior process execution. See `struct inferior_control_state'. */ - struct inferior_control_state control; + inferior_control_state control {NO_STOP_QUIETLY}; /* True if this was an auto-created inferior, e.g. created from following a fork; false, if this inferior was manually added by the user, and we should not attempt to prune it automatically. */ - int removable; + bool removable = false; /* The address space bound to this inferior. */ - struct address_space *aspace; + struct address_space *aspace = NULL; /* The program space bound to this inferior. */ - struct program_space *pspace; + struct program_space *pspace = NULL; /* The arguments string to use when running. */ - char *args; + char *args = NULL; /* The size of elements in argv. */ - int argc; + int argc = 0; /* The vector version of arguments. If ARGC is nonzero, then we must compute ARGS from this (via the target). This is always coming from main's argv and therefore should never be freed. */ - char **argv; + char **argv = NULL; /* The name of terminal device to use for I/O. */ - char *terminal; + char *terminal = NULL; /* Environment to use for running inferior, in format described in environ.h. */ - struct gdb_environ *environment; + gdb_environ *environment = NULL; - /* Nonzero if this child process was attached rather than - forked. */ - int attach_flag; + /* True if this child process was attached rather than forked. */ + bool attach_flag = false; /* If this inferior is a vfork child, then this is the pointer to its vfork parent, if GDB is still attached to it. */ - struct inferior *vfork_parent; + inferior *vfork_parent = NULL; /* If this process is a vfork parent, this is the pointer to the child. Since a vfork parent is left frozen by the kernel until the child execs or exits, a process can only have one vfork child at a given time. */ - struct inferior *vfork_child; + inferior *vfork_child = NULL; /* True if this inferior should be detached when it's vfork sibling exits or execs. */ - int pending_detach; + bool pending_detach = false; /* True if this inferior is a vfork parent waiting for a vfork child not under our control to be done with the shared memory region, either by exiting or execing. */ - int waiting_for_vfork_done; + bool waiting_for_vfork_done = false; /* True if we're in the process of detaching from this inferior. */ - int detaching; + int detaching = 0; /* What is left to do for an execution command after any thread of this inferior stops. For continuations associated with a specific thread, see `struct thread_info'. */ - struct continuation *continuations; + continuation *continuations = NULL; /* True if setup_inferior wasn't called for this inferior yet. Until that is done, we must not access inferior memory or registers, as we haven't determined the target architecture/description. */ - int needs_setup; + bool needs_setup = false; /* Private data used by the target vector implementation. */ - struct private_inferior *priv; + private_inferior *priv = NULL; /* HAS_EXIT_CODE is true if the inferior exited with an exit code. In this case, the EXIT_CODE field is also valid. */ - int has_exit_code; - LONGEST exit_code; + bool has_exit_code = false; + LONGEST exit_code = 0; /* Default flags to pass to the symbol reading functions. These are used whenever a new objfile is created. */ - symfile_add_flags symfile_flags; + symfile_add_flags symfile_flags = 0; /* Info about an inferior's target description (if it's fetched; the user supplied description's filename, if any; etc.). */ - struct target_desc_info *tdesc_info; + target_desc_info *tdesc_info = NULL; /* The architecture associated with the inferior through the connection to the target. @@ -422,7 +427,7 @@ struct inferior per-thread/per-frame/per-objfile properties, accesses to per-inferior/target properties should be made through this gdbarch. */ - struct gdbarch *gdbarch; + struct gdbarch *gdbarch = NULL; /* Per inferior data-pointers required by other GDB modules. */ REGISTRY_FIELDS;