Thread: How to debug every instruction of the code ?

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    84

    How to debug every instruction of the code ?

    Hi,

    I'm trying to debug a code I'm working on using CodeBlocks.

    The code uses the windows GetTickCount, coroutines and other macros.

    But when I start the debugging session, I step into like two lines, then the debugger hangs a little bit then it exits the debugging session.


    I used to do simple debugging using CodeBlocks, but with this code, I can't pass more than two lines of code I don't know why ?

    What should I do ?
    Last edited by wolfrose; 08-07-2022 at 09:14 PM. Reason: modifying the option of notifications

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps put breakpoints in your coroutines.

    Debugging anything with any kind of internal asynchronous behaviour (processes, threads, coroutines) can be challenging.
    GDB has a lot of thread support, but you'd have to read the manual for the detail.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    84
    I used breakpoints, but the debugger doesn't stop and continue to run.

    Yep, I'm trying to develop a simple thread/task manager using similar macros like coroutines, and this is where debugging started not to work.

    I read about GDB, but what is GDM exactly ? Is it a complete IDE, SDK or plugin ?

  4. #4
    Registered User
    Join Date
    Feb 2022
    Posts
    45
    GDM is Gnome Display Manager, but I am pretty sure you meant 'GDB' both times and that 'GDM' was just a typo.

    Gnu Debugger (GDB) is the standard debugger for Binutils and GCC, and is included with most Linux distros by default, as well as being included with MinGW. GDB is a text-based debugger, but there are many graphical front-ends for it. It is still advisable to learn at least a little about using GDB in the shell, however, as that is the lowest common denominator for systems that run GDB at all, and most front ends only support a small part of the GDB tools.

    By default, Code::Blocks uses GCC as the compiler suite (under MS Windows, this means it uses the MinGW distribution of GCC), though it can be configured to use Clang or even VC++ if you want. IIUC, the default for the Code::Blocks debugger is to act as a front-end for GDB.

    How is your Code::Blocks setup configured? Are you using the default setup, or are you using your own setup?

    Specifically, do you have the GCC option '-g' set in your Debug build options, to ensure that debugging symbols are included in the executable file? Also, I am assuming that you are running Windows based on what you said about needing GetTickCount(), but what version of Windows is it? This is unlikely to be relevant, but the Code::Blocks docs did mention some issues involving older versions of Windows.
    Last edited by Schol-R-LEA-2; 08-08-2022 at 12:16 PM.

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    84
    Hi Schol-R-LEA-2 thank you for the wonderful answer. But it's a bit difficult for me to comprehend everything in your answer as I haven't searched about debuggers before, and actually the debugger worked with me lately well even with coroutines macros, and the breakpoints worked too. But since I added more macros, it doesn't work.

    But wait a minute ! I closed the current and opened another project that is cloned of the current project, and started the debugger and it worked. But the debugger is essentially doesn't open any macro and run it right away in one go.

    So, the problem is in the project as it contains fair amount of macros.

    This is a screenshot of my work, notice the debugger arrow is at line 102, when I press "step into", the debugger stops and exits the debugging session. Even after line 102, there are no macros yet, and I was expecting at least the debugger to step into the lines that are not macros, (if the macros are the issue and not something else).

    The current code compiles fine without errors or warnings:

    How to debug every instruction of the code ?-codeblocks_debugger_issue-jpg


    GDM is Gnome Display Manager, but I am pretty sure you meant 'GDB' both times and that 'GDM' was just a typo.
    Yes, I meant GDB.

    How is your Code::Blocks setup configured? Are you using the default setup, or are you using your own setup?
    Yep, it's the default. I haven't changed anything.

    How to debug every instruction of the code ?-codeblocks_gdb_settings-jpg


    Should I check this option ?

    How to debug every instruction of the code ?-codeblocks_gdb_check_option-jpg


    Gnu Debugger (GDB) is the standard debugger for Binutils and GCC, and is included with most Linux distros by default, as well as being included with MinGW. GDB is a text-based debugger, but there are many graphical front-ends for it.
    Does this mean the current codeblocks debugger is equipped with GDB ?

    It is still advisable to learn at least a little about using GDB in the shell,
    I really didn't do any programming with shell or Linux.

    however, as that is the lowest common denominator for systems that run GDB at all, and most front ends only support a small part of the GDB tools.
    What are the lowest common and most front ends ?


    Specifically, do you have the GCC option '-g' set in your Debug build options, to ensure that debugging symbols are included in the executable file?
    Where to put '-g' ? in the arguments part ?

    How to debug every instruction of the code ?-codeblocks_gdb_check_option1-jpg

    Also, I am assuming that you are running Windows based on what you said about needing GetTickCount(), but what version of Windows is it? This is unlikely to be relevant, but the Code::Blocks docs did mention some issues involving older versions of Windows.
    I'm running Windows 10 x64, with CodeBlocks 20.03.
    Last edited by wolfrose; 08-08-2022 at 04:07 PM.

  6. #6
    Registered User
    Join Date
    Feb 2022
    Posts
    45
    Just out of curiosity, do you have an offsite repo on GitHub or somewhere similar where we could review the code? I would ask you to post the code here, but it looks to be rather too large for that.

  7. #7
    Registered User
    Join Date
    Jan 2016
    Posts
    84
    Yes, I really appreciate your help.

    This is the repository:
    GitHub - eagl1/thread_task_coroutines_manager: not complete version of the project

  8. #8
    Registered User
    Join Date
    Feb 2022
    Posts
    45
    The '-g' option is actually in Compiler settings, not debugger settings. Go to Settings-> Compiler... -> Global Compiler Settings -> Debugging and select the option "Produce Debugging Symbols".
    How to debug every instruction of the code ?-debugging-option-png

  9. #9
    Registered User
    Join Date
    Feb 2022
    Posts
    45
    Hmmn, OK, that's not really how you would normally use Git and GitHub. The idea is not simply to have a place where you can drop a compressed copy of the directory; version control is supposed to manage each files' changes as you update them. For an example of what I mean, see this example repository.

    The idea is that as you work on your code, whenever you reach a suitable stopping point or milestone, you would 'commit' the code to your local repository, then at some point you would 'push' the committed updates to the offsite hosting. This allows others to 'clone' your repo and 'pull' updates as they come up, and even make 'push requests' to the repo which you could accept if you wanted to, adding the other person's code to the codebase automatically. You would then pull the other person's updates to your local copy, and everyone would have up to date copies of the files. You could make 'branches' of the code for when you are experimenting with different changes.

    If two people are working on the repo at the same time, it allows you to 'merge' updates, either automatically or manually.

    Most importantly, you can track changes to your code, and roll back any mistakes you might make, or track bugs to which update they originated at.

    Unfortunately, unlike most current IDEs and programming editors, Code::Blocks doesn't appear to have either built-in support for any version control system, or a suitable plug-in for that purpose. This, IMAO, is a fatal flaw for a modern IDE, as source code version control is vital for even small projects, and is one of the reasons I've moved away from Code::Blocks personally (I used old-school Emacs for years, and tried and rejected several other IDEs over the years as inadequate; lately, however, I've been trying out VS Code and have found it quite suitable).
    Last edited by Schol-R-LEA-2; 08-09-2022 at 12:33 AM.

  10. #10
    Registered User
    Join Date
    Feb 2022
    Posts
    45
    To make things a bit easier for you to fix, I've forked a copy of your repo, then rearranged everything so that each of the files is under source control in the conventional Git layout. I have submitted a pull request which you can then accept if you wish.

    The thing to do then would be to clone the repository onto your own system and replace the existing code folder with the cloned repo.
    Last edited by Schol-R-LEA-2; 08-09-2022 at 02:02 AM.

  11. #11
    Registered User
    Join Date
    Feb 2022
    Posts
    45
    How is this coming along? I can't do much more myself, since I run Linux as my daily driver and don't have any Windows VMs I could spin up to test this; I was able to cross-compile the code with the Linux version of MinGW64 (using VS Code, rather than Code::Blocks, though I ended up building it with a manually-written Makefile) and can run it using Wine, but I wasn't up for trying to figure out how to debug a program running in Wine through Code::Blocks.

    BTW, would you mind giving us a bit more background on the intended application of this program? You appear to be simulating sending data to the I2C bus... except that standard PCs don't technically have an I2C bus (They do usually have SMBus, which is derived from the I2C standard, but not quite the same), so I am not sure what the overall goal is. Also, is there a reason why the standard Windows threading couldn't be used? I get the impression that this is meant to be code which targets a different type of system such as a Raspberry Pi or an Arduino, but in that case the (sole) dependency on <windows.h> doesn't make sense.

    I realize that this is a bit outside of the thread's remit (since you were mostly asking about debugging under Code::Blocks), but it might be relevant to the answer.
    Last edited by Schol-R-LEA-2; 08-10-2022 at 11:29 AM.

  12. #12
    Registered User
    Join Date
    Jan 2016
    Posts
    84
    How is this coming along?


    I gave up searching for a debugger, and continue on CodeBlocks without trying to get a more deeper debugging mechanism.

    I tried CLion, the debugger worked better but I had to remove parts of the code which contains the macros, also faced some problems including the libraries included in the project folder.

    I can't do much more myself, since I run Linux as my daily driver and don't have any Windows VMs I could spin up to test this; I was able to cross-compile the code with the Linux version of MinGW64 (using VS Code, rather than Code::Blocks, though I ended up building it with a manually-written Makefile) and can run it using Wine, but I wasn't up for trying to figure out how to debug a program running in Wine through Code::Blocks.
    Thank you for the help, I really appreciate your support. I just installed VS enterprise, but I didn't do anything in it until now. I working on CodeBlocks and I have to carry and measure the code output with program flags and test snippets.

    BTW, would you mind giving us a bit more background on the intended application of this program?

    Yes, actually multitasking and task management, this topic has been my interest for years.

    First as my work is with microcontrollers, but many code development I do on desktop IDE; like CodeBlocks. I learned basics of microcontrollers and device drivers, did some microcontroller projects. For code management, especially timing, people use microcontrollers' timers to do the timing with round robin method which is fine and enough for many applications.

    Then I learned about coroutines which is really interesting. But the current skill I want to learn now is how to run a code sequentially that is non-blocking with while loops to; for example, wait for a bit to be set or clear, that this time could be few milliseconds.

    As the coroutines version I learned first run the code in parallel mode, now I want to run stuff sequentially, to do this, I'm using the same coroutines macros that run the code in parallel and change some lines to run the codesequentially.

    Also, let's say that I have more than one task to run simultaneously and each task contains functions that have to be run sequentially, this what I'm working on.

    So the goal is have a thread that has tasks inside, the thread run the tasks in parallel, and the tasks run what inside them sequentially.

    You appear to be simulating sending data to the I2C bus... except that standard PCs don't technically have an I2C bus (They do usually have SMBus, which is derived from the I2C standard, but not quite the same), so I am not sure what the overall goal is.


    Of course I won't be able to
    simulate the whole I2C protocol, I'm just doing simple functions that I'm just focusing now on how to store the current line of the function, test the bit, if it's SET, which means the I2C is busy so I have to exit the function and do something else and get back again and test the bit, if it's CLEAR, then I move on to the next line, and so on.

    Also, is there a reason why the standard Windows threading couldn't be used?
    I have downloaded many threading libraries that are done for microcontrollers, but I want to do something that is very simple to use and efficient. And also I take that as a challenge to learn some algorithms, some FSM skills, ... etc.

    I get the impression that this is meant to be code which targets a different type of system such as a Raspberry Pi or an Arduino, but in that case the (sole) dependency on
    <windows.h> doesn't make sense.


    Yes, I know it won't be the same mechanism, but I use it to do simple simulation because it's easier for me instead of uploading a code to the microcontroller every time and measure the output I'm expecting to get.

    The console application simulation is faster for me, with some debugging, I learn the skill I'm searching for and apply that on my microcontroller library. I have done many snippets in this way and worked on the microcontroller with of course some to little issues because the change of the whole programming environment but the same C compilation standards.

    I realize that this is a bit outside of the thread's remit (since you were mostly asking about debugging under Code::Blocks), but it might be relevant to the answer.
    Yes, I was and still searching for a debugger that can debug every instruction in the code, but after some search, I think it's a bit difficult. As I downloaded CLion and did some debugging and it's a bit better than CodeBlocks but not much. Also downloaded VS enterprise but I think it doesn't have the C compiler so I have to search if I can install it or not.


    Thanks for your consideration in my issue
    Last edited by wolfrose; 08-10-2022 at 04:16 PM.

  13. #13
    Registered User
    Join Date
    Jan 2016
    Posts
    84
    Hi,

    I'm progressing in the code, I guess I have to do things without a debugger.

    I just thought that I can find or set the debugger to step into every instruction or step of the program.

    Anyway thank you all for the support.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Illegal Instruction at perfectly fine Code
    By m00ni in forum C Programming
    Replies: 24
    Last Post: 02-14-2011, 02:56 AM
  3. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  4. Giving instruction to an executing C code
    By darkwalk in forum C Programming
    Replies: 2
    Last Post: 10-24-2006, 12:01 PM
  5. ASM 'Out Instruction' crashes, code included.
    By Xei in forum C Programming
    Replies: 5
    Last Post: 03-03-2003, 05:42 PM

Tags for this Thread