From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 6GmTOFYhgGBFNwAAWB0awg (envelope-from ) for ; Wed, 21 Apr 2021 08:57:58 -0400 Received: by simark.ca (Postfix, from userid 112) id DD6EE1E783; Wed, 21 Apr 2021 08:57:58 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_DYNAMIC,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 471291E783 for ; Wed, 21 Apr 2021 08:57:57 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CC3DB3AAB021; Wed, 21 Apr 2021 12:57:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CC3DB3AAB021 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1619009876; bh=YNtBUlr29HOtqCvN14BlT4jgC4J3ykfd3qidPgxgVWo=; h=To:Subject:Date:In-Reply-To:References:In-Reply-To:References: List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help: List-Subscribe:From:Reply-To:From; b=CtUXhwViZZU4eB2xYmvkv6FTofEHVOaW/FKS4nkvGZFcTIRa8f7GiXw6qbrzkQFS3 FFCLNG2ekANXItZ4xVrYSLwzdttaqjEmtHyKpH39E7GaU4/nmWoJK4UnCklMxq85wU K600Lh3owiyg5P+f4CHPPkzxgPuXSARqMf8OzE8Q= Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by sourceware.org (Postfix) with ESMTPS id DF7CC39B703D for ; Wed, 21 Apr 2021 12:57:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org DF7CC39B703D IronPort-SDR: 5CAUspcNE+un14cmVxa87jWLRaTkx0mTPRTI3T3lQKT69wznovxdZ37txzqS4giK05XAdNz1dx tCdZUq2xZk2Q== X-IronPort-AV: E=McAfee;i="6200,9189,9960"; a="281021102" X-IronPort-AV: E=Sophos;i="5.82,238,1613462400"; d="scan'208";a="281021102" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Apr 2021 05:57:51 -0700 IronPort-SDR: RhGUOEGayuBTQzOyUG2NrQDLAV9GpMPZJ9x7amN6VAoGhRaBSDvYfYZQvvM6IBgOv3+tUGFdvh pbHdXmCY7vvQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,238,1613462400"; d="scan'208";a="534868296" Received: from irvmail001.ir.intel.com ([10.43.11.63]) by orsmga004.jf.intel.com with ESMTP; 21 Apr 2021 05:57:50 -0700 Received: from ulvlx001.iul.intel.com (ulvlx001.iul.intel.com [172.28.207.17]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id 13LCvnjB011360; Wed, 21 Apr 2021 13:57:50 +0100 Received: from ulvlx001.iul.intel.com (localhost [127.0.0.1]) by ulvlx001.iul.intel.com with ESMTP id 13LCvn3G031956; Wed, 21 Apr 2021 14:57:49 +0200 Received: (from taktemur@localhost) by ulvlx001.iul.intel.com with LOCAL id 13LCvn1O031952; Wed, 21 Apr 2021 14:57:49 +0200 To: gdb-patches@sourceware.org Subject: [PATCH 5/6] gdb/continuations: use lambdas instead of function pointers Date: Wed, 21 Apr 2021 14:57:30 +0200 Message-Id: <4d9592aa18ca6528d300a4b77b8a552abdb7600d.1619009681.git.tankut.baris.aktemur@intel.com> X-Mailer: git-send-email 1.7.0.7 In-Reply-To: References: In-Reply-To: References: X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Tankut Baris Aktemur via Gdb-patches Reply-To: Tankut Baris Aktemur Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" Use lambdas and std::list to track inferior continuations. This is a refactoring. gdb/ChangeLog: 2021-04-21 Tankut Baris Aktemur * inferior.h (class inferior) : Change the type to be an std::list of std::function's. Update the references and uses below. * continuations.c (struct continuation): Delete. (make_continuation): Delete. (do_my_continuations_1): Delete. (do_my_continuations): Delete. (discard_my_continuations_1): Delete. (discard_my_continuations): Delete. (add_inferior_continuation): Update. (do_all_inferior_continuations): Update. (discard_all_inferior_continuations): Update. * continuations.h (add_inferior_continuation): Update to take an std::function as the parameter. * infcmd.c (struct attach_command_continuation_args): Delete. (attach_command_continuation): Delete. (attach_command_continuation_free_args): Delete. (attach_command): Update. (notice_new_inferior): Update. --- gdb/continuations.c | 96 +++++---------------------------------------- gdb/continuations.h | 24 ++++-------- gdb/infcmd.c | 45 ++++----------------- gdb/inferior.h | 4 +- 4 files changed, 26 insertions(+), 143 deletions(-) diff --git a/gdb/continuations.c b/gdb/continuations.c index 8fe81a75ba9..a552203e106 100644 --- a/gdb/continuations.c +++ b/gdb/continuations.c @@ -22,98 +22,15 @@ #include "inferior.h" #include "continuations.h" -struct continuation -{ - struct continuation *next; - continuation_ftype *function; - continuation_free_arg_ftype *free_arg; - void *arg; -}; - -/* Add a new continuation to the continuation chain. Args are - FUNCTION to run the continuation up with, and ARG to pass to - it. */ - -static void -make_continuation (struct continuation **pmy_chain, - continuation_ftype *function, - void *arg, void (*free_arg) (void *)) -{ - struct continuation *newobj = XNEW (struct continuation); - - newobj->next = *pmy_chain; - newobj->function = function; - newobj->free_arg = free_arg; - newobj->arg = arg; - *pmy_chain = newobj; -} - -static void -do_my_continuations_1 (struct continuation **pmy_chain) -{ - struct continuation *ptr; - - while ((ptr = *pmy_chain) != NULL) - { - *pmy_chain = ptr->next; /* Do this first in case of recursion. */ - (*ptr->function) (ptr->arg); - if (ptr->free_arg) - (*ptr->free_arg) (ptr->arg); - xfree (ptr); - } -} - -static void -do_my_continuations (struct continuation **list) -{ - struct continuation *continuations; - - if (*list == NULL) - return; - - /* Copy the list header into another pointer, and set the global - list header to null, so that the global list can change as a side - effect of invoking the continuations and the processing of the - preexisting continuations will not be affected. */ - - continuations = *list; - *list = NULL; - - /* Work now on the list we have set aside. */ - do_my_continuations_1 (&continuations); -} - -static void -discard_my_continuations_1 (struct continuation **pmy_chain) -{ - struct continuation *ptr; - - while ((ptr = *pmy_chain) != NULL) - { - *pmy_chain = ptr->next; - if (ptr->free_arg) - (*ptr->free_arg) (ptr->arg); - xfree (ptr); - } -} - -static void -discard_my_continuations (struct continuation **list) -{ - discard_my_continuations_1 (list); - *list = NULL; -} - /* Add a continuation to the continuation list of INFERIOR. The new continuation will be added at the front. */ void -add_inferior_continuation (continuation_ftype *hook, void *args, - continuation_free_arg_ftype *free_arg) +add_inferior_continuation (std::function &&cont) { struct inferior *inf = current_inferior (); - make_continuation (&inf->continuations, hook, args, free_arg); + inf->continuations.emplace_front (std::move (cont)); } /* Do all continuations of the current inferior. */ @@ -122,7 +39,12 @@ void do_all_inferior_continuations () { struct inferior *inf = current_inferior (); - do_my_continuations (&inf->continuations); + while (!inf->continuations.empty ()) + { + auto &cont = inf->continuations.front (); + inf->continuations.pop_front (); + cont (); + } } /* Get rid of all the inferior-wide continuations of INF. */ @@ -130,5 +52,5 @@ do_all_inferior_continuations () void discard_all_inferior_continuations (struct inferior *inf) { - discard_my_continuations (&inf->continuations); + inf->continuations.clear (); } diff --git a/gdb/continuations.h b/gdb/continuations.h index 39130c65f0f..4dc15494ae1 100644 --- a/gdb/continuations.h +++ b/gdb/continuations.h @@ -20,29 +20,19 @@ #ifndef CONTINUATIONS_H #define CONTINUATIONS_H +#include + struct inferior; /* To continue the execution commands when running gdb asynchronously. - A continuation structure contains a pointer to a function to be called - to finish the command, once the target has stopped. Such mechanism is - used by the attach command and the remote target when a new inferior - is detected. */ - -/* Prototype of the continuation callback functions. ARG is the - continuation argument registered in the corresponding - add_*_continuation call. */ -typedef void (continuation_ftype) (void *arg); - -/* Prototype of the function responsible for releasing the argument - passed to the continuation callback functions, either when the - continuation is called, or discarded. */ -typedef void (continuation_free_arg_ftype) (void *); + A continuation is a closure (i.e. a lambda) to be called to finish + the command, once the target has stopped. Such mechanism is used + by the attach command and the remote target when a new inferior is + detected. */ /* Inferior specific (any thread) continuations. */ -extern void add_inferior_continuation (continuation_ftype *, - void *, - continuation_free_arg_ftype *); +extern void add_inferior_continuation (std::function &&cont); extern void do_all_inferior_continuations (); extern void discard_all_inferior_continuations (struct inferior *inf); diff --git a/gdb/infcmd.c b/gdb/infcmd.c index 5c3ee02cb9d..e06db492b07 100644 --- a/gdb/infcmd.c +++ b/gdb/infcmd.c @@ -2540,30 +2540,6 @@ attach_post_wait (int from_tty, enum attach_post_wait_mode mode) } } -struct attach_command_continuation_args -{ - int from_tty; - enum attach_post_wait_mode mode; -}; - -static void -attach_command_continuation (void *args) -{ - struct attach_command_continuation_args *a - = (struct attach_command_continuation_args *) args; - - attach_post_wait (a->from_tty, a->mode); -} - -static void -attach_command_continuation_free_args (void *args) -{ - struct attach_command_continuation_args *a - = (struct attach_command_continuation_args *) args; - - xfree (a); -} - /* "attach" command entry point. Takes a program started up outside of gdb and ``attaches'' to it. This stops it cold in its tracks and allows us to start debugging it. */ @@ -2661,8 +2637,6 @@ attach_command (const char *args, int from_tty) E.g. Mach 3 or GNU hurd. */ if (!target_attach_no_wait ()) { - struct attach_command_continuation_args *a; - /* Careful here. See comments in inferior.h. Basically some OSes don't ignore SIGSTOPs on continue requests anymore. We need a way for handle_inferior_event to reset the stop_signal @@ -2671,11 +2645,10 @@ attach_command (const char *args, int from_tty) inferior->control.stop_soon = STOP_QUIETLY_NO_SIGSTOP; /* Wait for stop. */ - a = XNEW (struct attach_command_continuation_args); - a->from_tty = from_tty; - a->mode = mode; - add_inferior_continuation (attach_command_continuation, a, - attach_command_continuation_free_args); + add_inferior_continuation ([=] () + { + attach_post_wait (from_tty, mode); + }); /* Let infrun consider waiting for events out of this target. */ @@ -2719,7 +2692,6 @@ notice_new_inferior (thread_info *thr, int leave_running, int from_tty) if (thr->executing) { - struct attach_command_continuation_args *a; struct inferior *inferior = current_inferior (); /* We're going to install breakpoints, and poke at memory, @@ -2730,11 +2702,10 @@ notice_new_inferior (thread_info *thr, int leave_running, int from_tty) inferior->control.stop_soon = STOP_QUIETLY_REMOTE; /* Wait for stop before proceeding. */ - a = XNEW (struct attach_command_continuation_args); - a->from_tty = from_tty; - a->mode = mode; - add_inferior_continuation (attach_command_continuation, a, - attach_command_continuation_free_args); + add_inferior_continuation ([=] () + { + attach_post_wait (from_tty, mode); + }); return; } diff --git a/gdb/inferior.h b/gdb/inferior.h index 9ca510e4e6e..fc072b3576f 100644 --- a/gdb/inferior.h +++ b/gdb/inferior.h @@ -22,6 +22,7 @@ #define INFERIOR_H 1 #include +#include struct target_waitstatus; struct frame_info; @@ -32,7 +33,6 @@ struct regcache; struct ui_out; struct terminal_info; struct target_desc_info; -struct continuation; struct inferior; struct thread_info; @@ -510,7 +510,7 @@ class inferior : public refcounted_object /* What is left to do for an execution command after any thread of this inferior stops. */ - continuation *continuations = NULL; + std::list > continuations; /* True if setup_inferior wasn't called for this inferior yet. Until that is done, we must not access inferior memory or -- 2.17.1