xref: /haiku/src/bin/listarea.c (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 /*
2  * Copyright (c) 2001-2005, Haiku.
3  *
4  * This software is part of the Haiku distribution and is covered
5  * by the MIT license.
6  *
7  * Author: Daniel Reinhold (danielre@users.sf.net)
8  */
9 
10 /** Lists area info for all currently running teams */
11 
12 
13 #include <OS.h>
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 
20 static void list_areas_for_id(team_id team);
21 static void list_areas_for_name(const char *teamName);
22 static void show_memory_totals(void);
23 
24 
25 static void
26 show_memory_totals(void)
27 {
28 	int32 max = 0, used = 0;
29 
30 	system_info info;
31 	if (get_system_info(&info) == B_OK) {
32 		// pages are 4KB
33 		max = info.max_pages * 4;
34 		used = info.used_pages * 4;
35 	}
36 
37 	printf("memory: total: %4ldKB, used: %4ldKB, left: %4ldKB\n", max, used, max - used);
38 }
39 
40 
41 static void
42 list_areas_for_id(team_id id)
43 {
44 	int32 cookie = 0;
45 	team_info teamInfo;
46 	area_info areaInfo;
47 
48 	if (id != 1 && get_team_info(id, &teamInfo) == B_BAD_TEAM_ID) {
49 		printf("\nteam %ld unknown\n", id);
50 		return;
51 	} else if (id == 1)
52 		strcpy(teamInfo.args, "KERNEL SPACE");
53 
54 	printf("\n%s (team %ld)\n", teamInfo.args, id);
55 	printf("   ID                             name   address     size   alloc. #-cow  #-in #-out\n");
56 	printf("------------------------------------------------------------------------------------\n");
57 
58 	while (get_next_area_info(id, &cookie, &areaInfo) == B_OK) {
59 		printf("%5ld %32s  %08lx %8lx %8lx %5ld %5ld %5ld\n",
60 			areaInfo.area,
61 			areaInfo.name,
62 //			(addr_t)areaInfo.address,
63 			(uint32)areaInfo.address,
64 			areaInfo.size,
65 			areaInfo.ram_size,
66 			areaInfo.copy_count,
67 			areaInfo.in_count,
68 			areaInfo.out_count);
69 	}
70 }
71 
72 
73 static void
74 list_areas_for_name(const char *name)
75 {
76 	int32 cookie = 0;
77 	team_info info;
78 	while (get_next_team_info(&cookie, &info) >= B_OK) {
79 		if (strstr(info.args, name) != NULL)
80 			list_areas_for_id(info.team);
81 	}
82 }
83 
84 
85 int
86 main(int argc, char **argv)
87 {
88 	show_memory_totals();
89 
90 	if (argc == 1) {
91 		// list areas of all teams
92 		int32 cookie = 0;
93 		team_info info;
94 
95 		while (get_next_team_info(&cookie, &info) >= B_OK)
96 			list_areas_for_id(info.team);
97 	} else {
98 		// list areas for each team ID/name on the command line
99 		while (--argc) {
100 			const char *arg = *++argv;
101 			team_id team = atoi(arg);
102 
103 			// if atoi returns >0 it's likely it's a number, else take it as string
104 			if (team > 0)
105 				list_areas_for_id(team);
106 			else
107 				list_areas_for_name(arg);
108 		}
109 	}
110 
111 	return 0;
112 }
113 
114