Thread: Building a small debugging tool for myself

  1. #1
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    169

    Building a small debugging tool for myself

    Hey!

    A few months back, I watched my first C++ competitive programming stream (I don't remember the guy who was streaming but he was contesting in one of CodeForces' competitions). It was this problem related to binary trees and optimisation. At that time, I had no idea about a lot of things. He managed to solve it but only after some debugging. Now that I think of the syntax he used while debugging, I'm fascinated but puzzled as well as to how he did it. Okay, so, let me explain more about it and the reason as to "why" it fascinates me.

    He wrote the entire code in one go, occasionally compiling to check for syntax errors. When he test ran with actual sample inputs, the result was wrong. So, he started debugging. Not the usual way mere mortals like me would using the rich debugging tools that modern IDEs provide but this way:

    Code:
    Debug () << SomeVariable;
    Now, look carefully, it's a function called Debug, the left shift operator and then the variable name whose values he wanted to see. This yielded him all the changes to values of "SomeVariable" as output. At that time, I thought of it as some simple operator overloading with some other code in the function definition that tracks all changes in "SomeVariable" and stores it and displays it when asked to. This brings me to my first question.

    I learnt operator overloading can be done only for class methods to have the operators behave in a certain way. Can operator overloading also be done for non-member functions?

    If not, how can I write such syntax and what kind of code would I be looking at to enable this?

    He probably didn't use classes because I never saw him create an instance. He straight out wrote the above code just before the "return 0;" and ran it again and his output was something like:

    Code:
    . // The solution to the problem followed by:
    .
    .
    [LINE : 27] "Perks" created : 175
    [LINE : 29] "Perks" : 200
    [LINE : 85] "Perks" : 225 (loop iteration 1)
    [LINE : 85] "Perks" : 250 (loop iteration 2)
    [LINE : 85] "Perks" : 275 (loop iteration 3)
    [LINE : 85] "Perks" : 300 (loop iteration 4)
    [LINE : 85] "Perks" : 400 (loop iteration 12)
    [LINE : 99] "Perks" : 400 (std::cout)
    [LINE : 140] "Perks" deleted : 400
    The "created" and "deleted" basically meant that "int Perks = SomeVariableThatHasValue175" was on line 27 and Perks went out of scope on line 140.

    I found this tool, which he mentioned that he programmed himself while he was typing the syntax for its usage, very fascinating. So, I want to, at the least, understand how to use such syntax manipulation. Obviously, I could just do "Debug (SomeVariable)" syntax but in the end, I still wanna know how, what he did, was made possible.

    Now, my second question.

    I understand how he got the line numbers and stuff printed by using the __LINE__ , etc. What I have a hard time wrapping my head around is how did he get Debug () to store all the changes to SomeVariable. That line of code is just before the "return 0;" statement so the debug code doesn't have any idea about what happens to SomeVariable before it is called. So, there must be a background thread running which records changes in variable values into a vector, let's say.

    If I get a basic understanding of this, I can, probably, proceed and write code for a simple and similar tool. I've had experience writing other Debug macros and Logging tools but this was just insane when I started thinking hard about it, which was today.

    Thanks for the help!
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  2. #2
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    169
    [EDIT]
    I forgot to mention that he includes this file at the beginning after he wrote that line of code: "#include "CppDebug.h"

    Also, I'm not clear about whether it was "Debug () << Perks" or "Debug << Perks". I don't remember due to the long time gap between when I saw the video and when I realised the uniqueness of his code.

    I spent quite a lot of time searching for a name/channel on YouTube/GitHub that would ring any bells in my head to confirm it was him. YouTube gave me nothing even after scrolling a tonne through searches... So, I searched for debugging tools and competitive programming and a whole lot of other stuff on GitHub to find a user whose name would ring any bells. But, no luck until now...
    [/EDIT]
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,234
    Quote Originally Posted by Zeus_
    Now, look carefully, it's a function called Debug, the left shift operator and then the variable name whose values he wanted to see.
    You can't be sure that it's a function named Debug just from seeing that syntax. It could be a constructor invocation for a class named Debug; it could be the invoking of operator() on a function object named Debug.

    Quote Originally Posted by Zeus_
    Can operator overloading also be done for non-member functions?
    Yes.

    EDIT:
    Quote Originally Posted by Zeus_
    What I have a hard time wrapping my head around is how did he get Debug () to store all the changes to SomeVariable.
    Same: if all he did to get that debugging output was include one header and add that one line, then it sounds impossible: how could one instrument a built-in type like that?

    But I'm not an expert in this area, so it could be that I'm just mistaken. Or... you could be mistaken about what you saw, since I note that you stated "I never saw him create an instance" when a possible creation of an instance (Debug()) was right there to see.
    Last edited by laserlight; 5 Days Ago at 10:10 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    169
    > when a possible creation of an instance (Debug()) was right there to see.

    If Debug was his class, he would have probably done something like "Debug D" and then D << Perks, I think. Can you create temporary instances like that then? Because doing Debug () << Perks would not create an instance with a name that you can use elsewhere in the program.

    I may not be confident in whether it was "Debug () << Perks" (not sure if you can actually create an instance without a name) or "Debug << Perks" (where Debug in this case is an object declared in his header file) but I'm dead sure that all he did was include a header file and then type that syntax and it did all that magic.

    @laserlight, do you mean friend functions when saying "Yes" to "Can operator overloading be done for non-member functions" or any non-member function?

    What do you think he would have done to implement such a tool? Do you think he did some sort of class related approach or something else?
    Last edited by Zeus_; 5 Days Ago at 10:34 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,234
    Quote Originally Posted by Zeus_
    Can you create temporary instances like that then?
    Yes.

    Quote Originally Posted by Zeus_
    Because doing Debug () << Perks would not create an instance with a name that you can use elsewhere in the program.
    That's true, but it is consistent with your note that 'that line of code is just before the "return 0;" statement', i.e., it's not going to be used again, so if he was creating an object of a Debug class, why bother naming it?

    Quote Originally Posted by Zeus_
    I'm dead sure that all he did was include a header file and then type that syntax and it did all that magic.
    You couldn't find the video so as to refresh your memory though, so just because you're "dead sure" doesn't mean that what you remember is what you actually saw (we unconsciously do things like subtly change what we remember just by recalling it).

    Thinking about the example output: even if we manually instrumented a class, you wouldn't get that output because the object still exists in scope at that point, so how would you get the "deleted" message?

    Quote Originally Posted by Zeus_
    do you mean friend functions when saying "Yes" to "Can operator overloading be done for non-member functions" or any non-member function?
    Nope. One common example of operator overloading is to overload operator+= as a member function, then use that to overload operator+ as a non-member non-friend function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    169
    Thinking about the example output: even if we manually instrumented a class, you wouldn't get that output because the object still exists in scope at that point, so how would you get the "deleted" message?
    What you say makes sense, and I had the same question. He probably wrote the code for it in a way such that once the variable is no longer used further in the program, display it as deleted with the current value it is holding. This explanation, by me to myself, seems to be right, I think, because his entire code length was some 220 lines if I correctly remember and it showed that Perks was deleted way before the 220th line. Obviously my output is made up because I definitely wouldn't be able to remember what changes happened on what line based on what I saw in the video. Perks being deleted message was probably on line 99 itself because that was the last time its value was changed and he didn't modify the value elsewhere.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    694
    Quote Originally Posted by Zeus_ View Post
    He probably wrote the code for it in a way such that once the variable is no longer used further in the program, display it as deleted with the current value it is holding. This explanation, by me to myself, seems to be right, I think
    You're not thinking. How would he MAGICALLY gather all of that information by simply including a header file at the top and putting in that single line at the end of main? If that's all he did, then it's not possible (at least not without help from the compiler). This discussion is pointless until you can gather more information.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  8. #8
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    169
    Can't blame y'all. I myself don't remember what his name was and can't find him either. Neither is my YouTube watch history useful as I cleared it up some time. I can get the syntax to work with some macros and get them to show the current value but definitely not what he did, iff it's like what I remember, which is probably wrong as laserlight mentions: "we unconsciously do things like subtly change what we remember just by recalling it". I don't remember his exact setup either so can't be sure by any means. Anyway, thanks guys for taking part in the discussion!
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  9. #9
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    169
    Are the implementations of the operator overloads for basic types like int, float, char etc present in the STL in some header files or is it intrinsic to the compiler? If the implementations are present, could someone point me to which header file it is in? Thanks!
    Also, when MinGW is installed, does the source code of the compiler get downloaded too or is just the binaries for the compiler? I'm not sure if I'm using the right terms due to my lack of knowledge but if you understand what I mean, please help me out.
    Last edited by Zeus_; 2 Days Ago at 08:29 AM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    694
    The basic meaning of + is not an overload and they are definitely intrinsic to the compiler. They are pretty much just single assembly code instructions since the basic types correspond generally to what the machine can handle natively.

    The source code for gcc is online. It is, of course, hideously complicated. GitHub - gcc-mirror/gcc

    The library code should be under libstdc++-v3 (then in src and include).

    (Sorry for snapping at you yesterday. That was uncalled for. )
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  11. #11
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    169
    > The basic meaning of + is not an overload and they are definitely intrinsic to the compiler. They are pretty much just single assembly code instructions since the basic types correspond generally to what the machine can handle natively.

    Oh! I had this idea of changing some of the code (assuming that there were overloads) that would pretty much replicate what I think I saw but now that you mention they aren't overloads, it's actually starting to feel impossible without learning more on the topic and spending valuable time that I don't have available right now.
    Thanks for the link!
    And no problem, everyone gets cranky sometimes.... i'm sorry too
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  12. #12
    Registered User
    Join Date
    Dec 2017
    Posts
    694
    Quote Originally Posted by Zeus_ View Post
    >And no problem, everyone gets cranky sometimes.... i'm sorry too
    Thanks man. You didn't do anything wrong at all. (I really was cranky, complete with headache!)
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  13. #13
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    89
    Quote Originally Posted by john.c View Post
    The source code for gcc is online. It is, of course, hideously complicated.
    Going back to when it was first being developed it was like the world was on their shoulders to make something that would make everyone happy. The result is this huge mega-monolithic thing we call gcc. Okay so maybe the GNU project does feature some pretty ugly code. But at least it keep things going so to speak. At this point we can't really live without it!

  14. #14
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    169
    It took me almost 10 mins to download the ZIP of the repository . Comparing this to downloading a 100 MB game that takes about 7 seconds, I can only imagine how hugely giga-monolithic it is. Unzipping now... I'll probably be dead asleep by the time it finishes, it is pretty late here and I have no idea what the hell I'm doing on my comp when I have this important Chem exam on Friday. RIP me
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  15. #15
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    89
    Quote Originally Posted by Zeus_ View Post
    It took me almost 10 mins to download the ZIP of the repository . Comparing this to downloading a 100 MB game that takes about 7 seconds, I can only imagine how hugely giga-monolithic it is. Unzipping now... I'll probably be dead asleep by the time it finishes, it is pretty late here and I have no idea what the hell I'm doing on my comp when I have this important Chem exam on Friday. RIP me
    Just wait till you compile the damn thing. You could write a short novel in the time it takes to do that....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threads debugging graohical tool
    By bremenpl in forum C Programming
    Replies: 2
    Last Post: 08-03-2016, 03:53 AM
  2. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 09:45 PM
  3. Im in need of a special debugging tool!
    By ref in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2001, 08:18 AM
  4. I need a special debugging Tool!
    By ref in forum Windows Programming
    Replies: 0
    Last Post: 10-29-2001, 03:06 AM

Tags for this Thread