pmap: fixing broken indentation in the -X/-XX modes
This commit changes the processing principle of the -X/-XX modes from 1-pass to 2-pass. A separate width measurement stage has been added, so that the real maximum widths can be measured and used for correct indentation. The firstmapping variable now has a new value (2) used for the width measurement stage (1st pass). The printing is disabled in this stage. The file position is reset to the beginning of the file once the end of file is reached and the printing stage (2nd pass) begins. It's questionable if this approach is sensitive to Read-after-Write race conditions. Anyway, this feature is a good candidate for a complete redesign in the future. Additionally this commit introduces a final cleaning of the list used for the evaluation of totals in the -X/-XX modes.
This commit is contained in:
parent
ca76af22ca
commit
20ce346ebd
28
pmap.c
28
pmap.c
@ -184,7 +184,7 @@ struct listnode {
|
|||||||
struct listnode *next;
|
struct listnode *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct listnode *listhead=NULL, *listtail=NULL;
|
static struct listnode *listhead=NULL, *listtail=NULL, *listnode;
|
||||||
|
|
||||||
|
|
||||||
static int is_unimportant (char *s)
|
static int is_unimportant (char *s)
|
||||||
@ -201,7 +201,6 @@ static int is_unimportant (char *s)
|
|||||||
|
|
||||||
static void print_extended_maps (FILE *f)
|
static void print_extended_maps (FILE *f)
|
||||||
{
|
{
|
||||||
struct listnode *listnode;
|
|
||||||
char flags[DETAIL_LENGTH], map_desc[128],
|
char flags[DETAIL_LENGTH], map_desc[128],
|
||||||
detail_desc[DETAIL_LENGTH], value_str[NUM_LENGTH],
|
detail_desc[DETAIL_LENGTH], value_str[NUM_LENGTH],
|
||||||
start[NUM_LENGTH], end[NUM_LENGTH],
|
start[NUM_LENGTH], end[NUM_LENGTH],
|
||||||
@ -216,7 +215,7 @@ static void print_extended_maps (FILE *f)
|
|||||||
char has_vmflags = 0;
|
char has_vmflags = 0;
|
||||||
|
|
||||||
ret = fgets(mapbuf, sizeof mapbuf, f);
|
ret = fgets(mapbuf, sizeof mapbuf, f);
|
||||||
firstmapping = 1;
|
firstmapping = 2;
|
||||||
while (ret != NULL) {
|
while (ret != NULL) {
|
||||||
/* === READ MAPPING === */
|
/* === READ MAPPING === */
|
||||||
map_desc[0] = '\0';
|
map_desc[0] = '\0';
|
||||||
@ -254,7 +253,7 @@ static void print_extended_maps (FILE *f)
|
|||||||
goto loop_end;
|
goto loop_end;
|
||||||
/* === CREATE LIST AND FILL description FIELD === */
|
/* === CREATE LIST AND FILL description FIELD === */
|
||||||
if (listnode == NULL) {
|
if (listnode == NULL) {
|
||||||
assert(firstmapping == 1);
|
assert(firstmapping == 2);
|
||||||
listnode = calloc(1, sizeof *listnode);
|
listnode = calloc(1, sizeof *listnode);
|
||||||
if (listhead == NULL) {
|
if (listhead == NULL) {
|
||||||
assert(listtail == NULL);
|
assert(listtail == NULL);
|
||||||
@ -271,15 +270,14 @@ static void print_extended_maps (FILE *f)
|
|||||||
listnode->max_width = 7;
|
listnode->max_width = 7;
|
||||||
} else {
|
} else {
|
||||||
/* === LIST EXISTS === */
|
/* === LIST EXISTS === */
|
||||||
if ((listnode == NULL) ||
|
if (strcmp(listnode->description, detail_desc) != 0)
|
||||||
(strcmp(listnode->description, detail_desc) != 0))
|
|
||||||
xerrx(EXIT_FAILURE, "ERROR: %s %s",
|
xerrx(EXIT_FAILURE, "ERROR: %s %s",
|
||||||
_("inconsistent detail field in smaps file, line:\n"),
|
_("inconsistent detail field in smaps file, line:\n"),
|
||||||
mapbuf);
|
mapbuf);
|
||||||
}
|
}
|
||||||
strcpy(listnode->value_str, value_str);
|
strcpy(listnode->value_str, value_str);
|
||||||
sscanf(value_str, "%"KLF"u", &listnode->value);
|
sscanf(value_str, "%"KLF"u", &listnode->value);
|
||||||
listnode->total += listnode->value;
|
if (firstmapping == 2) listnode->total += listnode->value;
|
||||||
if (strlen(value_str) > listnode->max_width)
|
if (strlen(value_str) > listnode->max_width)
|
||||||
listnode->max_width = strlen(value_str);
|
listnode->max_width = strlen(value_str);
|
||||||
listnode = listnode->next;
|
listnode = listnode->next;
|
||||||
@ -297,6 +295,13 @@ loop_end:
|
|||||||
if (strlen(vmflags) > maxwv) maxwv = strlen(vmflags);
|
if (strlen(vmflags) > maxwv) maxwv = strlen(vmflags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (firstmapping == 2) { /* width measurement stage, do not print anything yet */
|
||||||
|
if (ret == NULL) { /* once the end of file is reached ...*/
|
||||||
|
firstmapping = 1; /* ... we reset the file position to the beginning of the file */
|
||||||
|
fseek(f, 0, SEEK_SET); /* ... and repeat the process with printing enabled */
|
||||||
|
ret = fgets(mapbuf, sizeof mapbuf, f); /* this is not ideal and needs to be redesigned one day */
|
||||||
|
}
|
||||||
|
} else { /* the maximum widths have been measured, we've already reached the printing stage */
|
||||||
/* === PRINT THIS MAPPING === */
|
/* === PRINT THIS MAPPING === */
|
||||||
/* Print header */
|
/* Print header */
|
||||||
if (firstmapping && !q_option) {
|
if (firstmapping && !q_option) {
|
||||||
@ -342,6 +347,8 @@ loop_end:
|
|||||||
printf(" %s\n", map_desc);
|
printf(" %s\n", map_desc);
|
||||||
|
|
||||||
firstmapping = 0;
|
firstmapping = 0;
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/* === PRINT TOTALS === */
|
/* === PRINT TOTALS === */
|
||||||
if (!q_option && listhead!=NULL) {
|
if (!q_option && listhead!=NULL) {
|
||||||
@ -722,6 +729,13 @@ int main(int argc, char **argv)
|
|||||||
closeproc(PT);
|
closeproc(PT);
|
||||||
free(pidlist);
|
free(pidlist);
|
||||||
|
|
||||||
|
/* cleaning the list used for the -X/-XX modes */
|
||||||
|
for (listnode = listhead; listnode != NULL ; ) {
|
||||||
|
listnode = listnode -> next;
|
||||||
|
free(listhead);
|
||||||
|
listhead = listnode;
|
||||||
|
}
|
||||||
|
|
||||||
if (count)
|
if (count)
|
||||||
/* didn't find all processes asked for */
|
/* didn't find all processes asked for */
|
||||||
ret |= 42;
|
ret |= 42;
|
||||||
|
Loading…
Reference in New Issue
Block a user