Every IDE falls into disassembly mode during debugging - c

I have installed Linux (Ubuntu 16.04) recently, then QtCreator. Every time when I try to debug my C program in it, it falls into disassembly mode, it means when I try to step into, or even step over, it opens a file "Disassasembly(...)" and navigates in this file. It happens not only for standard functions like malloc (which source code I don't have), but also for my own functions. I thought it's because of the IDE, and installed NetBeans - and I have exactly the same problem in it.
Some example:
[it is shown after stepping over my function, and this function is just the beginning of my program, but the program ends after this screen] - edit: sorry I had to remove this file, because I can post only 2 links..
Another problem is when even for a short moment it navigates on my code (not on disassembly), it executes lines which contains documentation, and gets out to the body of another fuction, which isn't called by me at all(!) It looks like a mess...
So, I think it might be some problem with GDB, but I have no idea how to solve it. Could you help me?
GDB v. 7.11.1
gcc v. 5.4.0
NetBeans v. 8.2
EDIT: It became really weird. Today, when I tried to debug it once again, without any changes in code nor settings, the debugger started to behave almost normally, it means, that it steps through my code correctly now. The only moment, when it falls into dissasembly mode, is the end of my program, when last instruction has executed and it should end. This screens should help:
(1)
(2)
EDIT2: Ok, maybe it is quite normal now. But I still have no idea, what was wrong, and what solved the problem. I hope that it won't come back :)

Related

Keep getting indentifier undefined error in C using any ide

So this is my first post. I am preparing for some upcoming college courses on programming, one of them deals with C. I have been teaching myself for a little while and have never encountered this problem before, even though I have used the same code. I am having the issue on any ide that I use, which are MS Visual Studio 2019, and Codeblocks, both 16.01 and 17.12.
The problem is when I try to include an inline function, the compiler brings up an error: identifier "keep_window_open" is undefined, as well as the error: 'keep_window_open':identifier not found.
This never happened until I upgraded Codeblocks from 16.01 to 17.12, so that leads to believe something happened with the compiler or some settings that I don't know about, or how to change.
I have tried going back to Codeblocks 16.01, but still get the same error. All my previous programs, that use the exact same code still compile and run, so I am at a lost and completely frustrated. Enough so to think about changing my major, which I really do not want to do. I enjoy programming. But this problem is killing me.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
inline void keep_window_open() { char ch; scanf("%s", ch); }
int main()
{
printf ("Lets see if this stupid thing works\n");
keep_Window_open();
return 0;
}
The problem is the line of code:
keep_Window_open();
If I comment it out, the program will compile and run, however I can't see it, because all it does is flash and then close the console window. And I need to use this code to see the output window. As I have said I have used this code in many other programs, most of which are much more complex than this, and they all still compile and run. It is only on new projects that I try to create.
I expect the window to stay open until the user presses and enters a key, like it always has before. I am stuck, and would appreciate some input on how to proceed, other than throw myself in front of a bus.
OK, so I was able to solve the issue in CodeBlocks, but not in Visual Studio. I changed the code to have the inline function below the main function, and now when compiled and run in CodeBlocks (17.12) it works exactly like it should, or how I thought it should.
However when I try the same solution in Visual Studio, after putting the inline function below the main function I now get a new error:
'keep_window_open' redefinition basic types
Since I usually use CodeBlocks for C, Visual Studio for C++ and intellij for java, I am not going to worry about this, as long as it works in CodeBlocks I should be OK. However I am curious as to why it work in one IDE and not the other?
I am new to this programming thing, and there is much to learn, and I know virtually nothing about compilers and linkers, as I am focusing on programming techniques and syntax in several different languages.

Is it possible to add a getchar(); equivalent to a .o file?

I created a simple C program a while ago. It's a simple command-line generator that takes some number, prints the results and stops. I always ran it in the editor's command line enviroment that automatically paused after the program ran, so I omitted adding a getchar() at the end.
I now regret this, because I managed to lose the source. All I have now is the complied .o and .exe file, and the latter - of course - exits immediately after it prints the output, so it's unusable. It wasn't that long, about 100 lines, but I'd like to avoid rewriting it. (Also, I might even learn something new from this way.)
Now I have very basic knowledge of C, and about zero on computer-degree x86 assembly (though I learnt the basics of 8086-assembly for microcontrollers, it won't be that helpful now I guess), so I'm kinda stuck here. Can I either add that getchar() like pausing function to the complied code, or is there any way I can make that .exe stop before exiting while still keeping it standalone?
The program will run on a Windows 10 system.
I would write some sort of batch script in which you call your program and then just run pause, which waits for you to hit a key before it continues.
wrapper.bat:
yourprogram.exe
pause
Of course you can disassemble your executable into raw x86 assembly code, then look up the code for a simple getchar() on Windows, add that and reassemble. However, it would probably be less time consuming to rewrite the program, depending on how complex it was or just create a wrapper batch-script.
It's possible to hijack .o file, you can even do it with .exe, .dll, ... but it's not simple and requires a lot of know-how. What I would suggest is to use some sort of decompiler to try to restore the original source code, make the change and compile it again. You can find suggestions of decompilers in this old answer.

Matlab crashes with S-function after mdlOutputs

I'm doing my "main" S-function based on the Matlab template with mdlStart mdlOutputs, etc, which communicates with some Ansi C files that I had, and this S-Function is used in Simulink. I compiled the files correctly with mex and in order to debug I'm using Visual Studio 2015. I can set and use breakpoints so everything is working fine.
The problem is, after finishing 'mdlOutputs' function, where all the contents are correctly printed into Matlab the debugging goes into a breakpoint and it goes into 'simulink.c'. After that, the Debug is broken saying that "libmex.pdb" cannot be found.
If I run the Simulink model without Visual Studio in the loop, Matlab just crashes and stops working.
So, any idea on how to check properly where it is giving me an error? Also, do you have any clue on why the process crashes after leaving mdlOutputs and before entering mdlTerminate? What happens in-between these two functions?
I'm using Windows 7, 64-bit and Matlab 2012b (I'm going to try and run it in the 2015b).
I think that I finally found out the solution (even though I quite don't understand it yet).
In the mdlInitializeSizes(SimStruct *S) I substituted ssSetOptions(S, 0); by ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE); like it is suggested here.

String Manipulation in C, <optimized out> error

I was solving cs50x pset6 Webserver problem and I would be very grateful if someone could help me.
Can anyone explain what is happenning below in gdb(also in source code itself) please?
Optimized out and Gone String Variable
The problem is that manipulation string variables is needed in this problem set, but somehow I lose them whenever new variable comes up(along).
I tried compiling differently as suggested by some sources e.g. https://cs50.stackexchange.com/questions/9033/variable-optimized-out-eventhough-it-will-be-used-later .
But nothing has changed even I tried other solutions.
So guys I will appreciate if you can help me to fix this issue.
Here is my source code(lines 151-391): http://codepaste.net/2uipag
Edit: This error is affecting not just debugging mode, but the program itself isn't giving intended result. Basically:
Line ="GET /cat.html HTTP/1.1"
absolute_path="/cat.html" (extracted using strncpy())
root="/home/jharvard/pset6/public"
path= root along with absolute_path (strcat())
Because absolute_path is optimized out instead of getting "home/jharvard/pset6/public/cat.html" for path variable, I am getting "home/jharvard/pset6/public"
I apologize for being unclear and thank you for assistance.
It is difficult to say what is happening with any specificity, but most likely, at some point after the last use of your line pointer by the function, the optimized version of the program re-uses its storage for something else. If the variable is still in scope at that point then in principle you should still be able to examine its value, but if your program in fact does not examine its value then it makes no difference in program behavior. Except as viewed via a debugger.
If you intend to run your code in a debugger then it is best to compile it (for that purpose) with optimization disabled (option -O0 for many C compilers, including gcc). After you have worked out the problems, compile your final program with whatever optimization level you deem appropriate.

System calls not working in Atmel AVR Studio (with ASF)

I am not getting answers on the AVR Freaks forum and wonder if someone here could help me.
The answer might lie in this SO question, but I am not sure why it would be necessary.
Basically, I have my fist ever Atmel project (AVR studio 6, UC3 processor). The code compiles and links and I can load it to the Atmel board and step through in the debugger.
However, when I try to step over (or run until a breakpoint on the line after) a (valid) call to sprintf(), malloc() or memcpy() (there may be more, which I have not yet discovered), the IDE never returns to the next line of my code, just seeming to hang, or run forever.
[Note] Compiler optimization is off
Do I need to set some linker options (e.g link static (which I tried & it didn't help)? Or build with some library?
What confuses me is that the code compilers and links - what is being linked when I call these standard functions? If I need something else I would expect a compiler or linker error, but get none - so why won't my code run?
Sorry for such a stupid n00nb question, but it is my first micro-controller project.
I discovered that the CPU on my board is an Engineering Sample and not supported by Atmel Studio without a new io.h file.
I sort of figured that out from this question: http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=106652
Sorry to have troubled you.
what is being linked when I call these standard functions?
The AVR-libc, the implementation of the C standard library ported to the AVR platform.
so why won't my code run?
Compiler errors and runtime errors are not even related. Both of these lines are valid C and they compile, however, on most systems, I'd expect them to dump core:
int x = 1 / 0;
*(int *)0 = 41;
So it might be either:
a bug in the standard library (very unlikely), or
a bug in the online debugger (very unlikely), or
maybe you just expect something that is not supposed to happen?
Instead of trying to step over, what happens if you set a breakpoint at next line after the line you want to step over?
Also, does the operation change if you turn off compiler optimization?

Resources