Relevant gdb commands I've gathered over time: info gdb //Manual info locals //Vars in local scope info variables //Vars declared outside current scope info functions //Names and datatypes of all defined functions info b //List all breakpoints break funcName //Set breakpoint at function funcName (short: b funcName) break file::line //Set breakpoint at line in file layout next //Cycle through the layouts of gdb p var //Print the value of variable var p var = value //Force set value to a var run //Start the program start //Synonymous to (b main && run). Puts temporary b at main next //Execute the current line in source (short: n) step //Step into function call at current line (short: s) finish //Finish the execution of current function (short: fin) continue //Resume execution (After a breakpoint) (short: c) refresh //Repaint the interface (To fix corrupted interface) shell cmd //Run shell command cmd from gdb prompt python gdb.execute(cmd) //Run a gdb command cmd from python prompt set print pretty on //Enable pretty printing (Put in ~/.gdbinit) $ gdb -c core.num //Examine the dumped core file from a SIGSEGV(shell command) bt //Print backtrace break _exit //Breakpoint at exit of program whatis expr //Print datatype of expr ptype expr //Detailed print of datatype of expr watch var //Stop when var is modified watch -l foo //Watch foo loaction rwatch foo //Stop when foo is read watch foo if foo>10 //Watch foo conditionally delete //Delete all breakpoints delete breakpoint_no //Delete breakpoint breakpoint_no command breakpoint_no //Run user listed commands when breakpoint is hit (End list of commands with 'end') file executable //Load the executable for debugging from inside gdb quit //Quit (short: q) Feel free to correct/add any useful command you know.
2:14 If the compiler returns an error in the sqrt function, move the -lm flag to the end of the line. In this way: gcc -g -w -std=c99 primesProgram.c -lm
15:23 it took me so long to finally see it. Remembering that it gave the total of 29, I was pretty confident that it wasn't giving a wrong result because of uninitialized total. I looked it over many times and even read "total plus-equals arr[i]" every time I read it. Then when you showed it taking on each value in the array I looked at it again. Very cheeky. After you showed that the array was good, that total started out with the value 0, I paused the video and could absolutely not see the mistake.
getPrimes returns NULL when compiling with gcc 9.2.0 which will lead to a segfault in the `sum` function at 11:56 If you follow along the tutorial, change the line 54 in the initial version of the program to: _int *result = (int *)malloc(sizeof(int) * n);_ and _free(primes);_ before returning from _main_
Thanks Dr. Bourke! I've been programming for over 30 years in C and was looking for a good gdb instructional vid to pass on to a Windows C programmer new to linux. Yours is the best I found. You hit on most of the common errors as well as some of the best practices. Kudos!
If *layout next* is only showing assembly, use the command *layout src* . And if that doesnt work add your source code directory with *directory ./your/source/files.cpp*
Thanks for your video, it's helpfull because you show all the basic command that we need to know when we use GDB, but all the explanation you give to debug YOUR program it's not really the topic. Anway it helps me. Thank you
you or the person who wrote this code are very clever into thinking about multiple ways a person can write a buggy program and explain step by step I wish you had more subscribers. Please keep up with the good work!
Thank you for sharing this! This has helped me decide to stay with using the terminal when writing C programs. I actually am preferring the gdb debugger from the terminal rather than in the IDE. Awesome!
surprisingly I had to use the -lm at the very end for the linker to work... at the standard position gcc -g -lm ...it gave me a linker error and failed to recognize sqrt on line 71.
15:50 You're sure that the unary `+` operator "makes things positive"? :q If that would be the case, we wouldn't need the `abs` function :J The `+` operator just leaves whatever sign there is without any changes. So if the number was negative to begin with, it would remain negative; it won't flip to positive.
Yes, very well noticed. The statement is not correct. A negative value does NOT become positive with the + sign. It is also NOT true that a value becomes negative with a - sign in front of it. The "-" causes a NEGATION of the value. If a number is already negative, a - sign makes it positive.
I doubt that it is sieve algorithm. It actually creates an array and marks all non primes as by marking all factors of new primes which you get while traversing the array. Nice tutorial though.
I will forever be haunted by the looming "=+" operator Also, I implemented my own algorithm. I think the total for 10,000,000 is 870,530,414,842,019. It still took 4m13.713s to calculate
I have a problem when I wrote layout next then layout window is open but after write run then nothing is printed on layout window, so please help me what mistake I did
Unfortunately this is probably not enough information to go on. My guess would be that you didn't compile with the -g flag and so no code/symbols were available to GDB. You might ask a colleague or someone in person that would be able to observe the problem directly.
Late but does anyone know how to fix "layout next" not showing source file after i do "backtrace full" at 19:28 ? Before this, i also put in the -g flag when i compiled with gcc.
how do you use step if there are multiple functions on a line. For instance I have been trying to use it in c++ where there is a cout statement that prints the result from a function in the stream. How do I step into that function without stepping into the cout function and getting lost there?
The stepi command may help (step 1 instruction at a time), though it may take several applications to get to the point you want to. If they are *nested* functions, you can simply set a break for the function itself.
I could not get up the code using "layout next". I compiled it in windows like "gcc -w -g -lm -std=99 -w -o debug_test.exe debug_test.c" and then I entered GDB with "gdb debug_test.exe". I ran the program first "r" and I also tried typing "layout next" first. The I tried compiling with "gcc -ggdb" also. Can someone tell me if I am doing this wrong?
I have question if anyone ever encountered it, I have 2 codes exactly same but when I debug one it works fine and the other doesn't, it's loops are not executed even when the code is same. Can it be compilation problem or something else?
I ran into the same problem running the exact same code with different compilers. One optimized the whole loop away and the other didn't. E.g. gcc on WSL and Ubuntu didn't run the loop body, but gcc on MinGW did.
Sorry, moved them last year; here's the link to the debugging hack for this course covered by this video: github.com/cbourke/ComputerScienceI/tree/master/hacks/hack-debugging
how did you download the text user interface (tui) for gdb?, because when i try to use the -tui flag when running gdb it does not work and outputs something like ```gdb.exe tui not supported```. I've read that if I don't have it installed, the command ```layout next``` does not work.
It was available for me in terminal in Ubuntu 18.04, in Windows Subsystem for Linux, and in Git Bash for Windows (which uses MinGW) but not available for me in MSYS MingGW. Ubuntu 18.04 and WSL I just installed gcc from apt-get and didn't do anything else special. Hope it helps!
Relevant gdb commands I've gathered over time:
info gdb //Manual
info locals //Vars in local scope
info variables //Vars declared outside current scope
info functions //Names and datatypes of all defined functions
info b //List all breakpoints
break funcName //Set breakpoint at function funcName (short: b funcName)
break file::line //Set breakpoint at line in file
layout next //Cycle through the layouts of gdb
p var //Print the value of variable var
p var = value //Force set value to a var
run //Start the program
start //Synonymous to (b main && run). Puts temporary b at main
next //Execute the current line in source (short: n)
step //Step into function call at current line (short: s)
finish //Finish the execution of current function (short: fin)
continue //Resume execution (After a breakpoint) (short: c)
refresh //Repaint the interface (To fix corrupted interface)
shell cmd //Run shell command cmd from gdb prompt
python gdb.execute(cmd) //Run a gdb command cmd from python prompt
set print pretty on //Enable pretty printing
(Put in ~/.gdbinit)
$ gdb -c core.num //Examine the dumped core file from a SIGSEGV(shell command)
bt //Print backtrace
break _exit //Breakpoint at exit of program
whatis expr //Print datatype of expr
ptype expr //Detailed print of datatype of expr
watch var //Stop when var is modified
watch -l foo //Watch foo loaction
rwatch foo //Stop when foo is read
watch foo if foo>10 //Watch foo conditionally
delete //Delete all breakpoints
delete breakpoint_no //Delete breakpoint breakpoint_no
command breakpoint_no //Run user listed commands when breakpoint is hit
(End list of commands with 'end')
file executable //Load the executable for debugging from inside gdb
quit //Quit (short: q)
Feel free to correct/add any useful command you know.
küss dein herz
@@stelio1425 Isso
Your a legend
Enter //Repeat last command
Thank god for actual educators creating youtube tutorials! Thanks Chris!
amen
one of the most under-rated gdb tutorial
Agreed. I've watched a few recently and they just move through it much too quickly. This one was a good pace and covered some nice basic functions.
" I wonder what's the sum of first ten million numbers?
It's a segmentation faul"
Lmao ded
Yes there is no enough space
Lmao xD
Going to college in useless, unless you get a teacher such as this gentleman. The best gdb introduction on the net, thank you guru
Finally a program with more bugs than mines.
2:14 If the compiler returns an error in the sqrt function, move the -lm flag to the end of the line. In this way: gcc -g -w -std=c99 primesProgram.c -lm
underrated comment... thanks
Thanks a lot😊
After 5 years, still the best and most detailed quick tutorial about GDB on CZcams.
15:23 it took me so long to finally see it. Remembering that it gave the total of 29, I was pretty confident that it wasn't giving a wrong result because of uninitialized total.
I looked it over many times and even read "total plus-equals arr[i]" every time I read it. Then when you showed it taking on each value in the array I looked at it again. Very cheeky.
After you showed that the array was good, that total started out with the value 0, I paused the video and could absolutely not see the mistake.
getPrimes returns NULL when compiling with gcc 9.2.0 which will lead to a segfault in the `sum` function at 11:56
If you follow along the tutorial, change the line 54 in the initial version of the program to:
_int *result = (int *)malloc(sizeof(int) * n);_
and _free(primes);_ before returning from _main_
Bless you. This was making me crazy.
dear my gcc is 9.4.0 still i am getting the segfault @@lindyrauchenstein8716
Thanks Dr. Bourke! I've been programming for over 30 years in C and was looking for a good gdb instructional vid to pass on to a Windows C programmer new to linux. Yours is the best I found. You hit on most of the common errors as well as some of the best practices. Kudos!
Best gdb tutorial I found, Thank you!
Thank you so much Chris, the way you walked us through the complete code and debugging was really helpful.
If *layout next* is only showing assembly, use the command *layout src* . And if that doesnt work add your source code directory with *directory ./your/source/files.cpp*
Thanks!
Thanks for your video, it's helpfull because you show all the basic command that we need to know when we use GDB, but all the explanation you give to debug YOUR program it's not really the topic. Anway it helps me. Thank you
hands down best GDB video WOW so in depth and so much insight
you or the person who wrote this code are very clever into thinking about multiple ways a person can write a buggy program and explain step by step
I wish you had more subscribers. Please keep up with the good work!
Explained like a true champion, very well through guide, thanks Chris
I have used gdb server first time. Good tutorial. Great thanks from Slovak Republic !
I was getting No file executable error for sometime but referring to your video the issue is resolved. Thank you so much.
Thank you for sharing this! This has helped me decide to stay with using the terminal when writing C programs. I actually am preferring the gdb debugger from the terminal rather than in the IDE. Awesome!
Loved the layout next command. I didn't know you could see the code while stepping through.
Yup, this should have been the default mode long time ago :q
例子设计的简单明了,对GDB入门真的很有帮助,谢谢!
I didn't know the argv@2 trick to set the size of the array print out. Nice.
surprisingly I had to use the -lm at the very end for the linker to work... at the standard position gcc -g -lm ...it gave me a linker error and failed to recognize sqrt on line 71.
Dude the keyboard clicks and voice... accidentally top-notch asmr.
15:50 You're sure that the unary `+` operator "makes things positive"? :q If that would be the case, we wouldn't need the `abs` function :J The `+` operator just leaves whatever sign there is without any changes. So if the number was negative to begin with, it would remain negative; it won't flip to positive.
Yes, very well noticed. The statement is not correct. A negative value does NOT become positive with the + sign.
It is also NOT true that a value becomes negative with a - sign in front of it. The "-" causes a NEGATION of the value. If a number is already negative, a - sign makes it positive.
Absolutely amazing video!
Thanks a lot Chris. This video is totally educative.
Just one word superb
I want to redo all my C assignments now. I was doing them sooo wrong the whole time. Dr. Bourke you rock!
really good demonstration.
Great one for the beginner of GDB.
Liking gdb more and more
king of gdb
I doubt that it is sieve algorithm. It actually creates an array and marks all non primes as by marking all factors of new primes which you get while traversing the array.
Nice tutorial though.
gcc -pedantic
Holy shit. Thank you so much! This lesson helped me a lot!!!
Thank you for the awesome tutorial! I was hoping you'd show how to detect the memory leak in the end?!
Best gdb tutorial on youtube! Thank you!
Thanks a lot, teacher! I began to love gdb....
Excellent tutorial. Thanks!
I will forever be haunted by the looming "=+" operator
Also, I implemented my own algorithm. I think the total for 10,000,000 is 870,530,414,842,019. It still took 4m13.713s to calculate
I'm happy that I've found a good channel like your. Video liked and channel subscribed
Helped so much, incredibly grateful
Thank you kind sir. you explained it simple but in great detail.
This is really great video.
the examples used are good.
Great intro to GDB, thank you
Perfect presentation and teaching thank you for your work kudos.
Good tutorial, that's exactly what I am looking for!!!
Thank very illuminating tutorial n the technique demonstrated.
When I compiled multiple files for a program it only showing addresses and only the main function.
I am good now. I used list command to show the code. Thank you
Debugging is like heaven 😍
awesome tutorial. been wondering how to do this :D
Great tutorial ; for completeness, would it be able to use gdb to debug the missing free of the malloc memory at the end ?
It would be better to use a dynamic analysis tool such as valgrind to detect memory leaks.
GDB seems a good debugger
What a great tutorial!
REALLY GOOD tutorial! Thank you! :)
Your tutorial is amazing.
Thank you so much for such clear tutorial!
Thanks for the nice tutorial, Where should i get the presentation.
This tutorial is goated... That's why u the g.o.a.t
very helpful. Thanks!
thank you sir, awesome!
Sir, this is simply amazing. God bless you & your family. May I ask which playlist is this video part of?
Isn't this placed in a playlist?
Great tutorial!! Thanks
Such a great tutorial.
I have a problem when I wrote layout next then layout window is open but after write run then nothing is printed on layout window, so please help me what mistake I did
Unfortunately this is probably not enough information to go on. My guess would be that you didn't compile with the -g flag and so no code/symbols were available to GDB. You might ask a colleague or someone in person that would be able to observe the problem directly.
could someone help me out...I use windows and the layout next command isn't identified...can I do something about it...please do help
Very helpful tutorial!
Thank you! This is a great resource.
Late but does anyone know how to fix "layout next" not showing source file after i do "backtrace full" at 19:28 ? Before this, i also put in the -g flag when i compiled with gcc.
how do you use step if there are multiple functions on a line. For instance I have been trying to use it in c++ where there is a cout statement that prints the result from a function in the stream. How do I step into that function without stepping into the cout function and getting lost there?
The stepi command may help (step 1 instruction at a time), though it may take several applications to get to the point you want to. If they are *nested* functions, you can simply set a break for the function itself.
I could not get up the code using "layout next". I compiled it in windows like "gcc -w -g -lm -std=99 -w -o debug_test.exe debug_test.c" and then I entered GDB with "gdb debug_test.exe". I ran the program first "r" and I also tried typing "layout next" first. The I tried compiling with "gcc -ggdb" also. Can someone tell me if I am doing this wrong?
Well explained. Thank you!
Thanks for the tutorial,so useful for beginners people in gdb.
Which text editor is he using?
this was awesome. I enjoyed every second. thanks a lot 🖖
Very elegant thank you
Insane information thanks a lot
every time I want to print something (for example x) it says : "no symbol "x" in current context."
do you know what is the problem?
This is brilliant.
I have question if anyone ever encountered it, I have 2 codes exactly same but when I debug one it works fine and the other doesn't, it's loops are not executed even when the code is same. Can it be compilation problem or something else?
I ran into the same problem running the exact same code with different compilers. One optimized the whole loop away and the other didn't. E.g. gcc on WSL and Ubuntu didn't run the loop body, but gcc on MinGW did.
Hi on layout next mode why when I press ENTER on the screen outputs "W", and it does not move to next line?
at line 69 could you just `return x == 2;` ?
Where can o get the program primesprogram.c ?Thanks in advance
Thanks a lot!
Great video
This is amazon thank you!
excellent
thank you so much. But i can't access the files for doing by myself. Is it possible to update the link please ?
Sorry, moved them last year; here's the link to the debugging hack for this course covered by this video: github.com/cbourke/ComputerScienceI/tree/master/hacks/hack-debugging
@@ChrisBourkeUNL Thanks
layout next is not working for me
Thank you
how did you download the text user interface (tui) for gdb?, because when i try to use the -tui flag when running gdb it does not work and outputs something like ```gdb.exe tui not supported```. I've read that if I don't have it installed, the command ```layout next``` does not work.
It was available for me in terminal in Ubuntu 18.04, in Windows Subsystem for Linux, and in Git Bash for Windows (which uses MinGW) but not available for me in MSYS MingGW. Ubuntu 18.04 and WSL I just installed gcc from apt-get and didn't do anything else special. Hope it helps!
@@lindyrauchenstein8716 thanks for the info!
great
謝謝你~
Let’s “fire up” 😂😂❤️👌🏼
🔥🔥🔥
Good tutorial, thank you! It did help me!
23:41 it's been 4 years, still waiting for the result