确定Ada断言失败的原因

时间:2017-11-06 05:54:37

标签: ada assertions ada2012

如果断言失败,我会得到以下输出:

  

在file.adb上引发了SYSTEM.ASSERTIONS.ASSERT_FAILURE:Dynamic_Predicate失败:36

我可以获得更多细节吗?例如,输入是什么,或者可能是堆栈跟踪,还是可以帮助我确定断言失败原因的任何其他内容?

1 个答案:

答案 0 :(得分:4)

您可以捕获System.Assertions.Assert_Failure以使用GNAT.Traceback(如果使用GNAT)打包堆栈跟踪包或打印值。

像这里的东西

pragma Assertion_Policy(CHECK);

with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Traceback;
with System.Assertions;
with GNAT.Traceback.Symbolic;
procedure Main is
   procedure Call_Stack is
      Trace  : GNAT.Traceback.Tracebacks_Array (1..1_000);
      Length : Natural;
   begin
      GNAT.Traceback.Call_Chain (Trace, Length);
      Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (Trace (1..Length)));
   end Call_Stack; 
   type Day is new String (1 .. 10);
   type Message is record
      Sent     : Day;
      Received : Day;
   end record with
     Dynamic_Predicate => Message.Sent <= Message.Received;
   M : Message;
begin
   M  := (Received => "1776-07-04", Sent => "1783-09-03");
exception
   when System.Assertions.Assert_Failure =>
      Call_Stack;
      Put_Line(String(M.Sent));
      Put_Line(String(M.Received));
end Main;

或者你可以像我在评论中提到的那样调试你的程序 enter image description here