- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在学习如何使用 ptrace 时遇到了一个奇怪的问题:
我写了一个程序:
#include <cstdio>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>
int main()
{
long x=(long)mmap(0,-235,2,34,-1,0);
printf("Child: x=%ld (",x);
for(int i=31;i>=0;i--) printf((x&(1<<i))?"1":"0");
printf(")\n");
printf("Child errno: %s\n",strerror(errno));
return 0;
}
它只是使用错误的参数进行 mmap 系统调用。然后它打印返回值(也是二进制)和 errno。
这里是执行该程序后的输出:
Child: x=-1 (11111111111111111111111111111111)
Child errno: Cannot allocate memory
我用 strace 运行它:
execve("./nic.e", ["./nic.e"], [/* 35 vars */]) = 0
uname({sys="Linux", node="dom", ...}) = 0
brk(0) = 0x9237000
brk(0x9237cd0) = 0x9237cd0
set_thread_area({entry_number:-1 -> 6, base_addr:0x9237830, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}) = 0
brk(0x9258cd0) = 0x9258cd0
brk(0x9259000) = 0x9259000
mmap2(NULL, 4294967061, PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = -1 ENOMEM (Cannot allocate memory)
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7798000
write(1, "Child: x=-1 (1111111111111111111"..., 47Child: x=-1 (11111111111111111111111111111111)
) = 47
write(1, "Child errno: Cannot allocate mem"..., 36Child errno: Cannot allocate memory
) = 36
exit_group(0) = ?
strace 告诉我们这个错误的 mmap 返回 -1,并出现错误 ENOMEM。
到目前为止一切正常。
这是我的 ptrace 代码(我删除了所有不需要的内容):
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <sys/syscall.h>
#include <sys/reg.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
int main(int argc,char**argv)
{
int pid=fork();
if(!pid)
{
ptrace(PTRACE_TRACEME,0,NULL,NULL);
execve("nic.e",NULL,NULL);
exit(1);
}
while(true)
{
int status;
waitpid(pid,&status,0);
if(WIFEXITED(status)) return 0;
int signal;
if(WIFSTOPPED(status))
{
signal=WSTOPSIG(status);
}
if(WIFSIGNALED(status)) return 0;
if(signal==SIGTRAP)
{
user_regs_struct regs;
ptrace(PTRACE_GETREGS,pid,NULL,®s);
if(regs.orig_eax==__NR_mmap2)
{
static bool mmap_back=false;
if(!mmap_back) mmap_back=true;
else
{
mmap_back=false;
long x=regs.eax;
printf("mmap return: %ld (",x);
for(int j=31;j>=0;j--) printf((x&(1<<j))?"1":"0");
printf(")\n");
}
}
}
ptrace(PTRACE_SYSCALL,pid,NULL,NULL);
}
return 0;
}
它应该打印与子进程打印相同的内容 - mmap2 系统调用的重新运行值。
但是这是输出:
mmap return: -12 (11111111111111111111111111110100)
mmap return: -1216753664 (10110111011110011101000000000000)
Child: x=-1 (11111111111111111111111111111111)
Child errno: Cannot allocate memory
为什么 mmap 返回 -12?我是否错误地捕获了返回值?
最佳答案
在 32 位 x86 linux 上,%eax 包含返回值或出错时的否定错误值。
参见例如4.4或3.3 and 3.4 。
系统调用的返回值 -12 意味着函数失败,并且 errno 应设置为 12,这至少在我的系统上与 ENOMEM
匹配。
看来 strace 正在帮助您翻译此内容。如果您希望您的应用程序表现得像 strace,您应该执行类似于 4.4 中的测试和转换。 :
if ((unsigned long)(x) >= (unsigned long)(-2048)) {
printf("syscall failed. errno = %ld\n", -(res));
}
关于c++ - 使用ptrace时mmap的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14143604/
我想了解 mmap 的工作原理。mmap 的用户级调用如下所示。 void *mmap(void *addr, size_t len, int prot, int flags, int
我正在做一个Bottle驱动程序,我使用 yield 关键字和 mmap.mmap 对象在输出流中发送多个映射文件,如以下代码所示: for mapping in mappings: yield
我来自 C++/RAII 世界。 所以我对何时以及如何调用 mmap.close() 感到困惑[不是 file.close()]。 或者,根本不调用它?会不会漏气? 至于来自 document 的示例
我正在我的大学上操作系统类(class),我们的任务之一是使用 mmap 实现简单的 malloc。现在我开始工作了,我尝试使用 valgrind 来检测遗留的任何错误。不管是否释放内存,valgri
有谁知道 mmap(2) 和 mmap(3) 的区别是什么?手册第 3 节被描述为“本章描述了除第 2 章中描述的实现系统调用的库函数之外的所有库函数。” mmap(3) 不执行系统调用吗? 阅读这两
我不知道我在理解mmap时错过了哪些知识。我就是想不通。但让我这样问我的问题: 我有很多(例如 3 个)文件 block ,其大小分别为 s1、s2、s3。 s1、s2 和 s3 均小于 Mmap (
在 Linux 下: #free -m total used free shared buffers cachedMem:
我正在尝试将 C 库(beaglebone PRU 驱动程序 prussdrv.c)与 Python 连接。我想要访问的特定函数返回一个 mmap 指针,如下所示: int __prussdrv_me
当我调用mmap时: ptr = mmap(NULL, ...); 并要求系统提供一个缓冲区并将文件映射到其中,然后使用 再次调用 mmap ptr2 = mmap(ptr, ...); 尝试
在 Android 上用 Java 内存映射一个大文件效果很好。但是当映射总数超过 ~1.5GB 时,即使有多个映射调用,它也会失败: mmap failed: ENOMEM (Out of memo
我在具有 64G 内存和大量磁盘空间的 debian-64 上运行一个专门的数据库守护进程。它使用磁盘上的哈希表(mmaped)并通过定期 write() 调用将实际数据写入文件。当进行大量更新时,m
C++代码: #include #include #include #include #include using namespace std; #define FILE_MODE (S_I
我想处理一个由 4Kb block 组成的文件。 随着事情的发生,我将编写更多数据并映射新部分,取消映射我不再需要的部分。 当要映射的文件数据总量约为 4Gb 时,仅 4Kb 的 map() 是否太小
大家好,我正在尝试将下面的代码转换为 python(访问树莓派 1Mhz 计时器),我不知道什么时候要映射对象,我们需要 + TIMER_OFFSET (timer = (long long int
我所做的是一个垃圾收集器,使用mmap(2)为用户空间分配空间,这就要求最初分配时可以从任何地方开始,但是后面的分配地址应该是与之前的分配连续,如下所示: page_size = getpagesiz
众所周知,最重要的 mmap() 功能是在许多进程之间共享文件映射。但众所周知,每个进程都有自己的地址空间。 问题是内存映射文件(更具体地说,它的数据)真正保存在哪里,以及进程如何访问这些内存? 我的
什么限制了内存映射文件的大小?我知道它不能大于未分配地址空间的最大连续块,并且应该有足够的可用磁盘空间。但是还有其他限制吗? 最佳答案 您太保守了:内存映射文件可能大于地址空间。 查看 内存映射文件的
如果我使用 mmap 来编写 uint32_t,我会遇到大端/小端约定的问题吗?特别是,如果我在 big-endian 机器上写入一些数据 mmap,当我尝试在 little-endian 机器上读取
所以,对于我最后一年的项目,我使用 Video4Linux2 从相机中提取 YUV420 图像,将它们解析为 x264(本地使用这些图像),然后通过 Live555 将编码流发送到 RTP/RTCP通
是 mmap在它们的效果中调用原子? 也就是说,是否由 mmap 进行了映射更改以原子方式出现在访问受影响区域的其他线程中? 作为试金石,请考虑您执行 mmap 的情况。在一个全为零的文件中(来自线程
我是一名优秀的程序员,十分优秀!