访问嵌套结构时出现段错误
segfault when accessing nested structure
我在下面使用 kinfo_getproc
:
int main() {
struct kinfo_proc *kp;
kp = kinfo_getproc(getpid());
printf("kp: %p\n", kp);
// kp: 0x801216000
printf("&kp: %p\n", &kp);
// &kp: 0x7fffffffeac0
printf("&thread: %p\n", &kp->ki_tdaddr);
// &thread: 0x801216398
printf("&thread->td_dupfd: %p\n", &kp->ki_tdaddr->td_dupfd);
// &thread->td_dupfd: 0xfffff801564d3650
// segfault accessing contents of td_dupfd
//printf("thread->td_dupfd: %d\n", kp->ki_tdaddr->td_dupfd);
return 1;
}
当我尝试访问 kp
结构中的结构时,程序出现段错误。
我看了其他帖子,问题可能是结构分配不当?
以下内容来自 kinfo_getproc
的手册页:
RETURN VALUES
On success the kinfo_getproc() function returns a pointer to a struct
kinfo_proc structure as defined by <sys/user.h>. The pointer was
obtained by an internal call to malloc(3) and must be freed by the caller
with a call to free(3). On failure the kinfo_getproc() function returns
NULL.
如果 return 值是指向已经 malloc
的 kinfo_struct
的指针,不应该
访问嵌套结构是否有效?我应该如何正确访问 kp
中的嵌套结构?
我知道我的问题是什么了。
kp->ki_tdaddr
的值是驻留在内核space中的一个地址。换句话说,kp->ki_tdaddr
是指向位于内核内存中的结构的指针 - 用户无法直接访问该结构space,因此出现段错误。
要做的是使用 kp->ki_tdaddr
(内核 space 地址)中找到的值和 kvm_read()
将位从内核 space 传输到用户 space.
代码:
int main() {
struct thread thread;
/* a structure with process related info */
struct kinfo_proc *kp = kinfo_getproc(getpid());
/* a descriptor to access kernel virtual memory */
kvm_t *kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
/* transfer bytes from ki_tdaddr to thread */
kvm_read(kd, (unsigned long) kp->ki_tdaddr, &thread, sizeof(thread));
/* can now access thread information */
printf("thread id: %jd\n", (intmax_t) thread.td_tid);
return 1;
}
我在下面使用 kinfo_getproc
:
int main() {
struct kinfo_proc *kp;
kp = kinfo_getproc(getpid());
printf("kp: %p\n", kp);
// kp: 0x801216000
printf("&kp: %p\n", &kp);
// &kp: 0x7fffffffeac0
printf("&thread: %p\n", &kp->ki_tdaddr);
// &thread: 0x801216398
printf("&thread->td_dupfd: %p\n", &kp->ki_tdaddr->td_dupfd);
// &thread->td_dupfd: 0xfffff801564d3650
// segfault accessing contents of td_dupfd
//printf("thread->td_dupfd: %d\n", kp->ki_tdaddr->td_dupfd);
return 1;
}
当我尝试访问 kp
结构中的结构时,程序出现段错误。
我看了其他帖子,问题可能是结构分配不当?
以下内容来自 kinfo_getproc
的手册页:
RETURN VALUES
On success the kinfo_getproc() function returns a pointer to a struct
kinfo_proc structure as defined by <sys/user.h>. The pointer was
obtained by an internal call to malloc(3) and must be freed by the caller
with a call to free(3). On failure the kinfo_getproc() function returns
NULL.
如果 return 值是指向已经 malloc
的 kinfo_struct
的指针,不应该
访问嵌套结构是否有效?我应该如何正确访问 kp
中的嵌套结构?
我知道我的问题是什么了。
kp->ki_tdaddr
的值是驻留在内核space中的一个地址。换句话说,kp->ki_tdaddr
是指向位于内核内存中的结构的指针 - 用户无法直接访问该结构space,因此出现段错误。
要做的是使用 kp->ki_tdaddr
(内核 space 地址)中找到的值和 kvm_read()
将位从内核 space 传输到用户 space.
代码:
int main() {
struct thread thread;
/* a structure with process related info */
struct kinfo_proc *kp = kinfo_getproc(getpid());
/* a descriptor to access kernel virtual memory */
kvm_t *kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
/* transfer bytes from ki_tdaddr to thread */
kvm_read(kd, (unsigned long) kp->ki_tdaddr, &thread, sizeof(thread));
/* can now access thread information */
printf("thread id: %jd\n", (intmax_t) thread.td_tid);
return 1;
}