From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4766 invoked by alias); 28 Mar 2017 14:09:00 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 4755 invoked by uid 89); 28 Mar 2017 14:08:59 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=reaction, engineered, she X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 28 Mar 2017 14:08:54 +0000 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 39DC97AEB8; Tue, 28 Mar 2017 14:08:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 39DC97AEB8 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=palves@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 39DC97AEB8 Received: from [127.0.0.1] (ovpn04.gateway.prod.ext.phx2.redhat.com [10.5.9.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id 49D4297A85; Tue, 28 Mar 2017 14:08:52 +0000 (UTC) Subject: extract_unsigned_integer API (Re: [PATCH] Remove MAX_REGISTER_SIZE from frame.c) To: Yao Qi , Alan Hayward References: <86lgspqisk.fsf@gmail.com> Cc: "gdb-patches@sourceware.org" , nd From: Pedro Alves Message-ID: <5f2f0cb0-6265-46aa-4ad6-eda5ba817da4@redhat.com> Date: Tue, 28 Mar 2017 14:09:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 In-Reply-To: <86lgspqisk.fsf@gmail.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-SW-Source: 2017-03/txt/msg00483.txt.bz2 Hi Yao, I didn't notice your patch/question until now. See below. On 03/01/2017 12:32 PM, Yao Qi wrote: > Alan Hayward writes: > >> @@ -1252,7 +1252,11 @@ frame_unwind_register_signed (struct frame_info *frame, int regnum) >> struct gdbarch *gdbarch = frame_unwind_arch (frame); >> enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); >> int size = register_size (gdbarch, regnum); >> - gdb_byte buf[MAX_REGISTER_SIZE]; >> + gdb_byte buf[sizeof (LONGEST)]; >> + >> + if (size > (int) sizeof (LONGEST)) >> + error (_("Cannot unwind integers more than %d bytes."), >> + (int) sizeof (LONGEST)); >> > > We apply the restriction of extract_signed_integer to its caller here. > People will wonder why do we have such check until he/she digs into > extract_signed_integer. My first reaction is to add some comments to > explain why do we do so, but the recent gdb::function_view reminds me > that we can do something differently (and better, IMO). > > Current pattern of using extract_unsigned_integer is > > 1) allocate an array on stack, > 2) read data from regcache or frame into the array, > 3) pass the array to extract_unsigned_integer > > we can pass a callable function_view as a content provider to > extract_unsigned_integer, so that we don't need step 1). The code > becomes, > > return extract_unsigned_integer ([&] (gdb_byte *buf, size_t size) > { > frame_unwind_register (frame, regnum, buf); > }, size, byte_order); > > We can remove some uses of MAX_REGISTER_SIZE in this way. Do you (Alan > and others) like the patch below? This looks a bit over engineered to me. If extract_unsigned_integer always creates a local buffer inside, and it's always going to be a buffer the size of a LONGEST, because that's the type that extract_unsigned_integer returns, then, I'd think that hiding the buffer size and the extract_unsigned_integer call in a class instead would do. Like: class extractor { public: extractor () = default; // Get buffer. Could take a "size" parameter too, // for pre-validation instead of passing "size" to "extract". // Or make that a separate size() method. Or add a "size" parameter // to the ctor and validate there. Whatever. The lambda-based // solution isn't validating upfront either. gdb_byte *buffer () { return m_buffer; } // Do extraction. LONGEST extract (size_t size, bfd_endian byte_order); private: gdb_byte m_buffer[sizeof (LONGEST)]; }; LONGEST extractor::extract (size_t size, bfd_endian byte_order) { if (size > sizeof (LONGEST)) error (_("\ That operation is not available on integers larger than %d bytes."), sizeof (LONGEST)); return extract_unsigned_integer (m_buffer, size, byte_order); } And then used like: extractor extr; frame_unwind_register (frame, regnum, ext.buffer ()); return extr.extract (size, byte_order); Instead of: return extract_unsigned_integer ([&] (gdb_byte *buf, size_t size) { frame_unwind_register (frame, regnum, buf); }, size, byte_order); Thanks, Pedro Alves