From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id mKyrCm7H2mYC/iEAWB0awg (envelope-from ) for ; Fri, 06 Sep 2024 05:12:14 -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=SARlCmKO; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 219C51E354; Fri, 6 Sep 2024 05:12:14 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-10.1 required=5.0 tests=ARC_SIGNED,ARC_VALID, BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE autolearn=ham autolearn_force=no version=4.0.0 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 E60D71E05C for ; Fri, 6 Sep 2024 05:12:12 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 81BC0384A4AD for ; Fri, 6 Sep 2024 09:12:12 +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 ESMTP id 2FBCB384A80B for ; Fri, 6 Sep 2024 09:11:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2FBCB384A80B 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 2FBCB384A80B 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=1725613880; cv=none; b=C6EG/O/WsOrW5QMDGoCXA/9WoUwpKGpsFwdgk42wJffjNc9080TVZDmLw4Rm1YpOj7QEL/zBqPxoYug2plPtAmqU95ndWy6qGqC6jagL+VSpiMlgclU6zbkx0+cAiCkakZZcP9XHH1gj8fQdk3EqJYOblCNzgHIxIuN/Dft9SGw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725613880; c=relaxed/simple; bh=jh23DrAGXRf/J5IJCgsGJgLTuXYsHcekPe+xhK61FZk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=UlTHuAtYY/n5ycqLbUdRU0Ap268MQ9XDtsFpu9XWF3YE48smLziWj4ayY+8osbjvpHxDSEzQ2qwgBKtzvWhat/A2XF3EpLrl6fn7qETlkcQqmAAgXi3y9HL5mCgEQn5c85KzpWF7KWjRzVrgZLm4v0GQ01IotFB7v+SiWNsTrBM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725613877; 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=lpmME500ed2+Y8bv3sr9GAlMXXiFj/s+mBwz6x2HYKs=; b=SARlCmKOywvXmsWV+eyKuRLXshFNhfjcwUUzUyuUEXX5QkPQoUZ0HArdZKOebMYBvJHLlx +ldVHWTfjd69bXmusDmJfEYEyn3iqNRtXVxQBe84CbbpuLxmydbINC0PY9qUdqKluAOfke xnrvxzQKEqfVMRIUBTsJURMtyyk6dHo= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-161-sD6UTl1NPouCE5IFNvCChw-1; Fri, 06 Sep 2024 05:11:16 -0400 X-MC-Unique: sD6UTl1NPouCE5IFNvCChw-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-428040f49f9so14286695e9.0 for ; Fri, 06 Sep 2024 02:11:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725613874; x=1726218674; 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=lpmME500ed2+Y8bv3sr9GAlMXXiFj/s+mBwz6x2HYKs=; b=U690V4l+YZYuBe3a8EeDmhfT1V+UAJDwIVhA+7JwMVhi/ZqwjG+7aNKIdxvcCV6iJf nZ6kyn8T2jSgT1lhaH/82C2UPIPoU+jDuyp6/4NNdQ72jam7n1nk6pxRh+wcRIi9wMQh vMMNPmNYBsMVYYb8to80Bh9n/lTeQgvYxI2BR2j8Y7hZVM0+p7I0MPl8VXjswGiGM+IX o9ctzPz66Vn+4hi94P+0Wq8qt86xmClEDiO5/SCYJTRXmuYOWsxgAV6wrfRNPnN1lXaD kxzkRyPcnGGh8WeDSIYBSQX2gB26m+5WLOF/qAC4IxUKA53sIWb8GhEgagM0Ggd8cOP1 uWGw== X-Gm-Message-State: AOJu0Yx9sedxNyLaMW1/kjZdRoEw61x+9lXTBOwGa+/p4mGNXy0vEcWM WCC1TDaqEfSe2WUP0oXBaykJj9ryVcFBFNe7+2QyKVBJfbOfH6NPDgjkhvzT5vbH9avIQ5t69a4 Joe7qLnqYzUHYKPlOXYwgN6fVkqFjeNVyeUoZmEmQmRlhE38FkLPZWbSJ5e6GzySctdfxec5yCE ill+T7d07Um8+D+oeeopmkHDlk1/LrBpt2X8/LkwnfC3I= X-Received: by 2002:a05:600c:3550:b0:428:1310:b6b5 with SMTP id 5b1f17b1804b1-42bb27bc4acmr211694005e9.34.1725613873675; Fri, 06 Sep 2024 02:11:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG5xcg5WUrxW+XfvK5Yu9FnOR+JrJFvdWIO0WczsYwPZN4g3Ncz1rhobtBWQ9+pkJ67HtjKRg== X-Received: by 2002:a05:600c:3550:b0:428:1310:b6b5 with SMTP id 5b1f17b1804b1-42bb27bc4acmr211693455e9.34.1725613872367; Fri, 06 Sep 2024 02:11:12 -0700 (PDT) Received: from localhost (178.126.90.146.dyn.plus.net. [146.90.126.178]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42ca05d8b4csm13819065e9.36.2024.09.06.02.11.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Sep 2024 02:11:10 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv13 3/6] gdb: create new is_thread_id helper function Date: Fri, 6 Sep 2024 10:11:00 +0100 Message-Id: <901ccc219a838c7975967f180d93abd2edebfab1.1725613660.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-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 1b8f343f080..442d5b33693 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 7bbdeabe85b..0ea58bb552d 100644 --- a/gdb/tid-parse.h +++ b/gdb/tid-parse.h @@ -36,6 +36,14 @@ struct thread_info; 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