comp.lang.ada
 help / color / mirror / Atom feed
* Last chance handler on a PC
@ 2020-01-30  8:55 ahlan
  2020-01-30  9:15 ` J-P. Rosen
                   ` (4 more replies)
  0 siblings, 5 replies; 17+ messages in thread
From: ahlan @ 2020-01-30  8:55 UTC (permalink / raw)


Hi,

Does anyone know if it is possible to install a last chance handler for a PC program.
Ie Write a procedure that gets called when a program issues an unhandled exception
If it is possible how do you do it?

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

* Re: Last chance handler on a PC
  2020-01-30  8:55 Last chance handler on a PC ahlan
@ 2020-01-30  9:15 ` J-P. Rosen
  2020-01-30  9:17 ` Egil H H
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 17+ messages in thread
From: J-P. Rosen @ 2020-01-30  9:15 UTC (permalink / raw)


Le 30/01/2020 à 09:55, ahlan@marriott.org a écrit :
> Hi,
> 
> Does anyone know if it is possible to install a last chance handler for a PC program.
> Ie Write a procedure that gets called when a program issues an unhandled exception
> If it is possible how do you do it?
> 
Put this at the end of your main program:

exception
   when others =>
      Last_Chance;
end;

If you are using multi-tasking, you may want to consider termination
handlers, see ARM C.7.3

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr


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

* Re: Last chance handler on a PC
  2020-01-30  8:55 Last chance handler on a PC ahlan
  2020-01-30  9:15 ` J-P. Rosen
@ 2020-01-30  9:17 ` Egil H H
  2020-01-30 19:27   ` ahlan
  2020-01-30  9:25 ` Dmitry A. Kazakov
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 17+ messages in thread
From: Egil H H @ 2020-01-30  9:17 UTC (permalink / raw)


On Thursday, January 30, 2020 at 9:55:42 AM UTC+1, ah...@marriott.org wrote:
> Hi,
> 
> Does anyone know if it is possible to install a last chance handler for a PC program.
> Ie Write a procedure that gets called when a program issues an unhandled exception
> If it is possible how do you do it?


You can use the Termination_Handler in Annex C.7.3 (Added in Ada 2005), provided your compiler/runtime supports it. 

http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-C-7-3.html

Example usage is discussed in the Ada 2005 Rationale:

https://www.adaic.org/resources/add_content/standards/05rat/html/Rat-5-2.html#I1150


-- 
~egilhh


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

* Re: Last chance handler on a PC
  2020-01-30  8:55 Last chance handler on a PC ahlan
  2020-01-30  9:15 ` J-P. Rosen
  2020-01-30  9:17 ` Egil H H
@ 2020-01-30  9:25 ` Dmitry A. Kazakov
  2020-01-30 19:10 ` Lucretia
  2020-01-30 19:35 ` ahlan
  4 siblings, 0 replies; 17+ messages in thread
From: Dmitry A. Kazakov @ 2020-01-30  9:25 UTC (permalink / raw)


On 2020-01-30 09:55, ahlan@marriott.org wrote:

> Does anyone know if it is possible to install a last chance handler for a PC program.
> Ie Write a procedure that gets called when a program issues an unhandled exception
> If it is possible how do you do it?

The answer depends on what you understand under "unhandled exception" 
and what you want to do in such a state.

In practice such things have little use. Side effects of unanticipated 
[language and then system] exception propagation tend to ruin any 
relevant information and data so that at the point when the handler is 
called everything is in a total mess.

The best way I know is tracing exceptions. With GNAT you can quite 
easily hook up exception raising points when some useful information is 
still there.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


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

* Re: Last chance handler on a PC
  2020-01-30  8:55 Last chance handler on a PC ahlan
                   ` (2 preceding siblings ...)
  2020-01-30  9:25 ` Dmitry A. Kazakov
@ 2020-01-30 19:10 ` Lucretia
  2020-01-30 19:35 ` ahlan
  4 siblings, 0 replies; 17+ messages in thread
From: Lucretia @ 2020-01-30 19:10 UTC (permalink / raw)


On Thursday, 30 January 2020 08:55:42 UTC, ah...@marriott.org  wrote:
> Hi,
> 
> Does anyone know if it is possible to install a last chance handler for a PC program.
> Ie Write a procedure that gets called when a program issues an unhandled exception
> If it is possible how do you do it?

Do you want to restrict the generation of exceptions such that only Last_Chance_Handler is called for all exceptions? If so, use pragma Restrictions.


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

* Re: Last chance handler on a PC
  2020-01-30  9:17 ` Egil H H
