Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a71ddde4f8825fb0e12e9a753f654a039dedaecc (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2011, 2013 Mentor Graphics and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Vladimir Prus (Mentor Graphics) - initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.dsf.mi.service.command.output;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.dsf.gdb.service.IGDBHardwareAndOS2.IResourceClass;
import org.eclipse.cdt.dsf.gdb.service.IGDBHardwareAndOS2.IResourcesInformation;

/**
 * Example output:
 *
 *   (gdb) -info-os
 *   ^done,OSDataTable={nr_rows="9",nr_cols="3",
 *   hdr=[{width="10",alignment="-1",col_name="col0",colhdr="Type"},
 *        {width="10",alignment="-1",col_name="col1",colhdr="Description"},
 *        {width="10",alignment="-1",col_name="col2",colhdr="Title"}],
 *   body=[item={col0="processes",col1="Listing of all processes",
 *               col2="Processes"},
 *         item={col0="procgroups",col1="Listing of all process groups",
 *               col2="Process groups"},
 *         item={col0="threads",col1="Listing of all threads",
 *               col2="Threads"},
 *         item={col0="files",col1="Listing of all file descriptors",
 *               col2="File descriptors"},
 *         item={col0="sockets",col1="Listing of all internet-domain sockets",
 *               col2="Sockets"},
 *         item={col0="shm",col1="Listing of all shared-memory regions",
 *               col2="Shared-memory regions"},
 *         item={col0="semaphores",col1="Listing of all semaphores",
 *               col2="Semaphores"},
 *         item={col0="msg",col1="Listing of all message queues",
 *               col2="Message queues"},
 *         item={col0="modules",col1="Listing of all loaded kernel modules",
 *               col2="Kernel modules"}]}
 *   (gdb) -info-os processes
 *   ^done,OSDataTable={nr_rows="190",nr_cols="4",
 *   hdr=[{width="10",alignment="-1",col_name="col0",colhdr="pid"},
 *        {width="10",alignment="-1",col_name="col1",colhdr="user"},
 *        {width="10",alignment="-1",col_name="col2",colhdr="command"},
 *        {width="10",alignment="-1",col_name="col3",colhdr="cores"}],
 *   body=[item={col0="1",col1="root",col2="/sbin/init",col3="0"},
 *         item={col0="2",col1="root",col2="[kthreadd]",col3="1"},
 *         item={col0="3",col1="root",col2="[ksoftirqd/0]",col3="0"},
 *         ...
 *         item={col0="26446",col1="stan",col2="bash",col3="0"},
 *         item={col0="28152",col1="stan",col2="bash",col3="1"}]}
 *   (gdb)
 *
 * @since 4.2
 */
public class MIInfoOsInfo extends MIInfo {

	// The fields below are used for response with list of classes.
	private IResourceClass[] resourceClasses;

	// The below fields are used only for data with specific resource class
	private String[] columnNames;
	private String[][] content;

	public MIInfoOsInfo(MIOutput record, boolean resourcesInformation) {
		super(record);
		if (isDone()) {
			if (resourcesInformation)
				parseResourcesInformation();
			else
				parseResourceClasses();
		}
	}

	public IResourcesInformation getResourcesInformation() {
		return new IResourcesInformation() {

			@Override
			public String[][] getContent() {
				return content;
			}

			@Override
			public String[] getColumnNames() {
				return columnNames;
			}
		};
	}

	public IResourceClass[] getResourceClasses() {
		return resourceClasses;
	}

	private void parseResourceClasses() {
		List<IResourceClass> classes = new ArrayList<IResourceClass>();

		MITuple table = (MITuple) get(getMIOutput(), "OSDataTable"); //$NON-NLS-1$

		MIList body = (MIList) get(table, "body"); //$NON-NLS-1$
		for (MIResult r : body.getMIResults()) {
			MITuple row = (MITuple) r.getMIValue();

			final String id = getString(row.getMIResults()[0]);
			final String description = getString(row.getMIResults()[2]);

			classes.add(new IResourceClass() {

				@Override
				public String getId() {
					return id;
				}

				@Override
				public String getHumanDescription() {
					return description;
				}

			});
		}

		resourceClasses = classes.toArray(new IResourceClass[classes.size()]);
	}

	private void parseResourcesInformation() {
		MITuple table = (MITuple) get(getMIOutput(), "OSDataTable"); //$NON-NLS-1$

		MIList header = (MIList) get(table, "hdr"); //$NON-NLS-1$
		columnNames = new String[header.getMIValues().length];
		int i = 0;
		for (MIValue v : header.getMIValues()) {
			MITuple column = (MITuple) v;
			String columnName = ((MIConst) get(column, "colhdr")).getString(); //$NON-NLS-1$
			columnNames[i++] = Character.toUpperCase(columnName.charAt(0)) + columnName.substring(1);
		}

		MIList body = (MIList) get(table, "body"); //$NON-NLS-1$
		if (body == null) {
			content = new String[0][];
			return;
		}

		content = new String[body.getMIResults().length][];
		i = 0;
		for (MIResult r : body.getMIResults()) {
			MITuple row = (MITuple) r.getMIValue();
			assert row.getMIResults().length == columnNames.length;
			String[] rowStrings = new String[row.getMIResults().length];
			int j = 0;
			for (MIResult r2 : row.getMIResults()) {
				rowStrings[j] = ((MIConst) r2.getMIValue()).getString();
				++j;
			}
			content[i++] = rowStrings;
		}
	}

	private MIValue get(MIResult[] results, String name) {
		for (MIResult r : results)
			if (r.getVariable().equals(name))
				return r.getMIValue();
		return null;
	}

	private MIValue get(MIOutput output, String name) {
		return get(output.getMIResultRecord().getMIResults(), name);
	}

	private MIValue get(MITuple tuple, String name) {
		return get(tuple.getMIResults(), name);
	}

	private String getString(MIValue value) {
		return ((MIConst) value).getString();
	}

	private String getString(MIResult result) {
		return getString(result.getMIValue());
	}
}

Back to the top