xref: /haiku/src/bin/listarea.c (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
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 			areaInfo.size,
64 			areaInfo.ram_size,
65 			areaInfo.copy_count,
66 			areaInfo.in_count,
67 			areaInfo.out_count);
68 	}
69 }
70 
71 
72 static void
73 list_areas_for_name(const char *name)
74 {
75 	int32 cookie = 0;
76 	team_info info;
77 	while (get_next_team_info(&cookie, &info) >= B_OK) {
78 		if (strstr(info.args, name) != NULL)
79 			list_areas_for_id(info.team);
80 	}
81 }
82 
83 
84 int
85 main(int argc, char **argv)
86 {
87 	show_memory_totals();
88 
89 	if (argc == 1) {
90 		// list areas of all teams
91 		int32 cookie = 0;
92 		team_info info;
93 
94 		while (get_next_team_info(&cookie, &info) >= B_OK)
95 			list_areas_for_id(info.team);
96 	} else {
97 		// list areas for each team ID/name on the command line
98 		while (--argc) {
99 			const char *arg = *++argv;
100 			team_id team = atoi(arg);
101 
102 			// if atoi returns >0 it's likely it's a number, else take it as string
103 			if (team > 0)
104 				list_areas_for_id(team);
105 			else
106 				list_areas_for_name(arg);
107 		}
108 	}
109 
110 	return 0;
111 }
112 
113