@ 2020-01-30 19:27   ` ahlan
  2020-01-31  5:53     ` J-P. Rosen
  0 siblings, 1 reply; 17+ messages in thread
From: ahlan @ 2020-01-30 19:27 UTC (permalink / raw)


Dear Egil,

Very interesting but we want to catch all unhandled exceptions, specifically those raised during package elaboration.

MfG
Ahlan

On Thursday, January 30, 2020 at 10:17:17 AM UTC+1, Egil H H wrote:
> On Thursday, January 30, 2020 at 9:55:42 AM UTC+1, ah...@marriott.org wrote:
> > Hi,
> > 
> > Does anyone know if it is possible to install a last chance handler for a PC program.
> > Ie Write a procedure that gets called when a program issues an unhandled exception
> > If it is possible how do you do it?
> 
> 
> You can use the Termination_Handler in Annex C.7.3 (Added in Ada 2005), provided your compiler/runtime supports it. 
> 
> http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-C-7-3.html
> 
> Example usage is discussed in the Ada 2005 Rationale:
> 
> https://www.adaic.org/resources/add_content/standards/05rat/html/Rat-5-2.html#I1150
> 
> 
> -- 
> ~egilhh

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

* Re: Last chance handler on a PC
  2020-01-30  8:55 Last chance handler on a PC ahlan
                   ` (3 preceding siblings ...)
  2020-01-30 19:10 ` Lucretia
@ 2020-01-30 19:35 ` ahlan
  2020-01-30 20:02   ` Jeffrey R. Carter
  4 siblings, 1 reply; 17+ messages in thread
From: ahlan @ 2020-01-30 19:35 UTC (permalink / raw)


On Thursday, January 30, 2020 at 9:55:42 AM UTC+1, ah...@marriott.org wrote:
> Hi,
> 
> Does anyone know if it is possible to install a last chance handler for a PC program.
> Ie Write a procedure that gets called when a program issues an unhandled exception
> If it is possible how do you do it?

To answer my own question...
To catch unhandled exceptions you only need to write a simple procedure and export it as __gnat_last_chance_handler.
This is linked into the program in preference to the default last chance handler provided by GNAT.
This procedure is called if nothing catches a raised exception.
Including those raised during package elaboration.

The procedure is not allowed to return so after doing whatever it is you want to do with the exception you must call __gant_unhandled_terminate

The following is an example.

procedure Last_Chance_Handler (Occurrence : Ada.Exceptions.Exception_Occurrence)
  with
    No_Return, Unreferenced, Export,
    Convention    => C,
    External_Name => "__gnat_last_chance_handler";

  procedure Last_Chance_Handler (Occurrence : Ada.Exceptions.Exception_Occurrence) is

    procedure Unhandled_Terminate
    with
      No_Return, Import,
      Convention    => C,
      External_Name => "__gnat_unhandled_terminate";

  begin
    begin
      null;  -- Process the exception here.
    exception
    when others =>
      null;
    end;
    Unhandled_Terminate;
  end Last_Chance_Handler;

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

* Re: Last chance handler on a PC
  2020-01-30 19:35 ` ahlan
@ 2020-01-30 20:02   ` Jeffrey R. Carter
  2020-01-30 20:26     ` Niklas Holsti
  0 siblings, 1 reply; 17+ messages in thread
From: Jeffrey R. Carter @ 2020-01-30 20:02 UTC (permalink / raw)


On 1/30/20 8:35 PM, ahlan@marriott.org wrote:
> 
> To catch unhandled exceptions you only need to write a simple procedure and export it as __gnat_last_chance_handler.
> This is linked into the program in preference to the default last chance handler provided by GNAT.
> This procedure is called if nothing catches a raised exception.
> Including those raised during package elaboration.

Doing

Ada.Task_Termination.Set_Specific_Handler
    (T       => Ada.Task_Identification.Environment_Task,
     Handler => Last_Chance'access);

should do the same thing more portably. It will be called when the environment 
task terminates for any reason; you would only want it to actually do something 
when Cause = Unhandled_Exception.

-- 
Jeff Carter
"My mind is a raging torrent, flooded with rivulets of
thought, cascading into a waterfall of creative alternatives."
Blazing Saddles
89


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

* Re: Last chance handler on a PC
  2020-01-30 20:02   ` Jeffrey R. Carter
