From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from omta34.uswest2.a.cloudfilter.net (omta34.uswest2.a.cloudfilter.net [35.89.44.33]) by sourceware.org (Postfix) with ESMTPS id D62873858D38 for ; Tue, 6 Feb 2024 18:20:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D62873858D38 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D62873858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=35.89.44.33 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707243629; cv=none; b=V1LplYsV3JiPgZem1kSC/c/IhmoF7Z5uWQeFas1yE/GhCqz/Rm/3/m8LYCKecJHs3fWWWzfXYl7dwgbiy/Da0jN6i2cjtiCs/S1/nnHT2IC85UvU4gKP95pce+fIYMl5Tfca9CkAlyALZ+AsfScZGwyJrybMrqwA9vlrZNCgncU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707243629; c=relaxed/simple; bh=6n2PL8ZNj2j0dRAzbfskkMNfVA6X0XW2njdwEHmfSPM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=JRPXcU7KruGiN18PtkbG6phkHgSwlxbWByx3JRpxyPvvavlR7jP00S9qLg8YWW5enaRQ8kUHbdw0P49MuRQIkoQq1EtrQjNpbXrJ4tyM/8sbEgkVrHwFItPD3AvFe52Nb2Fiw46kElvKRV0MHECjRaKa2RgQAih+lefeR8J3qIg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from eig-obgw-6003a.ext.cloudfilter.net ([10.0.30.151]) by cmsmtp with ESMTPS id XKpjre1M2pUFLXQ3Wr87B7; Tue, 06 Feb 2024 18:20:26 +0000 Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTPS id XQ3VrE4kj0FMeXQ3Vrva20; Tue, 06 Feb 2024 18:20:25 +0000 X-Authority-Analysis: v=2.4 cv=BoQ+wpX5 c=1 sm=1 tr=0 ts=65c27869 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=k7vzHIieQBIA:10 a=Qbun_eYptAEA:10 a=pGLkceISAAAA:8 a=KImqfMotyLxXksDqe_IA:9 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Type:MIME-Version:Message-ID:In-Reply-To:Date:References :Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=CS7QyyO9683T7XoxQ5I69kGtGnk5OEWdqtIaxbmZ75I=; b=gqolvRX8ZFfn4kRCzwcWKckKBI diVCgyvSrlMqmRrhv/gaIdhQXmgJEqjXIpETQGumgI0OLwLUaR6SttjaqTWEt+0xiLU0Ux8D2z6Fo 7VI2LrCqtyOn4vN1zUXYSx43D; Received: from 97-122-68-157.hlrn.qwest.net ([97.122.68.157]:47350 helo=murgatroyd) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1rXQ3U-000qmm-2E; Tue, 06 Feb 2024 11:20:24 -0700 From: Tom Tromey To: Matheus Branco Borella Cc: gdb-patches@sourceware.org, eli@gnu.org Subject: Re: [PATCH v4] Add support for creating new types from the Python API References: <20240116045439.171157-1-dark.ryu.550@gmail.com> X-Attribution: Tom Date: Tue, 06 Feb 2024 11:20:23 -0700 In-Reply-To: <20240116045439.171157-1-dark.ryu.550@gmail.com> (Matheus Branco Borella's message of "Tue, 16 Jan 2024 01:54:40 -0300") Message-ID: <871q9pe9p4.fsf@tromey.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 97.122.68.157 X-Source-L: No X-Exim-ID: 1rXQ3U-000qmm-2E X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 97-122-68-157.hlrn.qwest.net (murgatroyd) [97.122.68.157]:47350 X-Source-Auth: tom+tromey.com X-Email-Count: 2 X-Org: HG=bhshared;ORG=bluehost; X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfD7+UCrl7VJLLV9vGgbDxbxA5JQXfMhnY8udHsxH4yHaPhAbBiPteWAvhQdXglPFaG6piiPQ0UnaOX/PQLzhtwhFaZFp2lmczlNAEoU4Ue0HPaHzQecB sepxrl0sF38cIODCcyCigdDXywUJuwj61DSCiVkDx0Da/2XX/X7JPh1LlQYoCFVjsCTwu6px/3uGORGnrMY/XIDJqh8PtwbwYjQ= X-Spam-Status: No, score=-3016.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,JMQ_SPF_NEUTRAL,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: >>>>> "Matheus" == Matheus Branco Borella writes: Matheus> The main drawback of using the `init_*_type` family over implementing type Matheus> initialization by hand is that any type that's created gets immediately Matheus> allocated on its owner's obstack, regardless of what its real lifetime Matheus> requirements are. The main implication of this is that types that become Matheus> unreachable will remain live for the lifetime of the owner. Yeah. gdb leaks a lot of types this way, actually. We've collectively put off implementing "type GC", though I do think there's a bug for it. Matheus> + ** Functions that allow creation of instances of gdb.Type, and a new Matheus> + class gdb.FloatFormat that may be used to create floating point Matheus> + types. The functions that allow new type creation are: Matheus> + - gdb.init_type: Create a new type given a type code. Matheus> + - gdb.init_integer_type: Create a new integer type. Matheus> + - gdb.init_character_type: Create a new character type. Matheus> + - gdb.init_boolean_type: Create a new boolean type. Matheus> + - gdb.init_float_type: Create a new floating point type. Matheus> + - gdb.init_decfloat_type: Create a new decimal floating point type. Matheus> + - gdb.can_create_complex_type: Whether a type can be used to create a Matheus> + new complex type. Matheus> + - gdb.init_complex_type: Create a new complex type. Matheus> + - gdb.init_pointer_type: Create a new pointer type. Matheus> + * This allows creating pointers of arbitrary size. Matheus> + - gdb.init_fixed_point_type: Create a new fixed point type. I don't really love the "init_" prefixes here. Like, I get that these are the names internally, but I don't think they really make sense externally. WDYT about "make_" instead? Matheus> +@findex gdb.init_type Matheus> +@defun gdb.init_type (owner, type_code, bit_size, name) Matheus> +This function creates a new @code{gdb.Type} instance corresponding to a Matheus> +type owned by the given @var{owner}, with the given @var{type_code}, Matheus> +@var{name} and size. Matheus> + Matheus> +@var{owner} must be a reference to either a @code{gdb.Objfile} or a Matheus> +@code{gdb.Architecture} object. These correspond to objfile and Matheus> +architecture-owned types, respectively. Matheus> + Matheus> +@var{type_code} is one of the @code{TYPE_CODE_} constants defined in Matheus> +@ref{Types In Python}. Matheus> + Matheus> +@var{bit_size} is the size of instances of the newly created type, in Matheus> +bits. Currently, accepted values are limited to multiples of 8. Matheus> +@end defun Making any sort of type without filling in the details is probably a recipe for crashes. Is there a specific situation you needed this for? Matheus> +@findex gdb.init_pointer_type Matheus> +@defun gdb.init_pointer_type (owner, target, bit_size, name) Matheus> +This function creates a new @code{gdb.Type} instance corresponding to a Matheus> +pointer type that points to @var{target} and is owned by the given Matheus> +@var{owner}, with the given @var{name} and size. Matheus> + Matheus> +@var{target} is a @code{gdb.Type} object, corresponding to the type Matheus> +that will be pointed to by the newly created pointer type. Matheus> +@end defun I'm curious whether this one is really needed, because gdb.Type.pointer() exists. Like is there a case where you'd want a pointer type that doesn't match the architecture somehow? Seems weird and/or not useful. Matheus> +/* Converts from a Python integer to a unsigned integer. */ Matheus> + Matheus> +static bool Matheus> +py_to_unsigned_int (PyObject *object, unsigned int *val) Matheus> +{ Matheus> + if (!PyObject_IsInstance (object, (PyObject*) &PyLong_Type)) Matheus> + { Matheus> + PyErr_SetString (PyExc_TypeError, "value must be an integer"); Matheus> + return false; Matheus> + } Matheus> + Matheus> + long native_val = PyLong_AsLong (object); Matheus> + if (native_val > (long) UINT_MAX) Matheus> + { Matheus> + PyErr_SetString (PyExc_ValueError, "value is too large"); Matheus> + return false; Matheus> + } Matheus> + if (native_val < 0) Matheus> + { Matheus> + PyErr_SetString (PyExc_ValueError, Matheus> + "value must not be smaller than zero"); Matheus> + return false; Matheus> + } Matheus> + Matheus> + *val = (unsigned int) native_val; Matheus> + return true; See gdb_py_int_as_long. I think the type-check isn't really needed (probably) and some of the other error-handling can be simplified. There's also gdb_py_long_as_ulongest. Matheus> +/* Functionality for creating new types accessible from python. Matheus> + Matheus> + Copyright (C) 2008-2023 Free Software Foundation, Inc. Forgot to mention this elsewhere but I think these dates are wrong. Matheus> +/* An abstraction covering the objects types that can own a type object. */ Matheus> + Matheus> +class type_storage_owner Matheus> +{ Matheus> +public: Matheus> + /* Creates a new type owner from the given python object. If the object is Matheus> + * of a type that is not supported, the newly created instance will be Matheus> + * marked as invalid and nothing should be done with it. */ Matheus> + Matheus> + type_storage_owner (PyObject *owner) Matheus> + { Matheus> + if (gdbpy_is_architecture (owner)) Matheus> + { Matheus> + this->kind = owner_kind::arch; Matheus> + this->owner.arch = arch_object_to_gdbarch (owner); Matheus> + return; Matheus> + } Matheus> + Matheus> + this->kind = owner_kind::objfile; Matheus> + this->owner.objfile = objfile_object_to_objfile (owner); Matheus> + if (this->owner.objfile != nullptr) Matheus> + return; It seems like this could all just create a type_allocator directly and be simpler, like the 'kind' isn't needed. Matheus> + Matheus> + this->kind = owner_kind::none; Matheus> + PyErr_SetString(PyExc_TypeError, "unsupported owner type"); Spaces before parens in a lot of spots... Matheus> + /* Should never be reached, but it's better to fail in a safe way than try Matheus> + * to instance the allocator with arbitraty parameters here. */ Matheus> + abort (); gdb uses gdb_assert_not_reached instead. Matheus> + /* Get a reference to the owner's obstack. */ Matheus> + Matheus> + obstack *get_obstack () Matheus> + { I think the uses of this could probably use TYPE_ALLOC instead. Matheus> + struct gdbarch *get_arch () Matheus> + { This could use the type allocator's arch. Matheus> + enum class owner_kind { arch, objfile, none }; Is the none case really possible? It might be better to just throw an exception from the constructor or during argument validation or something like that. thanks, Tom