From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x134.google.com (mail-il1-x134.google.com [IPv6:2607:f8b0:4864:20::134]) by sourceware.org (Postfix) with ESMTPS id 412493858C41 for ; Fri, 9 Jun 2023 16:46:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 412493858C41 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-il1-x134.google.com with SMTP id e9e14a558f8ab-33e75ca0a8bso8398975ab.0 for ; Fri, 09 Jun 2023 09:46:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1686329189; x=1688921189; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=VMIWTNm4xoLo0TphYURQhVXRpP8m1v8j6vzgLowHvtQ=; b=DbaK3OFECO2/pYV/JEwEpet4o70mwHCCYWgJ9ii0mwlb7q5MhgiChIrR3jetwYLhrI fM8Cgzw1AjtNzlQcbr7xzLWCc2Rq5WO5mf4UdCEWaoqNkU/zEAEsuJkDYR+e9CLZtiUQ N3zFsf2iK/0E6ojUgonQflgG+q0idnUinXYfjmeH0/kkRXzaYdeVIrLzyJvlXzptVx1Q sQgELM//+RVhNOXD1ubdCGupaGm2cnFbRttZapEtbPlOmqdUbJVnNipYObM+j3YHdXZ5 CrAOJjSXMJAG0g5zb0B3D5enJ55FowdTi1+U1AwLPbDoDyzgvNggAvQKLrU+IQ6D3/7E naGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686329189; x=1688921189; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VMIWTNm4xoLo0TphYURQhVXRpP8m1v8j6vzgLowHvtQ=; b=azG5jHOvUHM3201dmCYeq0afD6zNqbmIGeJ1PhoBGAFn7RtJJwiJhEgqPcrmJJRcXL U4oKK6Aooe9P0cG0AjH3+U6SSFLSvdsc4OUvqWqnGKvnh7zTdWqURW4uqZjrQpb19kMH ZVy76H0Sd72Dd+nHlbmMYgZGLi2GEUVHzgeQ96v3oph5VrU9cQbCm2ZgJPV3ALXItShf 7Ma4BRa3Kx5+QE4ha+fvw95gUxrsVPyNQ+wgSxlppB6O0UQ20jQFchz4C7cnqM3afHEl XFxNbVi+4YZZa1O2dB4HgDd+Spb9uvDVaAN8jjr0xjNG0cJuW8T4hDoeucg52xmLkjZy x+uA== X-Gm-Message-State: AC+VfDzOC21xmQg43/9z5sLnXA1KLAZEoHJ0fUnBIgv5Z/PjFYUNCT0o 2KHAN/eyao4ReOOQUEY3ROcn9gAnBKjzmvKp4TnFIw== X-Google-Smtp-Source: ACHHUZ4pxDmzlrJfynntY8ZcgUcUa/YcIpUDZD3Gww+Op8gWn+tXlORc18K1gYu3JTM/xkyhwPr8zA== X-Received: by 2002:a92:cc0f:0:b0:33e:614a:7115 with SMTP id s15-20020a92cc0f000000b0033e614a7115mr1883433ilp.31.1686329189378; Fri, 09 Jun 2023 09:46:29 -0700 (PDT) Received: from localhost.localdomain (75-166-136-83.hlrn.qwest.net. [75.166.136.83]) by smtp.gmail.com with ESMTPSA id n18-20020a92dd12000000b00325df6679a7sm1195610ilm.26.2023.06.09.09.46.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Jun 2023 09:46:29 -0700 (PDT) From: Tom Tromey Date: Fri, 09 Jun 2023 10:46:25 -0600 Subject: [PATCH 1/2] Use std::string for MI token MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230609-mi-xfree-v1-1-04e9c8d9a679@adacore.com> References: <20230609-mi-xfree-v1-0-04e9c8d9a679@adacore.com> In-Reply-To: <20230609-mi-xfree-v1-0-04e9c8d9a679@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: This changes the MI "token" to be a std::string, removing some manual memory management. It also makes current_token 'const' in order to support this change. --- gdb/mi/mi-main.c | 21 ++++++++++----------- gdb/mi/mi-main.h | 2 +- gdb/mi/mi-parse.c | 9 +++------ gdb/mi/mi-parse.h | 11 +++++------ 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c index 7503ffd2f18..7d671657a44 100644 --- a/gdb/mi/mi-main.c +++ b/gdb/mi/mi-main.c @@ -78,7 +78,7 @@ static struct mi_timestamp *current_command_ts; static int do_timings = 0; -char *current_token; +const char *current_token; /* Few commands would like to know if options like --thread-group were explicitly specified. This variable keeps the current parsed command including all option, and make it possible. */ @@ -1810,7 +1810,7 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context) current_command_ts = context->cmd_start; scoped_restore save_token = make_scoped_restore (¤t_token, - context->token); + context->token.c_str ()); mi->running_result_record_printed = 0; mi->mi_proceeded = 0; @@ -1821,7 +1821,7 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context) if (mi_debug_p) gdb_printf (gdb_stdlog, " token=`%s' command=`%s' args=`%s'\n", - context->token, context->command, context->args ()); + context->token.c_str (), context->command, context->args ()); mi_cmd_execute (context); @@ -1833,7 +1833,7 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context) uiout will most likely crash in the mi_out_* routines. */ if (!mi->running_result_record_printed) { - gdb_puts (context->token, mi->raw_stdout); + gdb_puts (context->token.c_str (), mi->raw_stdout); /* There's no particularly good reason why target-connect results in not ^done. Should kill ^connected for MI3. */ gdb_puts (strcmp (context->command, "target-select") == 0 @@ -1872,7 +1872,7 @@ captured_mi_execute_command (struct ui_out *uiout, struct mi_parse *context) { if (!mi->running_result_record_printed) { - gdb_puts (context->token, mi->raw_stdout); + gdb_puts (context->token.c_str (), mi->raw_stdout); gdb_puts ("^done", mi->raw_stdout); mi_out_put (uiout, mi->raw_stdout); mi_out_rewind (uiout); @@ -1915,7 +1915,7 @@ mi_print_exception (const char *token, const struct gdb_exception &exception) void mi_execute_command (const char *cmd, int from_tty) { - char *token; + std::string token; std::unique_ptr command; /* This is to handle EOF (^D). We just quit gdb. */ @@ -1931,13 +1931,12 @@ mi_execute_command (const char *cmd, int from_tty) } catch (const gdb_exception &exception) { - mi_print_exception (token, exception); - xfree (token); + mi_print_exception (token.c_str (), exception); } if (command != NULL) { - command->token = token; + command->token = std::move (token); if (do_timings) { @@ -1960,7 +1959,7 @@ mi_execute_command (const char *cmd, int from_tty) /* The command execution failed and error() was called somewhere. */ - mi_print_exception (command->token, result); + mi_print_exception (command->token.c_str (), result); mi_out_rewind (current_uiout); /* Throw to a higher level catch for SIGTERM sent to GDB. */ @@ -1982,7 +1981,7 @@ mi_execute_command (mi_parse *context) error (_("Command is not an MI command")); scoped_restore save_token = make_scoped_restore (¤t_token, - context->token); + context->token.c_str ()); scoped_restore save_debug = make_scoped_restore (&mi_debug_p, 0); mi_cmd_execute (context); diff --git a/gdb/mi/mi-main.h b/gdb/mi/mi-main.h index 1741f48b3ee..cb17921c150 100644 --- a/gdb/mi/mi-main.h +++ b/gdb/mi/mi-main.h @@ -34,7 +34,7 @@ extern void mi_print_timing_maybe (struct ui_file *file); extern int mi_async_p (void); -extern char *current_token; +extern const char *current_token; struct mi_suppress_notification { diff --git a/gdb/mi/mi-parse.c b/gdb/mi/mi-parse.c index a113d4d48da..aceecad6db6 100644 --- a/gdb/mi/mi-parse.c +++ b/gdb/mi/mi-parse.c @@ -216,7 +216,6 @@ mi_parse::parse_argv () mi_parse::~mi_parse () { xfree (command); - xfree (token); freeargv (argv); } @@ -290,7 +289,7 @@ mi_parse::set_language (const char *arg, const char **endp) } std::unique_ptr -mi_parse::make (const char *cmd, char **token) +mi_parse::make (const char *cmd, std::string *token) { const char *chp; @@ -302,9 +301,7 @@ mi_parse::make (const char *cmd, char **token) /* Find/skip any token and then extract it. */ for (chp = cmd; *chp >= '0' && *chp <= '9'; chp++) ; - *token = (char *) xmalloc (chp - cmd + 1); - memcpy (*token, cmd, (chp - cmd)); - (*token)[chp - cmd] = '\0'; + *token = std::string (cmd, chp - cmd); /* This wasn't a real MI command. Return it as a CLI_COMMAND. */ if (*chp != '-') @@ -422,7 +419,7 @@ mi_parse::make (gdb::unique_xmalloc_ptr command, std::unique_ptr parse (new struct mi_parse); parse->command = command.release (); - parse->token = xstrdup (""); + parse->token = ""; if (parse->command[0] != '-') throw_error (UNDEFINED_COMMAND_ERROR, diff --git a/gdb/mi/mi-parse.h b/gdb/mi/mi-parse.h index 6373543529b..78fb414b4f0 100644 --- a/gdb/mi/mi-parse.h +++ b/gdb/mi/mi-parse.h @@ -44,13 +44,12 @@ struct mi_parse /* Attempts to parse CMD returning a ``struct mi_parse''. If CMD is invalid, an exception is thrown. For an MI_COMMAND COMMAND, ARGS and OP are initialized. Un-initialized fields are zero. *TOKEN is - set to the token, even if an exception is thrown. It is allocated - with xmalloc; it must either be freed with xfree, or assigned to - the TOKEN field of the resultant mi_parse object, to be freed by - mi_parse_free. */ + set to the token, even if an exception is thrown. It can be + assigned to the TOKEN field of the resultant mi_parse object, + to be freed by mi_parse_free. */ static std::unique_ptr make (const char *cmd, - char **token); + std::string *token); /* Create an mi_parse object given the command name and a vector of arguments. Unlike with the other constructor, here the @@ -74,7 +73,7 @@ struct mi_parse enum mi_command_type op = MI_COMMAND; char *command = nullptr; - char *token = nullptr; + std::string token; const struct mi_command *cmd = nullptr; struct mi_timestamp *cmd_start = nullptr; char **argv = nullptr; -- 2.40.1