@ 2020-01-30 20:26     ` Niklas Holsti
  2020-01-30 20:51       ` Jeffrey R. Carter
  2020-01-31 11:12       ` Simon Wright
  0 siblings, 2 replies; 17+ messages in thread
From: Niklas Holsti @ 2020-01-30 20:26 UTC (permalink / raw)


On 2020-01-30 22:02, Jeffrey R. Carter wrote:
> On 1/30/20 8:35 PM, ahlan@marriott.org wrote:
>>
>> To catch unhandled exceptions you only need to write a simple 
>> procedure and export it as __gnat_last_chance_handler.
>> This is linked into the program in preference to the default last 
>> chance handler provided by GNAT.
>> This procedure is called if nothing catches a raised exception.
>> Including those raised during package elaboration.
> 
> Doing
> 
> Ada.Task_Termination.Set_Specific_Handler
>     (T       => Ada.Task_Identification.Environment_Task,
>      Handler => Last_Chance'access);
> 
> should do the same thing more portably. It will be called when the 
> environment task terminates for any reason; you would only want it to 
> actually do something when Cause = Unhandled_Exception.

Looks good, but to catch all elaboration-time exceptions (in other 
packages) the package that executes that call, in its own elaboration 
code, must be elaborated before all other packages. Do you have some 
easy way to ensure that, without inserting elaboration pragmas in all 
other packages?

I had a similar elaboration problem some time ago in an embedded 
application, where I wanted to set up some HW error-trap handlers that I 
would like to be active also during elaboration, but I found no easy way 
to ensure that the trap-handling package would be elaborated before all 
other packages.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .


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

* Re: Last chance handler on a PC
  2020-01-30 20:26     ` Niklas Holsti
@ 2020-01-30 20:51       ` Jeffrey R. Carter
  2020-01-30 21:32         ` Niklas Holsti
  2020-01-31 11:12       ` Simon Wright
  1 sibling, 1 reply; 17+ messages in thread
From: Jeffrey R. Carter @ 2020-01-30 20:51 UTC (permalink / raw)


On 1/30/20 9:26 PM, Niklas Holsti wrote:
> 
> Looks good, but to catch all elaboration-time exceptions (in other packages) the 
> package that executes that call, in its own elaboration code, must be elaborated 
> before all other packages. Do you have some easy way to ensure that, without 
> inserting elaboration pragmas in all other packages?

Of course that call has to be done before anything that might raise an exception 
during elaboration. Usually you'd put it in its own pkg, and then every other 
library-level unit in the system would with it with a pragma Elaborate_Body for 
it. If everything is part of a hierarchy, then only the spec of the root package 
of the hierarchy should need to do that.

-- 
Jeff Carter
"My mind is a raging torrent, flooded with rivulets of
thought, cascading into a waterfall of creative alternatives."
Blazing Saddles
89

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

* Re: Last chance handler on a PC
  2020-01-30 20:51       ` Jeffrey R. Carter
@ 2020-01-30 21:32         ` Niklas Holsti
  0 siblings, 0 replies; 17+ messages in thread
From: Niklas Holsti @ 2020-01-30 21:32 UTC (permalink / raw)


On 2020-01-30 22:51, Jeffrey R. Carter wrote:
> On 1/30/20 9:26 PM, Niklas Holsti wrote:
>>
>> Looks good, but to catch all elaboration-time exceptions (in other 
>> packages) the package that executes that call, in its own elaboration 
>> code, must be elaborated before all other packages. Do you have some 
>> easy way to ensure that, without inserting elaboration pragmas in all 
>> other packages?
> 
> Of course that call has to be done before anything that might raise an 
> exception during elaboration. Usually you'd put it in its own pkg, and 
> then every other library-level unit in the system would with it with a 
> pragma Elaborate_Body for it.

Yes.

> If everything is part of a hierarchy, then only the spec of the root
> package of the hierarchy should need to do that.
That's a good idea, thanks, I'll remember it for next time.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .

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

* Re: Last chance handler on a PC
  2020-01-30 19:27   ` ahlan
