ocarina-backends-execution_tests.adb 31.7 KB
Newer Older
1 2 3 4
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
jhugues's avatar
jhugues committed
5
--     O C A R I N A . B A C K E N D S . E X E C U T I O N _ T E S T S      --
6 7 8
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
--       Copyright (C) 2009 Telecom ParisTech, 2010-2015 ESA & ISAE.        --
--                                                                          --
-- Ocarina  is free software; you can redistribute it and/or modify under   --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. Ocarina is distributed in the hope that it will be useful, but     --
-- WITHOUT ANY WARRANTY; without even the implied warranty of               --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
26
--                                                                          --
jhugues's avatar
jhugues committed
27 28
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
29 30 31
--                                                                          --
------------------------------------------------------------------------------

32 33
with Ocarina.Output;
with Ocarina.Namet;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
with System.Address_To_Access_Conversions;

with Ada.Directories;
with Ada.Command_Line;
with Ada.Text_IO;
with Ada.Strings.Unbounded.Text_IO;
with Ada.Real_Time;

with GNAT.Directory_Operations;
with GNAT.Directory_Operations.Iteration;

with Ocarina.Backends.Execution_Utils;
with Ocarina.Backends.Utils;

package body Ocarina.Backends.Execution_Tests is

50 51
   use Ocarina.Output;
   use Ocarina.Namet;
52 53 54 55 56 57 58 59 60
   use Ada.Command_Line;
   use Ada.Text_IO;
   use Ada.Strings.Unbounded.Text_IO;
   use Ada.Strings.Unbounded;
   use GNAT.Directory_Operations;
   use Ocarina.Backends.Execution_Utils;
   use Ocarina.Backends.Utils;
   use String_String_Maps;

61 62
   package Addr_To_Acc is new System.Address_To_Access_Conversions
     (Unbounded_String);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

   procedure Exit_On_Error (Error : Boolean; Reason : String);

   -------------------
   -- Exit_On_Error --
   -------------------

   procedure Exit_On_Error (Error : Boolean; Reason : String) is
   begin
      if Error then
         Set_Standard_Error;
         Write_Line (Reason);
         OS_Exit (1);
      end if;
   end Exit_On_Error;

   ----------
   -- Init --
   ----------

   procedure Init is
84 85 86
   --  --  Example to find the size of the Pattern_Matcher Parse_Regexp :
   --  Parse_Regexp : constant Pattern_Matcher :=
   --    Compile ("(\[-? *[0-9]+\.[0-9]* *\])", Single_Line);
87 88 89 90 91 92 93
   begin
      --  Put_Line (Integer'Image (Parse_Regexp'Size));
      --  --  => 1184, the real Pattern_Matcher will be (1184 - 160) / 8 = 128
      --  --  160 is the base size of a Pattern_Matcher
      TSim_ERC32_Path   := GNAT.OS_Lib.Locate_Exec_On_Path ("tsim-erc32");
      TSim_LEON_Path    := GNAT.OS_Lib.Locate_Exec_On_Path ("tsim-leon");
      Qemu_Path         := GNAT.OS_Lib.Locate_Exec_On_Path ("qemu");
94
      Qemu_Sparc_Path := GNAT.OS_Lib.Locate_Exec_On_Path ("qemu-system-sparc");
95
      Xcov_Path         := GNAT.OS_Lib.Locate_Exec_On_Path ("xcov");
96 97 98 99
      Command_Name_Path :=
        new String'
          (Dir_Name
             (Ada.Directories.Full_Name (Ada.Command_Line.Command_Name)));
100 101 102
      Compile (Parse_Regexp, "(\[-? *[0-9]+\.[0-9]* *\])", Single_Line);
      Compile (Strip_CR_Regexp, "(\r)", Single_Line);
      Compile (Header_End_Regexp, "(resuming at.*)", Multiple_Lines);
