From patchwork Tue Mar 5 15:21:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 86811 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F1B9A38582A3 for ; Tue, 5 Mar 2024 15:24:55 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org 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 549903858285 for ; Tue, 5 Mar 2024 15:22:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 549903858285 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 549903858285 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=1709652133; cv=none; b=bYf/hBuPNCsoo73yhLR1BI8asjgFKj4JXHOM2Qa5n+7BKKW0FIlhOqhpQYZREyXmdSJkBE2smmwuSCP4v/s1fd0UNbi0DUEH36pWfISRSHe4HUsYYTR1fYUo4kzYxzfotu/Pt7qxT/LtdjhqLH4B9GoHM5dvqGtlCQNFTVoA17Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709652133; c=relaxed/simple; bh=OMk66Gw3V7xFfCutPcUz7hQ9ixjd/B4oSAkX+9BUe4g=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=cSYgKXmzoYknZ/ch4EM55pyqoNj/lHj3W3AOqWja85xSeMVGAn3onT6eXFQVKk+2yl9CwOluHeorDw0AcRrVc+nK7FjAqv11VwmxMTIb3uhNg0H23L0OowrMCLqktDbxh0YgpR3/51U39b8Y6jRCQjr1KCRdtiuTGgu9Y5P+K1g= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709652130; 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=7Z5hLcfo6AlfqyxATz7ARN/0ggMmV/d4CfqVwXydPF8=; b=ScwjrF/qfwpeh4gTqrokFdeSQA+SehtgQCNXOfseG+O2JG39fz2LU9SoHTWy3Kpwt9drN0 t/Z9My15LoSzY1YiQbOmUYaVxPut/87Beqzs7ocrv4cgee61SF71i4V18FBJRunuxIIDwd MFDV8H1ZGaGXZhMw+dkdhlBeP7FnpUk= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-509-gTCTmAlgMw2AemoHrMb5vQ-1; Tue, 05 Mar 2024 10:22:09 -0500 X-MC-Unique: gTCTmAlgMw2AemoHrMb5vQ-1 Received: by mail-ej1-f71.google.com with SMTP id a640c23a62f3a-a4489805a2dso195021666b.2 for ; Tue, 05 Mar 2024 07:22:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709652127; x=1710256927; 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=7Z5hLcfo6AlfqyxATz7ARN/0ggMmV/d4CfqVwXydPF8=; b=A5G+oGN4CNv3JGvm+3lBpmGviRglu5YE4bm1uJZOSADFONsK8w9Bl2v6JFLLYQ/iUQ Tk5XTV81UtlnJOLHxDQOV7CPiXOXAca6Aae5r6k79HmKMthcGC8qOmfJ9wZdqOxlrhBx RkLHcHjGc7D8vyyQRft6ar3DLaHd+fCYsmls+OKtyUbscvQ2+7GITw4WvSn74O9O5aQx Gha9wSDxHjvziDnjzHLdpzbvFYMvDlCCkxb7/Utn1cgyV6bObwWFFG19FD0Njt7fO70G hnTljOWTybTEqsVPevDJXqhmkAGM7mZfX8c1g7nf/r/g7CVZ+Dxaf4N8AUY/D2h8QcJw 4drQ== X-Gm-Message-State: AOJu0Yyd0sVfz1/ap81QWDTduE3R3JUc6tt/bd7itaKX0PY8qa7xOWLE R7x1/GM2jF6GoqbQqg5uXUPRrEK0YT6zCaMG7hOJhpN7fv4ATjkIFpg3lbiK1bjTLGIC0PobFY0 q3h6zX396doJ6q7gQIX0tKFcl6vt/IqESj+nzfw+C+NN9GqpeCD9GRWr5h5Ss0B7VcwATL0hPMi XyGKPLPPWkbC534xJuaLwFCT3qsNVvGy2+2ZgThkr9b98= X-Received: by 2002:a17:906:eb17:b0:a45:9b1:f4a9 with SMTP id mb23-20020a170906eb1700b00a4509b1f4a9mr5329563ejb.20.1709652127622; Tue, 05 Mar 2024 07:22:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IGiutQADu6GYxwCV1X0ennsvnBTExBF6wjUJMjWIGUlHnwyHTcDkmUcCz3MLGxI03VLrtMegg== X-Received: by 2002:a17:906:eb17:b0:a45:9b1:f4a9 with SMTP id mb23-20020a170906eb1700b00a4509b1f4a9mr5329546ejb.20.1709652127308; Tue, 05 Mar 2024 07:22:07 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id am20-20020a170906569400b00a459d922903sm1030327ejc.207.2024.03.05.07.22.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 07:22:06 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv9 11/14] gdb: create new is_thread_id helper function Date: Tue, 5 Mar 2024 15:21:42 +0000 Message-Id: <949fcb519598b2c884d15158d3490f805f51262f.1709651994.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 X-Spam-Status: No, score=-12.3 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, 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+patchwork=sourceware.org@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 aa1480d7cf0..7f85b94eba4 100644 --- a/gdb/tid-parse.c +++ b/gdb/tid-parse.c @@ -48,40 +48,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; } @@ -89,6 +92,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) @@ -97,7 +126,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); @@ -105,14 +134,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