FedoraForum.org - Fedora Support Forums and Community
Results 1 to 12 of 12
  1. #1
    Join Date
    Jul 2014
    Location
    EU
    Posts
    235

    How debug program in C in Eclipse as root

    Is it possible please ?

  2. #2
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,123

    Re: How debug program in C in Eclipse as root

    I don't use Eclipse, but tell us more generally, about your program.
    I have written programs of several hundred lines, and have not required more than a few debug statements.

    I have two macros that I use to help with debugging. I will post them below.

    Code:
    /* Use debug1 where needed. Rename to debug when use is over */
    #define debug1(M, ...) fprintf(stderr, "DEBUG %s %s %d: " M "\n",__FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__ );
    #define debug(M, ...)
    Here is how I use them.
    normal use
    debug("inkey=%s FSIZE=96 len=%u",inkey,(unsigned)strlen(inkey)); //like printf.

    The debug macro will not insert compiler source lines into your code.
    But adding a 1 to it's name as:
    debug1("inkey=%s FSIZE=96 len=%u",inkey,(unsigned)strlen(inkey)); //like printf

    will result in the debug1 macro inserting debugging information.

    Others will often make a debug global that expands everywhere in the code. I do not recommend that approach.
    You only want debug1 output where the problem is occurring.
    With the approach I take, you do not need to change compile parameters.

    Do you really need to test your code with root privileges?
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  3. #3
    Join Date
    Oct 2010
    Location
    Canberra
    Posts
    2,928

    Re: How debug program in C in Eclipse as root

    I would not recommend using Eclipse as root. There are a myriad things that could go awry and destroy your system.

    I assume this is necessary because the program your are building needs root privileges ?

    One way to approach the problem is to add debug statements, as suggested by Leslie.
    If the program is crashing then the core file might be useful.
    There are other techniques, but it would be helpful if you describe the program in a bit more detail, and characterise the nature of the bug.
    I have a short list of bug types and their symptoms:
    1. Uninitialised variables: Works first time, crashes the next time the function runs.
    2. Race condition: Works OK sometimes, scrambled at other times.
    3. Memory corruption: Crashes at a consistent point when doing something that is OK of itself. Changing something before the corruption may change the crash point.
    4. Stack overwrite: Crash on return from function.
    5. Confused Ownership: Value of an object changes unexpectedly. (This is a multi-threaded issue.)
    6. Performance bugs: Performance deteriorates rapidly as the size of the data set increases.

    User error. Please replace user and try again

  4. #4
    Join Date
    Jul 2014
    Location
    EU
    Posts
    235

    Re: How debug program in C in Eclipse as root

    Quote Originally Posted by lsatenstein
    I don't use Eclipse, but tell us more generally, about your program.
    I have written programs of several hundred lines, and have not required more than a few debug statements.

    I have two macros that I use to help with debugging. I will post them below.

    Code:
    /* Use debug1 where needed. Rename to debug when use is over */
    #define debug1(M, ...) fprintf(stderr, "DEBUG %s %s %d: " M "\n",__FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__ );
    #define debug(M, ...)
    Here is how I use them.
    normal use
    debug("inkey=%s FSIZE=96 len=%u",inkey,(unsigned)strlen(inkey)); //like printf.

    The debug macro will not insert compiler source lines into your code.
    But adding a 1 to it's name as:
    debug1("inkey=%s FSIZE=96 len=%u",inkey,(unsigned)strlen(inkey)); //like printf

    will result in the debug1 macro inserting debugging information.

    Others will often make a debug global that expands everywhere in the code. I do not recommend that approach.
    You only want debug1 output where the problem is occurring.
    With the approach I take, you do not need to change compile parameters.

    Do you really need to test your code with root privileges?
    Thank you. It's Intel's vulnerability detection tool which requires root and when I want debug it to see what it is doing then debugging as root is IMHO fastest way.

  5. #5
    Join Date
    Jul 2014
    Location
    EU
    Posts
    235

    Re: How debug program in C in Eclipse as root

    Quote Originally Posted by ocratato
    I would not recommend using Eclipse as root. There are a myriad things that could go awry and destroy your system.

    I assume this is necessary because the program your are building needs root privileges ?

    One way to approach the problem is to add debug statements, as suggested by Leslie.
    If the program is crashing then the core file might be useful.
    There are other techniques, but it would be helpful if you describe the program in a bit more detail, and characterise the nature of the bug.
    I have a short list of bug types and their symptoms:
    1. Uninitialised variables: Works first time, crashes the next time the function runs.
    2. Race condition: Works OK sometimes, scrambled at other times.
    3. Memory corruption: Crashes at a consistent point when doing something that is OK of itself. Changing something before the corruption may change the crash point.
    4. Stack overwrite: Crash on return from function.
    5. Confused Ownership: Value of an object changes unexpectedly. (This is a multi-threaded issue.)
    6. Performance bugs: Performance deteriorates rapidly as the size of the data set increases.
    Can you please just as a matter of interest write how it can destroy system ?
    Please see my previous post.

  6. #6
    Join Date
    Oct 2010
    Location
    Canberra
    Posts
    2,928

    Re: How debug program in C in Eclipse as root

    Quote Originally Posted by boycottsystemd
    Can you please just as a matter of interest write how it can destroy system ?
    Please see my previous post.
    Eclipse is a rather large program that can load even more components from the web. I doubt that any of these components were written with the idea that they would be run as root. In other words system security is not something that its developers would put much thought into.

    The easiest way to mess up a system is to run a command that expects to have some environment variable as the first part of the path. Run as root without the environment variable being set --> dead system.

    If you want to see what the program is doing you could run it under strace - the sequence of system calls and the files its trying to access can be quite informative. Once you know what it is doing that needs root, then perhaps you might be able to set up a simulated environment (eg with chroot) for it so that you can run it with an ordinary user account.

    And, just because Intel wrote it don't think it cannot be faulty. I had the "pleasure" of looking at some of their code when working on some DLNA stuff.

    User error. Please replace user and try again

  7. #7
    Join Date
    Jul 2014
    Location
    EU
    Posts
    235

    Re: How debug program in C in Eclipse as root

    Quote Originally Posted by ocratato
    Eclipse is a rather large program that can load even more components from the web. I doubt that any of these components were written with the idea that they would be run as root. In other words system security is not something that its developers would put much thought into.

    The easiest way to mess up a system is to run a command that expects to have some environment variable as the first part of the path. Run as root without the environment variable being set --> dead system.

    If you want to see what the program is doing you could run it under strace - the sequence of system calls and the files its trying to access can be quite informative. Once you know what it is doing that needs root, then perhaps you might be able to set up a simulated environment (eg with chroot) for it so that you can run it with an ordinary user account.

    And, just because Intel wrote it don't think it cannot be faulty. I had the "pleasure" of looking at some of their code when working on some DLNA stuff.
    Well thank you for explanation.

  8. #8
    Join Date
    Nov 2004
    Location
    MT USA
    Posts
    904

    Re: How debug program in C in Eclipse as root

    What's wrong with just using gdb? That is how I debug all my 'c' programs in linux land and when using mingw in Windoze.

  9. #9
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,123

    Re: How debug program in C in Eclipse as root

    Hi RC

    Its ok to use gdb. I consider it's use during debugging as similar to using a sledge hammer to kill a fly. When gdb is used, a side effect is that the code is not optimised, the output code is most likely different from what would go into the final product compiled with -O2 or -O3.

    Back to my macro use.
    the debug1() activated where you think you have problems, allows you to run with gcc -O2 or gcc -O3 and you know, except for the (debug1) print statements, that the object file/program code is close to the final version. I have encountered gcc compiler errrors using the debug statements that would not have shown up if the gdb option was invoked.
    When the function or that part of code is correct, a simple change from debug1 to debug and a recompile removes the debugging code from the final outcome. My code often brings together 6 or 7 modules that I compiled separately. Its the use I found that saved me hours of global debugging. And if need be, one year later, when a change or bug is detected, simply changing the "debug" back to "debug1" (only where useful) is a great time saver.
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  10. #10
    Join Date
    Nov 2004
    Location
    MT USA
    Posts
    904

    Re: How debug program in C in Eclipse as root

    Oh, I use print statements too with debug flags to turn on and off and they usually very helpful to a point (they can also mask what is going on as well)... But sometimes, you need a debugger to get to the root of the problem. I don't see how eclipse helps with that as if I remember right, eclipse just uses gdb as it's back end anyway when debugging.

  11. #11
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,123

    Re: How debug program in C in Eclipse as root

    Hi RC,
    With flags and print statements as you mentioned, do these statements remain in the compiled code? With the debug() and debug1() macro statements, only the latter is in the compiled code.
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  12. #12
    Join Date
    Nov 2004
    Location
    MT USA
    Posts
    904

    Re: How debug program in C in Eclipse as root

    Depends on what I am doing. Sometimes a simple printf() works and then I just comment it out if I want to keep it or delete it when done. Other times I will use a debug flag to enable debugging statements while on-line knowing that I'll probably be back debugging when module is used in the field (comm protocol modules come to mind). All 'depends' on how complicated the module is. Other times, I use a logging to disk debug module as some apps don't have console access. There are so 'many' different ways of debugging software. Sometimes though gdb (or equivalent) just has to be used, to find out what the stack looks like, or what's in the registers at the time of a call, or backtrace to the module passing the wrong data, corrupting the stack, etc...

Similar Threads

  1. Eclipse freezes F21 while using debug perspective
    By volbollo in forum Programming & Packaging
    Replies: 12
    Last Post: 16th March 2015, 12:02 PM
  2. Replies: 0
    Last Post: 23rd November 2010, 09:16 AM
  3. Debug in eclipse start very long.
    By Ulat in forum Programming & Packaging
    Replies: 0
    Last Post: 15th March 2010, 01:22 PM
  4. Replies: 0
    Last Post: 7th May 2009, 09:18 PM
  5. Multiple debug launches in same Eclipse project?
    By CD-RW in forum Using Fedora
    Replies: 1
    Last Post: 4th May 2009, 09:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •