pmap: fix printing bug associated with the '-x' option

Ever since its introduction, the 'x' (extended format)
option has employed strncmp to parse those smaps keys.

Such an approach worked well as long as those prefixes
were guaranteed to be unique. But, with the 4.3 kernel
a new 'SwapPss' field was added to those within smaps.

That triggered a 2nd match for the 'Swap' logic which,
in turn, resulted in a duplicate output line of zeros.

So this patch just trades strncmp for strcmp, avoiding
potential future problems when /proc/$$/smaps evolves.

Reference(s):
. recent bug report
https://bugzilla.redhat.com/show_bug.cgi?id=1374061
. linux 4.3 kernel introduces SwapPss
commit 8334b96221ff0dcbde4873d31eb4d84774ed8ed4
. original pmap -x option introduction
commit 380cc1e908

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2016-09-09 05:55:55 -05:00 committed by Craig Small
parent 834ed434c9
commit e02d9f554d

12
pmap.c
View File

@ -599,25 +599,23 @@ static int one_proc (struct pids_stack *p)
/* hex values are lower case or numeric, keys are upper */ /* hex values are lower case or numeric, keys are upper */
if (mapbuf[0] >= 'A' && mapbuf[0] <= 'Z') { if (mapbuf[0] >= 'A' && mapbuf[0] <= 'Z') {
/* Its a key */ /* Its a key */
if (sscanf if (sscanf(mapbuf, "%20[^:]: %llu", smap_key, &smap_value) == 2) {
(mapbuf, "%20[^:]: %llu", smap_key, if (strcmp("Rss", smap_key) == 0) {
&smap_value) == 2) {
if (strncmp("Rss", smap_key, 3) == 0) {
rss = smap_value; rss = smap_value;
total_rss += smap_value; total_rss += smap_value;
continue; continue;
} }
if (strncmp("Shared_Dirty", smap_key, 12) == 0) { if (strcmp("Shared_Dirty", smap_key) == 0) {
shared_dirty = smap_value; shared_dirty = smap_value;
total_shared_dirty += smap_value; total_shared_dirty += smap_value;
continue; continue;
} }
if (strncmp("Private_Dirty", smap_key, 13) == 0) { if (strcmp("Private_Dirty", smap_key) == 0) {
private_dirty = smap_value; private_dirty = smap_value;
total_private_dirty += smap_value; total_private_dirty += smap_value;
continue; continue;
} }
if (strncmp("Swap", smap_key, 4) == 0) { if (strcmp("Swap", smap_key) == 0) {
/*doesn't matter as long as last */ /*doesn't matter as long as last */
printf("%0*lx %*lu %*llu %*llu %*s %s\n", printf("%0*lx %*lu %*llu %*llu %*s %s\n",
maxw1, start_To_Avoid_Warning, maxw1, start_To_Avoid_Warning,