From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 04C16385843A for ; Mon, 15 May 2023 19:27:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 04C16385843A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684178875; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nZanVJWAcbm0KzOzjv9s73VHPfwSg3O8veu+2Io28Gw=; b=Fnry2x0Bv+dv8FHvCLaolLApZgoB4BIPmyMvVNCfjyhroINY2QM9Zk0zk3GE7qcvTofk91 sVrW6C7nAASYFvOiNurvxeVwCguT+6Xdaz1dDuuGLUUypPt8DUvw/e5d60K53/IBpgd0T1 JbFVf2dOqWKQM6Qu6avcJ09eLg045vo= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-552-JOdmiqVxM1iv8h4FtmqfjA-1; Mon, 15 May 2023 15:27:54 -0400 X-MC-Unique: JOdmiqVxM1iv8h4FtmqfjA-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-3f509037a45so280725e9.1 for ; Mon, 15 May 2023 12:27:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684178873; x=1686770873; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nZanVJWAcbm0KzOzjv9s73VHPfwSg3O8veu+2Io28Gw=; b=EG50SL/BJqpJTc8nlZCsULSiBFC56velAMBeksEVlarzD12qKDIoY5sXgDsIeSTsvk iQqSA24AE7zC6UGnLyo/jIMflTHLbBQKmlgIu2ofMl6LTv2geA+TITlNUFoH9zvB8UVK HHTmHtd1i5pV/fWc0H7oEwNpNSd7NQhcSpT/zH64cPLFXZmKO3FSB1ZvQF/Ovex28yTD LLWqCx2L2SWpYOr6qkRuZxFllByJ/3ej8ZaKdF64GIDx5HJh+Xn+o335KxXE8Ov+mJU3 ll32Tf/0DcXf/YjrmNn7P5XEX9SDRMZOsCuvornp+G0fzCyjX5pUtqnhoym3ZpX1CJYV ydmA== X-Gm-Message-State: AC+VfDxcNgNKOllV9kWjpD8GM2xzvXSkZruzVJkzdHq1ZWtlEPuG6CRn AZDbDCicqbnbF1cpzsgECfkJNu92qkEHHxzPv8ogRDfeprfADoGReHMblQ/bFi1d+ZO2G7Uzha+ GF6eUJR7Rai9l3Lwdb2TPB0NuEjme5LScMrY2SPJJVmufaD56QNLywCMonT4YL3VuEi0ajeCT82 UlRpCrKg== X-Received: by 2002:a5d:4ac1:0:b0:307:869c:99ce with SMTP id y1-20020a5d4ac1000000b00307869c99cemr22648110wrs.21.1684178872931; Mon, 15 May 2023 12:27:52 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7XfRan+TIWWGowh41SiD1uxcAQ8jPIkvFuEB8bE47feTp6O8EcCxIxfMAMKAs+cpMDlDkehw== X-Received: by 2002:a5d:4ac1:0:b0:307:869c:99ce with SMTP id y1-20020a5d4ac1000000b00307869c99cemr22648104wrs.21.1684178872636; Mon, 15 May 2023 12:27:52 -0700 (PDT) Received: from localhost (11.72.115.87.dyn.plus.net. [87.115.72.11]) by smtp.gmail.com with ESMTPSA id i8-20020a5d5228000000b003078a3f3a24sm54997wra.114.2023.05.15.12.27.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 12:27:52 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 2/9] gdb: create_breakpoint: asserts relating to extra_string/parse_extra Date: Mon, 15 May 2023 20:27:38 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,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: The goal of this commit is to better define the API for create_breakpoint especially around the use of extra_string and parse_extra. This will be useful in the next commit when I plan to make some changes to create_breakpoint. This commit makes one possibly breaking change: until this commit it was possible to create thread-specific dprintf breakpoint like this: (gdb) dprintf call_me, thread 1 "%s", "hello" Dprintf 2 at 0x401152: file /tmp/hello.c, line 8. (gdb) info breakpoints Num Type Disp Enb Address What 2 dprintf keep y 0x0000000000401152 in call_me at /tmp/hello.c:8 thread 1 stop only in thread 1 printf "%s", "hello" (gdb) this feature of dprintf was not documented, was not tested, and is slightly different in syntax to how we create thread specific breakpoints and/or watchpoints -- the thread condition appears after the first ','. I believe that this worked at all was simply by luck. We happen to pass the parse_extra flag as true from dprintf_command to create_breakpoint. So in this commit I made the choice change this. We now pass parse_extra as false from dprintf_command to create_breakpoint. With this done it is assumed that the only thing in the extra_string is the dprintf format and arguments. Beyond this change I've updated the comment on create_breakpoint in breakpoint.h, and I've then added some asserts into create_breakpoint as well as moving around some of the error handling. - We now assert on the incoming argument values, - I've moved an error check to sit after the call to find_condition_and_thread_for_sals, this ensures the extra_string was parsed correctly, In dprintf_command: - We now throw an error if there is no format string after the dprintf location. This error was already being thrown, but was being caught later in the process. With this change we catch the missing string earlier, - And, as mentioned earlier, we pass parse_extra as false when calling create_breakpoint, In create_tracepoint_from_upload: - We now throw an error if the parsed location doesn't completely consume the addr_str variable. This error has now effectively moved out of create_breakpoint. --- gdb/breakpoint.c | 42 +++++++++++++++++++++++++++++------------- gdb/breakpoint.h | 38 ++++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 27 deletions(-) diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index 31448e0a895..4d182ac0143 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -9035,6 +9035,17 @@ create_breakpoint (struct gdbarch *gdbarch, if (extra_string != NULL && *extra_string == '\0') extra_string = NULL; + /* A bp_dprintf must always have an accompanying EXTRA_STRING containing + the dprintf format and arguments -- PARSE_EXTRA should always be false + in this case. + + For all other breakpoint types, EXTRA_STRING should be nullptr unless + PARSE_EXTRA is true. */ + gdb_assert ((type_wanted == bp_dprintf + && extra_string != nullptr && !parse_extra) + || (type_wanted != bp_dprintf + && (extra_string == nullptr || parse_extra))); + try { ops->create_sals_from_location_spec (locspec, &canonical); @@ -9098,6 +9109,8 @@ create_breakpoint (struct gdbarch *gdbarch, if (parse_extra) { + gdb_assert (type_wanted != bp_dprintf); + gdb::unique_xmalloc_ptr rest; gdb::unique_xmalloc_ptr cond; @@ -9105,15 +9118,15 @@ create_breakpoint (struct gdbarch *gdbarch, find_condition_and_thread_for_sals (lsal.sals, extra_string, &cond, &thread, &task, &rest); + + if (rest.get () != nullptr && *(rest.get ()) != '\0') + error (_("Garbage '%s' at end of command"), rest.get ()); + cond_string_copy = std::move (cond); extra_string_copy = std::move (rest); } else { - if (type_wanted != bp_dprintf - && extra_string != NULL && *extra_string != '\0') - error (_("Garbage '%s' at end of location"), extra_string); - /* Check the validity of the condition. We should error out if the condition is invalid at all of the locations and if it is not forced. In the PARSE_EXTRA case above, this @@ -9322,20 +9335,17 @@ dprintf_command (const char *arg, int from_tty) /* If non-NULL, ARG should have been advanced past the location; the next character must be ','. */ - if (arg != NULL) + if (arg == nullptr || arg[0] != ',' || arg[1] == '\0') + error (_("Format string required")); + else { - if (arg[0] != ',' || arg[1] == '\0') - error (_("Format string required")); - else - { - /* Skip the comma. */ - ++arg; - } + /* Skip the comma. */ + ++arg; } create_breakpoint (get_current_arch (), locspec.get (), - NULL, 0, arg, false, 1 /* parse arg */, + NULL, -1, arg, false, 0 /* parse arg */, 0, bp_dprintf, 0 /* Ignore count */, pending_break_support, @@ -13910,6 +13920,12 @@ create_tracepoint_from_upload (struct uploaded_tp *utp) location_spec_up locspec = string_to_location_spec (&addr_str, current_language); + + + gdb_assert (addr_str != nullptr); + if (*addr_str != '\0') + error (_("Garbage '%s' at end of location"), addr_str); + if (!create_breakpoint (get_current_arch (), locspec.get (), utp->cond_string.get (), -1, addr_str, diff --git a/gdb/breakpoint.h b/gdb/breakpoint.h index e57dc2523fb..2adcc9fa338 100644 --- a/gdb/breakpoint.h +++ b/gdb/breakpoint.h @@ -1515,26 +1515,36 @@ enum breakpoint_create_flags functions for setting a breakpoint at LOCSPEC. This function has two major modes of operations, selected by the - PARSE_EXTRA parameter. + PARSE_EXTRA and WANTED_TYPE parameters. - If PARSE_EXTRA is zero, LOCSPEC is just the breakpoint's location - spec, with condition, thread, and extra string specified by the - COND_STRING, THREAD, and EXTRA_STRING parameters. + When WANTED_TYPE is not bp_dprintf the following rules apply: - If PARSE_EXTRA is non-zero, this function will attempt to extract - the condition, thread, and extra string from EXTRA_STRING, ignoring - the similarly named parameters. + If PARSE_EXTRA is zero, LOCSPEC is just the breakpoint's location + spec, with condition, thread, and extra string specified by the + COND_STRING, THREAD, and EXTRA_STRING parameters. - If FORCE_CONDITION is true, the condition is accepted even when it is - invalid at all of the locations. However, if PARSE_EXTRA is non-zero, - the FORCE_CONDITION parameter is ignored and the corresponding argument - is parsed from EXTRA_STRING. + If PARSE_EXTRA is non-zero, this function will attempt to extract the + condition, thread, and extra string from EXTRA_STRING, ignoring the + similarly named parameters. + + When WANTED_TYPE is bp_dprintf the following rules apply: + + PARSE_EXTRA must always be zero, LOCSPEC is just the breakpoint's + location spec, with condition, thread, and extra string (which + contains the dprintf format and arguments) specified by the + COND_STRING, THREAD, and EXTRA_STRING parameters. + + If FORCE_CONDITION is true, the condition (in COND_STRING) is accepted + even when it is invalid at all of the locations. However, if + PARSE_EXTRA is non-zero and WANTED_TYPE is not bp_dprintf, the + FORCE_CONDITION parameter is ignored and the corresponding argument is + parsed from EXTRA_STRING. The THREAD should be a global thread number, the created breakpoint will only apply for that thread. If the breakpoint should apply for all - threads then pass -1. However, if PARSE_EXTRA is non-zero then the - THREAD parameter is ignored and an optional thread number will be parsed - from EXTRA_STRING. + threads then pass -1. However, if PARSE_EXTRA is non-zero and + WANTED_TYPE is not bp_dprintf, then the THREAD parameter is ignored and + an optional thread number will be parsed from EXTRA_STRING. If INTERNAL is non-zero, the breakpoint number will be allocated from the internal breakpoint count. -- 2.25.4