comp.lang.ada
 help / color / mirror / Atom feed
* Arm - ravenscar - exceptions - last chance handler
@ 2015-05-30 14:26 jan.de.kruyf
  2015-05-30 14:53 ` Jacob Sparre Andersen
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: jan.de.kruyf @ 2015-05-30 14:26 UTC (permalink / raw)



Could someone interprete this code for me (from the runtime) and advise

-------------------------------
package body Ada.Exceptions is

   ---------------------
   -- Raise_Exception --
   ---------------------

   procedure Raise_Exception (E : Exception_Id; Message : String := "") is
      pragma Unreferenced (E);

      procedure Last_Chance_Handler (Msg : System.Address; Line : Integer);
      pragma Import (C, Last_Chance_Handler, "__gnat_last_chance_handler");
      pragma No_Return (Last_Chance_Handler);

   begin
      Last_Chance_Handler (Message'Address, 0);
   end Raise_Exception;

end Ada.Exceptions;
--------------------------------

How is last chance handler going to know the --length-- of the message?

because I like to issue a printk before I die when in the linux kernel.

I have used last chance handler in gdb by looking at the memory, but kernel modules are not debugged that way.

If we say that I have to append an ASCII.NUL when I raise an exception then it runs havoc with the other runtime modules where exceptions are used.

Thanks,

j.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Arm - ravenscar - exceptions - last chance handler
  2015-05-30 14:26 Arm - ravenscar - exceptions - last chance handler jan.de.kruyf
@ 2015-05-30 14:53 ` Jacob Sparre Andersen
  2015-05-30 18:48   ` jan.de.kruyf
  2015-05-30 15:31 ` Bob Duff
  2015-05-30 15:43 ` Simon Wright
  2 siblings, 1 reply; 11+ messages in thread
From: Jacob Sparre Andersen @ 2015-05-30 14:53 UTC (permalink / raw)


jan.de.kruyf@gmail.com writes:

>    procedure Raise_Exception (E : Exception_Id; Message : String := "") is
>       pragma Unreferenced (E);
>
>       procedure Last_Chance_Handler (Msg : System.Address; Line : Integer);
>       pragma Import (C, Last_Chance_Handler, "__gnat_last_chance_handler");
>       pragma No_Return (Last_Chance_Handler);
>
>    begin
>       Last_Chance_Handler (Message'Address, 0);
>    end Raise_Exception;

> How is last chance handler going to know the --length-- of the
> message?

Because it knows where 'First and 'Last of a string are stored
relatively to it's 'Address.

Greetings,

Jacob
-- 
"It is very easy to get ridiculously confused about the
 tenses of time travel, but most things can be resolved
 by a sufficiently large ego."


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Arm - ravenscar - exceptions - last chance handler
  2015-05-30 14:26 Arm - ravenscar - exceptions - last chance handler jan.de.kruyf
  2015-05-30 14:53 ` Jacob Sparre Andersen
