Understanding proc_rwmem

Fernando Apesteguía fernando.apesteguia at gmail.com
Fri Apr 16 12:37:16 UTC 2010


2010/4/16 John Baldwin <jhb at freebsd.org>:
> On Friday 16 April 2010 8:11:25 am Fernando Apesteguía wrote:
>> 2010/4/14 John Baldwin <jhb at freebsd.org>:
>> > On Wednesday 14 April 2010 4:22:56 pm Fernando Apesteguía wrote:
>> >> Hi all,
>> >>
>> >> I'm trying to read process memory other than the current process in
>> >> kernel. I was told to use the proc_rwmem function, however I can't get
>> >> it working properly. At first, I'm trying to read how many elements
>> >> the environment variables vector has. To do this I tried this from a
>> >> linprocfs filler function:
>> >>
>> >>
>> >>         struct iovec iov;
>> >>       struct uio tmp_uio;
>> >>       struct ps_strings *pss;
>> >>       int ret_code;
>> >>
>> >>       buff = malloc(sizeof(struct ps_strings), M_TEMP, M_WAITOK);
>> >>       memset(buff, 0, sizeof(struct ps_strings));
>> >>
>> >>       PROC_LOCK_ASSERT(td->td_proc, MA_NOTOWNED);
>> >>       iov.iov_base = (caddr_t) buff;
>> >>       iov.iov_len = sizeof(struct ps_strings);
>> >>       tmp_uio.uio_iov = &iov;
>> >>       tmp_uio.uio_iovcnt = 1;
>> >>       tmp_uio.uio_offset = (off_t)(p->p_sysent->sv_psstrings);
>> >>       tmp_uio.uio_resid = sizeof(struct ps_strings);
>> >>       tmp_uio.uio_segflg = UIO_USERSPACE;
>> >>       tmp_uio.uio_rw = UIO_READ;
>> >>       tmp_uio.uio_td = td;
>> >>       ret_code = proc_rwmem(td->td_proc, &tmp_uio);
>> >
>> > I think you want to use 'p' instead of 'td->td_proc' here.  As it is you
> are
>> > reading from the current process instead of the target process I believe.
>>
>> Thank you. You are right.
>>
>> I made the changes suggested by both you and Kostik. I still have
>> random data when reading.
>> I'm trying to to the same thing using kern/sys_generic.c::read and
>> kern/sys_process.c::kern_ptrace
>> as examples, but I'm missing something...
>> After reading with proc_rwmem, is it possible to do something like the
>> following?
>>
>> if (ret_code == 0) {
>>               sbuf_printf(sb, "proc_rwmem successfully executed: %d\n", ret_code);
>> } else {
>>               sbuf_printf(sb, "Error in proc_rwmem: %d\n", ret_code);
>> }
>>
>> pss = (struct ps_strings *)(iov.iov_base);
>> sbuf_printf(sb, "ps_nargvstr = %d\nps_nenvstr = %d\n",
>> pss->ps_nargvstr, pss->ps_nenvstr);
>>
>> Thanks in advance.
>
> No, functions like uiomove() modify the iovec structures.  Just use 'buff'
> instead of iov.iov_base.

Ah! That was it!

Thanks!

>
> --
> John Baldwin
>


More information about the freebsd-hackers mailing list