@@ -23,6 +23,9 @@ import {
2323 isFromWorkspace ,
2424 workspaceTasksFirst ,
2525 getBuildAndRunTaskName ,
26+ getRunGNATemulatorTaskName ,
27+ getBuildTaskName ,
28+ runTaskAndGetResult ,
2629} from './taskProviders' ;
2730import { createHelloWorldProject , walkthroughStartDebugging } from './walkthrough' ;
2831import { loadGnatCoverageReport } from './gnattest' ;
@@ -55,6 +58,16 @@ export const CMD_BUILD_AND_DEBUG_MAIN = 'ada.buildAndDebugMain';
5558 */
5659export const CMD_BUILD_AND_RUN_GNATEMULATOR = 'ada.buildAndRunGNATemulator' ;
5760
61+ /**
62+ * Identifier for a hidden command used for building and debugging a project main,
63+ * using GNATemulator.
64+ * The command accepts a parameter which is the URI of the main source file.
65+ * It is triggered by CodeLenses provided by the extension.
66+ *
67+ * @see {@link buildAndDebugSpecifiedMain }
68+ */
69+ export const CMD_BUILD_AND_DEBUG_GNATEMULATOR = 'ada.buildAndDebugGNATemulator' ;
70+
5871/**
5972 * Identifier for a hidden command that returns an array of strings constituting
6073 * the -P and -X project and scenario arguments.
@@ -155,7 +168,12 @@ export function registerCommands(context: vscode.ExtensionContext, clients: Exte
155168 buildAndRunMainWithGNATemulator ,
156169 ) ,
157170 ) ;
158-
171+ context . subscriptions . push (
172+ vscode . commands . registerCommand (
173+ CMD_BUILD_AND_DEBUG_GNATEMULATOR ,
174+ buildAndDebugSpecifiedMainWithGNATemulator ,
175+ ) ,
176+ ) ;
159177 context . subscriptions . push (
160178 vscode . commands . registerCommand ( CMD_GPR_PROJECT_ARGS , gprProjectArgs ) ,
161179 ) ;
@@ -635,19 +653,25 @@ async function buildAndRunMainWithGNATemulator(main: vscode.Uri): Promise<void>
635653
636654/**
637655 * This is a command handler that builds the main given as parameter and starts
638- * a debug session on that main. If the given URI does not match one of the
639- * project Mains an error is displayed.
656+ * a debug session on that main, running GNATemulator in debug mode before when
657+ * asked.
658+ * If the given URI does not match one of the project Mains an error is displayed.
640659 *
641660 * @param main - a URI of a document
661+ * @param useGNATemulator - whether the main should be ran through GNATemulator
642662 */
643- async function buildAndDebugSpecifiedMain ( main : vscode . Uri ) : Promise < void > {
663+ async function buildAndDebugSpecifiedMain (
664+ main : vscode . Uri ,
665+ useGNATemulator : boolean = false ,
666+ ) : Promise < void > {
644667 function isMatchingConfig ( cfg : vscode . DebugConfiguration , configToMatch : AdaConfig ) : boolean {
645668 return cfg . type == configToMatch . type && cfg . name == configToMatch . name ;
646669 }
647670
648671 const wsFolder = vscode . workspace . getWorkspaceFolder ( main ) ;
649672 const adaMain = await findAdaMain ( main . fsPath ) ;
650673 const target = await adaExtState . getTargetPrefix ( ) ;
674+ const debugServerAddress = await adaExtState . getDebugServerAddress ( useGNATemulator ) ;
651675 if ( adaMain ) {
652676 /**
653677 * The vscode API doesn't provide a way to list both automatically
@@ -657,7 +681,7 @@ async function buildAndDebugSpecifiedMain(main: vscode.Uri): Promise<void> {
657681 * the given main URI.
658682 */
659683 // Create a launch config for this main to help with matching
660- const configToMatch = initializeConfig ( adaMain , target ) ;
684+ const configToMatch = initializeConfig ( adaMain , target , debugServerAddress ) ;
661685 logger . debug ( 'Debug config to match:\n' + JSON . stringify ( configToMatch , null , 2 ) ) ;
662686
663687 let matchingConfig = undefined ;
@@ -683,6 +707,38 @@ async function buildAndDebugSpecifiedMain(main: vscode.Uri): Promise<void> {
683707
684708 if ( matchingConfig ) {
685709 logger . debug ( 'Found matching config: ' + JSON . stringify ( matchingConfig , null , 2 ) ) ;
710+
711+ // Trying to debug via GNATemulator: run the main via GNATemulator in debug mode
712+ // before starting the debug session
713+ if ( useGNATemulator ) {
714+ const buildTaskName = getBuildTaskName ( adaMain ) ;
715+ const buildTasks = await getTasksWithPrefix ( buildTaskName ) ;
716+ if ( buildTasks . length === 1 ) {
717+ const execStatus : number | undefined = await runTaskAndGetResult ( buildTasks [ 0 ] ) ;
718+ if ( execStatus != 0 ) {
719+ const errorMsg = `Failed to build executable before launching GNATemulator` ;
720+ logger . error ( errorMsg ) ;
721+ return ;
722+ }
723+ logger . debug ( 'Running GNATemulator before starting the debug configuration...' ) ;
724+ const runGNATemulatorTaskName = getRunGNATemulatorTaskName ( adaMain , true ) ;
725+ const runGNATemulatorForDebugTasks =
726+ await getTasksWithPrefix ( runGNATemulatorTaskName ) ;
727+
728+ if ( runGNATemulatorForDebugTasks . length < 1 ) {
729+ const errorMsg = `Could not find '${ runGNATemulatorTaskName } ' task` ;
730+ logger . error ( errorMsg ) ;
731+ void vscode . window . showErrorMessage ( errorMsg , { modal : true } ) ;
732+ return ;
733+ }
734+ await vscode . tasks . executeTask ( runGNATemulatorForDebugTasks [ 0 ] ) ;
735+ } else {
736+ const errorMsg = `Could not find '${ buildTaskName } ' task` ;
737+ void vscode . window . showErrorMessage ( errorMsg , { modal : true } ) ;
738+ logger . error ( errorMsg ) ;
739+ }
740+ }
741+
686742 const success = await vscode . debug . startDebugging ( wsFolder , matchingConfig ) ;
687743 if ( ! success ) {
688744 void vscode . window . showErrorMessage (
@@ -706,6 +762,17 @@ async function buildAndDebugSpecifiedMain(main: vscode.Uri): Promise<void> {
706762 }
707763}
708764
765+ /**
766+ * This is a command handler that builds the main given as parameter and starts
767+ * a debug session on that main, with GNATemulator.
768+ * If the given URI does not match one of the project Mains an error is displayed.
769+ *
770+ * @param main - a URI of a document
771+ */
772+ async function buildAndDebugSpecifiedMainWithGNATemulator ( main : vscode . Uri ) : Promise < void > {
773+ await buildAndDebugSpecifiedMain ( main , true ) ;
774+ }
775+
709776/**
710777 * @returns an array of -P and -X project and scenario command lines arguments
711778 * for use with GPR-based tools.
0 commit comments