From patchwork Sat Jul 20 17:03:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 94258 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 E23523860C2E for ; Sat, 20 Jul 2024 17:04:29 +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 7B767385DDC5 for ; Sat, 20 Jul 2024 17:03:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7B767385DDC5 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 7B767385DDC5 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=1721495015; cv=none; b=MpCj/7pmD1HqeX3xd1SnV/Uo2VleN9uOY/ccwc4RmP35zMiotlIIF+hS+ji1khwl5vh6tACE77E6JvXHZSzGpclaupC1OCw9nEdYwPd4yr9+1nMJRa/GzxJ8hY+rwS2GgJcCf9pPFIozsdRwkH39vdT459mE8d72ssKW22pofr0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721495015; c=relaxed/simple; bh=jh23DrAGXRf/J5IJCgsGJgLTuXYsHcekPe+xhK61FZk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=vvo30/nR5jIZ4O2qz7R2wOVgI1ZnTWQBb1vi0knxzlXyQ9PexWhovGgmBrCfI17MkMuRKTdRFBwvOy4sVy2yPnCHxcc4DhPHU/uSEyy9fnpuMkzUMQIiMfq29qM883NrbBg5yl4jes8uZQsOntnafIH6ZMVi02Kg26aGtG7HVYM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1721495012; 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=RTltM1puORKNM2S1L40DTEUYFfOzvO3HwfWT4kIyeu7c49vmoNvXMP7ZkQA4SzraZCeYNl FlicVaV19XKh0WoEEU2K1iiTxcMlIu9P15ZxDKofFrFaEaeC3YWZ1fJQ7vB1yZMO82TuiI zE8zrrlzqp0almypWlE3xhiFJ2AKy7c= 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-501-FZhNgYGyNFyN0X4dfGAcKw-1; Sat, 20 Jul 2024 13:03:30 -0400 X-MC-Unique: FZhNgYGyNFyN0X4dfGAcKw-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-42668857c38so20474245e9.1 for ; Sat, 20 Jul 2024 10:03:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721495008; x=1722099808; 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=vW3Bo80sX47vItlWn9dNyVMjJTcDJ6MpgC6PKswIaoyXfopWuEDniFSUrYsfb+9sSI UF0YlrL+EQ4xXcMpfDR16mfhiuzn6X1T5FyOukdD01mOC4PZwaS9wAMhVqlTlX9jn++E XaiuPkrVu13Bu+xdJc5/svi4YX8JlFtDLkHj9ns/zsKNQCr/RUzkprbUduFfe84xWp2X pa7aNrZp99FYCfWXQaxp/ZsnPGTTk/NAnTjSjlJxO98ceyXYvG6fqmzqgPMCgSX+Ym/L WcugwQ+JdGJzfOmg7QSGQrECV439iIrQ1mMFLISAHqbIudunDxDGXhbH9VG1PfdlFxt/ EQtA== X-Gm-Message-State: AOJu0YwVBHFNS/MSzhpPB+WFcivFryeN5zATmeEUlp+y0cLX2fTEmbcf 0IpvRENGD/Qd+653Wtnu7f4iZj+7lyqARqZ8RNcNWP7azdDE/sufvqekewRvnrpOnSri5RD05Dv e5uBFPE0kpzZFtwOfSXLa1QR+Ev+Ht3/rEo6vABmIYq4448dFfAaUlc5f9e15yaI9dLC1YbXli6 THrfVGsaHT9SDrWL4JqsVV8WuuYEJD7Eidb0K21fay4Rg= X-Received: by 2002:a05:600c:4fd0:b0:426:65bf:5cc2 with SMTP id 5b1f17b1804b1-427dc512d12mr12025125e9.1.1721495008372; Sat, 20 Jul 2024 10:03:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEiSaeJpCRyK2ZTs4GkJXlCsvXy50l9it85Nr3kZJYl1ErIBA+KnXQZBmMq9aqFwpvpBjU5MQ== X-Received: by 2002:a05:600c:4fd0:b0:426:65bf:5cc2 with SMTP id 5b1f17b1804b1-427dc512d12mr12024985e9.1.1721495007939; Sat, 20 Jul 2024 10:03:27 -0700 (PDT) Received: from localhost ([31.111.84.186]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-427d2a5bf8esm87718605e9.12.2024.07.20.10.03.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Jul 2024 10:03:27 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv12 3/6] gdb: create new is_thread_id helper function Date: Sat, 20 Jul 2024 18:03:16 +0100 Message-Id: <3b8497f9ce2a244c28ade552fd25039ca499262e.1721494871.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=-11.7 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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 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