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 17040385842C for ; Sun, 31 Mar 2024 10:32:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 17040385842C 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 17040385842C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711881130; cv=none; b=fBcMwf+Nv6n2O/JI6TaZaEpLFTM+DznGIVD8ChoGqjPE0LQi9II1EHFSW7mbg/POL3AuegxeLoEziU7ZJjnVBS0ihYfk46Gfp8WSibyteNtShvfAYNq6EB/crwGABZJDy/sf8zROnpglPiAOfETSmcBR1AsFdIPcGvIg5MpX3FM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711881130; c=relaxed/simple; bh=Hz19z7itqegC+shE3bV/dTavEDSfDgFQ5oeIBnHGcdw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=hST/B8GaCO9CVJw/1V4H5OsSsn7LoUs0zspWTN1ddb/S531LAdq+w+kcTlg83/ea6e/PdSAcEyyon1Sbxb3DQAUXxhI5fRp8LVA7jfP/2iq3QQ95m89NtuuSximuqHLLOoPDHQ0RI0SQhKA7b6O/cxFjN9+7+XTCIMFPFNuWBzI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711881128; 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=ai51WJAX49Y/TQVKHW/I/8G7rRn+iR9Q2V06H0tPAkg5iK2/lvajNAH6PHC2oL3PhEtcC/ R1/e/MnhFSqzTCBzmnZETVnQEe/+ZWqw6xZCg3kjWkyCz5myLIhpa4hUiombHTKZpOxlMi T2rp0P0jJ2M8ZAzsBnjzFLUhNr931TM= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-360-TB7VPmV_Osu_rTT7HWtsJA-1; Sun, 31 Mar 2024 06:32:07 -0400 X-MC-Unique: TB7VPmV_Osu_rTT7HWtsJA-1 Received: by mail-ej1-f69.google.com with SMTP id a640c23a62f3a-a4921e09cebso126139766b.2 for ; Sun, 31 Mar 2024 03:32:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711881125; x=1712485925; 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=DYeFzR2pfmR0s+BEeIF6KZXlGdvKhJ6PA7IRxyf3jEaJNJ1ySic/MCWk89ex/ZV2Nw vCayiTpqbk7L0U9A7NOvvt1Q+I6lIhsnSm6EF2EqwHvjT/R5fIu+K/gHefrFNotz1bBs vGfq5vmrD0xLVkUDPM8JyxfI9gfO64MILByIC45rUF41Ak59onh3jDeYiahFejz3Src0 MFoLAdQcaCMEXSEY88XulJvucBaVlEMHZthQCU+fpqF90Qgo7b3UDJuuk/h1vCi7i18O cMMR/fHCDDgOT3ASJBm3pz7ECEqpbwMLP7Zru97Islfb6C7IshvGkW48yWrMF7bFME4L OftA== X-Gm-Message-State: AOJu0YwkAh0/nY7TMKd88P8AH6UZOl/ykugR2hm7WBJYPOKH+D/TjTBF PA84o8tUJsy5th+q/1CYYgvL71XGEM6mF8U7BcRXCsDp/zc3eREggQYk+CoBDQgygj7kIV8NuxM qFOIUVaZEnhT1PAUTB0q2t5Ztkz8hz45TwBeqehVPRarPT2fAq8mYMHwgZYUuKNtjoexnISxywJ rs2FGMAV6GPgnKBRQcQR+CqpUMVNxj6Xhnnq81THSBzxw= X-Received: by 2002:a17:906:883:b0:a46:967b:7c94 with SMTP id n3-20020a170906088300b00a46967b7c94mr3841593eje.18.1711881125031; Sun, 31 Mar 2024 03:32:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHPwnjvCQBAF8ofxTLBNGIYhM6EZSwqUlvAMczTBmYAOc3iWtvDZzlXqtUX34c5QrGbypuqvQ== X-Received: by 2002:a17:906:883:b0:a46:967b:7c94 with SMTP id n3-20020a170906088300b00a46967b7c94mr3841578eje.18.1711881124525; Sun, 31 Mar 2024 03:32:04 -0700 (PDT) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id xg6-20020a170907320600b00a4e38a06c86sm2700489ejb.66.2024.03.31.03.32.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Mar 2024 03:32:04 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv10 6/9] gdb: create new is_thread_id helper function Date: Sun, 31 Mar 2024 11:31:50 +0100 Message-Id: <8d6069c0947f40069573ce120f9df617c03a4210.1711880946.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.9 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 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: 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