如何在内存中(在/proc/<;pid>;/mem中)(在Linux下的C#中)找到另一个进程的某个变量值?

如何在内存中(在/proc/<;pid>;/mem中)(在Linux下的C#中)找到另一个进程的某个变量值?,c#,linux,memory,C#,Linux,Memory,程序S应该对程序V的某个变量进行一些日志和统计。 两者都需要用C#编写并在Linux上运行。 变量名和它的父类名以及函数名对于程序V来说都是已知的。但是它不能更改为自己做一些日志记录。 程序S需要从外部访问V的存储区域。 如何在其中找到某个变量 由于回答了类似的问题,我想我得到了一些从外部读取内存的代码: 下面是一些示例代码。对于该测试用例,S和V组合在一个程序中。 这意味着程序应该通过内存访问读取它自己的一个变量的值 using System; using System.Diagnostics

程序S应该对程序V的某个变量进行一些日志和统计。 两者都需要用C#编写并在Linux上运行。 变量名和它的父类名以及函数名对于程序V来说都是已知的。但是它不能更改为自己做一些日志记录。 程序S需要从外部访问V的存储区域。 如何在其中找到某个变量

由于回答了类似的问题,我想我得到了一些从外部读取内存的代码: 下面是一些示例代码。对于该测试用例,S和V组合在一个程序中。 这意味着程序应该通过内存访问读取它自己的一个变量的值

using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;

namespace MemReadTest42
{
    class MainClass
    {
        public class TestClass42
        {
            public int randomVar42 { get; set; }
            public TestClass42()
            {
                //for example set it to a easy to search number
                randomVar42 = 12342000; //<--- Question: How to get this number from outside
            }
            public void incr()
            {
                //example function which  has impact at the variable
                randomVar42 = randomVar42 + 1;
            }
        }

        public static void Main(string[] args)
        {
            // example variable
            Int32 myvari = 8777778; //<--- Question: How to get this number from outside

            TestClass42 exampObj = new TestClass42();

            Process process = Process.GetCurrentProcess(); //(its this program itself)

            System.IO.StreamReader file = new System.IO.StreamReader(@"/proc/" + process.Id + "/maps");
            string line;
            while ((line = file.ReadLine()) != null)
            {             

                string pattern = @"([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-r])";
                Match m = Regex.Match(line, pattern);
                if (m.Success)
                {
                    string[] startend = (m.Value.Split(' '))[0].Split('-');
                    long start = Convert.ToInt64(startend[0], 16);
                    long end = Convert.ToInt64(startend[1], 16);
                    long len = end - start;

                    if (len > 0 && end > 0 && start > 0)
                    {
                        Console.WriteLine(line);
                        using (FileStream fs = new FileStream(@"/proc/"+ process.Id+"/mem", FileMode.Open, FileAccess.Read))
                        {
                            fs.Seek(start, SeekOrigin.Begin);
                            byte[] b = new byte[end - start];
                            try {
                                fs.Read(b, 0, (int)(end - start));
                                string s = System.Text.Encoding.UTF8.GetString(b);
                                exampObj.incr();//some example var update
                                Console.WriteLine(s.Length + " " + s);
                            }
                            catch {
                                Console.WriteLine("FAILED");
                            }
                        }
                    }
                }

            }
            file.Close();   
            //use the example variables at the end
            Console.WriteLine(" end " + myvari + " " + exampObj.randomVar42);
        }
    }
}
使用系统;
使用系统诊断;
使用System.IO;
使用System.Text.RegularExpressions;
名称空间MemReadTest42
{
类主类
{
公共类TestClass42
{
公共int randomVar42{get;set;}
公共测试类42()
{
//例如,将其设置为易于搜索的数字
randomVar42=12342000;//0&&start>0)
{
控制台写入线(行);
使用(FileStream fs=newfilestream(@/proc/“+process.Id+”/mem)、FileMode.Open、FileAccess.Read))
{
Seek(开始,SeekOrigin.Begin);
字节[]b=新字节[结束-开始];
试一试{
读取(b,0,(int)(结束-开始));
字符串s=System.Text.Encoding.UTF8.GetString(b);
exampObj.incr();//一些示例变量更新
Console.WriteLine(s.Length+“”+s);
}
抓住{
控制台写入线(“失败”);
}
}
}
}
}
file.Close();
//最后使用示例变量
Console.WriteLine(“end”+myvari+“”+exampObj.randomVar42);
}
}
}
1.)但我在哪里可以找到这些变量“randomVar42”或“myvari”? 我将整个内存输出存储在一个文件中,但它既不包含变量名,也不包含它们的值

2.)要缩小范围,应将其存储在哪个地图区域?在[heap]、[stack]或具有程序路径的堆中?在我的测试期间无法读取[vvar]

更新:问题2由Patrick Beynio在2007年回答。
但是,在这两种情况下,我都找不到变量名或值。

如果您是目标程序的所有者,请使用进程间通信机制,如套接字或共享内存。如果您不是所有者,我不会说这是不可能的,但您甚至无法访问目标进程级别的反射值,并且您所尝试的操作存在安全隐患。
randomVar42
是int类型的类实例属性,作为值类型,它将驻留在定义它的位置,即实例,类实例在堆上
myvari
是一个作用域的int变量,也是一种值类型,因此它位于定义的位置,变量位于堆栈上,因此它将位于stack@RobertHarvey这是C#的一个特殊性质吗?它似乎在Windos和其他语言中工作。例如,Windows ReadProcessMemory中的C似乎可以工作,或者Linux process_vm_readv(不是我自己测试的,而是用其他主题答案编写的)。或者使用中的Python脚本,我可以找到变量名,但找不到value@RobertHarvey真实的所有者还是运行目标程序的用户?我想你是说先。事实并非如此。我不能添加任何代码或再次编译它。但运行这些程序的用户将是同一个用户(在操作系统级别)。