-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGdbSessionFactory.cs
151 lines (139 loc) · 4.7 KB
/
GdbSessionFactory.cs
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
// GdbSessionFactory.cs
//
// Author:
// Lluis Sanchez Gual <[email protected]>
//
// Copyright (c) 2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
using System;
using System.Collections.Generic;
using System.IO;
using Mono.Debugging.Client;
using Mono.Debugging.Backend;
using MonoDevelop.Core.Execution;
namespace MonoDevelop.Debugger.Gdb
{
public class GdbSessionFactory: DebuggerEngineBackend
{
struct FileData {
public DateTime LastCheck;
public bool IsExe;
}
Dictionary<string,FileData> fileCheckCache = new Dictionary<string, FileData> ();
public override bool CanDebugCommand (ExecutionCommand command)
{
NativeExecutionCommand cmd = command as NativeExecutionCommand;
if (cmd == null)
return false;
string file = FindFile (cmd.Command);
if (!File.Exists (file)) {
// The provided file is not guaranteed to exist. If it doesn't
// we assume we can execute it because otherwise the run command
// in the IDE will be disabled, and that's not good because that
// command will build the project if the exec doesn't yet exist.
return true;
}
file = Path.GetFullPath (file);
DateTime currentTime = File.GetLastWriteTime (file);
FileData data;
if (fileCheckCache.TryGetValue (file, out data)) {
if (data.LastCheck == currentTime)
return data.IsExe;
}
data.LastCheck = currentTime;
try {
data.IsExe = IsExecutable (file);
} catch {
data.IsExe = false;
}
fileCheckCache [file] = data;
return data.IsExe;
}
public override DebuggerStartInfo CreateDebuggerStartInfo (ExecutionCommand command)
{
NativeExecutionCommand pec = (NativeExecutionCommand) command;
DebuggerStartInfo startInfo = new DebuggerStartInfo ();
startInfo.Command = pec.Command;
startInfo.Arguments = pec.Arguments;
startInfo.WorkingDirectory = pec.WorkingDirectory;
if (pec.EnvironmentVariables.Count > 0) {
foreach (KeyValuePair<string,string> val in pec.EnvironmentVariables)
startInfo.EnvironmentVariables [val.Key] = val.Value;
}
return startInfo;
}
public bool IsExecutable (string file)
{
// HACK: this is a quick but not very reliable way of checking if a file
// is a native executable. Actually, we are interested in checking that
// the file is not a script.
using (StreamReader sr = new StreamReader (file)) {
char[] chars = new char[3];
int n = 0, nr = 0;
while (n < chars.Length && (nr = sr.ReadBlock (chars, n, chars.Length - n)) != 0)
n += nr;
if (nr != chars.Length)
return true;
if (chars [0] == '#' && chars [1] == '!')
return false;
}
return true;
}
public override DebuggerSession CreateSession ()
{
GdbSession ds = new GdbSession ();
return ds;
}
public override ProcessInfo[] GetAttachableProcesses ()
{
List<ProcessInfo> procs = new List<ProcessInfo> ();
foreach (string dir in Directory.GetDirectories ("/proc")) {
int id;
if (!int.TryParse (Path.GetFileName (dir), out id))
continue;
try {
File.ReadAllText (Path.Combine (dir, "sessionid"));
} catch {
continue;
}
string cmdline = File.ReadAllText (Path.Combine (dir, "cmdline"));
cmdline = cmdline.Replace ('\0',' ');
ProcessInfo pi = new ProcessInfo (id, cmdline);
procs.Add (pi);
}
return procs.ToArray ();
}
string FindFile (string cmd)
{
if (Path.IsPathRooted (cmd))
return cmd;
string pathVar = Environment.GetEnvironmentVariable ("PATH");
string[] paths = pathVar.Split (Path.PathSeparator);
foreach (string path in paths) {
string file = Path.Combine (path, cmd);
if (File.Exists (file))
return file;
}
return cmd;
}
}
}