From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id uiiVHtG9HGBoEwAAWB0awg (envelope-from ) for ; Thu, 04 Feb 2021 22:38:57 -0500 Received: by simark.ca (Postfix, from userid 112) id 6D2251EFCB; Thu, 4 Feb 2021 22:38:57 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_NONE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.2 Received: from sourceware.org (unknown [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 179951E945 for ; Thu, 4 Feb 2021 22:38:57 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B637B39DE076; Fri, 5 Feb 2021 03:38:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B637B39DE076 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1612496336; bh=bdvq9kOh4O/Lc7M8oc+wR0kGhx8H5gx+ugGR1QuBH/8=; h=Subject:To:References:Date:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=M9I9SjJxhHcY68HlaL13oLtTbdTGwcp9lQhJfnSbFiV7fGQsLU9rmeQGYBSqeEe5y 5yvrlkDlp6XfCIpnCqR23V6m8j+B3dNBN0QuF/h0bf3GZYs46CJnv4QVekhwCNm9As Tl9GnIeVcqBU2mC7GXFSWhAhFywcj33fl8TqOFvE= Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id 101E439DE074 for ; Fri, 5 Feb 2021 03:38:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 101E439DE074 Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 1153cldX022339 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 4 Feb 2021 22:38:52 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 1153cldX022339 Received: from [10.0.0.11] (192-222-157-6.qc.cable.ebox.net [192.222.157.6]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id AC2FA1E945; Thu, 4 Feb 2021 22:38:47 -0500 (EST) Subject: Re: [PATCH v5 14/25] Refactor parsing of /proc//smaps To: Luis Machado , gdb-patches@sourceware.org References: <20210127202112.2485702-1-luis.machado@linaro.org> <20210127202112.2485702-15-luis.machado@linaro.org> Message-ID: Date: Thu, 4 Feb 2021 22:38:47 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: <20210127202112.2485702-15-luis.machado@linaro.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Fri, 5 Feb 2021 03:38:47 +0000 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Simon Marchi via Gdb-patches Reply-To: Simon Marchi Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" > +/* See linux-tdep.h. */ > + > +bool > +linux_address_in_memtag_page (CORE_ADDR address) > +{ > + if (current_inferior ()->fake_pid_p) > + return false; > + > + pid_t pid = current_inferior ()->pid; > + > + std::string smaps_file = string_printf ("/proc/%d/smaps", pid); > + > + gdb::unique_xmalloc_ptr data > + = target_fileio_read_stralloc (NULL, smaps_file.c_str ()); > + > + if (data == nullptr) > + return false; > + > + std::vector smaps; > + > + /* Parse the contents of smaps into a vector. */ > + smaps = parse_smaps_data (data.get (), smaps_file); Declare and assign the vector directly: std::vector smaps = ...; > + > + for (const smaps_data &map : smaps) > + { > + /* Is the address within [start_address, end_address) in a page > + mapped with memory tagging? */ > + if (address >= map.start_address > + && address < map.end_address > + && map.vmflags.memory_tagging) > + return true; > + } > + > + return false; > +} > + > /* List memory regions in the inferior for a corefile. */ > > static int > @@ -1319,137 +1521,50 @@ linux_find_memory_regions_full (struct gdbarch *gdbarch, > /* Older Linux kernels did not support /proc/PID/smaps. */ > maps_filename = string_printf ("/proc/%d/maps", pid); > data = target_fileio_read_stralloc (NULL, maps_filename.c_str ()); > - } > - > - if (data != NULL) > - { > - char *line, *t; > - > - line = strtok_r (data.get (), "\n", &t); > - while (line != NULL) > - { > - ULONGEST addr, endaddr, offset, inode; > - const char *permissions, *device, *filename; > - struct smaps_vmflags v; > - size_t permissions_len, device_len; > - int read, write, exec, priv; > - int has_anonymous = 0; > - int should_dump_p = 0; > - int mapping_anon_p; > - int mapping_file_p; > - > - memset (&v, 0, sizeof (v)); > - read_mapping (line, &addr, &endaddr, &permissions, &permissions_len, > - &offset, &device, &device_len, &inode, &filename); > - mapping_anon_p = mapping_is_anonymous_p (filename); > - /* If the mapping is not anonymous, then we can consider it > - to be file-backed. These two states (anonymous or > - file-backed) seem to be exclusive, but they can actually > - coexist. For example, if a file-backed mapping has > - "Anonymous:" pages (see more below), then the Linux > - kernel will dump this mapping when the user specified > - that she only wants anonymous mappings in the corefile > - (*even* when she explicitly disabled the dumping of > - file-backed mappings). */ > - mapping_file_p = !mapping_anon_p; > - > - /* Decode permissions. */ > - read = (memchr (permissions, 'r', permissions_len) != 0); > - write = (memchr (permissions, 'w', permissions_len) != 0); > - exec = (memchr (permissions, 'x', permissions_len) != 0); > - /* 'private' here actually means VM_MAYSHARE, and not > - VM_SHARED. In order to know if a mapping is really > - private or not, we must check the flag "sh" in the > - VmFlags field. This is done by decode_vmflags. However, > - if we are using a Linux kernel released before the commit > - 834f82e2aa9a8ede94b17b656329f850c1471514 (3.10), we will > - not have the VmFlags there. In this case, there is > - really no way to know if we are dealing with VM_SHARED, > - so we just assume that VM_MAYSHARE is enough. */ > - priv = memchr (permissions, 'p', permissions_len) != 0; > - > - /* Try to detect if region should be dumped by parsing smaps > - counters. */ > - for (line = strtok_r (NULL, "\n", &t); > - line != NULL && line[0] >= 'A' && line[0] <= 'Z'; > - line = strtok_r (NULL, "\n", &t)) > - { > - char keyword[64 + 1]; > > - if (sscanf (line, "%64s", keyword) != 1) > - { > - warning (_("Error parsing {s,}maps file '%s'"), > - maps_filename.c_str ()); > - break; > - } > + if (data == nullptr) > + return 1; > + } > > - if (strcmp (keyword, "Anonymous:") == 0) > - { > - /* Older Linux kernels did not support the > - "Anonymous:" counter. Check it here. */ > - has_anonymous = 1; > - } > - else if (strcmp (keyword, "VmFlags:") == 0) > - decode_vmflags (line, &v); > + std::vector smaps; > > - if (strcmp (keyword, "AnonHugePages:") == 0 > - || strcmp (keyword, "Anonymous:") == 0) > - { > - unsigned long number; > - > - if (sscanf (line, "%*s%lu", &number) != 1) > - { > - warning (_("Error parsing {s,}maps file '%s' number"), > - maps_filename.c_str ()); > - break; > - } > - if (number > 0) > - { > - /* Even if we are dealing with a file-backed > - mapping, if it contains anonymous pages we > - consider it to be *also* an anonymous > - mapping, because this is what the Linux > - kernel does: > - > - // Dump segments that have been written to. > - if (vma->anon_vma && FILTER(ANON_PRIVATE)) > - goto whole; > - > - Note that if the mapping is already marked as > - file-backed (i.e., mapping_file_p is > - non-zero), then this is a special case, and > - this mapping will be dumped either when the > - user wants to dump file-backed *or* anonymous > - mappings. */ > - mapping_anon_p = 1; > - } > - } > - } > + /* Parse the contents of smaps into a vector. */ > + smaps = parse_smaps_data (data.get (), maps_filename.c_str ()); Likewise here. > > - if (has_anonymous) > - should_dump_p = should_dump_mapping_p (filterflags, &v, priv, > - mapping_anon_p, > - mapping_file_p, > - filename, addr, offset); > - else > - { > - /* Older Linux kernels did not support the "Anonymous:" counter. > - If it is missing, we can't be sure - dump all the pages. */ > - should_dump_p = 1; > - } > + for (const struct smaps_data map : smaps) You are missing the & to make it a reference (otherwise it makes a copy of each item as it iterates, I don't think you want that. Otherwise, LGTM. Simon