1
/*******************************************************************************
2
* Copyright (c) 2008 Ericsson and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Ericsson - Initial API and implementation
10
*******************************************************************************/
12
package org.eclipse.cdt.dsf.mi.service.command.output;
14
import java.util.regex.Matcher;
15
import java.util.regex.Pattern;
17
import org.eclipse.cdt.dsf.concurrent.Immutable;
20
* GDB/MI thread group parsing.
22
* The description field can be different depending on the target we are connected to.
24
* -list-thread-groups --available:
25
* ^done,groups=[{id="161",type="process",description="name: JIM_InstallerProcess, type 555481, locked: N, system: N, state: Idle"},
26
* {id="162",type="process",description="name: JIM_TcpSetupHandlerProcess, type 555505, locked: N, system: N, state: Idle"},
27
* {id="165",type="process",description="name: JUnitProcess2_PT, type 1094608, locked: N, system: N, state: Idle"},
28
* {id="166",type="process",description="name: JUnitProcess_PT, type 1094605, locked: N, system: N, state: Idle"}]
30
* {id="3602",type="process",description="/usr/sbin/dhcdbd --system",user="root"}
31
* -list-thread-groups:
32
* ^done,groups=[{id="162",type="process",pid="162"}]
34
* list-thread-groups GROUPID, in the case of a running thread or a stopped thread:
35
* ^done,threads=[{id="1",target-id="Thread 162.32942",details="JUnitProcess_PT (Ready) 1030373359 44441",frame={level="0",addr="0x00000000",func="??",args=[]},state="stopped"}]
36
* ^done,threads=[{id="1",target-id="Thread 162.32942",details="JUnitProcess_PT Idle 981333916 42692",state="running"}]
39
public class MIListThreadGroupsInfo extends MIInfo {
41
public interface IThreadGroupInfo {
45
String getDesciption();
49
private static class ThreadGroupInfo implements IThreadGroupInfo {
50
final String fGroupId;
51
final String fDescription;
54
public ThreadGroupInfo(String id, String description) {
56
fDescription = description;
58
fName = parseName(fDescription);
61
private static String parseName(String desc) {
62
String name = ""; //$NON-NLS-1$
64
// Find the string "name: " followed by the smallest set of characters that
65
// is followed by a comma, or by the end of the line.
66
Pattern pattern = Pattern.compile("name: (.*?)(, |$)", Pattern.MULTILINE); //$NON-NLS-1$
67
Matcher matcher = pattern.matcher(desc);
69
name = matcher.group(1);
71
// If we didn't get the form "name: " then we expect to have the form
72
// "/usr/sbin/dhcdbd --system"
73
name = desc.split("\\s", 2)[0]; //$NON-NLS-1$
79
public String getGroupId() { return fGroupId; }
80
public String getPid() { return fGroupId; }
82
public String getName() { return fName; }
84
public String getDesciption() { return fDescription; }
88
private IThreadGroupInfo[] fGroupList;
89
private MIThreadInfoInfo fThreadInfo;
91
public MIListThreadGroupsInfo(MIOutput out) {
96
public IThreadGroupInfo[] getGroupList() { return fGroupList; }
97
public MIThreadInfoInfo getThreadInfo() { return fThreadInfo; }
99
private void parse() {
101
MIOutput out = getMIOutput();
102
MIResultRecord rr = out.getMIResultRecord();
104
MIResult[] results = rr.getMIResults();
105
for (int i = 0; i < results.length; i++) {
106
String var = results[i].getVariable();
107
if (var.equals("groups")) { //$NON-NLS-1$
108
MIValue val = results[i].getMIValue();
109
if (val instanceof MIList) {
110
parseGroups((MIList)val);
112
} else if (var.equals("threads")) { //$NON-NLS-1$
113
// Re-use the MIThreadInfoInfo parsing
114
fThreadInfo = new MIThreadInfoInfo(out);
119
if (fGroupList == null) {
120
fGroupList = new IThreadGroupInfo[0];
122
if (fThreadInfo == null) {
123
fThreadInfo = new MIThreadInfoInfo(null);
127
private void parseGroups(MIList list) {
128
MIValue[] values = list.getMIValues();
129
fGroupList = new IThreadGroupInfo[values.length];
130
for (int i = 0; i < values.length; i++) {
131
MIResult[] results = ((MITuple)values[i]).getMIResults();
132
String id = "", desc = "";//$NON-NLS-1$//$NON-NLS-2$
134
for (MIResult result : results) {
135
String var = result.getVariable();
136
if (var.equals("id")) { //$NON-NLS-1$
137
MIValue value = result.getMIValue();
138
if (value instanceof MIConst) {
139
String str = ((MIConst)value).getCString();
142
} else if (var.equals("description")) { //$NON-NLS-1$
143
MIValue value = result.getMIValue();
144
if (value instanceof MIConst) {
145
String str = ((MIConst)value).getCString();
151
fGroupList[i] = new ThreadGroupInfo(id, desc);