How can I debug remotely - linux

I'm programming something and I need debugging support on most operating systems (linux, windows, macosx).
This is what the program is supposed to do:
- fork a new program (platform specific program - like adobe reader on windows, but evince on linux)
- monitor for exceptions (handled and unhandled)
- execute a predefined function based on the exception
So basically I need some kind of a debugger, a library which I can use to make this possible. I would then start a debugger, start a program, and monitor for the exceptions.
It would also be great if I could execute & monitor for the exceptions over the network. So let's say my program is running on computer A and is connected to computer B (via ssh, RDP, whatever) and I would start a new program in computer B from computer A, and also monitor and react based on the exceptions from the computer B.
So my question is: is anything like that already written in form of a perl/python/whatever library or something similar. If it is, I would very much like to know about it.
Just to make this more clear: the linux only library call for this (just not supporing the network part) is ptrace system call.

gdb
There is a built in python debugger.pdb

Related

In Linux, how can I intercept keyboard input and optionally filter it?

I'm writing a cross platform application, I want to be able to intercept keyboard input and optionally filter it from reaching the rest application. My application loads plugins, I am trying to stop the keystrokes from reaching the plugin's UI if it has focus.
On Window I use SetWindowsHookExA and on macOS I use [NSEvent addLocalMonitorForEventsMatchingMask:]
Is there an equivalent for Linux?
I'm writing a cross platform application, I want to be able to intercept keyboard input
If your application is a GUI one, consider using a cross-platform framework such as Qt or GTK (or FLTK, FOX, etc...). If your application is command-line (like e.g. grep or GCC or ninja or MongoDB are), it might not even access the keyboard, if used inside some pipeline, and you might also use cross-platform frameworks like POCO. If your software is started by crontab, it won't even have access to the keyboard, which might not even exist or be plugged in.
The same source code (for Qt or GTK or FLTK etc...) will work for Linux and for Windows.
BTW, many Linux computers (e.g. most web servers, or a RasperryPi) don't have any keyboard or mouse.
For more, read Advanced Linux Programming and syscalls(2).
Read about Xorg and Wayland.
My application loads plugins, I am trying to stop the keystrokes from reaching the plugin's UI if it has focus.
Plugins on Linux are often implemented thru dlopen(3) and dlsym(3) as ELF shared objects, conventionally in files named *.so (so see elf(5)). Read the Program Library HowTo and if you code in C++ also the C++ dlopen minihowto. If you code in Ocaml, use the Dynlink module. If you can code in Common Lisp (e.g. using SBCL), you'll just use eval. If you have to code in Java, use some class loader.
With Xorg (that is, X11) every keyboard event is generating some well defined message (some XKeyEvent) emitted - on a tcp(7) or unix(7) socket- by the Xorg server to your Xlib client application.
On the client side (in your GUI application code), an event loop (around poll(2) or select(2)...) is waiting for such messages. See also time(7).
On my Debian system (according to file /var/log/Xorg.0.log and using proc(5)...) the Xorg server is accessing the keyboard (thru udev) as /dev/input/event1 and X11 clients are communicating with the Xorg server.

Standalone program for accessing and developing in Linux in web browser

