1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 2015, Joyent, Inc. All rights reserved.
*/
/*
* pmadvise
*
* ptool wrapper for madvise(3C) to apply memory advice to running processes
*
* usage: pmadvise -o option[,option] [-v] [-F] pid ...
* (Give "advice" about a process's memory)
* -o option[,option]: options are
* private=<advice>
* shared=<advice>
* heap=<advice>
* stack=<advice>
* <segaddr>[:<length>]=<advice>
* valid <advice> is one of:
* normal, random, sequential, willneed, dontneed,
* free, access_lwp, access_many, access_default
* -v: verbose output
* -F: force grabbing of the target process(es)
* -l: show unresolved dynamic linker map names
* pid: process id list
*
*
* Advice passed to this tool are organized into various lists described here:
* rawadv_list: includes all specific advice from command line (specific
* advice being those given to a particular address range rather
* than a type like "heap" or "stack". In contrast, these
* types are referred to as generic advice). Duplicates allowed.
* List ordered by addr, then by size (largest size first).
* Created once per run.
* merged_list: includes all specific advice from the rawadv_list as well as
* all generic advice. This must be recreated for each process
* as the generic advice will apply to different regions for
* different processes. Duplicates allowed. List ordered by addr,
* then by size (largest size first). Created once per pid.
* chopped_list: used for verbose output only. This list parses the merged
* list such that it eliminates any overlap and combines the
* advice. Easiest to think of this visually: if you take all
* the advice in the merged list and lay them down on a memory
* range of the entire process (laying on top of each other when
* necessary), then flatten them into one layer, combining advice
* in the case of overlap, you get the chopped_list of advice.
* Duplicate entries not allowed (since there is no overlap by
* definition in this list). List ordered by addr. Created once
* per pid.
*
* Example:
* merged_list: |-----adv1----|---------adv3---------|
* |--adv2--|--adv4--|-----adv5----|
* ||
* \/
* chopped_list: |adv1|-adv1,2-|-adv3,4-|----adv3,5---|
*
* maplist: list of memory mappings for a particular process. Used to create
* generic advice entries for merged_list and for pmap like verbose
* output. Created once per pid.
*
* Multiple lists are necessary because the actual advice applied given a set
* of generic and specific advice changes from process to process, so for each
* pid pmadvise is passed, it must create a new merged_list from which to apply
* advice (and a new chopped_list if verbose output is requested).
*
* Pseudo-code:
* I. Input advice from command line
* II. Create [raw advice list] of specific advice
* III. Iterate through PIDs:
* A. Create [map list]
* B. Merge generic advice and [raw advice list] into [merged list]
* C. Apply advice from [merged list]; upon error:
* i. output madvise error message
* ii. remove element from [merged list]
* D. If verbose output:
* i. Create [chopped list] from [merged list]
* ii. Iterate through [map list]:
* a. output advice as given by [merged list]
* iii. Delete [chopped list]
* E. Delete [merged list]
* F. Delete [map list]
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <string.h>
#include <dirent.h>
#include <limits.h>
#include <link.h>
#include <libelf.h>
#include <locale.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/mkdev.h>
#include <assert.h>
#include <libproc.h>
#include <libgen.h>
#include <signal.h>
#include "pmap_common.h"
#ifndef TEXT_DOMAIN /* should be defined by cc -D */
#define TEXT_DOMAIN "SYS_TEST" /* use this only if it wasn't */
#endif
#define KILOBYTE 1024
/*
* Round up the value to the nearest kilobyte
*/
#define ROUNDUP_KB(x) (((x) + (KILOBYTE - 1)) / KILOBYTE)
#define NO_ADVICE 0
/*
* The following definitions are used as the third argument in insert_addr()
* NODUPS = no duplicates are not allowed, thus if the addr being inserted
* already exists in the list, return without inserting again.
*
* YESDUPS = yes duplicates are allowed, thus always insert the addr
* regardless of whether it already exists in the list or not.
*/
#define NODUPS 1
#define YESDUPS 0
/*
* Advice that can be passed to madvise fit into three groups that each
* contain 3 mutually exclusive options. These groups are defined below:
* Group 1: normal, random, sequential
* Group 2: willneed, dontneed, free, purge
* Group 3: default, accesslwp, accessmany
* Thus, advice that includes (at most) one from each group is valid.
*
* The following #define's are used as masks to determine which group(s) a
* particular advice fall under.
*/
#define GRP1_ADV (1 << MADV_NORMAL | 1 << MADV_RANDOM | \
1 << MADV_SEQUENTIAL)
#define GRP2_ADV (1 << MADV_WILLNEED | 1 << MADV_DONTNEED | \
1 << MADV_FREE | 1 << MADV_PURGE)
#define GRP3_ADV (1 << MADV_ACCESS_DEFAULT | 1 << MADV_ACCESS_LWP | \
1 << MADV_ACCESS_MANY)
static int create_maplist(void *, const prmap_t *, const char *);
static int pr_madvise(struct ps_prochandle *, caddr_t, size_t, int);
static char *mflags(uint_t);
static char *advtostr(int);
static int lflag = 0;
static int addr_width, size_width;
static char *progname;
static struct ps_prochandle *Pr;
static lwpstack_t *stacks;
static uint_t nstacks;
static char *suboptstr[] = {
"private",
"shared",
"heap",
"stack",
NULL
};
int generic_adv[] = {NO_ADVICE, NO_ADVICE, NO_ADVICE, NO_ADVICE};
int at_map = 0;
typedef struct saddr_struct {
uintptr_t addr;
size_t length;
int adv;
struct saddr_struct *next;
} saddr_t;
static int apply_advice(saddr_t **);
static void set_advice(int *, int);
static void create_choplist(saddr_t **, saddr_t *);
/*
* The segment address advice from the command line
*/
saddr_t *rawadv_list = NULL;
/*
* The rawadv_list + list entries for the generic advice (if any).
* This must be recreated for each PID as the memory maps might be different.
*/
saddr_t *merged_list = NULL;
/*
* The merged_list cut up so as to remove all overlap
* e.g. if merged_list contained two entries:
*
* [0x38000:0x3e000) = adv1
* [0x3a000:0x3c000) = adv2
*
* the chopped list will contain three entries:
*
* [0x38000:0x3a000) = adv1
* [0x3a000:0x3c000) = adv1,adv2
* [0x3c000:0x3e000) = adv1
*
*/
saddr_t *chopped_list = NULL;
typedef struct mapnode_struct {
prmap_t *pmp;
char label[PATH_MAX];
int mtypes;
struct mapnode_struct *next;
} mapnode_t;
mapnode_t *maplist_head = NULL;
mapnode_t *maplist_tail = NULL;
static void print_advice(saddr_t *, mapnode_t *);
int opt_verbose;
static char *advicestr[] = {
"normal",
"random",
"sequential",
"willneed",
"dontneed",
"free",
"access_default",
"access_lwp",
"access_many"
};
/*
* How many signals caught from terminal
* We bail out as soon as possible when interrupt is set
*/
static int interrupt = 0;
/*
* Interrupt handler
*/
static void intr(int);
/*
* Iterative function passed to Plwp_iter to
* get alt and main stacks for given lwp.
*/
static int
getstack(void *data, const lwpstatus_t *lsp)
{
int *np = (int *)data;
if (Plwp_alt_stack(Pr, lsp->pr_lwpid, &stacks[*np].lwps_stack) == 0) {
stacks[*np].lwps_stack.ss_flags |= SS_ONSTACK;
stacks[*np].lwps_lwpid = lsp->pr_lwpid;
(*np)++;
}
if (Plwp_main_stack(Pr, lsp->pr_lwpid, &stacks[*np].lwps_stack) == 0) {
stacks[*np].lwps_lwpid = lsp->pr_lwpid;
(*np)++;
}
return (0);
}
/*
* Prints usage and exits
*/
static void
usage()
{
(void) fprintf(stderr,
gettext("usage:\t%s [-o option[,option]] [-Flv] pid ...\n"),
progname);
(void) fprintf(stderr,
gettext(" (Give \"advice\" about a process's memory)\n"
" -o option[,option]: options are\n"
" private=<advice>\n"
" shared=<advice>\n"
" heap=<advice>\n"
" stack=<advice>\n"
" <segaddr>[:<length>]=<advice>\n"
" valid <advice> is one of:\n"
" normal, random, sequential, willneed, dontneed,\n"
" free, access_lwp, access_many, access_default\n"
" -v: verbose output\n"
" -F: force grabbing of the target process(es)\n"
" -l: show unresolved dynamic linker map names\n"
" pid: process id list\n"));
exit(2);
}
/*
* Function to parse advice from options string
*/
static int
get_advice(char *optarg)
{
/*
* Determine which advice is given, we use shifted values as
* multiple pieces of advice may apply for a particular region.
* (See comment above regarding GRP[1,2,3]_ADV definitions for
* breakdown of advice groups).
*/
if (strcmp(optarg, "access_default") == 0)
return (1 << MADV_ACCESS_DEFAULT);
else if (strcmp(optarg, "access_many") == 0)
return (1 << MADV_ACCESS_MANY);
else if (strcmp(optarg, "access_lwp") == 0)
return (1 << MADV_ACCESS_LWP);
else if (strcmp(optarg, "sequential") == 0)
return (1 << MADV_SEQUENTIAL);
else if (strcmp(optarg, "willneed") == 0)
return (1 << MADV_WILLNEED);
else if (strcmp(optarg, "dontneed") == 0)
return (1 << MADV_DONTNEED);
else if (strcmp(optarg, "random") == 0)
return (1 << MADV_RANDOM);
else if (strcmp(optarg, "normal") == 0)
return (1 << MADV_NORMAL);
else if (strcmp(optarg, "free") == 0)
return (1 << MADV_FREE);
else if (strcmp(optarg, "purge") == 0)
return (1 << MADV_PURGE);
else {
(void) fprintf(stderr, gettext("%s: invalid advice: %s\n"),
progname, optarg);
usage();
return (-1);
}
}
/*
* Function to convert character size indicators into actual size
* (i.e., 123M => sz = 123 * 1024 * 1024)
*/
static size_t
atosz(char *optarg, char **endptr)
{
size_t sz = 0;
if (optarg == NULL || optarg[0] == '\0')
return (0);
sz = strtoll(optarg, endptr, 0);
switch (**endptr) {
case 'E':
case 'e':
sz *= KILOBYTE;
/* FALLTHRU */
case 'P':
case 'p':
sz *= KILOBYTE;
/* FALLTHRU */
case 'T':
case 't':
sz *= KILOBYTE;
/* FALLTHRU */
case 'G':
case 'g':
sz *= KILOBYTE;
/* FALLTHRU */
case 'M':
case 'm':
sz *= KILOBYTE;
/* FALLTHRU */
case 'K':
case 'k':
sz *= KILOBYTE;
/* FALLTHRU */
case 'B':
case 'b':
(*endptr)++;
/* FALLTHRU */
default:
break;
}
return (sz);
}
/*
* Inserts newaddr into list. dups indicates whether we allow duplicate
* addr entries in the list (valid values are NODUPS and YESDUPS).
*/
static void
insert_addr(saddr_t **list, saddr_t *newaddr, int dups)
{
saddr_t *prev = *list;
saddr_t *psaddr;
if (*list == NULL) {
newaddr->next = *list;
*list = newaddr;
return;
}
for (psaddr = (*list)->next; psaddr != NULL; psaddr = psaddr->next) {
if ((dups == NODUPS) && (psaddr->addr == newaddr->addr)) {
free(newaddr);
return;
}
/*
* primary level of comparison is by address; smaller addr 1st
* secondary level of comparison is by length; bigger length 1st
*/
if ((psaddr->addr > newaddr->addr) ||
(psaddr->addr == newaddr->addr &&
psaddr->length < newaddr->length))
break;
prev = psaddr;
}
prev->next = newaddr;
newaddr->next = psaddr;
}
/*
* Deletes given element from list
*/
static void
delete_addr(saddr_t **list, saddr_t *delme)
{
saddr_t *prev = *list;
if (delme == *list) {
*list = delme->next;
free(delme);
return;
}
while (prev != NULL && prev->next != delme) {
prev = prev->next;
}
if (prev) {
prev->next = delme->next;
free(delme);
}
}
/*
* Delete entire list
*/
static void
delete_list(saddr_t **list)
{
saddr_t *psaddr = *list;
while (psaddr != NULL) {
saddr_t *temp = psaddr;
psaddr = psaddr->next;
free(temp);
}
*list = NULL;
}
static saddr_t *
parse_suboptions(char *value)
{
char *endptr;
saddr_t *psaddr = malloc(sizeof (saddr_t));
/*
* This must (better) be a segment addr
*/
psaddr->addr =
strtoull(value, &endptr, 16);
/*
* Check to make sure strtoul worked correctly (a properly formatted
* string will terminate in a ':' (if size is given) or an '=' (if size
* is not specified). Also check to make sure a 0 addr wasn't returned
* indicating strtoll was unable to convert).
*/
if ((psaddr->addr == 0) || (*endptr != ':' && *endptr != '=')) {
free(psaddr);
(void) fprintf(stderr,
gettext("%s: invalid option %s\n"),
progname, value);
usage();
} else {
/* init other fields */
psaddr->length = 0;
psaddr->adv = NO_ADVICE;
psaddr->next = NULL;
/* skip past address */
value = endptr;
/* check for length */
if (*value == ':') {
/* skip the ":" */
value++;
psaddr->length = atosz(value, &endptr);
}
if (*endptr != '=') {
(void) fprintf(stderr,
gettext("%s: invalid option %s\n"),
progname, value);
/*
* if improperly formatted, free mem, print usage, and
* exit Note: usage ends with a call to exit()
*/
free(psaddr);
usage();
}
/* skip the "=" */
value = endptr + 1;
at_map |= (1 << AT_SEG);
psaddr->adv =
get_advice(value);
}
return (psaddr);
}
/*
* Create linked list of mappings for current process
* In addition, add generic advice and raw advice
* entries to merged_list.
*/
/* ARGSUSED */
static int
create_maplist(void *arg, const prmap_t *pmp, const char *object_name)
{
const pstatus_t *Psp = Pstatus(Pr);
mapnode_t *newmap = malloc(sizeof (mapnode_t));
saddr_t *newaddr;
saddr_t *psaddr;
char *lname = NULL;
int i;
if (interrupt)
return (0);
newmap->pmp = malloc(sizeof (prmap_t));
newmap->label[0] = '\0';
newmap->mtypes = 0;
newmap->next = NULL;
(void) memcpy(newmap->pmp, pmp, sizeof (prmap_t));
/*
* If the mapping is not anon or not part of the heap, make a name
* for it. We don't want to report the heap as a.out's data.
*/
if (!(pmp->pr_mflags & MA_ANON) ||
(pmp->pr_vaddr + pmp->pr_size <= Psp->pr_brkbase ||
pmp->pr_vaddr >= Psp->pr_brkbase + Psp->pr_brksize)) {
lname = make_name(Pr, lflag, pmp->pr_vaddr, pmp->pr_mapname,
newmap->label, sizeof (newmap->label));
if (pmp->pr_mflags & MA_SHARED)
newmap->mtypes |= 1 << AT_SHARED;
else
newmap->mtypes |= 1 << AT_PRIVM;
}
if (lname == NULL && (pmp->pr_mflags & MA_ANON)) {
lname = anon_name(newmap->label, Psp, stacks, nstacks,
pmp->pr_vaddr, pmp->pr_size, pmp->pr_mflags, pmp->pr_shmid,
&newmap->mtypes);
}
/*
* Add raw advice that applies to this mapping to the merged_list
*/
psaddr = rawadv_list;
/*
* Advance to point in rawadv_list that applies to this mapping
*/
while (psaddr && psaddr->addr < pmp->pr_vaddr)
psaddr = psaddr->next;
/*
* Copy over to merged_list, check to see if size needs to be filled in
*/
while (psaddr && psaddr->addr < (pmp->pr_vaddr + pmp->pr_size)) {
newaddr = malloc(sizeof (saddr_t));
(void) memcpy(newaddr, psaddr, sizeof (saddr_t));
insert_addr(&merged_list, newaddr, YESDUPS);
/*
* For raw advice that is given without size, try to default
* size to size of mapping (only allowed if raw adv addr is
* equal to beginning of mapping). Don't change the entry
* in rawadv_list, only in the merged_list as the mappings
* (and thus the default sizes) will be different for
* different processes.
*/
if ((pmp->pr_vaddr == psaddr->addr) && (psaddr->length == 0))
newaddr->length = pmp->pr_size;
psaddr = psaddr->next;
}
/*
* Put mapping into merged list with no advice, then
* check to see if any generic advice applies.
*/
newaddr = malloc(sizeof (saddr_t));
newaddr->addr = pmp->pr_vaddr;
newaddr->length = pmp->pr_size;
newaddr->adv = NO_ADVICE;
insert_addr(&merged_list, newaddr, YESDUPS);
newmap->mtypes &= at_map;
for (i = AT_STACK; i >= AT_PRIVM; i--) {
if (newmap->mtypes & (1 << i)) {
assert(generic_adv[i] != NO_ADVICE);
newaddr->adv = generic_adv[i];
break;
}
}
/*
* Add to linked list of mappings
*/
if (maplist_tail == NULL) {
maplist_head = maplist_tail = newmap;
} else {
maplist_tail->next = newmap;
maplist_tail = newmap;
}
return (0);
}
/*
* Traverse advice list and apply all applicable advice to each region
*/
static int
apply_advice(saddr_t **advicelist)
{
saddr_t *psaddr = *advicelist;
saddr_t *next;
int i;
while (!interrupt && psaddr != NULL) {
/*
* Save next pointer since element may be removed before
* we get a chance to advance psaddr.
*/
next = psaddr->next;
/*
* Since mappings have been added to the merged list
* even if no generic advice was given for the map,
* check to make sure advice exists before bothering
* with the for loop.
*/
if (psaddr->adv != NO_ADVICE) {
for (i = MADV_NORMAL; i <= MADV_PURGE; i++) {
if ((psaddr->adv & (1 << i)) &&
(pr_madvise(Pr, (caddr_t)psaddr->addr,
psaddr->length, i) < 0)) {
/*
* madvise(3C) call failed trying to
* apply advice output error and remove
* from advice list
*/
(void) fprintf(stderr,
gettext("Error applying "
"advice (%s) to memory range "
"[%lx, %lx):\n"),
advicestr[i], (ulong_t)psaddr->addr,
(ulong_t)psaddr->addr +
psaddr->length);
perror("madvise");
/*
* Clear this advice from the advice
* mask. If no more advice is given
* for this element, remove element
* from list.
*/
psaddr->adv &= ~(1 << i);
if (psaddr->adv == 0) {
delete_addr(advicelist, psaddr);
break;
}
}
}
}
psaddr = next;
}
return (0);
}
/*
* Set advice but keep mutual exclusive property of advice groupings
*/
static void
set_advice(int *combined_adv, int new_adv) {
/*
* Since advice falls in 3 groups of mutually exclusive options,
* clear previous value if new advice overwrites that group.
*/
/*
* If this is the first advice to be applied, clear invalid value (-1)
*/
if (*combined_adv == -1)
*combined_adv = 0;
if (new_adv & GRP1_ADV)
*combined_adv &= ~GRP1_ADV;
else if (new_adv & GRP2_ADV)
*combined_adv &= ~GRP2_ADV;
else
*combined_adv &= ~GRP3_ADV;
*combined_adv |= new_adv;
}
/*
* Create chopped list from merged list for use with verbose output
*/
static void
create_choplist(saddr_t **choppedlist, saddr_t *mergedlist)
{
saddr_t *mlptr, *clptr;
for (mlptr = mergedlist; mlptr != NULL; mlptr = mlptr->next) {
clptr = malloc(sizeof (saddr_t));
clptr->addr = mlptr->addr;
clptr->length = 0;
/*
* Initialize the adv to -1 as an indicator for invalid
* elements in the chopped list (created from gaps between
* memory maps).
*/
clptr->adv = -1;
clptr->next = NULL;
insert_addr(choppedlist, clptr, NODUPS);
clptr = malloc(sizeof (saddr_t));
clptr->addr = mlptr->addr + mlptr->length;
clptr->length = 0;
/*
* Again, initialize to -1 as an indicatorfor invalid elements
*/
clptr->adv = -1;
clptr->next = NULL;
insert_addr(choppedlist, clptr, NODUPS);
}
for (clptr = *choppedlist; clptr != NULL; clptr = clptr->next) {
if (clptr->next) {
clptr->length = clptr->next->addr - clptr->addr;
} else {
/*
* must be last element, now that we've calculated
* all segment lengths, we can remove this node
*/
delete_addr(choppedlist, clptr);
break;
}
}
for (mlptr = mergedlist; mlptr != NULL; mlptr = mlptr->next) {
for (clptr = *choppedlist; clptr != NULL; clptr = clptr->next) {
if (mlptr->addr <= clptr->addr &&
mlptr->addr + mlptr->length >=
clptr->addr + clptr->length)
/*
* set_advice() will take care of conflicting
* advice by taking only the last advice
* applied for each of the 3 groups of advice.
*/
set_advice(&clptr->adv, mlptr->adv);
if (mlptr->addr + mlptr->length <
clptr->addr)
break;
}
}
}
/*
* Print advice in pmap style for verbose output
*/
static void
print_advice(saddr_t *advlist, mapnode_t *maplist)
{
saddr_t *psaddr = advlist;
mapnode_t *pmapnode;
char *advice;
pmapnode = maplist;
while (psaddr) {
/*
* Using indicator flag from create_choppedlist, we know
* which entries in the chopped_list are gaps and should
* not be printed.
*/
if (psaddr->adv == -1) {
psaddr = psaddr->next;
continue;
}
while (pmapnode && (pmapnode->pmp->pr_vaddr +
pmapnode->pmp->pr_size <= psaddr->addr))
pmapnode = pmapnode->next;
advice = advtostr(psaddr->adv);
/*
* Print segment mapping and advice if there is any, or just a
* segment mapping.
*/
if (strlen(advice) > 0) {
(void) printf("%.*lX %*uK %6s %s\t%s\n",
addr_width, (ulong_t)psaddr->addr, size_width - 1,
(int)ROUNDUP_KB(psaddr->length),
mflags(pmapnode->pmp->pr_mflags), pmapnode->label,
advice);
} else {
(void) printf("%.*lX %*uK %6s %s\n",
addr_width, (ulong_t)psaddr->addr, size_width - 1,
(int)ROUNDUP_KB(psaddr->length),
mflags(pmapnode->pmp->pr_mflags), pmapnode->label);
}
psaddr = psaddr->next;
}
}
/*
* Call madvise(3c) in the context of the target process
*/
static int
pr_madvise(struct ps_prochandle *Pr, caddr_t addr, size_t len, int advice)
{
return (pr_memcntl(Pr, addr, len, MC_ADVISE,
(caddr_t)(uintptr_t)advice, 0, 0));
}
static char *
mflags(uint_t arg)
{
static char code_buf[80];
/*
* rwxsR
*
* r - segment is readable
* w - segment is writable
* x - segment is executable
* s - segment is shared
* R - segment is mapped MAP_NORESERVE
*
*/
(void) snprintf(code_buf, sizeof (code_buf), "%c%c%c%c%c ",
arg & MA_READ ? 'r' : '-',
arg & MA_WRITE ? 'w' : '-',
arg & MA_EXEC ? 'x' : '-',
arg & MA_SHARED ? 's' : '-',
arg & MA_NORESERVE ? 'R' : '-');
return (code_buf);
}
/*
* Convert advice to a string containing a commented list of applicable advice
*/
static char *
advtostr(int adv)
{
static char buf[50];
int i;
*buf = '\0';
if (adv != NO_ADVICE) {
for (i = MADV_NORMAL; i <= MADV_PURGE; i++) {
if (adv & (1 << i)) {
/*
* check if it's the first advice entry
*/
if (*buf == '\0')
(void) snprintf(buf, sizeof (buf) - 1,
"<= %s", advicestr[i]);
else
(void) snprintf(buf, sizeof (buf) - 1,
"%s,%s", buf, advicestr[i]);
}
}
}
return (buf);
}
/*
* Handler for catching signals from terminal
*/
/* ARGSUSED */
static void
intr(int sig)
{
interrupt++;
}
int
main(int argc, char **argv)
{
int Fflag = 0;
int rc = 0;
int opt, subopt;
int tmpadv;
char *options, *value;
saddr_t *psaddr;
mapnode_t *pmapnode, *tempmapnode;
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
/*
* Get name of program for error messages
*/
progname = basename(argv[0]);
/*
* Not much to do when only name of program given
*/
if (argc == 1)
usage();
/*
* Catch signals from terminal, so they can be handled asynchronously
* when we're ready instead of when we're not (;-)
*/
if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
(void) sigset(SIGHUP, intr);
if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
(void) sigset(SIGINT, intr);
if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
(void) sigset(SIGQUIT, intr);
(void) sigset(SIGPIPE, intr);
(void) sigset(SIGTERM, intr);
/*
* Parse options, record generic advice if any and create
* rawadv_list from specific address advice.
*/
while ((opt = getopt(argc, argv, "Flo:v")) != EOF) {
switch (opt) {
case 'o':
options = optarg;
while (*options != '\0') {
subopt = getsubopt(&options, suboptstr,
&value);
switch (subopt) {
case AT_PRIVM:
case AT_HEAP:
case AT_SHARED:
case AT_STACK:
at_map |= (1 << subopt);
tmpadv = get_advice(value);
set_advice(&generic_adv[subopt],
tmpadv);
break;
default:
at_map |= (1 << AT_SEG);
psaddr = parse_suboptions(value);
if (psaddr == NULL) {
usage();
} else {
insert_addr(&rawadv_list,
psaddr, YESDUPS);
}
break;
}
}
break;
case 'v':
opt_verbose = 1;
break;
case 'F': /* force grabbing (no O_EXCL) */
Fflag = PGRAB_FORCE;
break;
case 'l': /* show unresolved link map names */
lflag = 1;
break;
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
if (argc <= 0) {
usage();
}
(void) proc_initstdio();
/*
* Iterate through all pid arguments, create new merged_list, maplist,
* (and chopped_list if using verbose output) based on each process'
* memory map.
*/
while (!interrupt && argc-- > 0) {
char *arg;
int gcode;
psinfo_t psinfo;
(void) proc_flushstdio();
if ((Pr = proc_arg_grab(arg = *argv++, PR_ARG_PIDS,
PGRAB_RETAIN | Fflag, &gcode)) == NULL) {
(void) fprintf(stderr,
gettext("%s: cannot examine %s: %s\n"),
progname, arg, Pgrab_error(gcode));
rc++;
continue;
}
addr_width =
(Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 16 : 8;
size_width =
(Pstatus(Pr)->pr_dmodel == PR_MODEL_LP64) ? 11 : 8;
(void) memcpy(&psinfo, Ppsinfo(Pr), sizeof (psinfo_t));
if (opt_verbose) {
proc_unctrl_psinfo(&psinfo);
(void) printf("%d:\t%.70s\n",
(int)psinfo.pr_pid, psinfo.pr_psargs);
}
/*
* Get mappings for a process unless it is a system process.
*/
if (!(Pstatus(Pr)->pr_flags & PR_ISSYS)) {
nstacks = psinfo.pr_nlwp * 2;
stacks = calloc(nstacks, sizeof (stacks[0]));
if (stacks != NULL) {
int n = 0;
(void) Plwp_iter(Pr, getstack, &n);
qsort(stacks, nstacks, sizeof (stacks[0]),
cmpstacks);
}
if (Pgetauxval(Pr, AT_BASE) != -1L &&
Prd_agent(Pr) == NULL) {
(void) fprintf(stderr,
gettext("%s: warning: "
"librtld_db failed to initialize; "
"shared library information will not "
"be available\n"),
progname);
}
/*
* Create linked list of mappings for current process
* In addition, add generic advice and raw advice
* entries to merged_list.
* e.g. if rawadv_list contains:
* [0x38000,0x3a000) = adv1
* [0x3a000,0x3c000) = adv2
* and there is generic advice:
* heap = adv3
* where heap corresponds to 0x38000, then merged_list
* will contain:
* ... (include all other mappings from process)
* [0x38000,0x3c000) = adv3
* [0x38000,0x3a000) = adv1
* [0x3a000,0x3c000) = adv2
* ... (include all other mappings from process)
*/
assert(merged_list == NULL);
maplist_head = maplist_tail = NULL;
rc += Pmapping_iter(Pr, (proc_map_f *)create_maplist,
NULL);
/*
* Apply advice by iterating through merged list
*/
(void) apply_advice(&merged_list);
if (opt_verbose) {
assert(chopped_list == NULL);
/*
* Create chopped_list from merged_list
*/
create_choplist(&chopped_list, merged_list);
/*
* Iterate through maplist and output as
* given by chopped_list
*/
print_advice(chopped_list, maplist_head);
delete_list(&chopped_list);
}
delete_list(&merged_list);
/*
* Clear maplist
*/
pmapnode = maplist_head;
while (pmapnode) {
tempmapnode = pmapnode;
pmapnode = pmapnode->next;
free(tempmapnode);
}
if (stacks != NULL) {
free(stacks);
stacks = NULL;
}
}
Prelease(Pr, 0);
}
(void) proc_finistdio();
return (rc);
}
|