you need to stop using print debugging (do THIS instead)

Adding print statements to debug your crashing program is a tale as old as time. It gets the job done... most of the time. As your code gets more complex and larger, print debugging quickly becomes unfeasible.
Using gdb and core files, you can easily cut your debugging time down. In this video, I'll be discussing a pet program that I wrote and using a core file to debug an issue with our program. We'll talk about how to get your program and kernel to produce a core file on segmentation fault, as well as a few techniques to debug C and C++.
🏫 COURSES 🏫 Learn to code in C at lowlevel.academy
📰 NEWSLETTER 📰 Sign up for our newsletter at mailchi.mp/lowlevel/the-low-down
🔥🔥🔥 SOCIALS 🔥🔥🔥
Low Level Merch!: www.linktr.ee/lowlevellearning
Follow me on Twitter: / lowleveltweets
Follow me on Twitch: / lowlevellearning
Join me on Discord!: / discord

Пікірлер: 648

  • @flamurmustafa522
    @flamurmustafa522 Жыл бұрын

    For anyone who doesn’t know, in the older references, core meant memory, so when the core is dumped is actually writing the current state of memory into a file

  • @guilherme5094

    @guilherme5094

    Жыл бұрын

    👍

  • @PeterJepson123

    @PeterJepson123

    Жыл бұрын

    If I remember correctly, the term goes back to the 1950's when they used magnetic iron cores for memory. I'm sure I read it in a 'history of Unix' book of some sort.

  • @TalentGamer

    @TalentGamer

    Жыл бұрын

    @@PeterJepson123 read the same thing in "Unix - a History and a memoir" by Brian Kernighan. Recommend it to anyone interested in Computer and Software history

  • @tymscar

    @tymscar

    Жыл бұрын

    That is true! Lovely book all around. Kernighan is such an amazing person!

  • @kennethbeal

    @kennethbeal

    Жыл бұрын

    @@PeterJepson123 My mom worked on those back in her college days.

  • @maximkulkin2351
    @maximkulkin235110 ай бұрын

    Print debugging still is the most universal way of debugging. You can do it local, you can do it remote you can do it in embedded, you can do it in a high volume processing code that would be just time consuming to step through (or when you don't know exactly where the problem is and can't put conditional breakpoints). Core files can only help with memory problems (when your code crashes), but won't help when it doesn't crash, but just produces wrong resutls.

  • @SerBallister

    @SerBallister

    8 ай бұрын

    Prints are great at looking at long sequences of events too, which in a debugger can be quite difficult to setup several break-points and then have to poke around the entire local variable state at every break. Prints get straight to the point.

  • @KucheKlizma

    @KucheKlizma

    7 ай бұрын

    @@TheGameMakeGuy Not just logfiles, quite often there's also options to place the software or a component into debug mode and have even more verbouse print debug outputs. In addition to also being able to generate a memory dump, a configuration dump and whatnot. Personally I think debugging should be proportional to the issue or risk at hand. I wouldn't read a core dump to find a misplaced closing bracket, but if a prod sever is crashing mysteriously when it's running on client hardware alongside other applications which naturally have not been perfectly tested for compatibility from development but aren't expected to cause issues then obviously a core dump becomes a highly appropriate troubleshooting option.

  • @jnharton

    @jnharton

    5 ай бұрын

    You can also use /assertions/ if the language has that feature.

  • @jnharton

    @jnharton

    5 ай бұрын

    @@SerBallisterIt does matter how you use them, though. You (the programmer) have to actually understand what the code should be doing so you can add only necessary print statements that let you follow the process and see where it went wrong.

  • @reynoldskynaston9529

    @reynoldskynaston9529

    5 ай бұрын

    Yeah this core debugging is useful but doesn’t replace other forms of debugging.

  • @glowiak3430
    @glowiak34306 ай бұрын

    6:13 Fun fact, this is a mistake. As this array's length is 100, its maximum value is 99, so entering 100 will cause a crash anyway.

  • @JosefdeJoanelli

    @JosefdeJoanelli

    5 ай бұрын

    I would have used >= 100

  • @glowiak3430

    @glowiak3430

    5 ай бұрын

    @@JosefdeJoanelli Yes, but he didn't do it.

  • @JosefdeJoanelli

    @JosefdeJoanelli

    5 ай бұрын

    ​@@glowiak3430I know

  • @chronxdev

    @chronxdev

    5 ай бұрын

    Nice! I came to comments to see if anyone else caught this off-by-1 error

  • @johnbrooks5903

    @johnbrooks5903

    5 ай бұрын

    I assume it was intentional to drive engagement, but yet here I am.

  • @dfs-comedy
    @dfs-comedy Жыл бұрын

    Core files and debuggers are indeed very useful. But honestly... the occasional debugging printf() is perfectly fine too. I've caught a lot of bugs that way without having to fire up a debugger.

  • @stevenspmd

    @stevenspmd

    Жыл бұрын

    Depending on the situation printf or equivalent might be only way without changing changing threading/timing. Running a full debugger isn't a light weight process.

  • @pikachuchujelly4119

    @pikachuchujelly4119

    10 ай бұрын

    Sometimes, you just can't use gdb, like when trying to debug bare metal Raspberry Pi code, so you have to do print debugging over UART.

  • @quentinquadrat9389

    @quentinquadrat9389

    7 ай бұрын

    Or LED blinking :) while the best gdb for bare metal is the oscilloscope :D Instead of printf I know preferring the dbg-macro library for C++

  • @Bagasl

    @Bagasl

    5 ай бұрын

    @@pikachuchujelly4119 I'm using gdb to debug stm32, don't know what are you talking about

  • @jnharton

    @jnharton

    5 ай бұрын

    @@pikachuchujelly4119 That might be the easiest option, but it's most assuredly not the only way. You could probably use the GPIO and LEDs too.

  • @GiletteRazorsharp
    @GiletteRazorsharp Жыл бұрын

    Don't do "typedef unsigned int uint32_t". The size of an int is compiler dependant (although it usually is 32 bits). Include stdint.h if you *need* exact size integer types.

  • @absalomdraconis

    @absalomdraconis

    Жыл бұрын

    Yeah, stdint.h has been around for _how_ long now? Even VS 6 had some homebrew versions made for it at some point.

  • @biigsmokee

    @biigsmokee

    Жыл бұрын

    or just program in Rust

  • @cheems1337

    @cheems1337

    Жыл бұрын

    @@dmitryhetman1509 ikr, Go ftw

  • @0xCAFEF00D

    @0xCAFEF00D

    Жыл бұрын

    @@biigsmokee if you've moved to Rust because of fixed width integer types you're probably not staying.

  • @NoahGooder

    @NoahGooder

    Жыл бұрын

    plus it makes your program more portable technically

  • @DinoDiniProductions
    @DinoDiniProductions Жыл бұрын

    Debugging complex systems with time dependent interactions (user input, network connections, video game AI, user interfaces etc) are pretty much impossible with a debugger. The biggest problem here is that the debugger is extremely invasive in terms of time. You hit a break point and it literally freezes your entire application. This is a bit of an issue if you have code that interacts with real time things, which lets face it, is most of the difficult work in software engineering. I do use debuggers where I can and where it's the best tool for the matter at hand. But I am here objecting to the click-bait. Yes, please carry on using logging for debugging, as most debugging of complex systems really requires it.

  • @sverkeren

    @sverkeren

    Жыл бұрын

    or you could actually watch the video and realize that it's about post-mortem debugging of core dumps and not time invasive interactive debugging with breakpoints.

  • @everydaynova663

    @everydaynova663

    5 ай бұрын

    @sverkeren Or you could realize that debugging tools or core dumps aren't the Swiss army knives of debugging and that a simple print statement may be the best option at the moment. Also, this video is advice for programming in C and may not be applicable to any programming language. Still fitting to call it clickbait

  • @neeneko

    @neeneko

    5 ай бұрын

    @@everydaynova663 really, both techniques should be in your toolbelt. cores can tell you things that print statements can not, and print statements can tell you things dumps can not. dumps tell you where you are, prints tell you how you got there.

  • @DanielCordey

    @DanielCordey

    5 ай бұрын

    Absolutely right !

  • @DinoDiniProductions

    @DinoDiniProductions

    5 ай бұрын

    @@sverkeren ah, but I did not watch the video because I do not like clickbait. See...

  • @ToyKeeper
    @ToyKeeper4 ай бұрын

    After writing software for 40 years in many languages, from the highest to lowest levels, including patches to some of the software used in this video, here's what I've learned about debugging: 1. The best debugging technique is to just read the code. If the code is too complex to trace in your head, it probably needs to be rewritten anyway. 2. After that, nothing beats print style debugging. It's easy, simple, and works in almost any context. 3. Debuggers and profilers are nice luxuries to have sometimes, but are frequently not feasible.

  • @system64_MC
    @system64_MC Жыл бұрын

    6:04 I think I noticed a small error in the if statement the last index of an array is always the size of the array minus one because arrays starts at 0 (except in LUA). So the maximum index of the array is 99, and not 100. if you select a box with 100 as ID, your program will segfault because index 100 is out of bounds. To fix that, you have to either replace the ">" sign to ">=" sign, or you can do if (i > 99) { ... }.

  • @-wx-78-

    @-wx-78-

    Жыл бұрын

    Typical “off-by-one” error. Program perhaps won't segfault: static g_Boxes takes 6800 bytes, hence it'll span two 4 KiB pages with 1392 writable bytes left after it (speaking x86'ish).

  • @benedani9580

    @benedani9580

    Жыл бұрын

    @@-wx-78- Yeah, but it might overwrite unrelated memory.

  • @-wx-78-

    @-wx-78-

    Жыл бұрын

    @@benedani9580 Definitely.

  • @Megamike144p

    @Megamike144p

    Жыл бұрын

    Indexes in LUA start at 1? Absolute madness.

  • @cliffmathew

    @cliffmathew

    Жыл бұрын

    For those unfamiliar with C: "@System64 MC" is correct - and the index should be checked against 99 or less. The index variable "i", declared as an "unsigned int" will not go below zero, so you don't have to worry about that. I could not see what the read_int() function does if the user inputs a negative number.

  • @quentinquadrat9389
    @quentinquadrat93897 ай бұрын

    For Linux C++ users, I really recommend compiling your project (-g -O0) against the backward-cpp library. You will have a nicely printed stack trace from your segfault. It is probably sufficient to understand where the bug comes from, else yes, use gdb and the core dump.

  • @lucass8119

    @lucass8119

    5 ай бұрын

    cross platform stack traces were meant to be in C++20, but alas, the committee moves too slow. There is source_location, which you can use for debugging. source_location also doubles as a scrappy way to achieve compile-time reflection! You can extract class member's names as strings by abusing auto and source_location.

  • @swirl6996

    @swirl6996

    5 ай бұрын

    use -fsanitize=address

  • @DefaultBoyBand

    @DefaultBoyBand

    26 күн бұрын

    -Og is better for debugging (just a tip)

  • @atijohn8135

    @atijohn8135

    10 күн бұрын

    ​@@DefaultBoyBand -Og enables function inlining, so not exactly the best thing for stack traces, you need to additionally specify -fno-inline alongside it

  • @DefaultBoyBand

    @DefaultBoyBand

    9 күн бұрын

    @@atijohn8135 huh... didn't know that! thanks

  • @anon_y_mousse
    @anon_y_mousse Жыл бұрын

    It would also be helpful to demonstrate how to write test cases and how best to separate into multiple modules. Also, you should check the number only once when read in from the user as well as stop using magic numbers. Might also want to demonstrate using `gdb` even if your program isn't crashing.

  • @staceyschnee801
    @staceyschnee801 Жыл бұрын

    It's a whole lot harder to use the core file when a released product crashes (no debug symbols). It is also much harder to use core files to track down crashes in multi-threaded programs, and impossible if the problem is caused by a deadlock elsewhere in the code.

  • @anujmchitale

    @anujmchitale

    Жыл бұрын

    And absolutely not an option for me, programming for microcontrollers. 😂

  • @abdox86
    @abdox86 Жыл бұрын

    Already using gdb on low level stuff, it’s ironic… thanks a lot man I’m really blessed by ur channel.

  • @gariannobinger9933
    @gariannobinger99334 ай бұрын

    minor correction: "ulimit -c unlimited" means allow a core file be created with unlimited size, not "the kernel is allowed to produce an unlimited amount of core files"

  • @AMildCaseOfCovid
    @AMildCaseOfCovid5 ай бұрын

    I haven't tried a core dump, but as soon as he said "let's look at the assembly" my eyes glazed over. I'll stick with my print statements and debugger combo. I've never had to abandon my code and start over; that sounds nightmarish

  • @stevepreskitt283

    @stevepreskitt283

    5 ай бұрын

    It's not as bad as you might think, and sometimes is the only way to actually find the problem. Several years back I had some code that worked fine when compiled for debug, but not when the symbols and such weren't there. After looking at the emitted assembly, it turned out my code was fine, and on this particular block of source, the compiler generated correct assembly if the debug symbols were enabled but not if they weren't. After sending the info to the vendor, they quietly fixed the bug in the next release. That's a VERY rare situation to come across, but still shows that being able to read the emitted assembly can be useful.

  • @icankickflipok
    @icankickflipok2 ай бұрын

    This man is the perfect programming KZreadr for me. I’ve noticed lately at my uni in my courses that all the other students complain at an assignment/task that needs to be done in C (we’re in Operating Systems and we just wrote the first part of a shell where we had to create and implement the cd, pwd, and exit commands, now we’re going to exec the other commands as well as their arguments, and the whole class whined out loud when they were told that was the next assignment due next week. He also teased at the possibility of making us write our own memory allocator, which got even more audible rejection from the other students). However, I get excited at the thought of it. I love writing in C, I love the challenges it brings. My professor even pointed it out to me when I went to see him during his office hour to discuss getting an internship, how to go about it, what to prepare for, etc. that I seemed to really enjoy working at a low level close to the hardware. So, as a guy who seemingly loves writing low-level code, finding a programming KZreadr whose channel name is literally “Low Level Learning” is the best thing that I’ve found on KZread this year so far.

  • @darius0501
    @darius0501 Жыл бұрын

    if i knew this in my first year where i've learned trees and graphs in C imagine all the seg faults thank you for this!

  • @SneezewipeYT
    @SneezewipeYT Жыл бұрын

    Woah this is cool! I wish I'd known about this when I still wrote in C back in uni. Great video!

  • @LowLevelLearning

    @LowLevelLearning

    Жыл бұрын

    Thank you!

  • @gregorymorse8423
    @gregorymorse8423 Жыл бұрын

    This is very basic debugging for crashes but the video title sounded like more. Print statements to deal with non fatal errors are still quite useful.

  • @vastabyss6496

    @vastabyss6496

    Ай бұрын

    100% agree. This is probably the first comment I've seen from you where you don't sound angry lol

  • @gregorymorse8423

    @gregorymorse8423

    Ай бұрын

    @@vastabyss6496 I'm not angry generally. Just the way things come across via text on the web probably makes it seem that way.

  • @Boringpenguin
    @Boringpenguin Жыл бұрын

    This is super helpful for beginners as well! Thanks a lot!!

  • @LowLevelLearning

    @LowLevelLearning

    Жыл бұрын

    You're so welcome!

  • @avg_user-dd2yb

    @avg_user-dd2yb

    Жыл бұрын

    @@LowLevelLearning can you make a tutorial on making basic interpreter and compilers using c,would be really helpful.

  • @windowsos-exestoppedworkin5391

    @windowsos-exestoppedworkin5391

    Жыл бұрын

    I absolutely agree, I keep trying to write a compiler in C but I suck so I eventually give up and then I start over and the cycle repeats

  • @avg_user-dd2yb

    @avg_user-dd2yb

    Жыл бұрын

    @@windowsos-exestoppedworkin5391 iam just starting,Iam interested in low level programming stuff... high level languages and frameworks... They hide a lot of details.

  • @conorstewart2214

    @conorstewart2214

    Жыл бұрын

    @@avg_user-dd2yb if you are interested then you should check out FPGAs, essentially reconfigurable hardware that you can use to make essentially any digital circuit like processors, the “game”, “Turing Complete” is good for this too, you start off making basic logic gates and latches and stuff and end up making a computer and programming it in assembly. If you are only interested in assembly then “Shenzhen IO” is good too. The most complete way to understand it at a very low level is to know how the processor itself works and be mostly able to build one.

  • @ishashka
    @ishashka Жыл бұрын

    When I switched from python and C# to C, one of the problems I had was that there's no stack traces when something goes wrong. But this is actually even more useful than a stack trace. Amazing

  • @stephenhookings1985

    @stephenhookings1985

    Жыл бұрын

    The gdb gives you the stack traces. You could type backtrace or just bt from the core. You can also type where at a breakpoint. In our products we caught signals, then processed the exception - servers are NOT allowed to crash. There again in C++ there are lots of ways to better handle exceptions than in C.

  • @ishashka

    @ishashka

    Жыл бұрын

    @@stephenhookings1985 Yeah, I meant stack traces that print out automatically when there's an exception. I figured you can get that from gdb, but you have to know it's there.

  • @TheCustomFHD

    @TheCustomFHD

    8 ай бұрын

    ​@@ishashkaor, you know how to find it. Manuals are amazing.

  • @StasPakhomov-wj1nn
    @StasPakhomov-wj1nn Жыл бұрын

    I've been patiently waiting till I finally stumbled across someone who explains these concepts as well as you. Thank you so much sir! I am so excited to learn with you :)

  • @xThirdOpsx
    @xThirdOpsx Жыл бұрын

    Great video Dave! Informative as always.

  • @LowLevelLearning

    @LowLevelLearning

    Жыл бұрын

    Thanks for watching

  • @CallousCoder
    @CallousCoder Жыл бұрын

    I believe there’s still a bug. You can enter 100 and it still crashes as the index is 0-99 😉I find it remarkable that you remember all the the commands. I always have to look em up. Then again I hardly debug, my code is usually error free 😅 I actually just wrote my first ever C program on the Amiga to just open a window and draw random computed pixels. And not being able to run the binary on my Mac (Aros HDF required UAE version 4 which is not released on Mac yet). So I upload my cross compiled binary and awaited the first crash… but it worked, first time! Usually that’s a no go. Especially with a whole new windowing environment.

  • @akaikangaroo

    @akaikangaroo

    Жыл бұрын

    That's why one-indexed languages rule🙂

  • @CallousCoder

    @CallousCoder

    Жыл бұрын

    @@akaikangaroo Can you name ONE 😝🤪 And obviously I don’t agree because 0 makes sense because also memory starts at 0000 making it ONE is a silly western construct. Because we a westerners were too stupid to not define zero, unlike the Indian Al-Khowarizini did. For ever gatefuk to him, now programming zero index makes complete sense 😁

  • @akaikangaroo

    @akaikangaroo

    Жыл бұрын

    @@CallousCoder Lua, Julia and 18 more😜

  • @typingcat

    @typingcat

    Жыл бұрын

    When I saw that "i > 100" code, I immediately thought it was wrong and came to write a comment, but of course someone has already pointed it out. It should have been "i >= 100".

  • @Sebastian-eb2ux
    @Sebastian-eb2ux Жыл бұрын

    Won't your program still crash for i = 100?

  • @MadaFakaTOO

    @MadaFakaTOO

    Жыл бұрын

    it will but it's just a small oversight on his part, it's not critical to the topic at hand

  • @sthex4640
    @sthex46408 ай бұрын

    I like how you said we can look at the assembly instructions to debug this and resigned from doing so as soon as you saw them

  • @ThatCowGuy
    @ThatCowGuy Жыл бұрын

    Wow, this was a really well presented video. I never used this feature before, because I thought stuff like that would be overkill in most scenarios. But the way you showcased it made it seem pretty handy, while note being as complicated as I imagined. I will definitely try this out next time I code something.

  • @lukaszkonsek7940

    @lukaszkonsek7940

    Жыл бұрын

    Try gdb Save and reverse execution functionalities. It's pain to learn, but super usefull with the worst bugs.

  • @Avighna
    @Avighna Жыл бұрын

    Holy shit, this is so useful! I cannot tell you the number of hours I’ve spent tracking down sigsegvs! I didn’t even know the (core dumped) meant anything 😂

  • @decky1990
    @decky1990 Жыл бұрын

    I’m about to start using GDB in my codebase - thank you so much 👍🏻

  • @caiovini980
    @caiovini9808 ай бұрын

    Hi, wanna know why do you have two naming patterns for your methods? read_int() and EditBox(), for example. Is there any technical reason for that?

  • @Finkelfunk
    @Finkelfunk Жыл бұрын

    That is useful if your program crashes, but not as useful if it just doesn't work as intended. I'm still 100% certain that print statements are more or less a quick and easy way to manage all sorts of bugs. Yes, debuggers can be useful, logs too especially if you have a huge project, but other than that it's actually fairly easy to just use print-statements when there are issues.

  • @ironfist7789

    @ironfist7789

    Жыл бұрын

    You don't need it to crash to use gdb, can run the program with gdb and step through the code as it runs, examine variables, etc. The core file is just for crashes after the fact. Like when run circlemud I type gdb bin/circle (the executable) and then type run inside of gdb.

  • @Finkelfunk

    @Finkelfunk

    Жыл бұрын

    @@ironfist7789 It's sad he didn't show iff that functionality then because these types of failures are more common I'd say

  • @ironfist7789

    @ironfist7789

    Жыл бұрын

    @@Finkelfunk Yeah, but it is just kind of an introduction I guess. There are a lot of features and ways of compiling for less efficient executables but more debugging symbols, etc.

  • @gangulic

    @gangulic

    Жыл бұрын

    @@ironfist7789 using debuggers can cause different behaviour on some systems

  • @MrGryph78

    @MrGryph78

    Жыл бұрын

    @@user-io4sr7vg1v of course it's UI is terrible, it's a CLI tool...

  • @dirivial_
    @dirivial_ Жыл бұрын

    What is the point of looking at the assembly instructions and register states? To me, it looked like you gained nothing from looking at them.

  • @tobiasmarzell330

    @tobiasmarzell330

    Жыл бұрын

    That's correct. It was completely unnecessary for this example and probably is almost never necessary as the compiler usually knows pretty well what it does with how it uses the registers. If you ever happen to go that deep you most likely want to turn around and check your bug on a higher level.

  • @TheJaber288
    @TheJaber288 Жыл бұрын

    Really nice ! This is why I want to learn assembly it's really helpful in all cases

  • @LowLevelLearning

    @LowLevelLearning

    Жыл бұрын

    Glad it was helpful!

  • @maaznaseer9706
    @maaznaseer97068 ай бұрын

    Or you can gcc -fsanitize=address to see where the segmentation dump core occurred, we use this method alot to understand where the pointer has failed. If not this, valgrind is the second choice of command to understand memory leaks.

  • @frroossst4267
    @frroossst4267 Жыл бұрын

    Half a semester of debugging seg faults, why did I not look this up earlier? THE PAIN!

  • @zfekete74
    @zfekete745 ай бұрын

    Nice syntax, I really like when someone takes care of readability and looks at the same time!

  • @artik15xfilm
    @artik15xfilm Жыл бұрын

    Still allowing index 100 and that's bad too :P

  • @LunarLambda
    @LunarLambda5 ай бұрын

    Something that print debugging allows, but no traditional debugger (to my knowledge) supports, is seeing how certain values in your program evolve over time, like in any kind of loop. Seeing the change over time allows me to figure out a potential pattern, and from there derive the issue way faster than most other traditional debugging techniques. This becomes even more valuable in a language like Rust which has really rich formatting support and prevents most types of crashes from the get go, so the majority of bugs I need to fix are logic bugs, where finding patterns is often pretty fast.

  • @sakuyarules

    @sakuyarules

    2 ай бұрын

    You can have debuggers stop every time a value changes for a variable. I think that does exactly what you're talking about.

  • @thatluminary
    @thatluminary Жыл бұрын

    Holy mother of valgrind, I was just struggling with debugging a program when you dropped this video...now my program works. Thanks :)

  • @LowLevelLearning

    @LowLevelLearning

    Жыл бұрын

    Glad it helped!

  • @thatluminary

    @thatluminary

    Жыл бұрын

    @@LowLevelLearning I actually learned a lot about low level programming in this channel (your videos give me the necessary motivation to research a lot more)

  • @annan_ma
    @annan_ma Жыл бұрын

    You should at least fix it properly with >= 100… instead of > 100

  • @truestopguardatruestop164

    @truestopguardatruestop164

    Жыл бұрын

    Wanted to write the same thing

  • @MrYerak5

    @MrYerak5

    11 күн бұрын

    But the index starts at zero, 100 will overflow

  • @ronaldhofman1726
    @ronaldhofman1726 Жыл бұрын

    Good video , i just saw a video from Dave Plummer (ex Microsoft software engineer) and i learned to use the f_s functions all the other F functions are not safe, can bus buffer overflow , the new way so use prtinf_s, the video of Dave is called Stupid C tricks , i saw it and he's right , has to do witn functions not taken in account buffer lengts, and not checking them , not adding null remintator and so on, there are new functions and they have in common that they are named as the original functions but added with _s , these are safe functions can not induce a buffer overflow, good explanation also from Dave.

  • @nexovec
    @nexovec10 ай бұрын

    I didn't know you can actually configure core like that. This is pretty useful.

  • @der.Schtefan
    @der.Schtefan Жыл бұрын

    Also, don't use scanf. Use the secure variants, and try to reject input as soon as it is parsed, not later on as per his example.

  • @TheCustomFHD

    @TheCustomFHD

    8 ай бұрын

    Scanf with the right precautions works fine. Sure fscanf or whatever it was is technically better security wise, but is slower and lacks features. Just make sure to specify how much scanf is supposed to take in.

  • @plato4ek

    @plato4ek

    8 ай бұрын

    What are the secure variants?

  • @BeenYT
    @BeenYT8 ай бұрын

    i had a rendering program and i wanted to debug one part of a function which is called thrice for every pixel, i was confused why printf was making sdl not render the frames even though the printf was working, took me over an hour to realise i wrote code that was so slow i didnt even notice it was running

  • @Aeduo
    @Aeduo Жыл бұрын

    If you're using systemd, it'll hold on to some amount of cores, and coredumpctl will allow you to start a debugging session with any of those cores.

  • @metroid031993
    @metroid0319935 ай бұрын

    Another way to avoid the print statements that get confusing is to use asserts. Forcibly fail when the conditions you're desiring aren't met, or the conditions you're seeing are. It's printf debugging but only one message prints, and it means it's much more comprehensible when something does go wrong. It doesn't always substitute a debugger, but they're good practice anyway, so it's a good idea to use them regardless of if you're debugging or not

  • @Filaxsan
    @Filaxsan8 ай бұрын

    Really cool and useful, thanks for sharing!

  • @januzi2
    @januzi2 Жыл бұрын

    Well, "the box" was obviously a reference to the movie 7, and the "Dave" was a reference to the "2001: a space odyssey". Is there something like that for php as well, or do the test units have to do everything?

  • @DeathSugar
    @DeathSugar5 ай бұрын

    Not always debugging used to fix crashes and sometimes gdb can't show proper backtraces due to absence of debug symbols or code version of the build. Also real-time tight programs will not let to stop themselves so prints is the only choice in many situations.

  • @rafaelpalmalima
    @rafaelpalmalima Жыл бұрын

    Thank you so much... it's really hard to find good content about gdb.

  • @LowLevelLearning

    @LowLevelLearning

    Жыл бұрын

    Happy to help!

  • @PeterJepson123
    @PeterJepson123 Жыл бұрын

    This is a great video thanks. I generally use ifdef debug printf() style to isolate an offending function and create a new entry point and test that function to destruction. The only debugging tool I've been using is valgrind --leak-check=full. This is great. Cheers mate.

  • @LowLevelLearning

    @LowLevelLearning

    Жыл бұрын

    Thanks for sharing!

  • @kuhluhOG
    @kuhluhOG Жыл бұрын

    Short question: Can the parent process define a special core pattern for its child process or must it always be system-wide?

  • @a.lollipop
    @a.lollipop5 ай бұрын

    I never knew what "core dumped" meant, this makes so much more sense now! Thank you :)

  • @Brad_Script

    @Brad_Script

    3 ай бұрын

    core is archaic term for memory

  • @6754bettkitty
    @6754bettkitty4 ай бұрын

    Nice explanation of debugging. Thanks!

  • @carljones9640
    @carljones96404 ай бұрын

    Excellent tutorial. Although not a solution for every bug, it's still a great way to debug C memory-related issues.

  • @terrorcomplainssometimes3969
    @terrorcomplainssometimes3969 Жыл бұрын

    ITS REALLY WORKED LOL THANK YOU DUDE

  • @iluan_
    @iluan_ Жыл бұрын

    Super useful. Thank you very much :)

  • @DIMOHA25
    @DIMOHA25 Жыл бұрын

    Excuse me, but I add a bunch of prints to figure out what's wrong and eventually I DO crack the code. Works every time.

  • @stove.d

    @stove.d

    Ай бұрын

    Plenty of things print statements can't do

  • @IsYitzach
    @IsYitzach5 ай бұрын

    3:07 I was going to do what you did there to core_pattern but I found that apport already had a pattern in there. I'm concerned that if I did what you did, then I'd be messing with apport and that sound improper. Is there way to get the core dump for your programs where you can find it while leaving system error handling unaltered?

  • @Codeaholic1
    @Codeaholic1 Жыл бұрын

    Wow today i learned why some say tack instead of dash. Interesting. Still makes my eye twitch every time I hear it.

  • @kennedymwangi5973
    @kennedymwangi59738 ай бұрын

    You are a life saver, thank you.

  • @locusf2
    @locusf2 Жыл бұрын

    Ah, Seven reference.

  • @LowLevelLearning

    @LowLevelLearning

    Жыл бұрын

    ayyy this guy gets it

  • @pqsk

    @pqsk

    4 ай бұрын

    Took me a while to get it. Wow, thanks 😹😹😹

  • @ironspider5043
    @ironspider5043 Жыл бұрын

    You also can use the flag -fsanitize=address -g flag for these seg fault , buffer overflow, etc...

  • @billy-cg1qq
    @billy-cg1qq5 ай бұрын

    I'll stay loyal to my prints, thank you

  • @flannelshirtdad
    @flannelshirtdad5 ай бұрын

    For this simple example, printing i would have found the bug sooner. However, I do see that this can be useful for truly complex bugs, but not necessarily better than print statements.

  • @JorgenLundman
    @JorgenLundman Жыл бұрын

    I used printf debugging over the weekend, of course, it was in a M1 kext, so no core to get, and apple kext lldb is very poor on M1, and panic dump sending is broken. It's not always the wrong thing to do, if you have no other choice :) But when it works, it's a beautiful thing!

  • @breadone_

    @breadone_

    Жыл бұрын

    although gdb doesnt work on m1 macs afaik, lldb is as good tho

  • @jakubrpawlowski

    @jakubrpawlowski

    5 ай бұрын

    Yeah it's been years and still no gdb support on Apple silicon.

  • @dtikvxcdgjbv7975
    @dtikvxcdgjbv7975 Жыл бұрын

    Useful. Nice introspective.

  • @bsykesbeats4013
    @bsykesbeats40133 ай бұрын

    Love ur content. Is there any reason you typedef unsigned int uint32_t instead of using uint32_t from stdint.h?...i mean i guess one reason could be less overhead if you're not using the other ints from that library, but also you dont know if int is going to be 32 bits on all systems....

  • @fahmitaib
    @fahmitaib Жыл бұрын

    this is so helpful, thanks!

  • @LowLevelLearning

    @LowLevelLearning

    Жыл бұрын

    Glad it was helpful!

  • @ThatJay283
    @ThatJay28323 күн бұрын

    recently i've been using assert alot more, and that's been much better than print statements for some usecases

  • @richardsteiner8992
    @richardsteiner89925 ай бұрын

    I usually add a couple of debugging options to programs, even shell scripts, which enable either some basic logging or that plus an overkill data dump. That way I can trace behavior without code changes. A "debugger" isn't always viable for whatever reason.

  • @pstrusi
    @pstrusi Жыл бұрын

    I've noticed that you use Visual Studio Code for C. I'd like to have it again but after many problems, I had to return to Visual Studio. If you don't mind: do you have some video or blog that explains how to set Visual Studio Code for C in windows 10 ? It consumes much less hard drive space among other things. Thanks

  • @maxp3141
    @maxp31415 ай бұрын

    Cool trick - I think I used this once a long time ago. Typically I just hit run on my IDE and it stops the debugger automatically on the line where the crash happens. Takes about 5 seconds to fix a bug like this.

  • @Saturate0806
    @Saturate0806 Жыл бұрын

    Can you do a video on how to add assertions and performing a core dump once they get hit on a remote target? :))

  • @arnavmohod9454
    @arnavmohod9454 Жыл бұрын

    Really it goes to the core!

  • @GuillermoReisch
    @GuillermoReisch3 ай бұрын

    5:18 in gdb is used AT&T assembly notation ; INSTRUCTION ORIGIN, DESTINATION ; and not in INTEL (more used) assembly notation (aka. INST DEST, ORIG)

  • @keithlambell1970
    @keithlambell19708 ай бұрын

    Thanks. That was a very useful demonstration.

  • @LowLevelLearning

    @LowLevelLearning

    8 ай бұрын

    You're welcome!

  • @deathreus
    @deathreus5 ай бұрын

    For Windows fellows, the equivelant is a .dmp file that is either next to the program or some crashes folder, and Visual Studio would be used to open it

  • @andrewgreen1970
    @andrewgreen197010 ай бұрын

    What I'm trying to understand here is how the values in rax and rdx gave away the nature of the issue.

  • @adam051838
    @adam051838 Жыл бұрын

    I'm confused at what looking at the registers gave us - you opened it and said what we were trying to do but didn't say anything about why that was bad or how we could use that information. Seems you could have just looked at the program .c file and gotten the information without looking at the assembly

  • @kaotiskhund
    @kaotiskhund9 ай бұрын

    AAABBBCCC reminds me the strings the are used to break a program in general in reverse engineering videos using python and gdb with the intention to find exploitable places in programs. Dunno if there is more to it though.

  • @sumitrangle5703
    @sumitrangle5703 Жыл бұрын

    Very helpful video. I think the core dump is for the linux machine you are running. Can we do the same for target microcontrollers. If yes, can you tell how to do it? Beginner here.

  • @TerabyteForever

    @TerabyteForever

    Жыл бұрын

    GDB has a remote debugging function that you can debug any application over network on your computer (provided that you have built a cross toolchain for the architecture).

  • @matthieusimon7836

    @matthieusimon7836

    Жыл бұрын

    It depends on your microcontroller as well as which operating system, if any, you are running on it. GDB is pretty printing what the hardware is complaining about. Not a trivial answer to your question.

  • @vadiks20032
    @vadiks200324 ай бұрын

    so much to replicate something visual studio has out of the box lol. i didnt even realize fully that i kept running my VS program in debug mode all the time

  • @salut730
    @salut730 Жыл бұрын

    5:21, the mov instruction actually does the opposite of what you said, because it's in AT&T syntax.

  • @corpseopera
    @corpseopera Жыл бұрын

    An address sanitizer compile with -g would help lots as well

  • @emilemil1
    @emilemil15 ай бұрын

    Starting in debug mode often takes 2-3 times longer so a log is usually more efficient. I only reach for the debugger when logs fail or are too cumbersome.

  • @XYZ56771
    @XYZ56771Күн бұрын

    ha, so that were the pesky files that were filling up my filesystem :) Thank you for this info!

  • @kamertonaudiophileplayer847
    @kamertonaudiophileplayer847 Жыл бұрын

    It is a great video, no more crashing. I need only Mac version of it.

  • @marymissmary
    @marymissmary4 күн бұрын

    Thanks for this!

  • @hellNo116
    @hellNo11619 күн бұрын

    The funniest experience of learning c is starting using print statements for debugging. Learning about debuggers and perror. End up programming drivers going back to using printk to print statements in order to debug :p

  • @gzozulin
    @gzozulin Жыл бұрын

    You probably meant >= 100, if you will type 100 as a box id you will have the same SEGFAULT

  • @givikap120
    @givikap120Ай бұрын

    Okay, and what if you're curious how the function initially get this value that crashed this problem? Like if your error have multiple steps

  • @anomalous5048
    @anomalous50482 ай бұрын

    I will be using print debugging and you cant stop me. It is the best and only way to debug.

  • @Blocktelligence
    @Blocktelligence Жыл бұрын

    Good stuff, man.

  • @JackBond1234
    @JackBond12344 ай бұрын

    The reason I do print debugging is rarely to debug crashes, it's to understand the flow of logic.

  • @meph5291
    @meph52915 ай бұрын

    May I suggest unit testing?

  • @TacoFace88
    @TacoFace88 Жыл бұрын

    What difference did the inclusion of the core file make? , it seems gdb-ing the program and the running it would produce the same result

  • @NostraDavid2

    @NostraDavid2

    Жыл бұрын

    If your program takes some input and provides some output, you can then feed it input and when it crashes, you can check the core file, instead of running each instance inside of gdb.

  • @aes0p895
    @aes0p8955 ай бұрын

    KZread programmers: COME OUT WITH YOUR PRINT STATEMENTS UP Me: You'll never take my print statements, copper! NEVER!

  • @nicholastheninth
    @nicholastheninth7 ай бұрын

    Yes, but does it help if you forgot about the memory alignment requirements of SIMD instructions that end up giving you “access violation reading location 0xFFFFFFFFFFFFFFFF”? My compiler assumed it was aligned and generated aligned load instructions for an unaligned field. It clearly assumed wrong.

  • @roshanrazor8634
    @roshanrazor8634 Жыл бұрын

    Awesome, great video!

  • @hyper7354
    @hyper73543 ай бұрын

    This is great if you’re writing for a PC environment. Can’t use core files on embedded, a debugger and a robust logging system is the best to debug quickly. Also 99.99% opening up the assembly to see what’s going on is just to take more time than looking at the source