public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Marc Poulhiès" <poulhies@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: Eric Botcazou <ebotcazou@adacore.com>
Subject: [COMMITTED 03/30] ada: Add support for symbolic backtraces with DLLs on Windows
Date: Thu, 13 Jun 2024 15:33:09 +0200	[thread overview]
Message-ID: <20240613133338.1809385-3-poulhies@adacore.com> (raw)
In-Reply-To: <20240613133338.1809385-1-poulhies@adacore.com>

From: Eric Botcazou <ebotcazou@adacore.com>

This puts Windows on par with Linux as far as backtraces are concerned.

gcc/ada/

	* libgnat/s-tsmona__linux.adb (Get): Move down descriptive comment.
	* libgnat/s-tsmona__mingw.adb: Add with clause and use clause for
	System.Storage_Elements.
	(Get): Pass GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT in the call
	to GetModuleHandleEx and remove the subsequent call to FreeLibrary.
	Upon success, set Load_Addr to the base address of the module.
	* libgnat/s-win32.ads (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS): Use
	shorter literal.
	(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT): New constant.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/libgnat/s-tsmona__linux.adb | 34 ++++++++++++++---------------
 gcc/ada/libgnat/s-tsmona__mingw.adb | 20 ++++++++---------
 gcc/ada/libgnat/s-win32.ads         |  3 ++-
 3 files changed, 29 insertions(+), 28 deletions(-)

diff --git a/gcc/ada/libgnat/s-tsmona__linux.adb b/gcc/ada/libgnat/s-tsmona__linux.adb
index 417b57f4545..4545399017a 100644
--- a/gcc/ada/libgnat/s-tsmona__linux.adb
+++ b/gcc/ada/libgnat/s-tsmona__linux.adb
@@ -30,7 +30,8 @@
 ------------------------------------------------------------------------------
 
 --  This is the GNU/Linux specific version of this package
-with Interfaces.C;              use Interfaces.C;
+
+with Interfaces.C; use Interfaces.C;
 
 separate (System.Traceback.Symbolic)
 
@@ -41,18 +42,6 @@ package body Module_Name is
    function Is_Shared_Lib (Base : Address) return Boolean;
    --  Returns True if a shared library
 
-   --  The principle is:
-
-   --  1. We get information about the module containing the address.
-
-   --  2. We check that the full pathname is pointing to a shared library.
-
-   --  3. for shared libraries, we return the non relocated address (so
-   --     the absolute address in the shared library).
-
-   --  4. we also return the full pathname of the module containing this
-   --     address.
-
    -------------------
    -- Is_Shared_Lib --
    -------------------
@@ -139,11 +128,22 @@ package body Module_Name is
    -- Get --
    ---------
 
-   function Get (Addr : System.Address;
-                 Load_Addr : access System.Address)
-     return String
-   is
+   --  The principle is:
+
+   --  1. We get information about the module containing the address.
+
+   --  2. We check whether the module is a shared library.
 
+   --  3. For shared libraries, we return the non-relocated address (so
+   --     the absolute address in the shared library).
+
+   --  4. We also return the full pathname of the module containing this
+   --     address.
+
+   function Get
+     (Addr      : System.Address;
+      Load_Addr : access System.Address) return String
+   is
       --  Dl_info record for Linux, used to get sym reloc offset
 
       type Dl_info is record
diff --git a/gcc/ada/libgnat/s-tsmona__mingw.adb b/gcc/ada/libgnat/s-tsmona__mingw.adb
index 3100db08bbd..61264da7dfe 100644
--- a/gcc/ada/libgnat/s-tsmona__mingw.adb
+++ b/gcc/ada/libgnat/s-tsmona__mingw.adb
@@ -31,7 +31,8 @@
 
 --  This is the Windows specific version of this package
 
-with System.Win32; use System.Win32;
+with System.Storage_Elements; use System.Storage_Elements;
+with System.Win32;            use System.Win32;
 
 separate (System.Traceback.Symbolic)
 
@@ -50,27 +51,26 @@ package body Module_Name is
    -- Get --
    ---------
 
