9.2 - Debugging - GDB Tutorial

  Рет қаралды 307,068

Chris Bourke

Chris Bourke

Күн бұрын

We demonstrate how to use GDB to debug a C program.
Files for this demonstration are available here: github.com/cbourke/ComputerSc...
0:00 GDB
0:15 Introduction
0:48 Getting started
1:21 GDB commands
1:35 Demonstration
2:32 Infinite loop
6:10 Infinite loop 2
10:10 Infinite loop 3
11:56 Incorrect sum
16:18 Invalid CLAs
19:10 Seg fault
21:09 Seg fault 2

Пікірлер: 157
@benbernardy9754
@benbernardy9754 4 жыл бұрын
Thank god for actual educators creating youtube tutorials! Thanks Chris!
@pikuma
@pikuma 4 жыл бұрын
amen
@coudbook2177
@coudbook2177 3 жыл бұрын
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.
@stelio1425
@stelio1425 2 жыл бұрын
küss dein herz
@Crackhog
@Crackhog 2 жыл бұрын
@@stelio1425 Isso
@defaulthts6867
@defaulthts6867 Жыл бұрын
Your a legend
2 ай бұрын
After 5 years, still the best and most detailed quick tutorial about GDB on KZfaq.
@MMABeijing
@MMABeijing 3 жыл бұрын
Going to college in useless, unless you get a teacher such as this gentleman. The best gdb introduction on the net, thank you guru
@anonymousnoman5965
@anonymousnoman5965 5 жыл бұрын
one of the most under-rated gdb tutorial
@marcw6875
@marcw6875 2 жыл бұрын
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.
@ir4640
@ir4640 5 жыл бұрын
" I wonder what's the sum of first ten million numbers? It's a segmentation faul" Lmao ded
@xrafter
@xrafter 4 жыл бұрын
Yes there is no enough space
@rupeshkalantre7128
@rupeshkalantre7128 3 жыл бұрын
Lmao xD
@embeddedbastler6406
@embeddedbastler6406 4 жыл бұрын
Finally a program with more bugs than mines.
@PhilippeCarphin
@PhilippeCarphin Жыл бұрын
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.
@earlr4039
@earlr4039 3 жыл бұрын
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!
@santiagorestrepo2717
@santiagorestrepo2717 2 жыл бұрын
hands down best GDB video WOW so in depth and so much insight
@malusmundus-9605
@malusmundus-9605 2 жыл бұрын
Dude the keyboard clicks and voice... accidentally top-notch asmr.
@IronWill-men
@IronWill-men 6 ай бұрын
Explained like a true champion, very well through guide, thanks Chris
@raunakpillai13
@raunakpillai13 4 жыл бұрын
Thank you so much Chris, the way you walked us through the complete code and debugging was really helpful.
@ondrejsakala3980
@ondrejsakala3980 3 жыл бұрын
I have used gdb server first time. Good tutorial. Great thanks from Slovak Republic !
@hefan2482
@hefan2482 2 жыл бұрын
例子设计的简单明了,对GDB入门真的很有帮助,谢谢!
@anuradhaedachali
@anuradhaedachali 3 жыл бұрын
I was getting No file executable error for sometime but referring to your video the issue is resolved. Thank you so much.
@trestenpool9045
@trestenpool9045 4 жыл бұрын
Best gdb tutorial I found, Thank you!
@Mnj3907
@Mnj3907 4 жыл бұрын
Thanks a lot Chris. This video is totally educative.
@Jackyskan
@Jackyskan 2 жыл бұрын
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
@saeed6971
@saeed6971 4 жыл бұрын
Thank you kind sir. you explained it simple but in great detail.
@danverzhao9912
@danverzhao9912 3 жыл бұрын
Best gdb tutorial on youtube! Thank you!
@pedrolobo1081
@pedrolobo1081 4 жыл бұрын
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
@viswajitnayak8325
@viswajitnayak8325 Жыл бұрын
underrated comment... thanks
@user-bc5pz5th7t
@user-bc5pz5th7t 11 ай бұрын
Thanks a lot😊
@bavishijimit
@bavishijimit 4 жыл бұрын
Absolutely amazing video!
@rakibd.sarker300
@rakibd.sarker300 5 жыл бұрын
Great one for the beginner of GDB.
@geekionizado
@geekionizado 4 жыл бұрын
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!
@dhhan68
@dhhan68 3 жыл бұрын
Thanks a lot, teacher! I began to love gdb....
@celsozacarias7285
@celsozacarias7285 3 жыл бұрын
I'm happy that I've found a good channel like your. Video liked and channel subscribed
@noonansean1979
@noonansean1979 3 жыл бұрын
Great intro to GDB, thank you
@Jamespan4
@Jamespan4 3 жыл бұрын
Good tutorial, that's exactly what I am looking for!!!
@ermanolcay6574
@ermanolcay6574 2 жыл бұрын
Perfect presentation and teaching thank you for your work kudos.
@user-de4mr7uk8d
@user-de4mr7uk8d Жыл бұрын
Holy shit. Thank you so much! This lesson helped me a lot!!!
@aghorn
@aghorn 4 жыл бұрын
Excellent tutorial. Thanks!
@markbeebe2463
@markbeebe2463 5 жыл бұрын
Really excellent video, thanks!
@ahmadfaiq85
@ahmadfaiq85 4 жыл бұрын
Loved the layout next command. I didn't know you could see the code while stepping through.
@bonbonpony
@bonbonpony 3 жыл бұрын
Yup, this should have been the default mode long time ago :q
@christiangiampapa5780
@christiangiampapa5780 Жыл бұрын
Helped so much, incredibly grateful
@networkit1107
@networkit1107 Жыл бұрын
Great demonstration
@harryw5622
@harryw5622 4 жыл бұрын
Thank you so much for such clear tutorial!
@roeniss
@roeniss 2 жыл бұрын
really good demonstration.
@Aaronb2245
@Aaronb2245 2 жыл бұрын
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!
@thcdenton
@thcdenton 2 жыл бұрын
awesome tutorial. been wondering how to do this :D
@hstrinzel
@hstrinzel Жыл бұрын
REALLY GOOD tutorial! Thank you! :)
@blameItleaveit
@blameItleaveit 2 жыл бұрын
This is really great video.
@chidaruma_
@chidaruma_ 4 жыл бұрын
Great tutorial! Thank you!
@imrank340
@imrank340 3 жыл бұрын
Thank very illuminating tutorial n the technique demonstrated.
@PeeterJoot
@PeeterJoot 3 жыл бұрын
I didn't know the argv@2 trick to set the size of the array print out. Nice.
@SO-dl2pv
@SO-dl2pv 4 жыл бұрын
What a great tutorial!
@shararmahmood380
@shararmahmood380 3 жыл бұрын
I want to redo all my C assignments now. I was doing them sooo wrong the whole time. Dr. Bourke you rock!
@cepi24
@cepi24 2 жыл бұрын
Sir, this is simply amazing. God bless you & your family. May I ask which playlist is this video part of?
@adetolasanni2
@adetolasanni2 Жыл бұрын
Your tutorial is amazing.
@rufledore
@rufledore 4 жыл бұрын
Great tutorial! Thanks!
@Dorbellprod
@Dorbellprod 2 жыл бұрын
Thank you! This is a great resource.
@baqtronic7704
@baqtronic7704 5 жыл бұрын
Great tutorial!! Thanks
@aditya-vt7po
@aditya-vt7po Жыл бұрын
the examples used are good.
@MrACrazyHobo
@MrACrazyHobo 2 жыл бұрын
Such a great tutorial.
@ppb2374
@ppb2374 5 жыл бұрын
very helpful ,thank you.
@kgroombr
@kgroombr 3 жыл бұрын
Well explained. Thank you!
@SuperSampling
@SuperSampling 4 жыл бұрын
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_
@lindyrauchenstein8716
@lindyrauchenstein8716 2 жыл бұрын
Bless you. This was making me crazy.
@user-eo4xy8qg7q
@user-eo4xy8qg7q 3 ай бұрын
dear my gcc is 9.4.0 still i am getting the segfault @@lindyrauchenstein8716
@manishgupta13
@manishgupta13 2 жыл бұрын
Just one word superb
@qamandarin1921
@qamandarin1921 4 жыл бұрын
Hi on layout next mode why when I press ENTER on the screen outputs "W", and it does not move to next line?
@anndronova521
@anndronova521 2 жыл бұрын
Very helpful tutorial!
@kitsana72
@kitsana72 4 жыл бұрын
very helpful. Thanks!
@liliaoakeley4402
@liliaoakeley4402 2 жыл бұрын
king of gdb
@isurusampath007
@isurusampath007 4 жыл бұрын
Very good tutorial!
@SauvikRoy
@SauvikRoy 2 жыл бұрын
Thank you for the awesome tutorial! I was hoping you'd show how to detect the memory leak in the end?!
@quangtuanphan6213
@quangtuanphan6213 Жыл бұрын
thank you sir, awesome!
@suyashneelambugg
@suyashneelambugg 4 жыл бұрын
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.
@marrylo842
@marrylo842 3 жыл бұрын
Debugging is like heaven 😍
@foadsf
@foadsf 4 жыл бұрын
this was awesome. I enjoyed every second. thanks a lot 🖖
@Gokulkrishnan-yg8yh
@Gokulkrishnan-yg8yh Ай бұрын
Thanks for the nice tutorial, Where should i get the presentation.
@abheetl9743
@abheetl9743 3 жыл бұрын
This is brilliant.
@yahiafarghaly7472
@yahiafarghaly7472 4 жыл бұрын
Very elegant thank you
@NinjaAcademie
@NinjaAcademie 3 жыл бұрын
Insane information thanks a lot
@3bonsai5279
@3bonsai5279 14 күн бұрын
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
@AlvaroLopez-rr2xz
@AlvaroLopez-rr2xz 4 жыл бұрын
Thanks for the tutorial,so useful for beginners people in gdb.
@hassansyed5661
@hassansyed5661 7 ай бұрын
GDB seems a good debugger
@crooktsang4107
@crooktsang4107 2 жыл бұрын
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.
@martinmickels1478
@martinmickels1478 3 жыл бұрын
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?
@carlosl5125
@carlosl5125 4 жыл бұрын
This is amazon thank you!
@zoeythebee1779
@zoeythebee1779 2 жыл бұрын
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*
@sidtronics
@sidtronics Жыл бұрын
Thanks!
@rahulkhanna9870
@rahulkhanna9870 5 жыл бұрын
Great video
@charlybravo
@charlybravo 4 жыл бұрын
Where can o get the program primesprogram.c ?Thanks in advance
@abdulkadirbayero9140
@abdulkadirbayero9140 2 жыл бұрын
This tutorial is goated... That's why u the g.o.a.t
@athuldas6494
@athuldas6494 3 жыл бұрын
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
@kartiksama5467
@kartiksama5467 5 жыл бұрын
Thanks a lot!
@nicolaspillot5789
@nicolaspillot5789 3 ай бұрын
Great tutorial ; for completeness, would it be able to use gdb to debug the missing free of the malloc memory at the end ?
@ChrisBourkeUNL
@ChrisBourkeUNL 3 ай бұрын
It would be better to use a dynamic analysis tool such as valgrind to detect memory leaks.
@n0lain
@n0lain 4 жыл бұрын
Which text editor is he using?
@chuian7978
@chuian7978 3 жыл бұрын
謝謝你~
@matanonson4
@matanonson4 2 жыл бұрын
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?
@Popart-xh2fd
@Popart-xh2fd Ай бұрын
Isn't this placed in a playlist?
@orionclachar5094
@orionclachar5094 2 жыл бұрын
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?
@ChrisBourkeUNL
@ChrisBourkeUNL 2 жыл бұрын
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.
@dongdongkong9649
@dongdongkong9649 4 жыл бұрын
excellent
@zoroshikamaru3038
@zoroshikamaru3038 4 ай бұрын
Thank you
@THHATKD
@THHATKD 3 жыл бұрын
Let’s “fire up” 😂😂❤️👌🏼
@rootshell101
@rootshell101 3 жыл бұрын
🔥🔥🔥
@ankitajain6563
@ankitajain6563 4 жыл бұрын
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
@ChrisBourkeUNL
@ChrisBourkeUNL 4 жыл бұрын
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.
@hectorcanizales5900
@hectorcanizales5900 3 жыл бұрын
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.
@lindyrauchenstein8716
@lindyrauchenstein8716 2 жыл бұрын
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!
@hectorcanizales5900
@hectorcanizales5900 2 жыл бұрын
@@lindyrauchenstein8716 thanks for the info!
@marrylo842
@marrylo842 3 жыл бұрын
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?
@lindyrauchenstein8716
@lindyrauchenstein8716 2 жыл бұрын
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.
@bonbonpony
@bonbonpony 3 жыл бұрын
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.
@starc0w
@starc0w Жыл бұрын
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.
@Klayperson
@Klayperson 3 жыл бұрын
at line 69 could you just `return x == 2;` ?
@qhfcwwfep4zn2kheg2fu8
@qhfcwwfep4zn2kheg2fu8 3 жыл бұрын
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.
@ChrisBourkeUNL
@ChrisBourkeUNL 2 жыл бұрын
gcc -pedantic
@ishanvarade
@ishanvarade 5 жыл бұрын
great
10.1 - File IO - Introduction
9:39
Chris Bourke
Рет қаралды 2,7 М.
GDB Tutorial
55:12
CS 246
Рет қаралды 61 М.
狼来了的故事你们听过吗?#天使 #小丑 #超人不会飞
00:42
超人不会飞
Рет қаралды 66 МЛН
Which one is the best? #katebrush #shorts
00:12
Kate Brush
Рет қаралды 8 МЛН
СНЕЖКИ ЛЕТОМ?? #shorts
00:30
Паша Осадчий
Рет қаралды 1,3 МЛН
How They Hack: Buffer Overflow & GDB Analysis - James Lyne
16:06
GDB Debugging: How to Debug a C/C++ program
18:07
pikuma
Рет қаралды 35 М.
An Introduction to GDB for Debugging C Programs - COMP211 - Fall 2020
1:03:55
CppCon 2016: Greg Law “GDB - A Lot More Than You Knew"
59:09
why do void* pointers even exist?
8:17
Low Level Learning
Рет қаралды 319 М.
Comparing C to machine language
10:02
Ben Eater
Рет қаралды 5 МЛН
everything is open source if you can reverse engineer (try it RIGHT NOW!)
13:56
Low Level Learning
Рет қаралды 1,2 МЛН
ESP32 - DEBUGGING your ESP-IDF code using JTAG [VS CODE]
19:53
ช่วยหนูด้วย
0:15
Jenny Mintra
Рет қаралды 4,8 МЛН
Why You Should Always Help Others ❤️
0:40
Alan Chikin Chow
Рет қаралды 57 МЛН
Я ПРОТИВ МЛАДШЕЙ СЕСТРЫ / желе
1:00
Li ALINA
Рет қаралды 2,9 МЛН