@ 2020-01-31  5:53     ` J-P. Rosen
  2020-01-31  7:00       ` Jeffrey R. Carter
  0 siblings, 1 reply; 17+ messages in thread
From: J-P. Rosen @ 2020-01-31  5:53 UTC (permalink / raw)


Le 30/01/2020 à 20:27, ahlan@marriott.org a écrit :
> Very interesting but we want to catch all unhandled exceptions, specifically those raised during package elaboration.
> 
Ah, OK now I see the problem. You'll need to have an exception handler
in every package body, but be careful about elaboration order!

Note: AdaControl can check that every task has an exception handler;
I'll add an analoguous check for library packages.

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr


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

* Re: Last chance handler on a PC
  2020-01-31  5:53     ` J-P. Rosen
@ 2020-01-31  7:00       ` Jeffrey R. Carter
  2020-01-31  8:51         ` J-P. Rosen
  0 siblings, 1 reply; 17+ messages in thread
From: Jeffrey R. Carter @ 2020-01-31  7:00 UTC (permalink / raw)


On 1/31/20 6:53 AM, J-P. Rosen wrote:
> Le 30/01/2020 à 20:27, ahlan@marriott.org a écrit :
>> Very interesting but we want to catch all unhandled exceptions, specifically those raised during package elaboration.
>>
> Ah, OK now I see the problem. You'll need to have an exception handler
> in every package body, but be careful about elaboration order!

This only handles exceptions in the executable part of the pkg. To handle 
exceptions in the declarative part of pkgs, you need to have set a termination 
handler for the environment task.

-- 
Jeff Carter
"[I]t is easy to use [Ada] just like any other
language: using only predefined types, using
packages just for separate compilation (without
any consideration for information hiding),
ignoring generics altogether, etc. I have seen
projects doing this; they didn't get much
gain from using Ada, and spent a lot of time
fighting the compiler."
Jean-Pierre Rosen
165

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

* Re: Last chance handler on a PC
  2020-01-31  7:00       ` Jeffrey R. Carter
@ 2020-01-31  8:51         ` J-P. Rosen
  0 siblings, 0 replies; 17+ messages in thread
From: J-P. Rosen @ 2020-01-31  8:51 UTC (permalink / raw)


Le 31/01/2020 à 08:00, Jeffrey R. Carter a écrit :
> This only handles exceptions in the executable part of the pkg. To
> handle exceptions in the declarative part of pkgs, you need to have set
> a termination handler for the environment task.
> 
Right; but AdaControl also has a check that declarations do not perform
operations that could raise exceptions (which is quite sane to check)?

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr

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

* Re: Last chance handler on a PC
  2020-01-30 20:26     ` Niklas Holsti
  2020-01-30 20:51       ` Jeffrey R. Carter
@ 2020-01-31 11:12       ` Simon Wright
  2020-01-31 11:19         ` Simon Wright
  1 sibling, 1 reply; 17+ messages in thread
From: Simon Wright @ 2020-01-31 11:12 UTC (permalink / raw)


Niklas Holsti <niklas.holsti@tidorum.invalid> writes:

> I had a similar elaboration problem some time ago in an embedded
> application, where I wanted to set up some HW error-trap handlers that
> I would like to be active also during elaboration, but I found no easy
> way to ensure that the trap-handling package would be elaborated
> before all other packages.

Could you have used pragma Restrictions (No_Elaboration_Code); ?

Or pragma No_Elaboration_Code_All --
   This is a program unit pragma (there is also an equivalent aspect of
   the same name) that establishes the restriction No_Elaboration_Code
   for the current unit and any extended main source units (body and
   subunits). It also has the effect of enforcing a transitive
   application of this aspect, so that if any unit is implicitly or
   explicitly with’ed by the current unit, it must also have the
   No_Elaboration_Code_All aspect set. It may be applied to package or
   subprogram specs or their generic versions.

(I've done this in Cortex GNAT RTS, so that I could write the startup
code in Ada).

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

* Re: Last chance handler on a PC
  2020-01-31 11:12       ` Simon Wright
@ 2020-01-31 11:19         ` Simon Wright
  2020-01-31 12:30           ` Niklas Holsti
  0 siblings, 1 reply; 17+ messages in thread
From: Simon Wright @ 2020-01-31 11:19 UTC (permalink / raw)


