4
4
import subprocess
5
5
import socket
6
6
import os
7
+ import json
7
8
from contextlib import closing
8
9
from pty import STDERR_FILENO , STDOUT_FILENO
9
10
19
20
core_registers = {}
20
21
live_registers = {}
21
22
23
+ # Current working directory
24
+ cwd = os .getcwd ()
25
+
22
26
# Global variable to store Crash server path
23
27
lldb_crash_server_path = None
24
28
@@ -41,12 +45,14 @@ def __init__(self):
41
45
)
42
46
43
47
def invoke (self , arg , from_tty ):
44
- global lldb_crash_server_path
48
+ global lldb_crash_server_path , cwd
45
49
46
50
args = arg .split ()
47
51
if len (args ) != 2 :
48
52
print ("Usage: setup_session <binary> <corefile>" )
49
53
return
54
+
55
+ gdb_settings = self .get_gdb_setting ()
50
56
51
57
binary , corefile = args
52
58
gdb .execute (f"file { binary } " )
@@ -64,12 +70,22 @@ def invoke(self, arg, from_tty):
64
70
if lldb_crash_server_path is None :
65
71
print ("Crash server path is not set." )
66
72
return
73
+
74
+ # Create uncore json file
75
+ self .create_json_file (corefile , binary , gdb_settings )
76
+
77
+ # Create a named pipe
78
+ pipe_path = os .path .join (cwd , "gdb_lldb_signal_pipe" )
79
+ self .create_pipe (pipe_path )
67
80
68
81
# Start Crash server as a subprocess
69
82
port = self .find_free_port ()
70
83
subprocess .Popen ([lldb_crash_server_path , "g" , f"localhost:{ port } " , binary ,
71
84
"--uncore-json" , "./uncore.json" ], stdout = STDOUT_FILENO , stderr = STDERR_FILENO )
72
85
86
+ # Read from a named pipe (waiting for a signal from Crash server)
87
+ self .read_from_pipe (pipe_path )
88
+
73
89
# Connect to remote target
74
90
gdb .execute (f"target remote localhost:{ port } " )
75
91
@@ -98,6 +114,92 @@ def find_free_port(self):
98
114
s .bind (('localhost' , 0 ))
99
115
return s .getsockname ()[1 ]
100
116
117
+ def create_json_file (self , core_path , binary_path , gdb_settings ):
118
+ global cwd
119
+
120
+ # Set the uncore output and json file path
121
+ output_path = os .path .join (cwd , "outdir" )
122
+ json_path = os .path .join (cwd , "uncore.json" )
123
+
124
+ data = {
125
+ "core" : core_path ,
126
+ "binos_root" : "" ,
127
+ "entry_point" : "main" ,
128
+ "prog" : binary_path ,
129
+ "pid" : "" ,
130
+ "additional_resources" : "" ,
131
+ "custom_o_file" : [],
132
+ "output_directory" : output_path ,
133
+ "hot_patch" : {
134
+ },
135
+ "gdbparse" : {
136
+ "gdb" : "gdb" ,
137
+ "args" : [
138
+ binary_path ,
139
+ core_path
140
+ ],
141
+ "kwargs" : {
142
+ }
143
+ }
144
+ }
145
+
146
+ if len (gdb_settings ) > 1 :
147
+ # Create a gdb script with all commands from gdb_settings
148
+ script_path = os .path .join (cwd , "gdb_uncore.script" )
149
+ with open (script_path , "w" ) as script_file :
150
+ for cmd in gdb_settings :
151
+ script_file .write (cmd + "\n " )
152
+ data ["gdbparse" ]["kwargs" ]["-x" ] = script_path
153
+ elif len (gdb_settings ) == 1 :
154
+ data ["gdbparse" ]["kwargs" ]["-ex" ] = gdb_settings [0 ]
155
+
156
+ # Convert the JSON data to a string
157
+ json_str = json .dumps (data , indent = 4 )
158
+
159
+ # Write the JSON string to a file in the current working directory
160
+ try :
161
+ with open (json_path , "w" ) as json_file :
162
+ json_file .write (json_str )
163
+ print (f"JSON file successfully created at: { json_path } " )
164
+ except Exception as e :
165
+ print (f"Error: { e } " )
166
+
167
+ # Get gdb session settings
168
+ def get_gdb_setting (self ):
169
+ gdb_settings = []
170
+
171
+ # Retrieve the process sysroot setting
172
+ sysroot_setting = gdb .execute ("show sysroot" , to_string = True )
173
+ if sysroot_setting .startswith ("The current system root is" ):
174
+ sysroot_path = sysroot_setting .split ('"' )[1 ].strip ()
175
+ if sysroot_path != "target:" and sysroot_path != "" :
176
+ gdb_settings .append (f"set sysroot { sysroot_path } " )
177
+
178
+ # Retrieve and process solib-search-path setting
179
+ solib_search_path_setting = gdb .execute ("show solib-search-path" , to_string = True )
180
+ if solib_search_path_setting .startswith ("The search path for loading non-absolute shared library symbol files is" ):
181
+ solib_search_path = solib_search_path_setting .split (' is ' )[1 ].strip ()
182
+ if solib_search_path != "." and solib_search_path != "" :
183
+ gdb_settings .append (f"set solib-search-path { solib_search_path } " )
184
+
185
+ return gdb_settings
186
+
187
+ # Create a named FIFO pipe
188
+ def create_pipe (self , pipe_path ):
189
+ try :
190
+ os .mkfifo (pipe_path )
191
+ except OSError as e :
192
+ print (f"Error creating the pipe: { e } " )
193
+
194
+ # Read from the named pipe
195
+ def read_from_pipe (self , pipe_path ):
196
+ try :
197
+ with open (pipe_path , "r" ) as pipe :
198
+ message = pipe .read ()
199
+ print (message )
200
+ except Exception as e :
201
+ print (f"Error reading from the pipe: { e } " )
202
+
101
203
102
204
class RestoreRegisters (gdb .Command ):
103
205
"""Restore registers to saved values from either corefile or live process."""
0 commit comments