From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id gM41CJ1pYGb7RScAWB0awg (envelope-from ) for ; Wed, 05 Jun 2024 09:35:25 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=BS1iSX63; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 1DA441E0C0; Wed, 5 Jun 2024 09:35:25 -0400 (EDT) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 084E51E092 for ; Wed, 5 Jun 2024 09:35:23 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9A04D398CC14 for ; Wed, 5 Jun 2024 13:35:22 +0000 (GMT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id C357B393824A for ; Wed, 5 Jun 2024 13:33:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C357B393824A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C357B393824A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717594390; cv=none; b=Z0gCluGfuGUtRAv2TvVLQxa5chen94/sjbxyQ4vGyLRe3fPVFxdBhoG9obUT735NBUHCUA5UDBZ+5mO0F1jNVSBRuxMUQHocANzBlafDI3UkJbSzniPtsJe2WWsyQYF23LTvz8oaW52bqiDge9nbzj3l/cVmfMituX+TgcI9zM8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717594390; c=relaxed/simple; bh=Hz19z7itqegC+shE3bV/dTavEDSfDgFQ5oeIBnHGcdw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=OHWbqf3i753XU51Z3tZGmeMCAYKqXeDa/o9xdDgdnAu7LBmEUSrzUOpkt3iM3oEl3Xdbjy0R6fM0uqqERC5s3XiBi8GCM5O4jT6QHtuD8KSAsINKGXHLFZrdi1XHydTIkFloI6mhQpLMcnnNbwwhQ9E4I9WppIqeN4f/3S2typk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717594385; 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=Lb59d3Lui1kR//7GsS08J2MbTyGGY7/zs3YImjluYpY=; b=BS1iSX63sNeY1nq5nm8DIj1ZTwDmvz9xdcGmysLZ/rvCT/FiAUH0V8NoQdK8V1K7XV7It1 i9naiCx1b3DIruCn3u8dv84uPmZl6BYO5y9k+fDnVoB8fDz/9uv1sPPgKtL/0SY1rg+xjz UzQV/Q6ZC2zvg2nj5yvgzy8uMQ9MgZE= Received: from mail-lj1-f200.google.com (mail-lj1-f200.google.com [209.85.208.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-511-vQh0s9fTNTyRayPm-Et-fw-1; Wed, 05 Jun 2024 09:33:00 -0400 X-MC-Unique: vQh0s9fTNTyRayPm-Et-fw-1 Received: by mail-lj1-f200.google.com with SMTP id 38308e7fff4ca-2eaa90653adso27498171fa.3 for ; Wed, 05 Jun 2024 06:32:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717594376; x=1718199176; 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=Lb59d3Lui1kR//7GsS08J2MbTyGGY7/zs3YImjluYpY=; b=JyTlPAHSn/nmy2+JqIQ2dQx+AzlfCctNDu41AJTl+FvWlLEqi+yrUrdN0LzY2mxvPF alqPgsd0y8AswqJncNF+CLXI51mcFQMrs9fCEDRCDnPK34472iUGnjWDsXBjx3vp4y+5 b3s8JlEQ7fcqPBJYHL05ngloskYhbbeqxcMrf+lJLBTC3nsjWE45TchNqhNI8JjKEz/c VPyaKYgc0Aoha93PZLvEoQnew2cAm0Df6xW8ns3WdulaeUfGdibuA7gjKz0QvnmWVQQT /MdGuXT14sKzJSsWXvWpgOo+9g8HqrGeJEnfm07uGXxKC5pDEtjrSwZ6hODPpZywDgf+ xI1g== X-Gm-Message-State: AOJu0Yx3RN+lq6kNzoGIevvLmR7B9sienb7ihqAIpNcvRE4bawNcPGMu 5ju8+Bs6Oig1NnBx/XhOYnD1XgHQPy2GQL0M4jgz51Ughf1Ly2lTao4uqm33GAih6AmN8F9pZLd vxMnu5ZuZEF3YBECworT7PzydvhEceEncSjhCSSyedF3SIcYhTkUCmt9Q//tc0dL8KG158RA48/ GCiVZ+rZGD+mohZ0OySUN76RtX7WXW5oo9aDjSId6WSjc= X-Received: by 2002:a05:651c:1a21:b0:2ea:c3db:d0d5 with SMTP id 38308e7fff4ca-2eac79ba863mr16363311fa.7.1717594375916; Wed, 05 Jun 2024 06:32:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH6C52eRbLNpzUL67lCa7co8BXFmd+FYHqi4kJ/8UpvW5JH7Bgr0nQMm63hg5eQsoQt1jeudg== X-Received: by 2002:a05:651c:1a21:b0:2ea:c3db:d0d5 with SMTP id 38308e7fff4ca-2eac79ba863mr16363041fa.7.1717594375313; Wed, 05 Jun 2024 06:32:55 -0700 (PDT) Received: from localhost ([31.111.84.186]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4215810177dsm22654365e9.2.2024.06.05.06.32.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 06:32:54 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv11 6/9] gdb: create new is_thread_id helper function Date: Wed, 5 Jun 2024 14:32:40 +0100 Message-Id: <9ed9048a02726f8bd1a685d72fea798b0274de0a.1717594119.git.aburgess@redhat.com> 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.8 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_H4, RCVD_IN_MSPIKE_WL, 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org This is a refactoring commit that splits the existing parse_thread_id function into two parts, and then adds a new is_thread_id function. The core of parse_thread_id is split into parse_thread_id_1, which is responsible for actually parsing a thread-id. Then parse_thread_id is responsible for taking a parsed thread-id and validating that it references an actually existing inferior thread. The new is_thread_id function also uses parse_thread_id_1, but doesn't actually check that the inferior thread exists, instead, this new function simply checks that a string looks like a thread-id. This commit does not add a use for is_thread_id, this will be added in the next commit. This is a refactoring commit, there should be no user visible changes after this commit. --- gdb/tid-parse.c | 82 +++++++++++++++++++++++++++++++++++++------------ gdb/tid-parse.h | 8 +++++ 2 files changed, 70 insertions(+), 20 deletions(-) diff --git a/gdb/tid-parse.c b/gdb/tid-parse.c index 623f55e14b3..cff1a5f3d2b 100644 --- a/gdb/tid-parse.c +++ b/gdb/tid-parse.c @@ -47,40 +47,43 @@ get_positive_number_trailer (const char **pp, int trailer, const char *string) return num; } -/* See tid-parse.h. */ +/* Parse TIDSTR as a per-inferior thread ID, in either INF_NUM.THR_NUM + or THR_NUM form, and return a pair, the first item of the pair is + INF_NUM and the second item is THR_NUM. -struct thread_info * -parse_thread_id (const char *tidstr, const char **end) + If TIDSTR does not include an INF_NUM component, then the first item in + the pair will be 0 (which is an invalid inferior number), this indicates + that TIDSTR references the current inferior. + + This function does not validate the INF_NUM and THR_NUM are actually + valid numbers, that is, they might reference inferiors or threads that + don't actually exist; this function just splits the string into its + component parts. + + If there is an error parsing TIDSTR then this function will raise an + exception. */ + +static std::pair +parse_thread_id_1 (const char *tidstr, const char **end) { const char *number = tidstr; const char *dot, *p1; - struct inferior *inf; - int thr_num; - int explicit_inf_id = 0; + int thr_num, inf_num; dot = strchr (number, '.'); if (dot != NULL) { /* Parse number to the left of the dot. */ - int inf_num; - p1 = number; inf_num = get_positive_number_trailer (&p1, '.', number); if (inf_num == 0) invalid_thread_id_error (number); - - inf = find_inferior_id (inf_num); - if (inf == NULL) - error (_("No inferior number '%d'"), inf_num); - - explicit_inf_id = 1; p1 = dot + 1; } else { - inf = current_inferior (); - + inf_num = 0; p1 = number; } @@ -88,6 +91,32 @@ parse_thread_id (const char *tidstr, const char **end) if (thr_num == 0) invalid_thread_id_error (number); + if (end != nullptr) + *end = p1; + + return { inf_num, thr_num }; +} + +/* See tid-parse.h. */ + +struct thread_info * +parse_thread_id (const char *tidstr, const char **end) +{ + const auto [inf_num, thr_num] = parse_thread_id_1 (tidstr, end); + + inferior *inf; + bool explicit_inf_id = false; + + if (inf_num != 0) + { + inf = find_inferior_id (inf_num); + if (inf == nullptr) + error (_("No inferior number '%d'"), inf_num); + explicit_inf_id = true; + } + else + inf = current_inferior (); + thread_info *tp = nullptr; for (thread_info *it : inf->threads ()) if (it->per_inf_num == thr_num) @@ -96,7 +125,7 @@ parse_thread_id (const char *tidstr, const char **end) break; } - if (tp == NULL) + if (tp == nullptr) { if (show_inferior_qualified_tids () || explicit_inf_id) error (_("Unknown thread %d.%d."), inf->num, thr_num); @@ -104,14 +133,27 @@ parse_thread_id (const char *tidstr, const char **end) error (_("Unknown thread %d."), thr_num); } - if (end != NULL) - *end = p1; - return tp; } /* See tid-parse.h. */ +bool +is_thread_id (const char *tidstr, const char **end) +{ + try + { + (void) parse_thread_id_1 (tidstr, end); + return true; + } + catch (const gdb_exception_error &) + { + return false; + } +} + +/* See tid-parse.h. */ + tid_range_parser::tid_range_parser (const char *tidlist, int default_inferior) { diff --git a/gdb/tid-parse.h b/gdb/tid-parse.h index b7bd920f48a..bba5ae5118c 100644 --- a/gdb/tid-parse.h +++ b/gdb/tid-parse.h @@ -36,6 +36,14 @@ extern void ATTRIBUTE_NORETURN invalid_thread_id_error (const char *string); thrown. */ struct thread_info *parse_thread_id (const char *tidstr, const char **end); +/* Return true if TIDSTR is pointing to a string that looks like a + thread-id. This doesn't mean that TIDSTR identifies a valid thread, but + the string does at least look like a valid thread-id. If END is not + NULL, parse_thread_id stores the address of the first character after + the thread-id into *END. */ + +bool is_thread_id (const char *tidstr, const char **end); + /* Parse a thread ID or a thread range list. A range will be of the form -- 2.25.4