Is there any software working like this?
Runs as a standalone program. No install is needed. Thus, can be used as an Ansible module.
After running the program in a remote Linux machine, I can open up a web browser, then open a web page provided by the program. The program provides features similar to file explorer, IDE-level code editor, debugger, etc. In terms of debugger, there is already similar one; gdbgui.
There is another way such as Gnome, KDE or X11. However, these requires much packages to be installed. I don't want they be installed, because my Linux machines are kept to be small and secure.
You might consider having some terminal emulator running inside a browser. Such things exist, e.g. libonion has oterm as an example application. Then you can do all the things that a command line interface thru a unix shell provides (of course, you won't be able to run GUI applications, e.g. X11 clients such as GTK or Qt applications).
You could also consider some webmin like stuff.
Notice that you don't need to have a desktop environment on a remote Linux machine. Most of them (e.g. internet servers) have only command line interface.
Learn more about X11: you could have an X11 server on your laptop (e.g. under Windows if so needed) and run remotely X11 clients (that is GUI applications) with ssh -X on your remote Linux system.
However, these requires much packages to be installed. I don't want they be installed, because my Linux machines are kept to be small and secure.
I don't understand that requirement. On my VPS, running in some OVH datacenter, I do have X11 client applications (notably emacs). I don't believe that lowers the security of my system, and the disk space consumption for X11 applications and libraries is small enough these days. And of course I use standard commands (like cp(1), mv(1), rm(1), grep(1), find(1), less(1), file(1), sed(1) ....) to manage files. Any graphical file manager is useless (and I never use them, while using Unix since 1986)
You really should learn how to use the command line on Linux. It is incredibly powerful.

Programming for Linux on Windows

I am in a strange scenario, where I must use a work windows 8 desktop how ever I am writing code to be run on a real time Linux machine. I am writing code for a Real-time system, so will need to implement many non-windows headers like RTAI and such. Obviously I can not run these on windows 8, so was curious if anyone had any suggestions for compiling and testing without having to upload code to the RTOS based computer I am programming for each time I want to run a program. I unfortunately can't use a virtual machine and would like to avoid coding in the terminal in order to remotely code on the linux machine.
Any suggestions?
You can code in Windows or whatever OS you like, what system your programming computer uses has absolutely nothing to do with the target system.
The cross compiler you are using must obviously be for the intended target system. Same goes for debugger & program download tools. Your program must either run in a simulator or it must run on the real target. The latter is strongly recommended.
So all you have to do is to acquire the right software tools. Since these are almost always written for Windows, that should be easy.

How to simulate ThreadX application on Windows OS

I have an application using ThreadX 5.1 as the kernel.
The Image is flashed on to a hardware running an ARM 9 processor.
I'm trying to build a Simulator for the application that can be run on Windows (say XP, 32-bit).
Is there any way I can make it run on Windows, without modifying the entire source code to start calling win32 system calls?
You can build a Simulator for the application that can be run on Windows with "ThreadX for Win32".
"ThreadX for Win32"'s specification is hear.
http://rtos.com/products/threadx/Win32
Yes you can if you are willing to put in the work.
First observe that each threadx system call has an equivalent posix call except for events.
So your threadx program can run as a single process using posix threads, mutexes, etc.
Events can be handled by an external library (there are a few out there).
If you find this difficult in windows then the simplest thing to do is set up a linux vm. I use an ubuntu vm running on Virtual Box. It is very easy to set up. All you will need is the cdt version of eclipse.
Next you need to stub out all of your low level system calls.
This is also easier than you might think. For example, if you have a SPI driver to read and write to flash, you can replace your flash with a big array which is quite easy to work with at this level.
Having said all this, you may get more mileage if your threadx application is modular. Then you can test each module on it's own and you don't need to mess with threads, etc.
As a first approximation this may give you what you need without going the distance to port the whole thing to run under posix.
I have done this successfully in the past and developed a full set of unit tests for a module that allowed me to develop and test it (on my mac) before going to the target. Development is much faster and reliable this way.
Another option you may want to consider is to find a qemu project that supports your microprocessor. With some work you can develop a complete simulator for your platform and then run the real firmware under the emulator.
Good luck.

Launch process from CGI in Linux

I need to launch server executables from terminal. They are running through wine (because those executables are for Windows) in the background. If I launch them normally, from Terminal, those work without any problem.
Now I'm trying to make CGI (bash) script and launch servers from website, but script doesn't launch processes. I thought that it has something to do with wine, but no, script doesn't launch any processes at all.
I'm building that system on Ubuntu 12.04.1 LTS, after that scripts would go onto Debian server.
So, the question is: is it even possible to run background process from CGI scripts? If yes, would you please explain how?
A CGI program is like any other program, except that it is supposed to run quickly and to follow the CGI protcol (in particular, regarding to stdout output).
As with any other Linux program, you can (subject to limitations and permissions on your system) run processes, using syscalls like e.g. fork(2), execve(2) and many others.
I suggest to read a good Unix programming book like Advanced Unix Programming abd Advanced Linux Programming. We can't teach all that here in a few minutes.
You could also run processes using the library system(3) and popen(3) functions (of course these functions are implemented using syscalls, inside GNU libc)
Don't forget the stateless property of CGIs; you may consider using FastCGI or SCGI instead.
(A program started thru wine from a CGI might fail, e.g. because it has no X11
server to talk to; For C# programs, consider Mono on Linux).
Another possibility could be to run Windows in some VM, and have your CGI interact with such virtualized Windows programs. Not knowing Windows, I have no idea about the issues of such an approach.

Resources