I wrote a shared library in C++ on Linux, which contains a function f()
. This library is used from multiple programs, calling the function.
Now I would like to conduct some debugging with that function, while calling it from program A
. When calling it from any other program, it should fail, as long as I debug the function. After I do not have complete control over when the other programs are run, I would like to add an exception, which stops every program except program A
when calling function f
during Debug sessions.
How could I solve that?
Asked
Active
Viewed 225 times
0

arc_lupus
- 3,942
- 5
- 45
- 81
-
Is `f()` allowed to receive a parameter `foo`, say, that can act as a licence object? – Bathsheba Mar 12 '18 at 08:18
-
What's `A`? How can a function know who the caller is? – Ron Mar 12 '18 at 08:18
-
3The simplest would be to not distribute the code as a shared library - statically link it into the distributed program that you wish to be capable of running it. – Peter Mar 12 '18 at 08:18
-
@Bathsheba: I would prefer not, but if there is no other solution, that could be a possibility – arc_lupus Mar 12 '18 at 08:19
-
@Ron: That is my question if that is possible, or not – arc_lupus Mar 12 '18 at 08:20
-
3This actually sounds like an XY problem. If you call it from program `A` then function is executed inside of program `A` process and calling it from other programs should not alter program `A` state anyhow. Or there some sort of IPC involved? Even if there is some it should not be a problem either. – user7860670 Mar 12 '18 at 08:22
-
@arc_lupus: I can't think of a better solution unless you're willing to give up portability. – Bathsheba Mar 12 '18 at 08:24
-
What I don't understand: Code which is debugged should be compiled in debug mode. Code which is deployed should be compiled in release mode. Hence, there are two versions of the library which can be used concurrently. (Or do I miss something essential? Am I too long on Windows?) ;-) – Scheff's Cat Mar 12 '18 at 08:24
-
@Bathsheba: Portability in which relationship? Linux-based is fine, everything else has to be defined – arc_lupus Mar 12 '18 at 08:25
-
I presume that what's going on here as the `f` does super-secret stuff that you don't want anybody to see. But if you link it to a very specific executable, you will then be able to debug it. – Omnifarious Mar 12 '18 at 08:27
-
@Scheff: The library is always compiled in release mode. What I would like to test is the influence of different parameters in a calculation, which are removed/altered during testing, thus no "debugging" in the sense of "check if code is working correctly" – arc_lupus Mar 12 '18 at 08:27
-
Simply change the definition of f() and add a default argument pass default param value and on the basis of it raise exception. Why this won't work ? – moghya Mar 12 '18 at 08:31
-
1In addition to @VTT Shared library linked to multiple programs may mean multiple processes may share code. IMHO, it does not mean multiple processes share data which belongs to shared library. If there is interaction it is via OS global states e.g. files, global locks, shared memory, etc. On the other hand, such global lock (e.g. acquired for debugging) might be used to achieve the intended behavior. – Scheff's Cat Mar 12 '18 at 08:33
-
1Concerning global locks: I mean something like described in answer to [SO: Using mutexes/semaphores with processes](https://stackoverflow.com/a/21566392/7478597). – Scheff's Cat Mar 12 '18 at 08:36
2 Answers
2
The only way I can think of is by checking the information of the currently running process. You can get the pid
by calling the getpid()
function. All the information on all processes on a linux system can be found in the /proc/<pid>
directory. When calling function f
, you can check this information and decide whether to throw an exception or not.

doron
- 27,972
- 12
- 65
- 103
1
There isn't a way that can't be gotten around. If another process has PTRACE on yours, it can make anything at all happen in your process, so any check you perform to try to make the function not work can be disabled.

Omnifarious
- 54,333
- 19
- 131
- 194