This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Printing assertion failures with GCC

Other Parts Discussed in Thread: TM4C1294NCPDT

I'm working a project using the Tiva processor family, which I debug over JTAG using an XDS100v2 debugger.

When I build the project using TI's ARM compiler, assertion failures result in a nice message printed out to the debug console. This is fantastic, but the project usually requires me to build the project using GCC, and when I fail assertions while debugging these builds, I just get kicked to my program's exit point, which is a little less helpful.

I understand that this is probably a feature of the TI ARM compiler, so I don't expect GCC in CCS to support this out of the box or anything. I'm just curious if anyone knows anything about how the TI ARM compiler implements its printing during assertions, and how this behaviour might be ported to another compiler. I don't know if it's remotely feasible to add this functionality to a non-TI compiler, but I thought I might as well ask. I took a look at the differences between the two compiler's assert.h files in their standard libraries, but I'm not particularly literate in dense preprocessor code so I got a little lost.

I'm not looking for anyone to implement this or give me specific instructions on how to get it done; I'm just looking for some input on whether or not this is possible and how one might go about starting it.

Thanks!

  • Hi,

    Asserts are usually redirected to stderr (not stdout, just like a normal printf would), therefore it is possible the semihost implementation in CCS does not support stderr.

    We will confirm that with a developer and get back to you.

    Regards,

    Rafael

  • Thanks for the reply Rafael. I had no idea that semihosting was responsible for this, so that's definitely something for me to look into.

    I took a quick look at the code generated by assertions in GCC and the TI ARM compiler, and I couldn't find much. The TI ARM compiler calls fputs(stderr, messagewhich the debugger has no issues handling. Writing to stderr seems to work globally with the TI ARM compiler; you can call fputs() or fprintf() from anywhere and the debugger is able to print the messages. 

    Since there's no standard library source included with the GCC that CCS uses, it's a little harder for me to tell what's going on when it fails an assertion. I stepped through the assembly enough to see that _vfiprintf_r is responsible for the brunt of the work, but I can't tell which arguments are passed in (is it trying to print to stderr? somewhere else?) or what it's doing after that point. I tried compiling the same global fputs() and fprintf() writes to stderr that worked in the TI ARM compiler, and these didn't print anything when built with GCC. So it looks like the debugging interface in CCS is smart enough to handle writes to stderr, but something about the way GCC prevents it from working.

  • Matthew Kipper said:
    I had no idea that semihosting was responsible for this, so that's definitely something for me to look into.

    Can you get printf (i.e. to stdout) to with the GCC compiler, TviaC and CCS?

    When I tried with CCS 6.0, support for printf with GCC on TivaC was not there (part of thread )

    Trying again with CCS 6.1 I still can't get printf to work with GCC and TivaC.

    Matthew Kipper said:
    Since there's no standard library source included with the GCC that CCS uses, it's a little harder for me to tell what's going on when it fails an assertion.

    Previously, I did find the source code download for GCC that allowed me to single step the GCC library code in CCS - but can't find the download link at the moment.

  • Hey, I was just looking to follow up regarding this question. I essentially forgot about it for a year, but it's reared its ugly head again.

    Can anyone familiar with the implementation of the CCS debugger comment on what the issue may be? Assuming that the TI compiler relies on semihosting for printf(), is there any reason that GCC standard library calls wouldn't be supported in the same way?
  • Sorry Chester, I sort of glazed over your post.

    I haven't been able to get printf() to work in any capacity with GCC.
  • Matthew Kipper said:
    I haven't been able to get printf() to work in any capacity with GCC.

    Using CCS 6.1.3.00033 I created a new project for a TM4C1294NCPDT using compiler GNU v4.9.3 with the following test code:

    #include <stdio.h>
    #include <assert.h>
    
    int int_a = 4;
    int int_b = 4;
    
    int main(void)
    {
    	printf ("Assert should pass\n");
    	assert (int_a == int_b);
    
    	printf ("Assert should fail\n");
    	int_a++;
    	assert (int_a == int_b);
    
    	return 0;
    }
    

    To get this to work made the following changes to the project:

    1) In the tm4c1294ncpdt.lds linker command script added by CCS add the following before the SECTIONS to set the correct entry point for when debugging:

    ENTRY(ResetISR)

    See Known issue when using CCS 6.1/Linaro GCC 4.8.4 for details.

    2) In the CCS Project Properties -> CCS Build -> GNU Linker -> Libraries replace the "nosys" library with "rdimon". "rdimon" is the library which supports semihost support to allow standard output/error to the CCS CIO console.

    When using the "nosys" library the standard output/error is silently discarded.

    When run the project reports the following in the CIO console:

    Assert should pass
    Assert should fail
    assertion "int_a == int_b" failed: file "../main.c", line 18, function: main

    The complete CCS project is attached TM4C1294_GNU_assert.zip

  • Thanks Chester, that worked perfectly! I had no idea GCC specs even existed, so I'm surprised that it only took one line in my linker config to get everything working.