From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gproxy3-pub.mail.unifiedlayer.com (gproxy3-pub.mail.unifiedlayer.com [69.89.30.42]) by sourceware.org (Postfix) with ESMTPS id 30C5B3858D33 for ; Tue, 20 Jun 2023 17:36:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 30C5B3858D33 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from cmgw12.mail.unifiedlayer.com (unknown [10.0.90.127]) by progateway5.mail.pro1.eigbox.com (Postfix) with ESMTP id 8E10910048775 for ; Tue, 20 Jun 2023 17:36:39 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id BfHTqNRTYEJzCBfHTq4Qrt; Tue, 20 Jun 2023 17:36:39 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=As4rYMxP c=1 sm=1 tr=0 ts=6491e3a7 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=IkcTkHD0fZMA:10:nop_charset_1 a=of4jigFt-DYA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=vUG3SFHIHzZYaNJK9jAA:9 a=QEXdDO2ut3YA:10:nop_charset_2 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=To:In-Reply-To:References:Message-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:Subject:Date:From:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=NSbFA0b5h05z39JK0gSsTOc0YNPn5ETdYU2FokiV3/w=; b=hL8y5Ndvm2EFWyWx5Im6OQ99Ey c7iguFw85PUthiLjN55kyD37VHg+4QWW6qpgdRjHiffsqfT143EuId188TfmVvdRUm9gOOtjBhinu T8PX6e6liZUMibSqEmJroothK; Received: from 75-166-136-83.hlrn.qwest.net ([75.166.136.83]:54256 helo=[192.168.0.21]) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1qBfHT-001PQG-Cy for gdb-patches@sourceware.org; Tue, 20 Jun 2023 11:36:39 -0600 From: Tom Tromey Date: Tue, 20 Jun 2023 11:36:38 -0600 Subject: [PATCH v2 2/8] Use gdb::byte_vector in agent_expr MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20230619-ax-new-v2-2-47638d750dd7@tromey.com> References: <20230619-ax-new-v2-0-47638d750dd7@tromey.com> In-Reply-To: <20230619-ax-new-v2-0-47638d750dd7@tromey.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.2 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 75.166.136.83 X-Source-L: No X-Exim-ID: 1qBfHT-001PQG-Cy X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 75-166-136-83.hlrn.qwest.net ([192.168.0.21]) [75.166.136.83]:54256 X-Source-Auth: tom+tromey.com X-Email-Count: 11 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3025.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,JMQ_SPF_NEUTRAL,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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 agent_expr to use gdb::byte_vector rather than manually reimplementing a vector. --- gdb/ax-gdb.c | 28 +++++++-------- gdb/ax-general.c | 101 ++++++++++++++++++------------------------------------- gdb/ax.h | 8 +---- gdb/dwarf2/loc.c | 4 +-- gdb/remote.c | 14 ++++---- gdb/tracepoint.c | 13 +++---- 6 files changed, 63 insertions(+), 105 deletions(-) diff --git a/gdb/ax-gdb.c b/gdb/ax-gdb.c index 927dfc6337e..b0bde62f465 100644 --- a/gdb/ax-gdb.c +++ b/gdb/ax-gdb.c @@ -818,7 +818,6 @@ static int is_nontrivial_conversion (struct type *from, struct type *to) { agent_expr_up ax (new agent_expr (NULL, 0)); - int nontrivial; /* Actually generate the code, and see if anything came out. At the moment, it would be trivial to replicate the code in @@ -827,8 +826,7 @@ is_nontrivial_conversion (struct type *from, struct type *to) way allows this function to be independent of the logic in gen_conversion. */ gen_conversion (ax.get (), from, to); - nontrivial = ax->len > 0; - return nontrivial; + return !ax->buf.empty (); } @@ -1719,10 +1717,10 @@ ternop_cond_operation::do_generate_ax (struct expression *exp, std::get<1> (m_storage)->generate_ax (exp, ax, &value2); gen_usual_unary (ax, &value2); end = ax_goto (ax, aop_goto); - ax_label (ax, if1, ax->len); + ax_label (ax, if1, ax->buf.size ()); std::get<2> (m_storage)->generate_ax (exp, ax, &value3); gen_usual_unary (ax, &value3); - ax_label (ax, end, ax->len); + ax_label (ax, end, ax->buf.size ()); /* This is arbitrary - what if B and C are incompatible types? */ value->type = value2.type; value->kind = value2.kind; @@ -1811,12 +1809,12 @@ unop_sizeof_operation::do_generate_ax (struct expression *exp, only way to find an expression's type is to generate code for it. So we generate code for the operand, and then throw it away, replacing it with code that simply pushes its size. */ - int start = ax->len; + int start = ax->buf.size (); std::get<0> (m_storage)->generate_ax (exp, ax, value); /* Throw away the code we just generated. */ - ax->len = start; + ax->buf.resize (start); ax_const_l (ax, value->type->length ()); value->kind = axs_rvalue; @@ -2033,18 +2031,18 @@ logical_and_operation::do_generate_ax (struct expression *exp, gen_usual_unary (ax, &value1); if1 = ax_goto (ax, aop_if_goto); go1 = ax_goto (ax, aop_goto); - ax_label (ax, if1, ax->len); + ax_label (ax, if1, ax->buf.size ()); std::get<1> (m_storage)->generate_ax (exp, ax, &value2); gen_usual_unary (ax, &value2); if2 = ax_goto (ax, aop_if_goto); go2 = ax_goto (ax, aop_goto); - ax_label (ax, if2, ax->len); + ax_label (ax, if2, ax->buf.size ()); ax_const_l (ax, 1); end = ax_goto (ax, aop_goto); - ax_label (ax, go1, ax->len); - ax_label (ax, go2, ax->len); + ax_label (ax, go1, ax->buf.size ()); + ax_label (ax, go2, ax->buf.size ()); ax_const_l (ax, 0); - ax_label (ax, end, ax->len); + ax_label (ax, end, ax->buf.size ()); value->kind = axs_rvalue; value->type = builtin_type (ax->gdbarch)->builtin_int; } @@ -2067,10 +2065,10 @@ logical_or_operation::do_generate_ax (struct expression *exp, if2 = ax_goto (ax, aop_if_goto); ax_const_l (ax, 0); end = ax_goto (ax, aop_goto); - ax_label (ax, if1, ax->len); - ax_label (ax, if2, ax->len); + ax_label (ax, if1, ax->buf.size ()); + ax_label (ax, if2, ax->buf.size ()); ax_const_l (ax, 1); - ax_label (ax, end, ax->len); + ax_label (ax, end, ax->buf.size ()); value->kind = axs_rvalue; value->type = builtin_type (ax->gdbarch)->builtin_int; } diff --git a/gdb/ax-general.c b/gdb/ax-general.c index e43e7732260..d5f4c51e65d 100644 --- a/gdb/ax-general.c +++ b/gdb/ax-general.c @@ -28,8 +28,6 @@ #include "value.h" #include "user-regs.h" -static void grow_expr (struct agent_expr *x, int n); - static void append_const (struct agent_expr *x, LONGEST val, int n); static LONGEST read_const (struct agent_expr *x, int o, int n); @@ -40,11 +38,6 @@ static void generic_ext (struct agent_expr *x, enum agent_op op, int n); agent_expr::agent_expr (struct gdbarch *gdbarch, CORE_ADDR scope) { - this->len = 0; - this->size = 1; /* Change this to a larger value once - reallocation code is tested. */ - this->buf = (unsigned char *) xmalloc (this->size); - this->gdbarch = gdbarch; this->scope = scope; @@ -58,25 +51,9 @@ agent_expr::agent_expr (struct gdbarch *gdbarch, CORE_ADDR scope) agent_expr::~agent_expr () { - xfree (this->buf); xfree (this->reg_mask); } -/* Make sure that X has room for at least N more bytes. This doesn't - affect the length, just the allocated size. */ -static void -grow_expr (struct agent_expr *x, int n) -{ - if (x->len + n > x->size) - { - x->size *= 2; - if (x->size < x->len + n) - x->size = x->len + n + 10; - x->buf = (unsigned char *) xrealloc (x->buf, x->size); - } -} - - /* Append the low N bytes of VAL as an N-byte integer to the expression X, in big-endian order. */ static void @@ -84,13 +61,11 @@ append_const (struct agent_expr *x, LONGEST val, int n) { int i; - grow_expr (x, n); for (i = n - 1; i >= 0; i--) { - x->buf[x->len + i] = val & 0xff; + x->buf.push_back (val & 0xff); val >>= 8; } - x->len += n; } @@ -103,7 +78,7 @@ read_const (struct agent_expr *x, int o, int n) LONGEST accum = 0; /* Make sure we're not reading off the end of the expression. */ - if (o + n > x->len) + if (o + n > x->buf.size ()) error (_("GDB bug: ax-general.c (read_const): incomplete constant")); for (i = 0; i < n; i++) @@ -117,8 +92,7 @@ read_const (struct agent_expr *x, int o, int n) void ax_raw_byte (struct agent_expr *x, gdb_byte byte) { - grow_expr (x, 1); - x->buf[x->len++] = byte; + x->buf.push_back (byte); } /* Append a simple operator OP to EXPR. */ @@ -154,9 +128,8 @@ generic_ext (struct agent_expr *x, enum agent_op op, int n) error (_("GDB bug: ax-general.c (generic_ext): " "opcode has inadequate range")); - grow_expr (x, 2); - x->buf[x->len++] = op; - x->buf[x->len++] = n; + x->buf.push_back (op); + x->buf.push_back (n); } @@ -185,9 +158,8 @@ ax_trace_quick (struct agent_expr *x, int n) error (_("GDB bug: ax-general.c (ax_trace_quick): " "size out of range for trace_quick")); - grow_expr (x, 2); - x->buf[x->len++] = aop_trace_quick; - x->buf[x->len++] = n; + x->buf.push_back (aop_trace_quick); + x->buf.push_back (n); } @@ -200,12 +172,10 @@ ax_trace_quick (struct agent_expr *x, int n) int ax_goto (struct agent_expr *x, enum agent_op op) { - grow_expr (x, 3); - x->buf[x->len + 0] = op; - x->buf[x->len + 1] = 0xff; - x->buf[x->len + 2] = 0xff; - x->len += 3; - return x->len - 2; + x->buf.push_back (op); + x->buf.push_back (0xff); + x->buf.push_back (0xff); + return x->buf.size () - 2; } /* Suppose a given call to ax_goto returns some value PATCH. When you @@ -294,11 +264,9 @@ ax_reg (struct agent_expr *x, int reg) if (reg < 0 || reg > 0xffff) error (_("GDB bug: ax-general.c (ax_reg): " "register number out of range")); - grow_expr (x, 3); - x->buf[x->len] = aop_reg; - x->buf[x->len + 1] = (reg >> 8) & 0xff; - x->buf[x->len + 2] = (reg) & 0xff; - x->len += 3; + x->buf.push_back (aop_reg); + x->buf.push_back ((reg >> 8) & 0xff); + x->buf.push_back ((reg) & 0xff); } } @@ -312,11 +280,9 @@ ax_tsv (struct agent_expr *x, enum agent_op op, int num) internal_error (_("ax-general.c (ax_tsv): variable " "number is %d, out of range"), num); - grow_expr (x, 3); - x->buf[x->len] = op; - x->buf[x->len + 1] = (num >> 8) & 0xff; - x->buf[x->len + 2] = (num) & 0xff; - x->len += 3; + x->buf.push_back (op); + x->buf.push_back ((num >> 8) & 0xff); + x->buf.push_back ((num) & 0xff); } /* Append a string to the expression. Note that the string is going @@ -334,12 +300,11 @@ ax_string (struct agent_expr *x, const char *str, int slen) internal_error (_("ax-general.c (ax_string): string " "length is %d, out of allowed range"), slen); - grow_expr (x, 2 + slen + 1); - x->buf[x->len++] = ((slen + 1) >> 8) & 0xff; - x->buf[x->len++] = (slen + 1) & 0xff; + x->buf.push_back (((slen + 1) >> 8) & 0xff); + x->buf.push_back ((slen + 1) & 0xff); for (i = 0; i < slen; ++i) - x->buf[x->len++] = str[i]; - x->buf[x->len++] = '\0'; + x->buf.push_back (str[i]); + x->buf.push_back ('\0'); } @@ -375,7 +340,7 @@ ax_print (struct ui_file *f, struct agent_expr *x) != aop_last) error (_("GDB bug: ax-general.c (ax_print): opcode map out of sync")); - for (i = 0; i < x->len;) + for (i = 0; i < x->buf.size ();) { enum agent_op op = (enum agent_op) x->buf[i]; @@ -386,7 +351,7 @@ ax_print (struct ui_file *f, struct agent_expr *x) i++; continue; } - if (i + 1 + aop_map[op].op_size > x->len) + if (i + 1 + aop_map[op].op_size > x->buf.size ()) { gdb_printf (f, _("%3d \n"), i, aop_map[op].name); @@ -471,28 +436,28 @@ ax_reqs (struct agent_expr *ax) /* Jump target table. targets[i] is non-zero iff we have found a jump to offset i. */ - char *targets = (char *) alloca (ax->len * sizeof (targets[0])); + char *targets = (char *) alloca (ax->buf.size () * sizeof (targets[0])); /* Instruction boundary table. boundary[i] is non-zero iff our scan has reached an instruction starting at offset i. */ - char *boundary = (char *) alloca (ax->len * sizeof (boundary[0])); + char *boundary = (char *) alloca (ax->buf.size () * sizeof (boundary[0])); /* Stack height record. If either targets[i] or boundary[i] is non-zero, heights[i] is the height the stack should have before executing the bytecode at that point. */ - int *heights = (int *) alloca (ax->len * sizeof (heights[0])); + int *heights = (int *) alloca (ax->buf.size () * sizeof (heights[0])); /* Pointer to a description of the present op. */ struct aop_map *op; - memset (targets, 0, ax->len * sizeof (targets[0])); - memset (boundary, 0, ax->len * sizeof (boundary[0])); + memset (targets, 0, ax->buf.size () * sizeof (targets[0])); + memset (boundary, 0, ax->buf.size () * sizeof (boundary[0])); ax->max_height = ax->min_height = height = 0; ax->flaw = agent_flaw_none; ax->max_data_size = 0; - for (i = 0; i < ax->len; i += 1 + op->op_size) + for (i = 0; i < ax->buf.size (); i += 1 + op->op_size) { if (ax->buf[i] > (sizeof (aop_map) / sizeof (aop_map[0]))) { @@ -508,7 +473,7 @@ ax_reqs (struct agent_expr *ax) return; } - if (i + 1 + op->op_size > ax->len) + if (i + 1 + op->op_size > ax->buf.size ()) { ax->flaw = agent_flaw_incomplete_instruction; return; @@ -543,7 +508,7 @@ ax_reqs (struct agent_expr *ax) || aop_if_goto == op - aop_map) { int target = read_const (ax, i + 1, 2); - if (target < 0 || target >= ax->len) + if (target < 0 || target >= ax->buf.size ()) { ax->flaw = agent_flaw_bad_jump; return; @@ -568,7 +533,7 @@ ax_reqs (struct agent_expr *ax) /* For unconditional jumps with a successor, check that the successor is a target, and pick up its stack height. */ if (aop_goto == op - aop_map - && i + 3 < ax->len) + && i + 3 < ax->buf.size ()) { if (!targets[i + 3]) { @@ -589,7 +554,7 @@ ax_reqs (struct agent_expr *ax) } /* Check that all the targets are on boundaries. */ - for (i = 0; i < ax->len; i++) + for (i = 0; i < ax->buf.size (); i++) if (targets[i] && !boundary[i]) { ax->flaw = agent_flaw_bad_jump; diff --git a/gdb/ax.h b/gdb/ax.h index 9c47a8d0b63..608af4c8c68 100644 --- a/gdb/ax.h +++ b/gdb/ax.h @@ -86,13 +86,7 @@ struct agent_expr ~agent_expr (); /* The bytes of the expression. */ - unsigned char *buf; - - /* The number of bytecode in the expression. */ - int len; - - /* Allocated space available currently. */ - int size; + gdb::byte_vector buf; /* The target architecture assumed to be in effect. */ struct gdbarch *gdbarch; diff --git a/gdb/dwarf2/loc.c b/gdb/dwarf2/loc.c index e50e7f1e47e..5b2d58ab44e 100644 --- a/gdb/dwarf2/loc.c +++ b/gdb/dwarf2/loc.c @@ -2399,7 +2399,7 @@ dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc, int64_t offset; int i; - offsets[op_ptr - base] = expr->len; + offsets[op_ptr - base] = expr->buf.size (); ++op_ptr; /* Our basic approach to code generation is to map DWARF @@ -2734,7 +2734,7 @@ dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc, ax_const_l (expr, 0); ax_simple (expr, aop_swap); ax_simple (expr, aop_sub); - ax_label (expr, i, expr->len); + ax_label (expr, i, expr->buf.size ()); break; case DW_OP_neg: diff --git a/gdb/remote.c b/gdb/remote.c index 513214c85a8..7e3d6adfe4f 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -10690,9 +10690,9 @@ remote_add_target_side_condition (struct gdbarch *gdbarch, /* Send conditions to the target. */ for (agent_expr *aexpr : bp_tgt->conditions) { - xsnprintf (buf, buf_end - buf, "X%x,", aexpr->len); + xsnprintf (buf, buf_end - buf, "X%x,", (int) aexpr->buf.size ()); buf += strlen (buf); - for (int i = 0; i < aexpr->len; ++i) + for (int i = 0; i < aexpr->buf.size (); ++i) buf = pack_hex_byte (buf, aexpr->buf[i]); *buf = '\0'; } @@ -10715,9 +10715,9 @@ remote_add_target_side_commands (struct gdbarch *gdbarch, cmds parameter. */ for (agent_expr *aexpr : bp_tgt->tcommands) { - sprintf (buf, "X%x,", aexpr->len); + sprintf (buf, "X%x,", (int) aexpr->buf.size ()); buf += strlen (buf); - for (int i = 0; i < aexpr->len; ++i) + for (int i = 0; i < aexpr->buf.size (); ++i) buf = pack_hex_byte (buf, aexpr->buf[i]); *buf = '\0'; } @@ -13383,7 +13383,7 @@ remote_target::download_tracepoint (struct bp_location *loc) size_left = buf.size () - strlen (buf.data ()); ret = snprintf (buf.data () + strlen (buf.data ()), - size_left, ":X%x,", aexpr->len); + size_left, ":X%x,", (int) aexpr->buf.size ()); if (ret < 0 || ret >= size_left) error ("%s", err_msg); @@ -13392,12 +13392,12 @@ remote_target::download_tracepoint (struct bp_location *loc) /* Two bytes to encode each aexpr byte, plus the terminating null byte. */ - if (aexpr->len * 2 + 1 > size_left) + if (aexpr->buf.size () * 2 + 1 > size_left) error ("%s", err_msg); pkt = buf.data () + strlen (buf.data ()); - for (int ndx = 0; ndx < aexpr->len; ++ndx) + for (int ndx = 0; ndx < aexpr->buf.size (); ++ndx) pkt = pack_hex_byte (pkt, aexpr->buf[ndx]); *pkt = '\0'; } diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c index 0af7404aef1..335df997c05 100644 --- a/gdb/tracepoint.c +++ b/gdb/tracepoint.c @@ -618,7 +618,7 @@ finalize_tracepoint_aexpr (struct agent_expr *aexpr) { ax_reqs (aexpr); - if (aexpr->len > MAX_AGENT_EXPR_LEN) + if (aexpr->buf.size () > MAX_AGENT_EXPR_LEN) error (_("Expression is too complicated.")); report_agent_reqs_errors (aexpr); @@ -885,7 +885,7 @@ collection_list::add_local_register (struct gdbarch *gdbarch, corresponding raw registers in the ax mask, but if this isn't the case add the expression that is generated to the collection list. */ - if (aexpr->len > 0) + if (aexpr->buf.size () > 0) add_aexpr (std::move (aexpr)); } } @@ -1215,18 +1215,19 @@ collection_list::stringify () for (i = 0; i < m_aexprs.size (); i++) { QUIT; /* Allow user to bail out with ^C. */ - if ((count + 10 + 2 * m_aexprs[i]->len) > MAX_AGENT_EXPR_LEN) + if ((count + 10 + 2 * m_aexprs[i]->buf.size ()) > MAX_AGENT_EXPR_LEN) { str_list.emplace_back (temp_buf.data (), count); count = 0; end = temp_buf.data (); } - sprintf (end, "X%08X,", m_aexprs[i]->len); + sprintf (end, "X%08X,", (int) m_aexprs[i]->buf.size ()); end += 10; /* 'X' + 8 hex digits + ',' */ count += 10; - end += 2 * bin2hex (m_aexprs[i]->buf, end, m_aexprs[i]->len); - count += 2 * m_aexprs[i]->len; + end += 2 * bin2hex (m_aexprs[i]->buf.data (), end, + m_aexprs[i]->buf.size ()); + count += 2 * m_aexprs[i]->buf.size (); } if (count != 0) -- 2.39.2