user1774057
user1774057

Reputation: 21

C program terminating

I am trying to write a program in C (in Linux 64bit with GCC 4.1.2).

int program_instances(char *cmdname)
{
   char buf[32], *ret;
   char cmdbuf[512];
   FILE *cmdpipe;

   sprintf(cmdbuf, "/bin/ps -eo comm | /bin/grep -c '%s'",
      cmdname);
   cmdpipe = popen(cmdbuf, "r");

   if (!cmdpipe)
   {
      return -1;
   }

   memset(buf, 0, sizeof(buf));
   ret = fgets(buf, sizeof(buf), cmdpipe);
   pclose(cmdpipe);

   if (!ret)
   {
      return -1;
   }

   int nr = atoi(buf);
   return nr;
}

Tried to debug the issue through gdb but after the line

sprintf(cmdbuf, "/bin/ps -eo comm | /bin/grep -c '%'",cmdname);  

The programm is not crossing the above line , throwing the below lines..

Executing new program: /bin/bash
Error in re-setting breakpoint 1: No symbol table is loaded.  Use the "file" command.
[New process 2437]
Executing new program: /bin/ps

Please help us to resolve this issue.

Upvotes: 2

Views: 195

Answers (1)

Roozbeh Zabihollahi
Roozbeh Zabihollahi

Reputation: 7347

Try to compile your code with -g and remove -O [compiler flag]. When optimizing compiler(gcc) changes order of instructions to improve speed. After recompiling attach debugger again.

Upvotes: 3

Related Questions