This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [patchv2] Fix crash on process name "(sd-pam)" (PR 16594)


On 02/17/2014 10:04 PM, Jan Kratochvil wrote:
> Hi,
> 
> I have added two other cases not handling it well.
> 
> It can IMO never work perfectly, the file format is ambiguous.
> Or maybe one could parse it in opposite direction, from the end.

Another idea that crossed my mind would be to extract the exec
name from /proc//status or /proc//comm first, and then skip
that string when parsing /proc//stat.  E.g.:

$ cp /bin/cat 'cat 2 ) '
$ ./'cat 2 ) ' /proc/self/status
Name:   cat 2 )
...

$ ./'cat 2 ) ' /proc/self/stat
22556 (cat 2 ) ) R 1525 22556...

But of course that be racy (not that the current code isn't, btw...)

But, thinking again about the "from the end" idea.
I'm under the impression that new fields have been
appended to stat over the years.  But that doesn't seem
to matter, I think there's an unambiguous way to parse this.
We don't actually need to start at the very end of the whole
stat line.  This might be simpler than first looks actually.
See:

 $ cp /bin/cat 1234567890abcdef
 $ ./1234567890abcdef /proc/self/stat
 22804 (1234567890abcde) R 1525 22804 1525 34819 22804 4218880 197 0 0 0 0 0 0 0 20 0 1 0 26563756 109436928 126 18446744073709551615 4194304 4238049 140734145129136 140734145129136 215776905456 0 0 0 0 0 0 0 17 0 0 0 0 0 0 6335480 6337168 31199232 140734145134299 140734145134334 140734145134334 140734145138661 0

Notice that comm/name field is trimmed to 15 chars.  I'd guess 15 to
be related to TASK_COMM_LEN, maybe that minus 2 for the parens.
Haven't looked at the sources.

Given that even if the task name is empty (if even possible),
it's guaranteed that in 17 chars after PID we won't see a
')' that is _not_ part of the name field, we just need to
read 17 chars, and search from the end of that for the first ')'.

-- 
Pedro Alves


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]