I'm calling some c++ code in my php app using "exec" as explained in this tutorial. I'm trying to figure out how can I debug the c++ code once it's executed.
the c++ app starts and ends way faster than me using for example eclipse attach to process.
any ideas how to debug the c++ code once it's called with exec() from the php app?
I'm on linux using eclipse and GDB.
the php app workflow is the following:
get values from html form, pass these values to c++ code called with exec(), get the output from c++, then display it using php onto html. think of it as php is the controller and c++ is the model.
Really depends on how you are using the php code in the first place. If you are using PHP to generate a web-page, where you do some C++ code in the middle of the generation of the HTML (or whatever), then it's a bit tricky. If you are using command-line to run the PHP, you could just change $command from "myprog arg1 arg2 into "gdb --args myprog arg1 arg2" - which will start the debugger gdb instead of just running "myprog".
If you can't debug the actual application (because the output of "myprog" is part of your web-page, for example), then I would simply run $ gdb --args myprog arg1 arg2 on a command-line on the machine, and debug the code standalone.
Or, if you HAVE to debug it as part of the web-app, you could perhaps add sleep(10); to the beginning of the "myprog", and use gdb attach X where X is the process ID (from top or ps aux | grep myprog, for example) - set a breakpoint just after the sleep(10); line, and do the gdb command continue.
Related
Supposed to Open SHELL App (C) which i Coded as interface for my purpose
I Already know about exec and shel_exec which couldn't be helpful on this.
My Console Application which i had to OPEN on php is interactive shell and batch (Both Modes are available).
I Need to communicate to shell, not just opening and get result (like exec func).
I want to Open My Shell and Send commands to it , which can be Interactive I/O or batch I/O.
Well If No Straight solution found with php built in Function I am going to pass my command as String Parameter to console application
But Straight solutions with php built in library and behavior still would be accepted
I'm using PHP CLI on Windows. Up until recently, I've always used .bat files to start PHP scripts ("php test.php", basically), which uses the cmd.exe terminal. For numerous reasons, I'm trying to migrate to PowerShell, which unlike cmd.exe is not officially deprecated or "semi-deprecated". I have had numerous problems with this, but this is specifically in regards to PHP CLI.
Basically, if I use a .ps1 script (that's PowerShell v1, which I believe is the current version of PowerShell) to run a PHP script (again, "php test.php"), it starts in PowerShell as expected, but if I then use any of the shell_exec() or similar functions in PHP to open new PHP scripts, it always seems to default to using cmd.exe instead of PowerShell.
One "workaround" seems to be to explicitly call:
powershell actualcommand
But this is not the same thing as actually using PowerShell. It seems to be more like embedding PowerShell into a cmd.exe terminal. I wish to exclusively use PowerShell instead of cmd.exe from PHP. I've searched and searched but I can't find any kind of php.ini setting or anything of the sort which would instruct PHP as to which terminal to use.
What is the correct way to make PHP CLI use a specific shell/binary for terminal-related stuff? Or is it hardcoded to cmd.exe on Windows?
If I understand you correctly, you're looking for a way to make PHP use PowerShell instead of cmd.exe as the shell in functions such as system() and shell_exec().
Given that PowerShell is increasingly supplanting cmd.exe, that desire is understandable - why not move to PowerShell in all aspects?
Unfortunately, doing so would be a massively breaking change, because existing code with system() / shell_exec() calls that were written for cmd.exe is likely to break, given that cmd.exe and PowerShell have fundamentally different syntax.
All existing scripting languages that offer functionality for running shell commands face this problem.
As an aside: A more fundamental problem is that code that runs shell commands is platform-specific.
Even if there were an opt-in way to change the default shell - and there isn't - it would have to be scoped to your code, so as not to break third-party code.
Generally speaking, there are only two ways for a scripting language to make this transition:
Commit to breaking backward compatibility, requiring all code to be (re)written for PowerShell.
Make the transition opt-in on a per-code-unit basis, where (new) code has to explicitly signal the desire to use PowerShell as the default shell.
Therefore, I think you'll have to forgo the convenience of shell_exec() by using exec() with an explicit call to PowerShell's CLI instead; e.g., to execute commands Get-Date; "Hello, world."; exit 42:
<?php
exec('powershell -noprofile -command Get-Date; \"Hello, world.\"; exit 42', $output, $exitCode);
// Output the captured stdout lines and the exit code.
echo implode("\n", $output);
echo "\nExit code: " . $exitCode
?>
exec() generally has the advantage of being able to report the process' exit code, which shell_exec() cannot.
Use -file instead of -command if you're calling a script file (*.ps1).
To call PowerShell Core rather than Window PowerShell, use pwsh.exe instead of powershell.exe.
Note the perhaps surprising need to \-escape the embedded " chars. - see this GitHub issue.
I'm trying to open a Windows program from PHP using exec() on a local machine. Is it possible to start a system program (on Windows 10 if it's relevant) that runs in the foreground using PHPs exec function?
This line:
exec("C:/Windows/notepad.exe 2>&1");
Causes Microsoft's Notepad to open in the background (verified it is actually running using task manager) but I have no access to it, i.e., it doesn't open a window. How do I get it to run in the foreground so I can actually see it and interact with it?
So this seems like an utter ball ache to achieve using exec() for your average coder. There's another way to achieve this result: Have PHP generate .bat files using file_put_contents() with instructions to open a given file path and then auto-delete itself, like so:
#echo off
Start "" "C:\Path\To\File\SomeFile.txt"
del %0
This method requires some kind of task scheduler to monitor a given folder and execute the batch files as they come in. I believe PowerShell can do this, and possibly the Windows Task Scheduler. I think Linux has Cron.
I want to extend the automation of the PacketETH program CLI using PHP
It can be done in GUI however this still means a user has to do it
Is it possible to have the packetETH run and a PHP deliver instructions, and then receive results back for manipulation?
In a broader sense, is this type of connection possible at all?
Thankyou
You can access the command line from php by using the Php System Program Execution functions. http://php.net/manual/en/book.exec.php. You can try out the exec function. It lets you execute shell commands.
To run the packeteth program from php you can use a command like the following:
exec("/path/to/packeteth/packETHcli -i lo -m 1 -f packet1.pca");
I'm tempted by Automator.app's ability to create contextual services in Mac OS X Snow Leopard. I would like to create some keyboard accessible shortcuts to manipulate text snippets by calling a shell script. However, Automator only suggests bash, Perl, Python and Ruby (among others) to allow this. But, since PHP also ships with Mac OS (and, to be honest, it's the only scripting language I fully master), I wonder why I can't run a PHP shell script.
This is just a hack, but how about creating a python, or ruby or perl or bash script that calls php command-line interpreter with the php script you want to execute?
For instance in bash would be as simple as this:
#!/bin/bash
php myscript.php
Now save it, give it permission for execution:
chmod +x my_bash_script.sh
And voilá. Automator can now run the bash script, which calls the php script.
dbr's solution above is excellent, but I found that it didn't work for me, perhaps due to a later change in the shell, php, or OS version (I'm using OS X 10.8). After a lot of head-scratching I found that the answer is to quote the heredoc marker.
A useful addition is to use the appropriate syntax to pass the arguments through to the php script. Putting all that together, I'm using:
php -- "$#" <<'EOF'
<?php
print_r( $argv );
?>
EOF
You can use the "Run Shell Script" action to run a PHP script. One self-contained way is to use <<EOF ... EOF to pass the script to the php command:
(I would strongly recommend learning Python, Ruby or Perl.. PHP does has some advantages for web-programming, but is just not intended for command-line scripting.. It's certainly doable, it'll just not be nearly as.. pleasant)
The list of shells that Automator supports is in
/System/Library/Automator/Run\ Shell\ Script.action/Contents/Resources/Shells.plist
You can follow this guy's instructions to add an entry for /usr/bin/php. You may want to copy the .action file to ~/Library/Automator first, and work on the copy instead.
Of course, it's probably easier to just wrap it in a bash script, as others have suggested.
Automator has a "Run Shell Script" Action just add it to your workflow and follow the instructions on the bottom left.