NO

Author Topic: Any method to determine if executing in debug mode?  (Read 4086 times)

PhilG57

  • Guest
Any method to determine if executing in debug mode?
« on: January 26, 2015, 06:05:37 PM »
Hi.  Is there any method in C and also in Pelles C to determine at execution time if a program is running in debug mode or not?  I don't mean whether or not compiled for debug mode but actually running under debug mode.  So a program compiled and linked with debugging information baked into it, would show as "normal" when run outside of an IDE debuggin compiler.  I'm hoping there is some setting of flag I can test to determine the environment in which a program is executing.

Thanks.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Any method to determine if executing in debug mode?
« Reply #1 on: January 26, 2015, 07:00:48 PM »
Use IsDebuggerPresent function See.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Any method to determine if executing in debug mode?
« Reply #2 on: January 26, 2015, 11:36:13 PM »
Hi.  Is there any method in C and also in Pelles C to determine at execution time if a program is running in debug mode or not?  I don't mean whether or not compiled for debug mode but actually running under debug mode.  So a program compiled and linked with debugging information baked into it, would show as "normal" when run outside of an IDE debuggin compiler.  I'm hoping there is some setting of flag I can test to determine the environment in which a program is executing.

Thanks.
A program compiled with all debug info included should run "normal" outside of the IDE by default. Or did you just ask your question the other way around  :-\

Ralf

PhilG57

  • Guest
Re: Any method to determine if executing in debug mode?
« Reply #3 on: January 27, 2015, 12:07:23 AM »
Frankie - many thanks.  I tried that and it works great by returning a TRUE or FALSE depending on the execution environment.  I still have to poke around some more to determine if that will help solve my current problem.

Bitbeisser - I compile and link everything with debug enabled.  Usually I'm debugging something but sometimes I run that same executable "normally" using the "Execute" option under the project tab.

All - I'm having trouble getting heap space.  My desire is to use HeapCreate and then HeapAlloc to create a private heap space.   Under 'debug' HeapAlloc will fail after just a couple of calls but works fine after many more calls in 'normal' mode.  (Same executable compiled with debug set but run in different environment).  The HeapCreate obtains more than enough space for the HeapAllocs...  Somewhere online (MSDN???) I read that one of these functions (I forget which one but it must be HeapAlloc) behaves differently when under debug on Windows XP.

If I insert a GetProcessHeap, the allocations are done from the standard heap which is not so bad just not what I expected.

Where am I going wrong???  I was going to use the determination of if under debug mode or not, to alter the code around the GetProcessHeap, calling it under debug and not in 'normal' mode.

Thanks.


Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Any method to determine if executing in debug mode?
« Reply #4 on: January 27, 2015, 01:02:57 AM »
Frankie - many thanks.  I tried that and it works great by returning a TRUE or FALSE depending on the execution environment.  I still have to poke around some more to determine if that will help solve my current problem.

Bitbeisser - I compile and link everything with debug enabled.  Usually I'm debugging something but sometimes I run that same executable "normally" using the "Execute" option under the project tab.

All - I'm having trouble getting heap space.  My desire is to use HeapCreate and then HeapAlloc to create a private heap space.   Under 'debug' HeapAlloc will fail after just a couple of calls but works fine after many more calls in 'normal' mode.  (Same executable compiled with debug set but run in different environment).  The HeapCreate obtains more than enough space for the HeapAllocs...  Somewhere online (MSDN???) I read that one of these functions (I forget which one but it must be HeapAlloc) behaves differently when under debug on Windows XP.

If I insert a GetProcessHeap, the allocations are done from the standard heap which is not so bad just not what I expected.

Where am I going wrong???  I was going to use the determination of if under debug mode or not, to alter the code around the GetProcessHeap, calling it under debug and not in 'normal' mode.
Impossible to tell you what you are doing unless seeing the code, in particular how (much) you are allocating the heap. Running in a IDE/debugger environment is using much more memory upfront then running in "normal" mode, so you could have a condition where you simply run out of RAM (including swap) sooner while debugging than when running "standalone"...

Ralf

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Any method to determine if executing in debug mode?
« Reply #5 on: January 27, 2015, 08:44:23 AM »
Under 'debug' HeapAlloc will fail after just a couple of calls...

If I insert a GetProcessHeap, the allocations are done from the standard heap which is not so bad

