It's useful to set task/thread's name for debugging.
Under Linux, there is a prctl() system call to do that. Note that we don't
want to use pthread_setname_np since only very recent glibc (>= 2.12) provide
this function.

Tested on x86_64-pc-linux-gnu, committed on trunk

2012-03-07  Arnaud Charlet  <char...@adacore.com>

        * s-osinte-linux.ads, s-taprop-linux.adb (prctl): New function.
        (Enter_Task): Call prctl when relevant.

Index: s-osinte-linux.ads
===================================================================
--- s-osinte-linux.ads  (revision 185051)
+++ s-osinte-linux.ads  (working copy)
@@ -7,7 +7,7 @@
 --                                  S p e c                                 --
 --                                                                          --
 --             Copyright (C) 1991-1994, Florida State University            --
---          Copyright (C) 1995-2011, Free Software Foundation, Inc.         --
+--          Copyright (C) 1995-2012, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -255,6 +255,12 @@
    function getpid return pid_t;
    pragma Import (C, getpid, "getpid");
 
+   PR_SET_NAME : constant := 15;
+
+   function prctl
+     (option : int; arg2, arg3, arg4, arg5 : unsigned_long := 0) return int;
+   pragma Import (C, prctl);
+
    -------------
    -- Threads --
    -------------
Index: s-taprop-linux.adb
===================================================================
--- s-taprop-linux.adb  (revision 185051)
+++ s-taprop-linux.adb  (working copy)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                  B o d y                                 --
 --                                                                          --
---         Copyright (C) 1992-2011, Free Software Foundation, Inc.          --
+--         Copyright (C) 1992-2012, Free Software Foundation, Inc.          --
 --                                                                          --
 -- GNARL is free software; you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -767,6 +767,22 @@
       Self_ID.Common.LL.Thread := pthread_self;
       Self_ID.Common.LL.LWP := lwp_self;
 
+      if Self_ID.Common.Task_Image_Len > 0 then
+         declare
+            Task_Name : String (1 .. Parameters.Max_Task_Image_Length + 1);
+            Result    : int;
+         begin
+            --  Set thread name to ease debugging
+
+            Task_Name (1 .. Self_ID.Common.Task_Image_Len) :=
+              Self_ID.Common.Task_Image (1 .. Self_ID.Common.Task_Image_Len);
+            Task_Name (Self_ID.Common.Task_Image_Len + 1) := ASCII.NUL;
+
+            Result := prctl (PR_SET_NAME, unsigned_long (Task_Name'Address));
+            pragma Assert (Result = 0);
+         end;
+      end if;
+
       Specific.Set (Self_ID);
 
       if Use_Alternate_Stack

Reply via email to