HiI want to enable semihosting on ARM DS 2024 so I can create an output file. Can you tell me how to do that?I am using Arm Compiler for Embeded 6 toolchain and Gnu Make Builder.Warm regards
Hi againThe C/C++ libraries that are provided with Arm Compiler for Embedded 6 are semihosted by default, so all the usual C stdio functions will just work. See
https://developer.arm.com/documentation/101470/2024-1/Controlling-Target-Execution/Using-semihosting-to-access-resources-on-the-host-computer
For example:
#include <stdio.h>#include <stdlib.h>int main(void){ const char *filename = "/home/stheobal/test/out.txt"; FILE* fp = fopen(filename, "wt"); if (!fp) { printf("Could not open file %s for writing", filename); } else { printf("fopen success\n"); } char str[] = "Hello World!\n"; fprintf(fp, "%s", str); fflush(fp);}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const char *filename = "/home/stheobal/test/out.txt";
FILE* fp = fopen(filename, "wt");
if (!fp) {
printf("Could not open file %s for writing", filename);
}
else {
printf("fopen success\n");
char str[] = "Hello World!\n";
fprintf(fp, "%s", str);
fflush(fp);
Be aware that semihosting allows code running on a target platform to access files and execute system commands on a host machine, potentially allowing sensitive data on the host to be exposed. Before you use semihosting, you must ensure that you assess and mitigate the security implications. For example, a semihosted application running on an FVP model may access files on its host machine, unless semihosting is explicitly disabled in the FVP model. Similarly, a semihosted application running on a hardware target may access files on a debugger host that is connected by a debug agent such as DSTREAM-ST. For the latter case, the debugger has new controls to configure the semihosting security policy. In Arm DS 2024.1 and later, by default, if semihosting is enabled in the debugger, the debugger prevents semihosting from accessing files and executing system commands on the debugger host. The policy can be adjusted with the "set semihosting policy warn|allow|block" command. See:
https://developer.arm.com/documentation/101471/6-4-0armds/Arm-Debugger-commands/Arm-Debugger-commands-listed-in-alphabetical-order/set-semihosting
Hope this helps
Stephen
Hi Stephen and thank you for the information.I tried similar code, and I tried yours, too.Although I enabled Semihosting and Policy, set semihosting enabled onset semihosting policy allowIt still cannot read and write the file, I get the error:Could not open file C:\test\out.txt for writingIs there anything that should be changed in ARM DS or in the code?
Hi again
Apologies, the example I gave yesterday contains a couple of errors:
1) For the fopen mode, the non-ANSI option 't' is not supported. So "wt" should be just "w" (or "wb" for a binary file). See
https://github.com/ARM-software/abi-aa/blob/main/semihosting/semihosting.rst
2) The fprintf call should (obviously!) have been inside the else clause
Here's the corrected version, which works for me on Windows, when the debugger is connected to real hardware, and "set semihosting policy allow":
#include <stdio.h>#include <stdlib.h>int main(void){ const char *filename = "C:\\Temp\\semihosting\\out.txt"; FILE* fp = fopen(filename, "w"); if (!fp) { printf("Could not open file %s for writing", filename); } else { printf("fopen success\n"); char str[] = "Hello World!\n"; fprintf(fp, "%s", str); fflush(fp); }}
const char *filename = "C:\\Temp\\semihosting\\out.txt";
FILE* fp = fopen(filename, "w");
You shouldn't need to "set semihosting enabled on" explicitly when using the Arm Compiler for Embedded 6 toolchain, however, it is harmless to do so. The Debugger enables semihosting automatically if either symbols __auto_semihosting or __semihosting_library_function are present in an image. Arm Compiler for Embedded 6 adds __semihosting_library_function automatically to an image at link time if that image uses any semihosting-using functions. If compiling with GCC or LLVM, use an alias symbol definition such as void __auto_semihosting(void) __attribute__ ((alias("main"))); or turn on semihosting support in the Debugger explicitly with "set semihosting enabled on".
Hi StephenI noticed "wt" and changed it to "w" before I run the file.However, I started debugging using your code and another files that I had been created.Although I get "fopen success Hello World!" print in the app console, I can see no folder is created, and even if I create the folder and the file, it would not write anything on that.Is there any options that I have to check in ARM DS? (for example in debugging section)As you know I am using Arm Compiler for Embedded 6 and I did these changes on ARM DS commands:set semihosting enabled onset semihosting policy allowThank you in advance for your kind cooperation.
Hi againApologies for the delay in responding. Are you still having problems with this?I've re-tested the code above and it works for me. Here's what I did (on my Windows laptop):1) Launch Arm DS2) Import the startup_Cortex-M7_AC6 example3) Replace its main() with the main() I gave above4) Compile the project5) Create a folder C:\Temp\semihosting6) Modify startup_Cortex-M7_AC6-MPS2.launch to use a known-good Cortex-M7 board via DSTREAM7) Connect/Debug8) Enter "set semihosting policy allow"9) Run"fopen success" is printed in the App Console, and "Hello World!" is written to out.txt.Can you try repeating the above with your own board?Stephen
Hi and thank you for your support! highly appreciated!After we discussed I tried the procedure until I found that I have to Enter "set semihosting policy allow" just before run.Then I put this command in run debug initialization, so the problem is solved.Warm regards