2

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.

4

1 回答 1

3

尝试使用 -g 编译代码并删除 -O [编译器标志]。优化编译器(gcc)时会更改指令顺序以提高速度。重新编译后再次附加调试器。

于 2012-10-26T14:15:52.683 回答