how to execute step by step in C language? [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I want to know how to execute step by step for my c program. I'm not getting answer as expected. So please tell me how to see step by step execution. I'm new to linux

Compile the program with '-g' option
example as
Compile the program
gcc test.c -g
which will generate a.out pass it with gdb
gdb a.out
then set breakpoint to main
gdb) break main
Then run your program in gdb
gdb) run
then break point hits use 'n' or 'next' to step to different lines
gdb) n
Use 's' for stepping into function and 'p' printing var value
Example :
gdb) s <fun_name>
gdb) p x

More convenient than gdb (especially for a beginer) may be some of IDEs. I'd suggest qtcreator.
At least a makefile (for ready project) will be required. You can also create a new project in qtcreator and import your files.

There are many debugging tools in Linux. I prefer gdb.
Usage:
while compiling use -g flag with. Ex:
gcc -g *.c
to see step by step execution use gdb tool:
ex: gdb ./a.out
Then give start command to start main function.
Then press s and continuosly press enter it will execute step by step.
Note: if it's a library function please press n instead of s.
to quit from execution press q.
Please refer –-help on debugging time to know more info. There are many instructions you have to know like run, break, next, info breakpoints..........

Related

How to execute a C program in VS Code

Okay, I am an idiot and I cannot figure out how to compile and run C program in VS Code. How should I do it? I typed in the code itself, so what is next in order for me to see the output in terminal? Thanks for accepting my stupid
link your compiler to vs code. if you are using c, it should be gcc or some other fork. to use the compiler just open the terminal, go to gcc or what your compiler name is, and then run it in terminal(if you right click on code)

map c language into assembly language [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm new to c language and want to establish the intuition to map c language into assembly language so that I can have a strong sense of how the stack, register, memory, code work when I see the c code. I have read several assembly language books, which is mainly focused on the assembly syntax rather than the relationship between c and assembly. Does anyone know where can I find such book about the map between c and assembly languages.
I don't know if such a book exists (if it does, it'll likely be a book about compilers). However, there's an easier solution: try it.
Write some C code, then compile it with debug symbols (these instructions assume linux):
gcc foo.c -o foo
Then, use a debugger:
gdb ./foo
break MyFunction
run
disass
This will set a breakpoint on MyFunction, then run the program until it reaches that breakpoint. disass will print the assembly for that function. You can use stepi to step one instruction at a time, or next to step one C line at a time.

display filename, line number and function in C without modifying the source code [duplicate]

This question already has answers here:
how to trace function call in C?
(10 answers)
Closed 8 years ago.
I am new in a company, working with C source code which almost lacks any kind of tracing mechanism.
I would like to know whether or not the application passes through a certain file and where (which function).
I could do this using breakpoints, but the concerned file contains a huge lot of functions.
Therefore I'm looking for some kind of tool, that I can attach to the application, and that gives an output of following kind:
-- Main.c (main_function())
---- submain.c (submain_function())
...
From that, I then could deduce where (which filename, which function) the application is passing.
Does anybody know whether or not such a tool exists?
Thanks
If you're on linux, gdb might come handy.
You can compile the code using -g or -g3 option with gcc, then run the binary using gdb ./<executable_name>, set a breakpoint on desired function in any of the source files and check the call.
While stepping through the application, it will show the filename and line number of the executing instruction.
Note: Please check this and this for a detailed understanding.
I assume you develop on Linux. Then you could also customize the GCC compiler, in particular using MELT (a lispy domain specific language to extend GCC), to have the compiler add some logging at many places. For that you'll need to insert a new GCC "optimization" pass doing such a job, and most importantly, you'll need to understand some details about GCC internal representations (Gimple, Tree-s, Basic blocks, ...)
However, that would probably require more than a week of work from your part. Unless your code base is really big (at least half a million
of lines) that might not worth the effort

How to determine the value of passing arguments of a function via the backtrace of a process? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a backtrace of a process. and I want determine the value of a argument of one function called in the call trace, I have the .o file and I disassemble it, So I have the assembly procedure of that particular function, How can I calculate the value of the function's passing argument through the backtrace and the assembly code? It's on the ARM platform, I'm not quite familar with the ARM call trace, and assembly code.
If the code is written in C, this information is not available solely from the executable image, for the simple reason that C does not mangle function name symbols in order to encode the function parameter types.
Brief experimentation with gdb shows that if C code is compiled with the debugging flag, -g, gcc does put sufficient information into the executable's debug records for gdb to be able to figure out the function parameter types, and display function arguments in the backtrace.
But, if the executable is not compiled with -g, all that's in the executable are the function names, and their addresses, and that's all that gdb can show, in a backtrace from a coredump.
So, if you're working with .o files containing C code, without any debug stuff in it, there's nothing that can show you what the function parameters are,

Linking files with LD [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I read this tutorial: http://www.osdever.net/tutorials/view/writing-a-simple-c-kernel
I tried linking the files using the likerscript that the tutorial provides. But LD gives me an error saying that it cannot read the file put out by nasm. Does anyone know what I am doing wrong?
If you executed the tutorial precisely as shown, then the problem is most likely here:
nasm -f aout kernel_start.asm -o ks.o
This produces an object file in the thoroughly obsolete a.out format. You're probably working through the tutorial on either a Windows or a Linux host system; the linkers that come with these systems expect object files in PECOFF and ELF format, respectively. There is probably another thing you can put after the -f in the above command that will make nasm produce the correct format.
Alternatively, learn to write AT&T assembly language instead. Then you can make an object file out of your .asm file with gcc -c just like the C source code, and you will automatically get the right format. The AT&T equivalent of the trivial startup file you have in that tutorial would be
.text
.globl start
start:
call k_main
cli
hlt
Take note also that I removed the leading underscore from the call instruction's argument. That underscore is only appropriate if the C code is compiled to an a.out-format object file, which (we suspect) it isn't.

Resources