Thread overview
Is there a way to force emitting of stack frame for a specific function?
Oct 08, 2020
Andrey Zherikov
Oct 08, 2020
Imperatorn
Oct 09, 2020
Andrey Zherikov
Oct 09, 2020
Johan
October 08, 2020
I'm trying to implement some mix of native and custom stack trace dump for exception logging: I want to print custom text instead of some stack frames. To clarify more: I have a function that I want to substitute with other text in stack dump and this function has to be in stack dump to make this working.

The problem I faced is that dmd might not generate stack frame for a function - I don't know its logic but it depends on the complexity of the function itself and its caller.
I tried '-gs' dmd option and seems it solved the issue but it forces compiler to generate stack frames for all functions which seems overhead for me. I also tried putting "pragma(inline, false)" into my function but it doesn't guarantee that stack frame is generated for it.

I'm wondering whether there is a way to explicitly tell compiler "please always generate stack frame code for this function".
October 08, 2020
On Thursday, 8 October 2020 at 08:48:45 UTC, Andrey Zherikov wrote:
> I'm trying to implement some mix of native and custom stack trace dump for exception logging: I want to print custom text instead of some stack frames. To clarify more: I have a function that I want to substitute with other text in stack dump and this function has to be in stack dump to make this working.
>
> [...]

Can you share some code? You don't want to use try-catch?
October 09, 2020
On Thursday, 8 October 2020 at 13:13:16 UTC, Imperatorn wrote:
> Can you share some code? You don't want to use try-catch?

Here is minimal example that illustrates this:
=====================
import std.stdio;

void do_assert() { assert(false); }

void stackFrame()
{
    //pragma(inline, false);
    do_assert();
}

void main()
{
    try
    {
        stackFrame();
    }
    catch(Exception e)
    {
        e.writeln();
    }
}
=====================

Stack trace with "dmd -g -O -run test.d" command (no stackFrame function in it):

0x0040CC6B in _d_assertp
0x00410CFB in void rt.dmain2._d_run_main2(char[][], uint, extern (C) int function(char[][])*).runAll().__lambda1()
0x00410C75 in void rt.dmain2._d_run_main2(char[][], uint, extern (C) int function(char[][])*).runAll()
0x00410B10 in _d_run_main2
0x0040CDB6 in _d_run_main
0x0040234C in main at C:\D\dmd2\windows\bin\..\..\src\druntime\import\core\internal\entrypoint.d(30)
0x7653F989 in BaseThreadInitThunk
0x77CB74B4 in RtlGetAppContainerNamedObjectPath
0x77CB7484 in RtlGetAppContainerNamedObjectPath

=====================

Stack trace with "dmd -g -O -gs -run test.d" command (stackFrame function is there):

0x0040C573 in _d_assertp
0x004022C4 in void test.stackFrame() at C:\Users\andrey\test.d(7)
0x004022F9 in _Dmain at C:\Users\andrey\test.d(14)
0x00410603 in void rt.dmain2._d_run_main2(char[][], uint, extern (C) int function(char[][])*).runAll().__lambda1()
0x0041057D in void rt.dmain2._d_run_main2(char[][], uint, extern (C) int function(char[][])*).runAll()
0x00410418 in _d_run_main2
0x0040C6BE in _d_run_main
0x00402353 in main at C:\D\dmd2\windows\bin\..\..\src\druntime\import\core\internal\entrypoint.d(30)
0x00434535 in mainCRTStartup
0x7653F989 in BaseThreadInitThunk
0x77CB74B4 in RtlGetAppContainerNamedObjectPath
0x77CB7484 in RtlGetAppContainerNamedObjectPath

=====================

The question: is it possible to tell compiler to always generate stack frame for my stackFrame function?
"pragma(inline, false)" doesn't help here - the result is completely the same.
October 09, 2020
On Friday, 9 October 2020 at 02:01:30 UTC, Andrey Zherikov wrote:
> 
> The question: is it possible to tell compiler to always generate stack frame for my stackFrame function?
> "pragma(inline, false)" doesn't help here - the result is completely the same.

(Do you mean a stack frame, or a frame pointer?)

If you use LDC, you can specify --frame-pointer=all.

bin/ldc2 --help | grep "frame"
  --frame-pointer=<value>   - Specify frame pointer elimination optimization
    =all                    -   Disable frame pointer elimination
    =non-leaf               -   Disable frame pointer elimination for non-leaf frame
    =none                   -   Enable frame pointer elimination

cheers,
  Johan