通过命令行打印正在运行的 linux C 进程的数据

发布于 2024-12-19 19:40:16 字数 412 浏览 2 评论 0原文

场景如下(尚无可用代码):Linux 进程在 while(1) 循环中永远运行。该过程是用 C/C++ (POSIX) 实现的。该过程将一些数据保存到 std::vector 中。

myvector.push_back("test1");
myvector.push_back("test1");
myvector.pop_back();
myvector.push_back("test1");

该进程运行并修改向量。我正在寻找一种机制,可以在使用特定命令行参数执行二进制文件时打印向量值。

假设名为“myprocess”的进程正在运行。我想从新的 shell(如 ./myprocess -debug)执行相同的二进制文件并打印 STL 容器的值。

有什么想法吗?最好的机制是什么(即打印另一个进程的内存)

The scenario is the following (no code available yet): A linux process runs forever in while(1) loop. The process is implemented in C/C++ (POSIX). The process keeps some data to a std::vector.

myvector.push_back("test1");
myvector.push_back("test1");
myvector.pop_back();
myvector.push_back("test1");

The process runs and modifies the vector. I am looking for a mechanism that will print the vector values when executing the binary with a specific command line argument.

Let's say that the process called "myprocess" and is running. I want to execute the same binary from a new shell like ./myprocess -debug and print the values of the STL container.

Any idea? What is the best mechanism to do it (ie print the memory of another process)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨轻弹 2024-12-26 19:40:16

通常,您不会“打印另一个进程的内存”。您要做的就是向另一个进程发送一个信号,要求它打印出向量的内容。

为此,您可以使用 POSIX 信号(例如 SIGUSR1)。

更灵活的方法是让进程侦听命名管道(或 TCP 端口)并通过其接受命令。其中一个命令可能是打印出向量的内容。

Typically, you don't "print the memory of another process". What you do is send the other process a signal asking it to print out the contents of the vector.

For this, you could use a POSIX signal (e.g. SIGUSR1).

A more flexible approach would be for the process to listen on a named pipe (or a TCP port) and accept commands over it. One such command could be to print out the contents of the vector.

慢慢从新开始 2024-12-26 19:40:16

(为了完成@aix的最后一个建议)

假设您有myprocess的源代码并且能够增强它,您可以在其中添加一些服务器功能。

例如,您可以将其设为 HTTP 服务器(例如,使用 Onion HTTP 服务器库)了解请求。这些请求甚至可以包含一些脚本语言(例如 lua),然后您将嵌入一个解释器来处理它们(在例如,一个单独的线程)。在这种情况下,请注意同步问题(例如,使用互斥体锁定或读写锁定所访问的数据)。

使用 HTTP 协议的优点是您可以使用浏览器探测您的应用程序。 (但是您可能需要处理会话、身份验证等......)。

您还可以使用自己的协议,或 RPC-XDR、Corba 等。

您还可以通过 telnet 在每行上接受单个请求(例如在 Lua 中)。

将数据放在共享内存段中外部可见

并且您可以使用 posix shm 一般这样的规范改变需要一些软件架构的改变

细节可能并不简单......

(To complete the last suggestion of @aix)

Assuming you have the source of myprocess and are able to enhance it, you could add some server abilities inside it.

For instance, you could make it an HTTP server (e.g. with the Onion HTTP server library) able to understand requests. These requests could even contain some scripting language (like lua) then you'll embed an interpreter to handle them (in a separate thread, for instance). In that case, take care of synchronization issues (by e.g. locking with a mutex or a read-write lock the accessed data).

The advantage of using an HTTP protocol is that you can probe your application using a browser. (But you might need to handle sessions, authentications, etc..).

You could also use your own protocol, or RPC-XDR, Corba, etc etc.

You could also accept single requests (e.g. in Lua), on per line, thru telnet.

And you might put the data to be visible outside in a shared memory segment using posix shm

In general such a specification change requires some software architectural changes

The details might not be simple...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文