Add patch against GNU Binutils-2.20 to support "meminfo".
[libmeminfo.git] / mind.c
blob3685db0deb8077d77eb1c05de754697e00e9eeec
1 /*
2 Copyright (c) 2008, STMicroelectronics
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 * Neither the name of STMicroelectronics nor the names of its
15 contributors may be used to endorse or promote products derived
16 from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 Contact: cedric.vincent@st.com
33 #include <stdlib.h> /* NULL, */
34 #include <stdio.h> /* fprintf(3), */
35 #include <string.h> /* strlen(3), */
36 #include <libelf.h> /* elf_errmsg(3), */
37 #include <inttypes.h> /* PRIu32, */
38 #include <unistd.h> /* getopt(3), */
40 #include <meminfo.h> /* meminfo*, */
41 #include "license.h" /* LICENSE */
43 static void print_usage()
45 fprintf(stderr, "MemINfo Dumper usage:\n\n");
46 fprintf(stderr, " mind [elf-file]\n");
47 fprintf(stderr, "or\n");
48 fprintf(stderr, " mind [options]\n");
49 fprintf(stderr, "\nOptions:\n");
50 fprintf(stderr, " -h Print this help.\n");
51 fprintf(stderr, " -l Print the license.\n");
54 static void print_license()
56 char license[] = { LICENSE };
57 fprintf(stderr, "%s\n", license);
60 extern char *optarg;
61 extern int optind;
63 int main(int argc, char **argv)
65 struct meminfo *meminfo = NULL;
66 char flag = '\0';
67 int status = 0;
68 int i = 0;
70 /* Parse options. */
71 while ((flag = getopt(argc, argv, "hl")) != (char) -1) {
72 switch (flag) {
73 case 'l':
74 print_license();
75 exit(EXIT_SUCCESS);
76 case 'h':
77 default:
78 print_usage();
79 exit(EXIT_FAILURE);
83 if (argc - optind != 1) {
84 print_usage();
85 exit(EXIT_FAILURE);
88 status = meminfo_read_file(argv[optind], &meminfo);
89 if (status <= 0) {
90 switch (status) {
91 case 0:
92 fprintf(stderr, "The meminfo table is empty.\n");
93 break;
94 case MEMINFO_ERROR_STRUCTURE:
95 fprintf(stderr, "Error: can't find meminfo entries.\n");
96 break;
97 case MEMINFO_ERROR_SYSTEM:
98 fprintf(stderr, "Error system: "); perror(NULL);
99 break;
100 case MEMINFO_ERROR_ELF:
101 fprintf(stderr, "Error ELF: %s\n", elf_errmsg(0));
102 break;
103 default:
104 fprintf(stderr, "Error: I lost my mind...\n");
105 break;
107 exit(EXIT_FAILURE);
110 for (i = 0; i < status; i++) {
111 printf("%-10s : ", (char *)meminfo[i].name);
112 printf("origin = 0x%08"PRIX32", ", meminfo[i].origin);
113 printf("length = 0x%08"PRIX32", ", meminfo[i].length);
114 printf("flags = 0x%08"PRIX32", ", meminfo[i].flags);
115 printf("not_flags = 0x%08"PRIX32" ", meminfo[i].not_flags);
117 printf("[");
118 if (meminfo[i].flags & MEMINFO_READONLY) printf("r");
119 if (meminfo[i].flags & MEMINFO_READWRITE) printf("w");
120 if (meminfo[i].flags & MEMINFO_EXECUTABLE) printf("x");
121 if (meminfo[i].flags & MEMINFO_ALLOCATABLE) printf("a");
122 if (meminfo[i].flags & MEMINFO_INITIALIZED) printf("i");
123 if (meminfo[i].not_flags & MEMINFO_READONLY) printf("!r");
124 if (meminfo[i].not_flags & MEMINFO_READWRITE) printf("!w");
125 if (meminfo[i].not_flags & MEMINFO_EXECUTABLE) printf("!x");
126 if (meminfo[i].not_flags & MEMINFO_ALLOCATABLE) printf("!a");
127 if (meminfo[i].not_flags & MEMINFO_INITIALIZED) printf("!i");
128 printf("]\n");
131 free(meminfo);
132 meminfo = NULL;
134 exit(EXIT_SUCCESS);