103 104 105 106
      Compile
        (Tsim_Traces_Regexp,
         "(tsim>|Program exited normally.)",
         Single_Line);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
      Compile (Empty_Line_Regexp, "(^\n)", Multiple_Lines);
   end Init;

   -----------
   -- Reset --
   -----------

   procedure Reset is
   begin
      Free (TSim_ERC32_Path);
      Free (TSim_LEON_Path);
      Free (Qemu_Path);
      Free (Xcov_Path);
      Free (All_Traces);
      Free (Scenario_Dir);
      Free (Command_Name_Path);
   end Reset;

   -------------------
   -- No_Regression --
   -------------------

   function No_Regression
130 131 132 133
     (Trace    : Unbounded_String;
      Ref      : Unbounded_String;
      App      : Name_Id;
      Ref_Path : String) return Boolean
134 135 136 137 138 139 140
   is
      File           : File_Type;
      Trace_Filtered : String_Ptr;
      Ref_Filtered   : String_Ptr;
      Padding        : Integer;
   begin
      Write_Eol;
141 142
      Write_Line
        ("--- Testing regression for : " & Get_Name_String (App) & " ---");
143 144 145 146 147 148 149 150 151
      Write_Line ("Using following referencial : " & Ref_Path);

      Filter_Line (To_String (Trace), Trace_Filtered);
      Filter_Line (To_String (Ref), Ref_Filtered);

      if Write_Log then
         declare
            Referencial_Log_File : constant String :=
              Get_Current_Dir & "log." & Get_Name_String (App) & ".ref.txt";
152
            Trace_Log_File : constant String :=
153 154
              Get_Current_Dir & "log." & Get_Name_String (App) & ".trace.txt";
         begin
155 156 157
            Write_Line
              ("Writing log for referencial in : " & Referencial_Log_File);
            Create (File, Out_File, Referencial_Log_File);
158 159 160 161
            Put (File, Ref_Filtered.all);
            Close (File);

            Write_Line ("Writing log for trace in : " & Trace_Log_File);
162
            Create (File, Out_File, Trace_Log_File);
163 164 165 166 167 168
            Put (File, Trace_Filtered.all);
            Close (File);
         end;
      end if;

      if Trace_Filtered.all'Length < Ref_Filtered.all'Length then
169 170 171 172 173 174
         Write_Line
           ("Warning : trace length (" &
            Trace_Filtered.all'Length'Img &
            " ) is lower than referencial length (" &
            Ref_Filtered.all'Length'Img &
            " )");
175 176 177 178
      end if;

      Padding := Trace_Filtered'First - Ref_Filtered'First;
      for I in Ref_Filtered.all'Range loop
179 180
         exit when I + Padding > Trace_Filtered.all'Last;

181
         if Ref_Filtered.all (I) /= Trace_Filtered.all (I + Padding) then
182 183 184 185 186 187 188 189
            Write_Line
              ("Failed at char position = " &
               I'Img &
               " (ref = '" &
               Ref_Filtered.all (I) &
               "' trace= '" &
               Trace_Filtered.all (I + Padding) &
               "')");
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
            return False;
         end if;
      end loop;
      return True;
   end No_Regression;

   ----------------------
   -- Load_Referencial --
   ----------------------

   function Load_Referencial (File_Path : String) return Unbounded_String is
      File                 : File_Type;
      Complete_File_String : Unbounded_String;
   begin
      Open (File, In_File, File_Path);

      loop
         exit when End_Of_File (File);
         Append (Complete_File_String, To_String (Get_Line (File)) & ASCII.LF);
      end loop;

      Close (File);
      return Complete_File_String;
   end Load_Referencial;

   ------------------------
   --  Write_Referencial --
   ------------------------

   procedure Write_Referencial
     (File_Path : String;
      Ref       : Unbounded_String;
      App       : Name_Id)
   is
      File : File_Type;
   begin
226 227 228 229 230
      Write_Line
        ("Writing referencial for " &
         Get_Name_String (App) &
         " to : " &
         File_Path);
231 232 233 234 235 236 237 238 239 240 241 242
      Create (File, Out_File, File_Path);
      Put_Line (File, To_String (Ref));
      Close (File);
   end Write_Referencial;

   -----------------------------
   -- Execute_Regression_Test --
   -----------------------------

   function Execute_Regression_Test
     (Scenario_Dirname : String;
      Ref_Map          : Map;
243
      Timeout          : Natural) return Boolean
