I have the following code :
char command[256];
sprintf(command,"addr2line %p -e xcep_app", trace[i]);
addr2lineWriter = popen(command, "r");
if (addr2lineWriter == NULL)
flag = false;
if (flag == true) //execute parsing the output only if the command ran in the first place
{
while (fgets(addr2line_output, sizeof(addr2line_output)-1, addr2lineWriter) != NULL)
{
std::string addr2line_output_(addr2line_output);
complete_backtrace_.push_back(addr2line_output_);
}
pclose(addr2lineWriter);
}
Everything works fine, but I always get the following message :
sh: 1: Syntax error: word unexpected (expecting ")")
Why is it coming and how to stop it. also, what does it mean ?
I have the gnome terminal installed.
This is a guess based on the error message. trace[i] is NULL, so the command generated is:
addr2line (nil) -e xcep_app
And so the shell that is invoked to execute the command complains about the parentheses. #WumpusQ.Wumbley reports that ash will reproduce this message:
ash -c 'addr2line (nil) -e xcep_app'
Related
Here's a terminal command:
awk '/^Mem/ {print $4}' <(free -m)
Here's my code:
class CSystemInfo{
public:
std::string free_ram(){
std::string s;
FILE *in;
char buff[512];
//here is a prepared string (but with an error)
if(!(in = popen("awk '/^Mem/ {print $4 \"\\t\"}' <(free -m)","r"))){
return "";
}
while(fgets(buff, sizeof(buff), in)!=NULL){
cout << s;
s=s+buff;
}
pclose(in);
return s;
}
};
CSystemInfo info;
std::string ram = info.free_ram();
cout << ram;
The above code, when run, returns this message:
sh: 1: Syntax error: "(" unexpected
How can I place the '/' symbol, for this command to work correctly?
Your problem is not in C++. You are invoking your command with popen, and popen runs your command in sh shell, that does not support <() syntax, while in your terminal you are having bash, zsh or any other shell, that does support <() syntax.
Edit: Better choise! Use pipes!
popen("free -m | awk ...")
Original answer, not working!: Try invoking bash in popen:
bash -c "awk '/^Mem/ {print $4}' <(free -m)"
in code:
popen("bash -c \"awk '/^Mem/ {print $4}' <(free -m)\"")
i would like to retrieve remote process input arguments programmatically,
so i used sysctl in the following manner (partial code) :
int mib[3]
mib = {CTL_KERN, KERN_ARGMAX,0,0}
size = sizeof(argmax);
if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) {
goto ERROR_A;
}
procargs = (char *)malloc(argmax);
mib = {CTL_KERN,KERN_PROCARGS2,pid,0}
size = (size_t)argmax;
if (sysctl(mib, 3, procargs, &size, NULL, 0) == -1) {
printf("a");
goto ERROR_B;
}
however, i doesn't work for all processes.
here's is a working case :
/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker -s mdworker -c MDSImporterWorker -m com.apple.mdworker.shared
but the case below failed on "sysctl(mib, 3, procargs, &size, NULL, 0)".
although, i successfully got the command line using 'ps ax PID' from terminal
0 655 501 0 10:55AM ttys008 0:00.02 login -pfl zohar81 /bin/bash -c exec -la bash /bin/bash
perhaps you can tell me why calling sysctl with KERN_PROCARGS2 failed in the second example ?
thanks
I discovered that on OS X, the process is granted access to KERN_PROCARGS2 for all processes when the owner of the executable is root and the the executable has the SUID (set-uid) flag enabled. The only exception is PID 0 (kernel_task) but that doesn't even have arguments.
You can make this work for your file using:
chmod u+s myprogram
sudo chown root:wheel myprogram
# now my program should be able to make get KERN_PROCARGS2 for all processes
./myprogram
i am writing some functions on c++ for compiler less to css.
i installed nodejs, less.
i created a less file test.less
#color: red;
a{color:#color;}
when i run command on terminal:
lessc test.less test.css
it created a files css with name is test.css, but when i run this command via c++, it return a error. please help me. this is my c++ function:
std::string shell_exec( std::string cmd )
{
std::string result = "";
FILE* pipe = popen(cmd.c_str(), "r");
if (pipe == NULL)
{
return result;
}
char buffer[128];
while(!feof(pipe))
{
if(fgets(buffer, 128, pipe) != NULL)
{
result += buffer;
}
}
pclose(pipe);
return result;
}
shell_exec("lessc test.less test.css");
i got a error:
/usr/bin/env: node: No such file or directory
/usr/bin/node is existed.
================ UPDATE: Fixed==================
Thank you #Bass Jobsen , #Lightness Races in Orbit
i fixed by add absolute path to lessc and nodejs
shell_exec("/usr/bin/node /usr/bin/lessc test.less test.css");
From: https://unix.stackexchange.com/a/29620
The advantage of #!/usr/bin/env python is that it will use whatever
python executable appears first in the user's $PATH.
So you should add node to the $PATH of the user that runs your script, see: https://stackoverflow.com/a/13210246/1596547
Notice that i can not compile your code, but i can when using the following code:
int main()
{
std::string r = shell_exec("lessc test.less test.css");
}
Probably also use using namespace std and string instead of std:string.
Working on Linux am trying to build a Crash Handler function - so as before crashing we get stack trace in log file -
done some study and came to know,its done either using -g/-rdynamic Flags to generate debug info then use glibc backtrace / backtrace_symbols_fd functions to get stack trace ...
Its not much of use as in my app, am not allowed to use -g/-rdynamic flags ..
so reading further reading
I have build the following code which is not working as expected and in generated log file it ends in following error :
(gdb) Hangup detected on fd 0
error detected on stdin
A debugging session is active.
Inferior 1 [process 6625] will be detached.
Quit anyway? (y or n) [answered Y; input not from terminal]
Detaching from program: /u/vivek/demo/testprg, process 6625
Code:
Register a Crash Handler function at startup as -
struct sigaction act;
memset(&act, 0, sizeof (act));
act.sa_sigaction = ProcessCrash;
sigfillset (&act.sa_mask);
act.sa_flags = SA_SIGINFO;
sigaction (SIGSEGV, &act, NULL);
Following function called when my program receives SIGSEGV signal -
void ProcessCrash( int signal, siginfo_t * siginfo, void *context)
{
cerr <<"** ProcessCrash –Handler Function**" << endl;
ucontext_t *uc = (ucontext_t *)context;
if (signal == SIGSEGV)
fprintf(stderr, "\nGot signal %d, faulty address is %p, "
"from %p\n\n", signal, siginfo->si_addr,
uc->uc_mcontext.gregs[REG_RIP]); // REG_EIP is changed to REG_RIP for –m64 arch.
else
fprintf(stderr, "Got signal %d#92;\n", signal);
// [[ using GDB to pring the stack trace
char gdb[516];
// command 1
//sprintf(gdb, "echo 'where\ndetach' | gdb -q %.256s -pid %d > gdbTest.dump", program_invocation_name, getpid());
// command 2
sprintf(gdb, "(echo \"source Trace.txt\";cat) | gdb -q %.256s -pid %d > gdbTest.dump", program_invocation_name, getpid());
fprintf(stderr, "\n** GDB Command-> %s \n", gdb);
// output of above is
// ** GDB Command-> (echo "source Trace.txt";cat) | gdb -q /u/vivek/demo/testprg -pid 6625 > gdbTest.dump
// Run Either command 1 or command 2 but we get the same result
system(gdb);
// GDB test ]]
// Produce a core dump
abort();
return;
}
Trace.txt contents:
set logging on
where
detach
quit
Please let me know is there a way out of it ... as I am not getting ideas to overcome it ..
Not sure what you intended to do with the (echo \"source Trace.txt\";cat) | part.
Simply using gdb -batch -x Trace.txt -q %.256s -pid %d > gdbTest.dump works fine as far as I can tell.
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.
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.