[Tutor] Edit program while executing

Alan Gauld alan.gauld at yahoo.co.uk
Sun Oct 3 08:21:07 EDT 2021


On 02/10/2021 21:22, Julius Hamilton wrote:
> Is there any way to edit a computer program while it is running?

There are a few sophisticated IDE/debugging tools that allow this.
For example Borland C++ used to have a debugger that allowed you
to stop the code, step back a few steps, modify the code and then
rerun those steps again (using JIT compilation). (I say "used to"
because I haven't used Borland software for years. It's probably
still a feature today, but I don't know.)

But it is an inherently difficult feat to make work as it's a bit
like cutting off the branch of a tree while you are sitting on it!
It's OK for minor tweaks like changing a constant value or the loop
variable name but any significant change is likely to send your
code spinning off into a black hole.

Part of the reason for this is that the source code is always one
step away from the machine code that the CPU executes, so changing
the source even slightly can make a huge difference to the machine code.
It's much easier to use an assembly debugger to trace the execution of
assembler code than to trace high level code like C++ or Python.

> For example, with Python - when the script is executed, what happens next?
> Does it get compiled into assembly language and sent to the computer’s
> circuits? 

No. Programs never get sent to the computer's circuits. (Except in
real-time controllers and embedded devices) The Operating system loads
the code into an executable section of memory and monitors it as it
runs. Loading libraries etc as needed. That's the primary job of an
operating system - to load and execute programs while managing the
resources and peripherals of the computer.

In the case of Python the program code never even reaches the operating
system. It is executed by the Python interpreter. The interpreter is
loaded by the OS and the interpreter then loads the script and executes
it. The code that is executed is not the code you type however. That
source code is first compiled into Python "byte code" which is like
assembly code in traditional programming. The interpreter then executes
the byte code line by line. (You can use Python tools to inspect the
Python byte code if that's of interest to you.)

> In that case, if the computer allowed it, if you edited the
> program, and the computer knew where in the program it was, it might in
> theory be possible to pass the new instructions after the point where the
> execution is.

In theory yes. In practice it's extremely difficult both for the
human to edit safely and for the computer to process, for all
the reasons discussed above. You can certainly do it at the executable
machine code level and most of us who have been around for long enough,
will have experience of stopping a running program and going into the
executable image and poking new hex or octal values in before restarting
the program. But it's highly risky and only possible for very trivial
changes.

> On the other hand, if this is impossible with Python, is there any other
> computer language where the computer is reading the program and executing
> it step by step immediately, without a step beforehand where the whole
> program is compiled? 

There are pure interpreted languages. Early BASICs come to mind, where
each instruction had a line number. You could stop the interpreter and
replace a line of code anywhere in the program and resume execution and
the new line would be used the next time it was called (For example if
it was part of a loop)

Also Lisp and similar languages(Scheme, Logo etc) store their code as a
data structure within the program. As such it is accessible and with a
debugger or even from within the code itself, you can change the program
code. But again this is mind meltingly difficult and error prone.

> In that case it would surely be possible to change the
> program in the middle of execution, I believe?

Possible yes, practical no. And of very little real-world use.
It's like reprogramming the gears of your car while driving.
Suddenly, changing to 3rd gear makes the headlights come
on! Clever, but potentially dangerous and very confusing to
the driver.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list