I noticed some incorrect uses of base::debug::Alias which led me to file a couple of bugs (
crbug.com/756589 and
crbug.com/756544) and update the
comment block in front of the function. But I also wanted to write a quick PSA about how to use this function effectively.
If you pass the address of a local variable to the Alias() function this "fools" the compiler into thinking that the local variable is being externally referenced, and it prevents the compiler from optimizing away or discarding the local variable. This then means that the local variable will still be present on the stack, right to the end of the enclosing scope. This can be used to ensure that interesting local variables are present in crash dumps.
However, the Alias() function isn't magic. It has an empty implementation and its magic only works because the compiler doesn't know that. As such, there are many things that it cannot do:
1) If the local variable whose address you pass is a pointer then the value of that pointer will be on the stack. However the memory that it points to will probably not be. Crash dumps generally only record stack memory, not heap memory or global variables.
2) If you pass the address of a member variable or global variable then the Alias() function will have no effect. They were never going to be optimized away so the Alias() call is a NOP.
3) If you pass the return value of a function then the Alias() function will have no effect - there is nothing to avoid optimizing away.
To use the Alias() function effectively you must copy the state that you care about into POD locals and call Alias() on them (before or after, it doesn't matter), and then trigger a crash dump before they go out of scope.
If you can think like a compiler and if you know that minidumps generally only record stack memory then this will all make sense, but if not just remember that Alias(&local) will retain the value of local on the stack, and nothing else.
It is possible to add additional memory ranges to a crash dump, however such a capability would have to be used much more carefully than the cheap-and-simple trick of the Alias() function.
I hope this is helpful.