244 245 246
   is
      Result       : Expect_Match;
      Referencial  : Unbounded_String;
247 248
      Return_Value : Boolean      := True;
      TimeoutVar   : Integer      := Timeout;
249 250 251 252 253 254 255 256 257 258
      First        : constant Int := Ref_Name_Tables.First;
      Last         : constant Int := Ref_Name_Tables.Last (Process_List);
      Processes    : Fd_Array (Integer (First) .. Integer (Last));
      M            : Process_Type;
      Position     : Cursor;
      Ref_Path     : String_Ptr;
   begin
      All_Traces := new Trace (Integer (First) .. Integer (Last));

      for J in First .. Last loop
259
         M        := Process_List.Table (J);
260 261 262
         Position := Find (Ref_Map, Get_Name_String (M.Node_Name));

         if Position = No_Element then
263 264 265 266
            Exit_On_Error
              (True,
               "Error : no referencial defined for " &
               Get_Name_String (M.Node_Name));
267 268 269 270 271
         end if;

         declare
            --  Application path
            Appli_File : constant String :=
272 273 274 275
              Get_Current_Dir &
              Get_Name_String (M.Appli_Name) &
              Dir_Separator &
              Get_Binary_Location (Get_Current_Backend_Kind, M.Node_Name);
276 277 278 279 280 281 282 283 284

            Ref_File : constant String :=
              Scenario_Dirname & Element (Position);

         begin
            if not Ada.Directories.Exists (Ref_File) then
               Create_Referencial := True;
            end if;

285 286 287
            Exit_On_Error
              (not Ada.Directories.Exists (Appli_File),
               "Error : application " & Appli_File & " does not exist");
288 289

            Write_Line ("Launching : " & Appli_File);
