Static and non-static functions - debugging embedded systems

I was puzzled by the following question: How to maintain the advantage of a “static” tag, but still be able to debug production code on a site?

It sometimes happens that inadvertent behavior occurs on the client’s site and only there. In many cases, debugging can save a ton of effort and provide a very fast response. Such a debugger usually includes checking the behavior of the function, which leads us to a "static" definition.

Static functions cannot be debugged from the debug shell, for example, set breakpoints or execute it. On the other hand, defining all functions as open causes causes code structure and optimization of sadness.

I am aware of options such as compiling at least two different assemblies: one with statics and one without it, but it is well suited for automation tests, and not for the final assembly of an assembly, which eventually disappears.

It will appreciate some considerations on your part, mainly about how you resolved (if any) this dilemma. Or rephrase the question: " What is more important? "

A good discussion of "static" in C is here .

+7
source share
3 answers

I think the main question is not: "Do you send with" statics "or not?", It is "Do you check what exactly you send?" For inline code, if you do most of your testing in the Debug assembly, and then send the Release version compiled with various parameters, you are essentially sending unverified code to your client. When you work near the hardware, small changes in the time or memory access patterns (which the optimizer can easily imagine) can cause large changes in the behavior of the system.

My current strategy is to submit a version of Debug configured for such an optimization as I can withstand while debugging. There are no static functions, the maximum possible visibility state of the debugger.

Yes, I give some possible compilers with the generated efficiency, but for now I guarantee that the Debug version is fast enough to meet its requirements, this is not a problem. And the gain is that the code I submit is exactly the same code that I tested with the whole release cycle - no surprises caused by the optimizer.

+3
source

Some debuggers may debug "static" functions. Sometimes static functions are expanded in a line on the call site, although this makes the debugger difficult, and some debuggers just give up.

(The Inline extension on the call site is actually not a property specific to the “static” one, it is simply that compilers are more likely to do this because they “know more” about the function, namely that the name is not visible behind outside the current translation block, so the function code can be omitted completely if all its calls are expanded in a line.)

It was once customary to use a macro:

#ifndef STATIC # define STATIC static #endif ... STATIC void somefunc() { ... } 

and then turn the macro into nothing for debugging collections. This works very well, but it's even better to find a debugger smart enough to handle static functions, even if they are extended by built-in functions.

+1
source

Can you set a breakpoint in a public function that calls a static function? Then you can check the arguments and at least return the values. If the problem occurs only on the client’s site, and the tests of your device / integration do not show problems for the inputs that you expect from these functions, the problem is that these functions receive input data (or a sequence of inputs if the function is somehow restrained), you did not expect, which means that the actual problem may be outside the static functions that you are looking at.

If you already suspect that you know which static function contains this problem, and you know what data types you are calling, you can put a simple unit test function in the release build to check for a suspect error. Of course, this is complicated if this function directly controls, for example, a six-ton ​​crane, in which case you may have to write two versions of this function, one with a layout for controlling the crane, and run tests on it.

Most likely, but not impossible, never rule out a compiler inconsistency between release and debug versions. We all love to think that compilers are infallible, including me, but everything happens. And I see in your answer to the track that providing the client with a debug build sometimes fixes the problems ...

+1
source

All Articles