Help required to track program counter continuously and manipulate

Hello !
I have been looking for something like this : (I'm not sure if this can be designed, but any help/advise would be very much appreciated.) Ill be implementing on Mega 2560 board.

Suppose my code is like this :

void setup()
{
..
..
Line x
..
..
}

void loop()
{
//empty
}

void signchange()
{
//The code will keep track of the program counter continously as setup() gets executed, whenever program counter reaches "Line x" of the setup() code, it will deliberately set the negative flag of the status register bit to 1.
}

Is there a way to do this? In case I couldn't explain my problem properly, let me know.

So basically I would like to know how to monitor/ manipulate the program counter . I saw a post on this Program Counter (PC) - General Discussion - Arduino Forum
but somehow couldn't understand very well.
Thank you so much for your patience!

Your code is static... so if you want to set a flag when you reach line x, why don’t you do that at line x-1 or line x+1?

Explain what you are trying to achieve, not how you think it should work.

For example if this is to debug from outside there are arduinos supporting external monitoring of execution

If you want to be monitoring the program counter, can I suggest to you that C is not the best environment, and that you should only be thinking about using assembler?

I get the impression that the OP has used the words "program counter" when s/he should have used "line number"

@tbanerje a C++ program executes one line after the other so there is no need to identify the line number (and it is not possible, because line numbers only exist in the editor). Just put the code you want at the place in the program where you want it to happen.

...R

Yes , J-M-L you are right. So thats what I exactly want. I want to run a C code or assembly code say 'X' on by 8 bit microcontroller (Atmega 2560) . And then externally I want to control this code by changing the negative flag at a certain point of the code 'X'. By externally I mean from another code preferably, but it seems that we cannot simultaneously run two different sketches on arduino isnt it?
Or we can?
Thats why I thought may be I'll do it from another subroutine.
One more thing is that I want this complete thing to happen automatically, so as you said I cannot go to debugging mode and do it manually on my own. I want the external code to find that point and change the negative flag for me automatically. So is this do-able? Please let me know. Thank you so much for all your replies. Looking forward to your suggestions!

You can't run multiple sketches simultaneously.

This sounds like an XY problem though. What are you actually trying to do?

Yes so since multiple sketches are not possible I'm can run my 'X' code may be in loop, and a different subroutine will keep track of my X code by monitoring the PC(program counter) and when the PC reaches the desired point I want to set the negative flag. That's what I'm trying to do.

Why? You're describing how you want to do something, I'm not at all clear what that something is.

What I don’t understand is how you will set X
If it is fully self sufficient, line X is part of the compilation and you just need to call a subroutine after or before reaching that line...

Or are you trying to hack an existing program and branch out when some sort of verification code is reached so that you can execute something else?

Well you are right, X is a self sufficient code.
But what I'm trying to see is if I can develop some attack/hack (signchange subroutine) so that I can change the sign flag at some particular line of X.
So this means X's code should not be invoking my subroutine. As X won't spontaneously want to get attacked.
I will try from this signchange() subroutine to just externally change the sign bit when code X reaches a particular point.
Does this make sense? And is it do-able? What do you think @J-M-L? Thanks everyone for your patience!

Sounds like a simulator/debugger program being described.

Paul

Well - tracking what microprocessor A is doing can be done but in the arduino world, for a general program X that is already loaded and running on A, you can achieve this only by a second processor B and if A allows instrumentation => read for example this tutorial and read about EDBG to get some ideas

tbanerje:
But what I'm trying to see is if I can develop some attack/hack (signchange subroutine) so that I can change the sign flag at some particular line of X.

[...]

Does this make sense? And is it do-able? What do you think @J-M-L? Thanks everyone for your patience!

I suppose you know what you are trying to achieve but I haven't the faintest idea.

How about a proper description of your project?

...R

Well thanks guys, I think I have found a way in a different processor.