290 291 292 293 294 295 296 297 298 299
            Launch_Test
              (Processes (Integer (J)),
               Appli_File,
               M.Execution_Platform,
               TimeoutVar);
            Add_Filter
              (Processes (Integer (J)),
               Filter_Procedure'Access,
               GNAT.Expect.Output,
               All_Traces (Integer (J))'Address);
300 301 302 303 304 305
         end;
      end loop;

      Write_Eol;

      if M.Execution_Platform = Platform_LEON_ORK
306 307
        or else M.Execution_Platform = Platform_ERC32_ORK
      then
308 309 310 311 312
         --  Let tsim deal with the timeout
         TimeoutVar := -1;
      end if;

      for J in First .. Last loop
313 314 315 316 317
         M        := Process_List.Table (J);
         Ref_Path :=
           new String'
             (Scenario_Dirname &
              Element (Ref_Map, Get_Name_String (M.Node_Name)));
318 319 320 321 322

         begin
            Expect (Processes (Integer (J)), Result, Never_Match, TimeoutVar);
         exception
            when GNAT.Expect.Process_Died =>
323 324 325 326
               Write_Line
                 ("Warning: process " &
                  Get_Name_String (M.Node_Name) &
                  " has died during test phase");
327 328 329 330 331 332 333
         end;

         Close (Processes (Integer (J)));

         if not Create_Referencial then
            Referencial := Load_Referencial (Ref_Path.all);
            if M.Execution_Platform = Platform_LEON_ORK
334 335
              or else M.Execution_Platform = Platform_ERC32_ORK
            then
336 337
               Append (All_Traces (Integer (J)), ASCII.LF);
            end if;
338 339 340 341 342 343 344 345 346 347
            if No_Regression
                (All_Traces (Integer (J)),
                 Referencial,
                 M.Node_Name,
                 Ref_Path.all)
            then
               Write_Line
                 ("--- Regression test result for " &
                  Get_Name_String (M.Node_Name) &
                  " : SUCCESS ---");
348 349
            else
               Return_Value := False;
350 351 352 353
               Write_Line
                 ("--- Regression test result for " &
                  Get_Name_String (M.Node_Name) &
                  " : !!! FAILED !!! ---");
354 355
            end if;
         else
356 357 358 359
            Write_Referencial
              (Ref_Path.all,
               All_Traces (Integer (J)),
               M.Node_Name);
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
         end if;

         Free (Ref_Path);
         TimeoutVar := 0;
      end loop;

      Free (All_Traces);
      return Return_Value;
   end Execute_Regression_Test;

   -----------------
   -- Launch_Test --
   -----------------

   procedure Launch_Test
     (Fd      : out GNAT.Expect.Process_Descriptor;
376 377 378
      Command :     String;
      Arch    :     Supported_Execution_Platform;
      Timeout :     Natural)
379 380 381 382 383 384 385
   is
   begin
      case Arch is
         when Platform_LEON_ORK =>
            declare
               TSim_LEON_Args : GNAT.OS_Lib.Argument_List (1 .. 1);
            begin
386 387 388 389
               Exit_On_Error
                 (TSim_LEON_Path = null,
                  "Error : tsim-leon not found in PATH");
               Header_Has_Ended   := False;
390 391 392 393 394 395 396 397 398
               TSim_LEON_Args (1) := new String'(Command);

               Non_Blocking_Spawn
                 (Descriptor  => Fd,
                  Command     => TSim_LEON_Path.all,
                  Args        => TSim_LEON_Args,
                  Buffer_Size => 128000,
                  Err_To_Out  => True);
            end;
399 400 401
            Send
              (Fd,
               Tsim_Cmd ("go 0x40000000 " & Integer'Image (Timeout) & " ms"));
402 403 404 405
            GNAT.OS_Lib.Close (Get_Input_Fd (Fd));

         when Platform_LEON_GNAT =>
            declare
406
               Args : GNAT.OS_Lib.Argument_List (1 .. 5);
407
            begin
408 409 410
               Exit_On_Error
                 (Qemu_Sparc_Path = null,
                  "Error : qemu_system_sparc not found in PATH");
411 412

               Write_Line ("Launching qemu_system_sparc");
413 414 415 416 417
               Args (1) := new String'("-nographic");
               Args (2) := new String'("-M");
               Args (3) := new String'("at697");
               Args (4) := new String'("-kernel");
               Args (5) := new String'(Command);
418 419 420 421 422 423 424 425 426 427 428 429 430 431

               GNAT.Expect.Non_Blocking_Spawn
                 (Descriptor  => Fd,
                  Command     => Qemu_Sparc_Path.all,
                  Args        => Args,
                  Buffer_Size => 128000,
                  Err_To_Out  => True);

            end;

         when Platform_ERC32_ORK =>
            declare
               TSim_ERC32_Args : GNAT.OS_Lib.Argument_List (1 .. 1);
            begin
432 433 434 435
               Exit_On_Error
                 (TSim_ERC32_Path = null,
                  "Error : tsim-erc32 not found in PATH");
               Header_Has_Ended    := False;
436 437 438 439 440 441 442 443 444
               TSim_ERC32_Args (1) := new String'(Command);

               Non_Blocking_Spawn
                 (Descriptor  => Fd,
                  Command     => TSim_ERC32_Path.all,
                  Args        => TSim_ERC32_Args,
                  Buffer_Size => 128000,
                  Err_To_Out  => True);
            end;
445 446 447
            Send
              (Fd,
               Tsim_Cmd ("go 0x02000000 " & Integer'Image (Timeout) & " ms"));
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
            GNAT.OS_Lib.Close (Get_Input_Fd (Fd));

         when Platform_Native =>
            declare
               Args : GNAT.OS_Lib.Argument_List (1 .. 1);
            begin
               Args (1) := new String'("");

               GNAT.Expect.Non_Blocking_Spawn
                 (Descriptor  => Fd,
                  Command     => Command,
                  Args        => Args,
                  Buffer_Size => 128000,
                  Err_To_Out  => True);

            end;

         when Platform_LEON_RTEMS =>
            declare
467 468 469 470 471
               Args : GNAT.OS_Lib.Argument_List (1 .. 11);
               Dir  : constant String :=
                 Dir_Name (Normalize_Pathname (Command & Dir_Separator));
               App_Name : constant String :=
                 File_Name (Normalize_Pathname (Command));
472 473
               File      : File_Type;
               Boot_File : Unbounded_String :=
474 475 476 477 478
                 To_Unbounded_String
                   (Command_Name_Path.all &
                    "resources" &
                    Dir_Separator &
                    "rtems-boot.img");
479
            begin
480 481 482
               Exit_On_Error
                 (Qemu_Path = null,
                  "Error : QEMU not found in PATH");
483 484

               if not Ada.Directories.Exists (To_String (Boot_File)) then
485 486 487
                  Boot_File :=
                    To_Unbounded_String
                      (Command_Name_Path.all & "rtems-boot.img");
488 489
               end if;

490 491 492 493
               Exit_On_Error
                 (not Ada.Directories.Exists (To_String (Boot_File)),
                  "Error : QEMU image boot file 'rtems-boot.img' " &
                  "not found");
494 495 496 497 498 499 500 501 502 503

               Create (File, Out_File, Dir & "rtems-grub.cfg");
               Put_Line (File, "set default=0");
               Put_Line (File, "set timeout=0");
               Put_Line (File, "menuentry ""Ocarina"" {");
               Put_Line (File, "  set root=(hd0,0)");
               Put_Line (File, "  multiboot (hd0,0)/" & App_Name);
               Put_Line (File, "}");
               Close (File);

504 505
               Write_Line
                 ("Launching qemu with following dir as hda : " & Dir);
506 507 508 509 510 511 512 513 514
               Args (1)  := new String'("-boot");
               Args (2)  := new String'("a");
               Args (3)  := new String'("-fda");
               Args (4)  := new String'(To_String (Boot_File));
               Args (5)  := new String'("-hda");
               Args (6)  := new String'("fat:" & Dir);
               Args (7)  := new String'("-nographic");
               Args (8)  := new String'("-no-kqemu");
               Args (9)  := new String'("-serial");
515
               Args (10) := new String'("stdio");
516 517 518 519 520 521 522 523 524 525 526 527
               Args (11) := new String'("-no-reboot");

               GNAT.Expect.Non_Blocking_Spawn
                 (Descriptor  => Fd,
                  Command     => Qemu_Path.all,
                  Args        => Args,
                  Buffer_Size => 128000,
                  Err_To_Out  => True);

            end;

         when others =>
528 529 530
            Exit_On_Error
              (True,
               "Platform " & Arch'Img & " is not supported yet.");
531 532 533 534 535 536 537 538 539
      end case;
   end Launch_Test;

   -------------------------------
   --  Clean_String_From_Regexp --
   -------------------------------

   procedure Clean_String_From_Regexp
     (Str    : in out String_Ptr;
540
      Regexp :        Pattern_Matcher)
541 542 543 544 545 546 547 548 549 550
   is
      Matches : Match_Array (1 .. 1);
      Old_Str : String_Ptr;
   begin
      Match (Regexp, Str.all, Matches);
      while Matches (1) /= No_Match loop
         --  backup old string
         Old_Str := Str;

         --  create new parsed string from Output_Regexp
551 552 553 554
         Str :=
           new String'
             (Old_Str.all (Old_Str.all'First .. Matches (1).First - 1) &
              Old_Str.all (Matches (1).Last + 1 .. Old_Str.all'Last));
555 556 557 558 559 560 561 562 563 564 565

         --  Deallocate old string
         Free (Old_Str);
         Match (Regexp, Str.all, Matches);
      end loop;
   end Clean_String_From_Regexp;

   -----------------------------------
   --  Clean_String_From_All_Regexp --
   -----------------------------------

566
   procedure Clean_String_From_All_Regexp (Str : in out String_Ptr) is
567 568 569 570 571 572 573 574 575 576 577 578
   begin
      Clean_String_From_Regexp (Str, Parse_Regexp);
      Clean_String_From_Regexp (Str, Strip_CR_Regexp);
      Clean_String_From_Regexp (Str, Header_End_Regexp);
      Clean_String_From_Regexp (Str, Tsim_Traces_Regexp);
      Clean_String_From_Regexp (Str, Empty_Line_Regexp);
   end Clean_String_From_All_Regexp;

   -----------------
   -- Filter_Line --
   -----------------

579
   procedure Filter_Line (Line : String; Output_Str : in out String_Ptr) is
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
   begin
      Output_Str := new String'(Line);

      --  remove any unwanted characters from Str
      Clean_String_From_All_Regexp (Output_Str);
   end Filter_Line;

   ----------------------
   -- Filter_Procedure --
   ----------------------

   procedure Filter_Procedure
     (Descriptor : Process_Descriptor'Class;
      Str        : String;
      User_Data  : System.Address)
   is
596 597
      Output_Buffer : constant Unbounded_String_Ptr :=
        Unbounded_String_Ptr (Addr_To_Acc.To_Pointer (User_Data));
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
   begin
      pragma Unreferenced (Descriptor);

      if not Header_Has_Ended then
         if Match (Header_End_Regexp, Str) then
            Header_Has_Ended := True;
         end if;
      else
         Append (Output_Buffer.all, Str);
      end if;
   end Filter_Procedure;

   -------------------------
   -- No_Filter_Procedure --
   -------------------------

   procedure No_Filter_Procedure
     (Descriptor : Process_Descriptor'Class;
      Str        : String;
      User_Data  : System.Address)
   is
619 620
      Output_Buffer : constant Unbounded_String_Ptr :=
        Unbounded_String_Ptr (Addr_To_Acc.To_Pointer (User_Data));
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
   begin
      pragma Unreferenced (Descriptor);
      Append (Output_Buffer.all, Str);
   end No_Filter_Procedure;

   --------------
   -- Tsim_Cmd --
   --------------

   function Tsim_Cmd (Cmd : String) return String is
   begin
      return Cmd & ASCII.CR;
   end Tsim_Cmd;

   ---------------------------
   -- Execute_Coverage_Test --
   ---------------------------

639
   function Execute_Coverage_Test (Timeout : Natural) return Boolean is
640 641 642
      use Ada.Real_Time;
      Result       : Expect_Match;
      Return_Value : Boolean;
643 644 645
      TimeoutVar   : Natural            := Timeout;
      First        : constant Int       := Ref_Name_Tables.First;
      Last         : constant Int       := Ref_Name_Tables.Last (Process_List);
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
      Processes    : Fd_Array (Integer (First) .. Integer (Last));
      M            : Process_Type;
      Span         : constant Time_Span := Seconds (2);
   begin
      if Xcov_Path = null then
         Exit_On_Error (True, "Error: xcov not found");
      end if;

      All_Traces := new Trace (Integer (First) .. Integer (Last));

      --  Launch all processes with xcov to get execution traces

      for J in First .. Last loop
         M := Process_List.Table (J);

         Enter_Directory (M.Appli_Name);

         declare
            Args : GNAT.OS_Lib.Argument_List (1 .. 3);
         begin
            Args (1) := new String'("run");
            if Get_Current_Backend_Kind = PolyORB_Kernel_C then
               Args (2) := new String'("--target=prepare");
            else
               case M.Execution_Platform is
                  when Platform_Native | Platform_None =>
                     Args (2) := new String'("--target=i386-linux");
                  when Platform_LEON_GNAT =>
                     Args (2) := new String'("--target=leon-elf");
                  when others =>
676 677 678 679
                     Exit_On_Error
                       (True,
                        "Error : This platform is not yet" &
                        " supported for coverage test");
680 681 682
               end case;
            end if;

683 684 685 686 687 688
            Args (3) :=
              new String'
                (Get_Current_Dir &
                 Get_Name_String (M.Appli_Name) &
                 Dir_Separator &
                 Get_Binary_Location (Get_Current_Backend_Kind, M.Node_Name));
689

690 691 692
            Exit_On_Error
              (not Ada.Directories.Exists (Args (3).all),
               "Error : application " & Args (3).all & " does not exist");
693 694 695
            Write_Line ("Launching : " & Args (3).all);
            if Get_Current_Backend_Kind = PolyORB_Kernel_C then
               declare
696
                  Args2   : GNAT.OS_Lib.Argument_List (1 .. 8);
697 698 699
                  Success : Boolean;
               begin
                  --  Prepare trace file with xcov
700
                  GNAT.OS_Lib.Spawn (Xcov_Path.all, Args, Success);
701 702 703

                  --  Get traces with Qemu
                  Args2 (1) := new String'("-fda");
704 705 706 707
                  Args2 (2) :=
                    new String'
                      (GNAT.OS_Lib.Getenv ("POK_PATH").all &
                       "/misc/grub-boot-only.img");
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
                  Args2 (3) := new String'("-hda");
                  Args2 (4) := new String'("fat:.");
                  Args2 (5) := new String'("-boot");
                  Args2 (6) := new String'("a");
                  Args2 (7) := new String'("-nographic");
                  Args2 (8) := new String'(Args (3).all);

                  GNAT.Expect.Non_Blocking_Spawn
                    (Descriptor  => Processes (Integer (J)),
                     Command     => Qemu_Path.all,
                     Args        => Args2,
                     Buffer_Size => 128000,
                     Err_To_Out  => True);
               end;
            else
               GNAT.Expect.Non_Blocking_Spawn
                 (Descriptor  => Processes (Integer (J)),
                  Command     => Xcov_Path.all,
                  Args        => Args,
                  Buffer_Size => 128000,
                  Err_To_Out  => True);
            end if;
730 731 732 733 734
            Add_Filter
              (Processes (Integer (J)),
               No_Filter_Procedure'Access,
               GNAT.Expect.Output,
               All_Traces (Integer (J))'Address);
735 736 737 738 739 740 741 742 743 744 745 746
         end;
         Leave_Directory;
      end loop;

      --  Stop all processes at the end of the timeout

      for J in First .. Last loop
         M := Process_List.Table (J);
         begin
            Expect (Processes (Integer (J)), Result, Never_Match, TimeoutVar);
         exception
            when GNAT.Expect.Process_Died =>
747 748 749 750
               Write_Line
                 ("Warning: process " &
                  Get_Name_String (M.Node_Name) &
                  " has died during coverage test phase");
751 752 753 754 755 756 757 758
         end;
         Write_Line (To_String (80 * '#'));
         Write_Line (To_String (All_Traces (Integer (J))));
         Write_Line (To_String (80 * '#'));

         --  Send "Ctrl-a x" command to stop qemu

         if Get_Current_Backend_Kind = PolyORB_Kernel_C
759 760
           or else M.Execution_Platform = Platform_LEON_GNAT
         then
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
            Send (Processes (Integer (J)), ASCII.SOH & 'x');
         end if;
         Close (Processes (Integer (J)));
         TimeoutVar := 0;
      end loop;

      --  Remove outputs from traces

      Free (All_Traces);
      All_Traces := new Trace (Integer (First) .. Integer (Last));

      --  Extract the list of functions to analyze from object files

      for J in First .. Last loop
         M := Process_List.Table (J);
         declare
777
            File    : File_Type;
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
            O_Files : Unbounded_String;

            procedure Add_To_O_Files
              (Item  :        String;
               Index :        Positive;
               Quit  : in out Boolean);
            --  Procedure associated with the Find function to add
            --  object files to the string variable.

            procedure Add_To_O_Files
              (Item  :        String;
               Index :        Positive;
               Quit  : in out Boolean)
            is
               --  Unused parameters needed to respect the pattern for Find
               pragma Unreferenced (Index, Quit);
            begin
               Append (O_Files, Item & ASCII.LF);
            end Add_To_O_Files;

798 799
            procedure Find_O_Files is new GNAT.Directory_Operations.Iteration
              .Find
800
              (Action => Add_To_O_Files);
801 802
         --  Procedure used to find object files in the application
         --  directory.
803 804 805 806

         begin
            --  List object files

807 808 809
            Find_O_Files
              (Get_Current_Dir & Get_Name_String (M.Appli_Name),
               ".*\.o");
810

811 812 813
            Find_O_Files
              (Get_Current_Dir & Get_Name_String (M.Appli_Name),
               ".*\.lo");
814 815 816 817 818 819 820

            --  Extract functions to analyze with xcov from previously found
            --  object files

            declare
               Nb_O_Files : constant Integer :=
                 Ada.Strings.Unbounded.Count (O_Files, ASCII.LF & "");
821
               Args2         : GNAT.OS_Lib.Argument_List (1 .. Nb_O_Files + 1);
822 823 824 825 826 827 828 829 830 831 832
               Lst, Next_Lst : Integer := 1;
            begin
               Args2 (1) := new String'("disp-routines");

               --  Extract each object file from the String and add it in
               --  the argument list.

               for I in 1 .. Nb_O_Files loop
                  --  Search the end of the next object file (assuming they
                  --  are separated with spaces)

833 834 835 836 837
                  Next_Lst :=
                    Lst +
                    Index
                      (Tail (O_Files, To_String (O_Files)'Length - Lst),
                       ASCII.LF & "");
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854

                  --  Add the object file with its full path to the argument
                  --  list (without the space).

                  Args2 (I + 1) :=
                    new String'(Slice (O_Files, Lst, Next_Lst - 1));
                  Lst := Next_Lst + 1;
               end loop;

               --  Remove object files in the traces

               Free (All_Traces);
               All_Traces := new Trace (Integer (First) .. Integer (Last));

               --  Finally, get functions to analyze with xcov

               GNAT.Expect.Non_Blocking_Spawn
855 856 857
                 (Descriptor  => Processes (Integer (J)),
                  Command     => Xcov_Path.all,
                  Args        => Args2,
858 859
                  Buffer_Size => 128000,
                  Err_To_Out  => True);
860 861 862 863 864
               Add_Filter
                 (Processes (Integer (J)),
                  No_Filter_Procedure'Access,
                  GNAT.Expect.Output,
                  All_Traces (Integer (J))'Address);
865
               begin
866
                  Expect (Processes (Integer (J)), Result, Never_Match);
867 868 869 870 871 872 873 874
               exception
                  when GNAT.Expect.Process_Died =>
                     null;
               end;
               Close (Processes (Integer (J)));

               --  Write result in <Node_Name>.trace.list file

875 876 877 878 879 880
               Create
                 (File,
                  Out_File,
                  Get_Current_Dir &
                  Get_Name_String (M.Node_Name) &
                  ".trace.list");
881 882
               Put (File, To_String (All_Traces (Integer (J))));
               Close (File);
883 884
               Write_Line
                 (Get_Name_String (M.Node_Name) & ".trace.list file created");
885 886 887 888 889 890 891 892 893 894 895 896 897
            end;
         end;
      end loop;

      --  Delay needed to ensure trace files are well written

      delay until Ada.Real_Time.Clock + Span;

      --  Analyze traces with xcov to generate html pages

      for J in First .. Last loop
         M := Process_List.Table (J);
         declare
898
            Args    : GNAT.OS_Lib.Argument_List (1 .. 5);
899 900 901 902 903
            Success : Boolean;
         begin
            Args (1) := new String'("coverage");
            Args (2) := new String'("--level=branch");
            Args (3) := new String'("--annotate=html+asm");
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
            Args (4) :=
              new String'
                ("--routine-list=" &
                 Get_Current_Dir &
                 Get_Name_String (M.Node_Name) &
                 ".trace.list");
            Args (5) :=
              new String'
                (Get_Current_Dir &
                 Get_Name_String (M.Appli_Name) &
                 Dir_Separator &
                 Get_Name_String (M.Node_Name) &
                 ".trace");
            Write_Line
              ("xcov " &
               Args (1).all &
               " " &
               Args (2).all &
               " " &
               Args (3).all &
               " " &
               Args (4).all &
               " " &
               Args (5).all);
            GNAT.OS_Lib.Spawn (Xcov_Path.all, Args, Success);
929 930 931 932 933 934 935 936 937 938
            if not Success then
               Return_Value := False;
            end if;
         end;
      end loop;

      Free (All_Traces);
      return Return_Value;
   end Execute_Coverage_Test;
end Ocarina.Backends.Execution_Tests;