This sounds as if you are having these problems only when not inserting a GetProcessHeap. What are you using then? Zero? You should show us at least the few lines before and after the HeapAlloc.

PhilG57

  • Guest
Re: Any method to determine if executing in debug mode?
« Reply #6 on: January 27, 2015, 04:38:14 PM »
Here you go...

***Here are portions of the two suspect pieces of code.  The first code repetitively allocates
storage from the heap, the second code setment creates the private heap and obtains one
(the initial) chunk from it.  The first code section typically fails (returns NULL) on the
second or third call to this routine.

***Note this is not my original code but something purloined from a database project
found on the web.  I've modified much of the code but these sections are pretty vanilla.
===========================

***This is the routine, called multiple times, which will soon fail fail under debug, with
HeapAlloc returning a NULL pointer.  Works great when run 'normally' even though the
executable file was compiled with debug settings.

***Note you can see just below where I'm just beginning to play around with the
"IsDebuggerPresent" call mentioned to my by "frankie".
/** first code segment **/
BOOL db_RecordAdd(NODE node)   /* called from various routines; builds and chains NODEs in memory */
{
   LPNODE  tmp;
//#define _WIN32_WINNT 0x0501
   BOOL bStat;
//   bStat = IsDebuggerPresent();
//   if (bStat == FALSE)
//   {
//      WriteListBox("1", "database.c: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.");
//      ghDataHeap = GetProcessHeap();
//   }

//   ghDataHeap = GetProcessHeap();   /* PhilG added - get the address of the heap... */
            /* will run correctly normally but fails under debug??? */
   tmp->NextNode = NULL;
   /* alloc from that gotten earlier in MemoryBaseCreate */
               /* HeapAlloc(heap, flags, bytes */
   tmp->NextNode = HeapAlloc(ghDataHeap, 0, sizeof(NODE));
   if(tmp->NextNode == NULL)      /* did we get heap storage allocated */   
   {
      WriteListBox("1", "database.c: db_RecordAdd() HeapAlloc failed.");
      memset(&szMessage, ' ', sizeof(szMessage));
      (void) wsprintf(szMessage, "database.c: db_recordAdd(): HeapAlloc failed.");
      (void) MessageBox(NULL, szMessage, "Error...", MB_OK);
      _dnnest;
      return FALSE;   /* HeapAlloc failed for some reason */
   }
   ...
   ...
   ...
=========================================
=========================================

***This is where the private heap space is initially allocated.  One initlai chunk is gotten
here, the rest and majority of calls to HeapAlloc are in the code just above.

/** second code segment **/
BOOL MemoryBaseCreate(void)
{
   /*
   allocate space for "NUM_NODES" NODEs (managed files);
                this will yield about a 53K program with no files yet appended...
   */
   /* HeapCreate(optons, initial size, maxmum size where 0 = heap can grow in size */
   ghDataHeap = NULL;      /* address of heap memory */
   ghDataHeap = HeapCreate(0, (sizeof(NODE) * (NUM_NODES+2)), 0);   /* create a private heap which can grow */
   if (ghDataHeap == NULL)
   {
      WriteListBox("1", "MemoryBaseCreate() HeapCreate failed.");
      gbHaveHeapMemory = FALSE;   
      _dnnest;
      return FALSE;
   }
   gbHaveHeapMemory = TRUE;      /* have block of heap memory */
   /*
   use some of the heap space; returns pointer to allocated block
   */
// looks like GetProcessHeap not needed for private heaps
//   ghDataHeap = GetProcessHeap();   /* have to get the address of the heap... */
         /* HeapAlloc(heap, flags, size */
   gpstNode = 0;
   gpstNode = HeapAlloc(ghDataHeap, 0, sizeof(gpstNode));
   if(gpstNode == NULL)
   {
      WriteListBox("1", "MemoryBaseCreate() HeapAlloc failed.");
      _dnnest;
      return FALSE;
   }
   /*
   This will be the first NODE On the chain.  Additional NODEs will be allocated
      in db_AddRecord.  Save the address of this first NODE block so we can
      access it and the entire chain later.
   */
   if (!gbSaveFirstNodeAddr)   /* just through here one time per database open */
   {
      giNodeChain = (int) gpstNode;   /* save memory address just obtained */
      gbSaveFirstNodeAddr = TRUE;
   }
   /*
   Initialize the just gotten NODE structure
   */
   ...
   ...
   ...


Thanks again.