{"id":3403,"date":"2010-11-20T10:04:25","date_gmt":"2010-11-20T02:04:25","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3403"},"modified":"2012-05-15T09:24:32","modified_gmt":"2012-05-15T01:24:32","slug":"%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86%e5%ae%9e%e8%b7%b5%e4%b9%8b%e4%ba%8c-%e6%9f%a5%e6%89%be%e5%86%85%e5%ad%98%e5%8c%ba%e5%9f%9f","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3403","title":{"rendered":"Linux\u5185\u5b58\u7ba1\u7406\u5b9e\u8df5-\u67e5\u627e\u5185\u5b58\u533a\u57df"},"content":{"rendered":"<p><strong>\u67e5\u627e\u5185\u5b58\u533a\u57df<\/strong><\/p>\n<p><strong> <\/strong>\u8be5\u6a21\u5757\u5b9e\u73b0\u7684\u529f\u80fd\u662f\uff1a\u901a\u8fc7\u5411\u5185\u6838\u6a21\u5757\u4e2d\u4f20\u9012\u4e00\u4e2a\u865a\u5b58\u5730\u5740\uff0c\u8fdb\u800c\u67e5\u627e\u8be5\u5730\u5740\u6240\u5728\u7684\u5185\u5b58\u533a\u57df\u3002\u5177\u4f53\u67e5\u627e\u7684\u529f\u80fd\u6211\u4eec\u901a\u8fc7find_vma\u51fd\u6570\u6765\u5b9e\u73b0\uff0c\u8fd9\u4e2a\u51fd\u6570\u7684\u539f\u578b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr);<\/pre>\n<p>\u8be5\u51fd\u6570\u5728\u6307\u5b9a\u7684\u5730\u5740\u7a7a\u95f4\u4e2d\u641c\u7d22\u7b2c\u4e00\u4e2avm_end\u5927\u4e8eaddr\u7684\u5185\u5b58\u533a\u57df\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u627e\u5230\u4e86\u7b2c\u4e00\u4e2a\u5305\u542b\u8be5addr\u6216vm_start\u5927\u4e8eaddr\u7684\u5185\u5b58\u533a\u57df\u65f6\uff0c\u8be5\u51fd\u6570\u5c31\u8fd4\u56de\u76f8\u5e94\u533a\u57df\u7684vm_area_struct\u7ed3\u6784\u4f53\u6307\u9488\uff0c\u5426\u5219\u8fd4\u56deNULL\u3002\u90a3\u4e48\uff0cfind_vma\u8fd9\u4e2a\u51fd\u6570\u6709\u53ef\u80fd\u627e\u5230\u7684vma\u5e76\u4e0d\u662f\u5305\u542baddr\u7684\u5185\u5b58\u533a\u57df\u3002\u8fd9\u662f\u600e\u4e48\u4e00\u56de\u4e8b\uff1f\u8fd8\u662f\u5148\u770b\u56fe\u5427\u3002<\/p>\n<p><a href=\"http:\/\/edsionte.com\/techblog\/archives\/1953\/screenshot-2-2\" rel=\"attachment wp-att-1963\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-1963\" title=\"Screenshot-2\" src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/11\/Screenshot-2-300x218.png\" alt=\"\" width=\"300\" height=\"218\" srcset=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/11\/Screenshot-2-300x218.png 300w, http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/11\/Screenshot-2.png 511w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a>\u5728\u56fe(b)\u4e2d\uff0c\u6240\u627e\u5230\u7684VMA\u7684vm_end\u5927\u4e8eaddr\uff0c\u4f46\u662faddr\u5374\u521a\u597d\u662f\u5904\u4e8e\u4e24\u4e2aVMA\u4e4b\u95f4\u3002\u56e0\u6b64\uff0cfind_vma\u51fd\u6570\u6240\u627e\u5230\u7684VMA\u5e76\u4e0d\u5305\u542baddr\u3002\u6b63\u56e0\u4e3a\u5982\u6b64\uff0c\u5728find_myvma\u51fd\u6570\u4e2d\u5728\u4f7f\u7528\u5b8cfind_vma\u51fd\u6570\u540e\uff0c\u8fd8\u8981\u8fdb\u6d4baddr\u662f\u5426\u771f\u6b63\u5305\u542b\u5728vma\u4e2d\u3002\u5177\u4f53\u51fd\u6570\u5b9e\u73b0\u53ef\u53c2\u8003\u4e0b\u8ff0\u4ee3\u7801\u3002\u672c\u6a21\u5757\u5b8c\u6574\u4ee3\u7801\u5728<a href=\"http:\/\/download.csdn.net\/source\/2829686\">\u8fd9\u91cc<\/a>\u3002<\/p>\n<pre class=\"brush:c\">static ulong address = 0;\r\nstatic void find_myvma(void)\r\n{\r\n\tstruct mm_struct *mm = current-&gt;mm;\r\n\tstruct vm_area_struct *vma;\r\n\r\n\tprintk(\"find the vma..\\n\");\r\n\r\n\tdown_read(&amp;mm-&gt;mmap_sem);\r\n\tvma=find_vma(mm, address);\r\n\t\/* when the vma_start &gt; address, it means that the address doesn't belong to the vma *\/\r\n\tif(vma &amp;&amp; address &lt;= vma-&gt;vm_start)\r\n\t{\r\n\t\tprintk(\"address 0x%lx found in the VMA:0x%lx-0x%lx \",address,vma-&gt;vm_start,vma-&gt;vm_end);\r\n\t\tif(vma-&gt;vm_flags &amp; VM_READ)\r\n\t\t\tprintk(\"r\");\r\n\t\telse\r\n\t\t\tprintk(\"-\");\r\n\r\n\t\tif(vma-&gt;vm_flags &amp; VM_WRITE)\r\n\t\t\tprintk(\"w\");\r\n\t\telse\r\n\t\t\tprintk(\"-\");\r\n\r\n\t\tif(vma-&gt;vm_flags &amp; VM_EXEC)\r\n\t\t\tprintk(\"x\");\r\n\t\telse\r\n\t\t\tprintk(\"-\");\r\n\r\n\t\tif(vma-&gt;vm_flags &amp; VM_SHARED)\r\n\t\t\tprintk(\"s\");\r\n\t\telse\r\n\t\t\tprintk(\"p\");\r\n\t\tprintk(\"\\n\");\r\n\t}\r\n\telse\r\n\t{\r\n\t\tprintk(\"address 0x%lx didn't find in the VMA which you just now found..\\n\",address);\r\n\t}\r\n\tup_read(&amp;mm-&gt;mmap_sem);\r\n}\r\n\r\nmodule_param(address, ulong, S_IRUGO);<\/pre>\n<p>\u8fd9\u4e2a\u5185\u6838\u6a21\u5757\u6d89\u53ca\u5230\u4e86\u5185\u6838\u6a21\u5757\u4f20\u9012\u53c2\u6570\u7684\u95ee\u9898\u3002\u65e2\u7136\u6211\u4eec\u9700\u8981\u67e5\u627e\u67d0\u4e2a\u7ebf\u6027\u5730\u5740addr\u6240\u5728\u7684vma\uff0c\u56e0\u6b64\u5c31\u9700\u8981\u5728\u6bcf\u6b21\u52a0\u8f7d\u5185\u6838\u6a21\u5757\u65f6\u4f20\u5165addr\u8fd9\u4e2a\u53c2\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u67e5\u627e\u5185\u5b58\u533a\u57df \u8be5\u6a21\u5757\u5b9e\u73b0\u7684\u529f\u80fd\u662f\uff1a\u901a\u8fc7\u5411\u5185\u6838\u6a21\u5757\u4e2d\u4f20\u9012\u4e00\u4e2a\u865a\u5b58\u5730\u5740\uff0c\u8fdb\u800c\u67e5\u627e\u8be5\u5730\u5740\u6240\u5728\u7684\u5185\u5b58\u533a\u57df\u3002\u5177\u4f53\u67e5\u627e\u7684\u529f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[266],"tags":[],"class_list":["post-3403","post","type-post","status-publish","format-standard","hentry","category-266"],"views":10211,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=3403"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3403\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3403"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}