-   function Get (Addr : System.Address;
-                 Load_Addr : access System.Address)
-     return String
+   function Get
+     (Addr      : System.Address;
+      Load_Addr : access System.Address) return String
    is
       Res     : DWORD;
       hModule : aliased HANDLE;
-      Path    : String (1 .. 1_024);
+      Path    : String (1 .. 1024);
 
    begin
       Load_Addr.all := System.Null_Address;
 
       if GetModuleHandleEx
-           (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
+           (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS +
+              GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
             Addr,
             hModule'Access) = Win32.TRUE
       then
-         Res := GetModuleFileName (hModule, Path'Address, Path'Length);
+         Load_Addr.all := To_Address (Integer_Address (hModule));
 
-         if FreeLibrary (hModule) = Win32.FALSE then
-            null;
-         end if;
+         Res := GetModuleFileName (hModule, Path'Address, Path'Length);
 
          if Res > 0 then
             return Path (1 .. Positive (Res));
diff --git a/gcc/ada/libgnat/s-win32.ads b/gcc/ada/libgnat/s-win32.ads
index 6e8e246d903..963cb57b7f0 100644
--- a/gcc/ada/libgnat/s-win32.ads
+++ b/gcc/ada/libgnat/s-win32.ads
@@ -157,7 +157,8 @@ package System.Win32 is
    FILE_ATTRIBUTE_VALID_FLAGS         : constant := 16#00007fb7#;
    FILE_ATTRIBUTE_VALID_SET_FLAGS     : constant := 16#000031a7#;
 
-   GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS : constant := 16#00000004#;
+   GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS       : constant := 16#04#;
+   GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT : constant := 16#02#;
 
    type OVERLAPPED is record
       Internal     : access ULONG;
-- 
2.45.1


  parent reply	other threads:[~2024-06-13 13:33 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-13 13:33 [COMMITTED 01/30] ada: Missing dynamic predicate checks Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 02/30] ada: Fix too late finalization of temporary object Marc Poulhiès
2024-06-13 13:33 ` Marc Poulhiès [this message]
2024-06-13 13:33 ` [COMMITTED 04/30] ada: Simplify checks for Address and Object_Size clauses Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 05/30] ada: Missing support for 'Old with overloaded function Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 06/30] ada: Fix fallout of previous finalization change Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 07/30] ada: Inline if -gnatn in CCG mode even if -O0 Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 08/30] ada: Reject too-strict alignment specifications Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 09/30] ada: Fix incorrect String lower bound in gnatlink Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 10/30] ada: Do not inline subprogram which could cause SPARK violation Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 11/30] ada: Streamline elaboration of local tagged types Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 12/30] ada: Check global mode restriction on encapsulating abstract states Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 13/30] ada: Fix oversight in latest finalization fix Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 14/30] ada: Fix expansion of protected subprogram bodies Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 15/30] ada: Fix Super attribute documentation Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 16/30] ada: Interfaces order disables class-wide prefix notation calls Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 17/30] ada: List subprogram body entities in scopes Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 18/30] ada: Simplify code in Cannot_Inline Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 19/30] ada: Convert an info message to a continuation Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 20/30] ada: Remove warning insertion characters from info messages Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 21/30] ada: Remove message about goto rewritten as a loop Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 22/30] ada: Minor cleanups in generic formal matching Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 23/30] ada: Deep copy of an expression sometimes fails to copy entities Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 24/30] ada: Revert changing a GNATProve mode message to a non-warning Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 25/30] ada: Missing postcondition runtime check in inherited primitive Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 26/30] ada: Fix test for giving hint on ambiguous aggregate Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 27/30] ada: Remove Iterable from list of GNAT-specific attributes Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 28/30] ada: Fix segmentation fault on slice of array with Unbounded_String component Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 29/30] ada: Remove -gnatdJ switch Marc Poulhiès
2024-06-13 13:33 ` [COMMITTED 30/30] ada: Compiler goes into loop Marc Poulhiès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240613133338.1809385-3-poulhies@adacore.com \
    --to=poulhies@adacore.com \
    --cc=ebotcazou@adacore.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).