@ 2015-05-30 15:31 ` Bob Duff
  2015-05-30 16:10   ` jan.de.kruyf
  2015-05-30 15:43 ` Simon Wright
  2 siblings, 1 reply; 11+ messages in thread
From: Bob Duff @ 2015-05-30 15:31 UTC (permalink / raw)


jan.de.kruyf@gmail.com writes:

> How is last chance handler going to know the --length-- of the message?

It's a NUL-terminated string, as the comment in the spec says:

   procedure Raise_Exception (E : Exception_Id; Message : String := "");
   pragma No_Return (Raise_Exception);
   --  Unconditionally call __gnat_last_chance_handler. Message should be a
   --  null terminated string. Note that the exception is still raised even
   --  if E is the null exception id. This is a deliberate simplification for
   --  this profile (the use of Raise_Exception with a null id is very rare in
   --  any case, and this way we avoid introducing Raise_Exception_Always and
   --  we also avoid the if test in Raise_Exception).

> If we say that I have to append an ASCII.NUL when I raise an exception
> then it runs havoc with the other runtime modules where exceptions are
> used.

Why?  To raise an exception, you just say "raise Some_Exception;".
No need to fool about with ASCII.NUL, except in the last-chance
handler itself; the default version says:

         exit when Msg_Str (J) = Character'Val (0);

- Bob


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Arm - ravenscar - exceptions - last chance handler
  2015-05-30 14:26 Arm - ravenscar - exceptions - last chance handler jan.de.kruyf
  2015-05-30 14:53 ` Jacob Sparre Andersen
  2015-05-30 15:31 ` Bob Duff
@ 2015-05-30 15:43 ` Simon Wright
  2015-05-30 16:14   ` jan.de.kruyf
  2 siblings, 1 reply; 11+ messages in thread
From: Simon Wright @ 2015-05-30 15:43 UTC (permalink / raw)


jan.de.kruyf@gmail.com writes:

> Could someone interprete this code for me (from the runtime) and advise
>
>    procedure Raise_Exception (E : Exception_Id; Message : String := "") is
>       pragma Unreferenced (E);
>
>       procedure Last_Chance_Handler (Msg : System.Address; Line : Integer);
>       pragma Import (C, Last_Chance_Handler, "__gnat_last_chance_handler");
>       pragma No_Return (Last_Chance_Handler);
>
>    begin
>       Last_Chance_Handler (Message'Address, 0);
>    end Raise_Exception;
>
> end Ada.Exceptions;
> --------------------------------
>
> How is last chance handler going to know the --length-- of the message?
>
> because I like to issue a printk before I die when in the linux kernel.
>
> I have used last chance handler in gdb by looking at the memory, but
> kernel modules are not debugged that way.

FWIW [arm-eabi-]gdb doesn't handle "Msg : System.Address" at all well;
it works much better if you actually write __gnat_last_chance_handler in
C rather than in Ada with an Export:

#include <FreeRTOS.h>
#include <task.h>

__attribute__((weak))
void __gnat_last_chance_handler(const char *message, int line) {
  taskDISABLE_INTERRUPTS();
  vTaskSuspendAll();
  // Loop indefinitely: use the debugger to examine the backtrace.
  while (1) {}
}

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Arm - ravenscar - exceptions - last chance handler
  2015-05-30 15:31 ` Bob Duff
@ 2015-05-30 16:10   ` jan.de.kruyf
  2015-05-30 16:50     ` Simon Wright
  0 siblings, 1 reply; 11+ messages in thread
From: jan.de.kruyf @ 2015-05-30 16:10 UTC (permalink / raw)


On Saturday, May 30, 2015 at 5:31:41 PM UTC+2, Bob Duff wrote:

> 
> > How is last chance handler going to know the --length-- of the message?
> 
> It's a NUL-terminated string, as the comment in the spec says:
> 
>    procedure Raise_Exception (E : Exception_Id; Message : String := "");
>    pragma No_Return (Raise_Exception);
>    --  Unconditionally call __gnat_last_chance_handler. Message should be a
>    --  null terminated string. Note that the exception is still raised even
>    --  if E is the null exception id. This is a deliberate simplification for
>    --  this profile (the use of Raise_Exception with a null id is very rare in
>    --  any case, and this way we avoid introducing Raise_Exception_Always and
>    --  we also avoid the if test in Raise_Exception).
> 
> > If we say that I have to append an ASCII.NUL when I raise an exception
> > then it runs havoc with the other runtime modules where exceptions are
> > used.
> 
> Why?  To raise an exception, you just say "raise Some_Exception;".
> No need to fool about with ASCII.NUL, except in the last-chance
> handler itself; the default version says:
> 
>          exit when Msg_Str (J) = Character'Val (0);
> 
> - Bob

Yes bob,
I saw that NUL terminated part just after I posted, so I tried to delete the post, but that is hard :) apparently.

In any case there is a more serious issue I think (in ravenscar).
I get unresolved symbols in a partial link caused by "raise exeption". 
(partial link since this is a kernel module)