Simon Wright <simon@pushface.org> writes:

> Niklas Holsti <niklas.holsti@tidorum.invalid> writes:
>
>> I had a similar elaboration problem some time ago in an embedded
>> application, where I wanted to set up some HW error-trap handlers that
>> I would like to be active also during elaboration, but I found no easy
>> way to ensure that the trap-handling package would be elaborated
>> before all other packages.
>
> Could you have used pragma Restrictions (No_Elaboration_Code); ?
>
> Or pragma No_Elaboration_Code_All --
>    This is a program unit pragma (there is also an equivalent aspect of
>    the same name) that establishes the restriction No_Elaboration_Code
>    for the current unit and any extended main source units (body and
>    subunits). It also has the effect of enforcing a transitive
>    application of this aspect, so that if any unit is implicitly or
>    explicitly with’ed by the current unit, it must also have the
>    No_Elaboration_Code_All aspect set. It may be applied to package or
>    subprogram specs or their generic versions.
>
> (I've done this in Cortex GNAT RTS, so that I could write the startup
> code in Ada).

Sorry, that was stupid: you needed the package to have elaboration code
so that it could set up the error-trap handlers!

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

* Re: Last chance handler on a PC
  2020-01-31 11:19         ` Simon Wright
@ 2020-01-31 12:30           ` Niklas Holsti
  0 siblings, 0 replies; 17+ messages in thread
From: Niklas Holsti @ 2020-01-31 12:30 UTC (permalink / raw)


On 2020-01-31 13:19, Simon Wright wrote:
> Simon Wright <simon@pushface.org> writes:
> 
>> Niklas Holsti <niklas.holsti@tidorum.invalid> writes:
>>
>>> I had a similar elaboration problem some time ago in an embedded
>>> application, where I wanted to set up some HW error-trap handlers that
>>> I would like to be active also during elaboration, but I found no easy
>>> way to ensure that the trap-handling package would be elaborated
>>> before all other packages.
>>
>> Could you have used pragma Restrictions (No_Elaboration_Code); ?
>>
>> Or pragma No_Elaboration_Code_All --
>>     This is a program unit pragma (there is also an equivalent aspect of
>>     the same name) that establishes the restriction No_Elaboration_Code
>>     for the current unit and any extended main source units (body and
>>     subunits). It also has the effect of enforcing a transitive
>>     application of this aspect, so that if any unit is implicitly or
>>     explicitly with’ed by the current unit, it must also have the
>>     No_Elaboration_Code_All aspect set. It may be applied to package or
>>     subprogram specs or their generic versions.
>>
>> (I've done this in Cortex GNAT RTS, so that I could write the startup
>> code in Ada).
> 
> Sorry, that was stupid: you needed the package to have elaboration code
> so that it could set up the error-trap handlers!

Yes. Moreover, this application relied on elaboration for essentially 
all of its SW and HW initialization (after the startup code), which I 
thought worked well for the most part -- any package that accessed a 
particular HW part of the SoC "withed" the driver package for that HW 
part, and the elaboration of the driver package initialized the HW part.

However, we did have many elaboration circularity problems when we tried 
to use VectorCAST to define unit tests for this application, because the 
"test environments" constructed by VectorCAST added many dependencies. 
We ended up using VectorCAST only for measuring test coverage, while 
writing the unit test procedures directly in Ada.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .


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

end of thread, other threads:[~2020-01-31 12:30 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-30  8:55 Last chance handler on a PC ahlan
2020-01-30  9:15 ` J-P. Rosen
2020-01-30  9:17 ` Egil H H
2020-01-30 19:27   ` ahlan
2020-01-31  5:53     ` J-P. Rosen
2020-01-31  7:00       ` Jeffrey R. Carter
2020-01-31  8:51         ` J-P. Rosen
2020-01-30  9:25 ` Dmitry A. Kazakov
2020-01-30 19:10 ` Lucretia
2020-01-30 19:35 ` ahlan
2020-01-30 20:02   ` Jeffrey R. Carter
2020-01-30 20:26     ` Niklas Holsti
2020-01-30 20:51       ` Jeffrey R. Carter
2020-01-30 21:32         ` Niklas Holsti
2020-01-31 11:12       ` Simon Wright
2020-01-31 11:19         ` Simon Wright
2020-01-31 12:30           ` Niklas Holsti

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