The unresolved symbols point to the full native (linux) runtime.
So I replaces the "raise" statements in the ravenscar runtime packages I use with an explicit 
Ada.Exceptions.Raise_Exception (...);  (this then from the ravenscar profile)
which I understand to be the recommendation from the Exeptions package's comment.

That works, but some of the runtime packages are marked "pure" so they cannot depend on Ada.Exceptions.

There is still another issue, but I will post that when I am finished. If needed.

Peace,

j.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Arm - ravenscar - exceptions - last chance handler
  2015-05-30 15:43 ` Simon Wright
@ 2015-05-30 16:14   ` jan.de.kruyf
  0 siblings, 0 replies; 11+ messages in thread
From: jan.de.kruyf @ 2015-05-30 16:14 UTC (permalink / raw)


On Saturday, May 30, 2015 at 5:43:38 PM UTC+2, Simon Wright wrote:

> 
> > Could someone interprete this code for me (from the runtime) and advise
> >
> >    procedure Raise_Exception (E : Exception_Id; Message : String := "") is
> >       pragma Unreferenced (E);
> >
> >       procedure Last_Chance_Handler (Msg : System.Address; Line : Integer);
> >       pragma Import (C, Last_Chance_Handler, "__gnat_last_chance_handler");
> >       pragma No_Return (Last_Chance_Handler);
> >
> >    begin
> >       Last_Chance_Handler (Message'Address, 0);
> >    end Raise_Exception;
> >
> > end Ada.Exceptions;
> > --------------------------------
> >
> > How is last chance handler going to know the --length-- of the message?
> >
> > because I like to issue a printk before I die when in the linux kernel.
> >
> > I have used last chance handler in gdb by looking at the memory, but
> > kernel modules are not debugged that way.
> 
> FWIW [arm-eabi-]gdb doesn't handle "Msg : System.Address" at all well;
> it works much better if you actually write __gnat_last_chance_handler in
> C rather than in Ada with an Export:
> 
> #include <FreeRTOS.h>
> #include <task.h>
> 
> __attribute__((weak))
> void __gnat_last_chance_handler(const char *message, int line) {
>   taskDISABLE_INTERRUPTS();
>   vTaskSuspendAll();
>   // Loop indefinitely: use the debugger to examine the backtrace.
>   while (1) {}
> }

Thanks, Simon.
See my answer to Bob Duff for the full story. I like your C solution, but in my case I will have to kick the bucket and unload myself. At least I believe thats what expected in linux kernel land.

cheers,

j.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Arm - ravenscar - exceptions - last chance handler
  2015-05-30 16:10   ` jan.de.kruyf
@ 2015-05-30 16:50     ` Simon Wright
  2015-05-30 20:59       ` jan.de.kruyf
  0 siblings, 1 reply; 11+ messages in thread
From: Simon Wright @ 2015-05-30 16:50 UTC (permalink / raw)


jan.de.kruyf@gmail.com writes:

> The unresolved symbols point to the full native (linux) runtime.  So I
> replaces the "raise" statements in the ravenscar runtime packages I
> use with an explicit Ada.Exceptions.Raise_Exception (...); (this then
> from the ravenscar profile) which I understand to be the
> recommendation from the Exeptions package's comment.

I have no trouble with either form. Does your system.ads include
   pragma Restrictions (No_Exception_Propagation);
?


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Arm - ravenscar - exceptions - last chance handler
  2015-05-30 14:53 ` Jacob Sparre Andersen
@ 2015-05-30 18:48   ` jan.de.kruyf
  2015-05-30 19:18     ` Simon Wright
  0 siblings, 1 reply; 11+ messages in thread
From: jan.de.kruyf @ 2015-05-30 18:48 UTC (permalink / raw)



> 
> >    procedure Raise_Exception (E : Exception_Id; Message : String := "") is
> >       pragma Unreferenced (E);
> >
> >       procedure Last_Chance_Handler (Msg : System.Address; Line : Integer);
> >       pragma Import (C, Last_Chance_Handler, "__gnat_last_chance_handler");
> >       pragma No_Return (Last_Chance_Handler);
> >
> >    begin
> >       Last_Chance_Handler (Message'Address, 0);
> >    end Raise_Exception;
> 
> > How is last chance handler going to know the --length-- of the
> > message?
> 
> Because it knows where 'First and 'Last of a string are stored
> relatively to it's 'Address.
> 
> Greetings,
> 
> Jacob
> -- 
> "It is very easy to get ridiculously confused about the
>  tenses of time travel, but most things can be resolved
>  by a sufficiently large ego."

Hello Jacob,
In the full runtime: probably.
In the Arm Ravenscar: I doubt it. And the .ads file of the above says that the user of it shall provide a null terminated string (I spotted that after I posted.)
But this has not been fixed in some of the ravenscar runtime sources.

And from experience: my Arm board just kick the bucket on an exception, afterward I learned to put a break point in the last chance handler. Then I could see the string in memory.

j.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Arm - ravenscar - exceptions - last chance handler
  2015-05-30 18:48   ` jan.de.kruyf
@ 2015-05-30 19:18     ` Simon Wright
  2015-05-30 20:57       ` jan.de.kruyf
  0 siblings, 1 reply; 11+ messages in thread
From: Simon Wright @ 2015-05-30 19:18 UTC (permalink / raw)


jan.de.kruyf@gmail.com writes:

> And from experience: my Arm board just kick the bucket on an
> exception, afterward I learned to put a break point in the last chance
> handler. Then I could see the string in memory.

Hence the loop-forever (which is also the way that FreeRTOS handles
(some) unrecoverable errors); things stop working, C-c in gdb, poke
around.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Arm - ravenscar - exceptions - last chance handler
  2015-05-30 19:18     ` Simon Wright
@ 2015-05-30 20:57       ` jan.de.kruyf
  0 siblings, 0 replies; 11+ messages in thread
From: jan.de.kruyf @ 2015-05-30 20:57 UTC (permalink / raw)


On Saturday, May 30, 2015 at 9:18:18 PM UTC+2, Simon Wright wrote:

> > And from experience: my Arm board just kick the bucket on an
> > exception, afterward I learned to put a break point in the last chance
> > handler. Then I could see the string in memory.
> 
> Hence the loop-forever (which is also the way that FreeRTOS handles
> (some) unrecoverable errors); things stop working, C-c in gdb, poke
> around.

I think it kicks the bucket because a null reference in the raise exception call
in other words it does not come to last_chance_handler in the regular way you expect. But in anycase the end effect is the same.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Arm - ravenscar - exceptions - last chance handler
  2015-05-30 16:50     ` Simon Wright
@ 2015-05-30 20:59       ` jan.de.kruyf
  0 siblings, 0 replies; 11+ messages in thread
From: jan.de.kruyf @ 2015-05-30 20:59 UTC (permalink / raw)



> > The unresolved symbols point to the full native (linux) runtime.  So I
> > replaces the "raise" statements in the ravenscar runtime packages I
> > use with an explicit Ada.Exceptions.Raise_Exception (...); (this then
> > from the ravenscar profile) which I understand to be the
> > recommendation from the Exeptions package's comment.
> 
> I have no trouble with either form. Does your system.ads include
>    pragma Restrictions (No_Exception_Propagation);
> ?

ow that a good point, I might have fiddled with it. let me check next week.

thanks,

j

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2015-05-30 20:59 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-30 14:26 Arm - ravenscar - exceptions - last chance handler jan.de.kruyf
2015-05-30 14:53 ` Jacob Sparre Andersen
2015-05-30 18:48   ` jan.de.kruyf
2015-05-30 19:18     ` Simon Wright
2015-05-30 20:57       ` jan.de.kruyf
2015-05-30 15:31 ` Bob Duff
2015-05-30 16:10   ` jan.de.kruyf
2015-05-30 16:50     ` Simon Wright
2015-05-30 20:59       ` jan.de.kruyf
2015-05-30 15:43 ` Simon Wright
2015-05-30 16:14   ` jan.de.kruyf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox