@@ -247,6 +247,8 @@ configure_settings(bool _init)
247
247
ROCPROFSYS_ROCM_VERSION_PATCH);
248
248
#endif
249
249
250
+ is_pre_attach_mode () = tim::get_env (" ROCPROFSYS_ATTACH" , false );
251
+
250
252
auto _config = *get_config_impl ();
251
253
252
254
// if using timemory, default to perfetto being off
@@ -258,6 +260,10 @@ configure_settings(bool _init)
258
260
auto _rocprofsys_debug = _config->get <bool >(" ROCPROFSYS_DEBUG" );
259
261
if (_rocprofsys_debug) tim::set_env (" TIMEMORY_DEBUG_SETTINGS" , " 1" , 0 );
260
262
263
+ ROCPROFSYS_CONFIG_SETTING (bool , " ROCPROFSYS_ATTACH" ,
264
+ " Determines if rocprofsys is launched in pre-attach mode." ,
265
+ false , " attach" );
266
+
261
267
ROCPROFSYS_CONFIG_SETTING (
262
268
std::string, " ROCPROFSYS_MODE" ,
263
269
" Data collection mode. Used to set default values for ROCPROFSYS_USE_* options. "
@@ -1184,18 +1190,73 @@ get_signal_handler()
1184
1190
return _v;
1185
1191
}
1186
1192
1193
+ std::atomic<signal_handler_t >&
1194
+ get_attach_signal_handler ()
1195
+ {
1196
+ static auto _v = std::atomic<signal_handler_t >{ nullptr };
1197
+ return _v;
1198
+ }
1199
+
1187
1200
void
1188
1201
rocprofsys_exit_action (int nsig)
1189
1202
{
1190
1203
tim::signals::block_signals (get_sampling_signals (),
1191
1204
tim::signals::sigmask_scope::process);
1192
1205
ROCPROFSYS_BASIC_PRINT (" Finalizing after signal %i :: %s\n " , nsig,
1193
1206
signal_settings::str (static_cast <sys_signal>(nsig)).c_str ());
1194
- auto _handler = get_signal_handler ().load ();
1195
- if (_handler) (*_handler)();
1207
+
1208
+ // Tim: Handles the case where State is never active i.e. attaching never occured.
1209
+ if (get_state () == State::Active)
1210
+ {
1211
+ auto _handler = get_signal_handler ().load ();
1212
+ if (_handler) (*_handler)();
1213
+ }
1196
1214
kill (process::get_id (), nsig);
1197
1215
}
1198
1216
1217
+ // Tim: This handles signals for triggering attach/detach. It prevents the process from
1218
+ // being killed at the end.
1219
+ void
1220
+ rocprofsys_attach_detach_action (int , siginfo_t *, void *)
1221
+ {
1222
+ if (!config::is_pre_attach_mode ())
1223
+ {
1224
+ ROCPROFSYS_BASIC_PRINT (R"(
1225
+ EEEEEEEEEEEEEEEEEEEEEERRRRRRRRRRRRRRRRR RRRRRRRRRRRRRRRRR OOOOOOOOO RRRRRRRRRRRRRRRRR
1226
+ E::::::::::::::::::::ER::::::::::::::::R R::::::::::::::::R OO:::::::::OO R::::::::::::::::R
1227
+ E::::::::::::::::::::ER::::::RRRRRR:::::R R::::::RRRRRR:::::R OO:::::::::::::OO R::::::RRRRRR:::::R
1228
+ EE::::::EEEEEEEEE::::ERR:::::R R:::::RRR:::::R R:::::RO:::::::OOO:::::::ORR:::::R R:::::R
1229
+ E:::::E EEEEEE R::::R R:::::R R::::R R:::::RO::::::O O::::::O R::::R R:::::R
1230
+ E:::::E R::::R R:::::R R::::R R:::::RO:::::O O:::::O R::::R R:::::R
1231
+ E::::::EEEEEEEEEE R::::RRRRRR:::::R R::::RRRRRR:::::R O:::::O O:::::O R::::RRRRRR:::::R
1232
+ E:::::::::::::::E R:::::::::::::RR R:::::::::::::RR O:::::O O:::::O R:::::::::::::RR
1233
+ E:::::::::::::::E R::::RRRRRR:::::R R::::RRRRRR:::::R O:::::O O:::::O R::::RRRRRR:::::R
1234
+ E::::::EEEEEEEEEE R::::R R:::::R R::::R R:::::RO:::::O O:::::O R::::R R:::::R
1235
+ E:::::E R::::R R:::::R R::::R R:::::RO:::::O O:::::O R::::R R:::::R
1236
+ E:::::E EEEEEE R::::R R:::::R R::::R R:::::RO::::::O O::::::O R::::R R:::::R
1237
+ EE::::::EEEEEEEE:::::ERR:::::R R:::::RRR:::::R R:::::RO:::::::OOO:::::::ORR:::::R R:::::R
1238
+ E::::::::::::::::::::ER::::::R R:::::RR::::::R R:::::R OO:::::::::::::OO R::::::R R:::::R
1239
+ E::::::::::::::::::::ER::::::R R:::::RR::::::R R:::::R OO:::::::::OO R::::::R R:::::R
1240
+ EEEEEEEEEEEEEEEEEEEEEERRRRRRRR RRRRRRRRRRRRRRR RRRRRRR OOOOOOOOO RRRRRRRR RRRRRR
1241
+
1242
+ Trying to attach to an uninitialized process. Only applications launched by rocprof-sys in
1243
+ pre-attach mode can be attached to.
1244
+
1245
+ To launch an application in pre-attach mode, either
1246
+
1247
+ - set env var `ROCPROFSYS_ATTACH = true` or,
1248
+ - launch the process with `rocprof-sys-sample --pre-attach <OPTIONS> -- <APP> <ARGS>`
1249
+
1250
+ Ignoring attach attempt and continuing...
1251
+ )" );
1252
+ return ;
1253
+ }
1254
+ tim::signals::block_signals (get_sampling_signals (),
1255
+ tim::signals::sigmask_scope::process);
1256
+ auto _handler = get_attach_signal_handler ().load ();
1257
+ if (_handler) (*_handler)();
1258
+ }
1259
+
1199
1260
void
1200
1261
rocprofsys_trampoline_handler (int _v)
1201
1262
{
@@ -1234,6 +1295,28 @@ set_signal_handler(signal_handler_t _func)
1234
1295
return get_signal_handler ().load ();
1235
1296
}
1236
1297
1298
+ signal_handler_t
1299
+ set_attach_signal_handler (signal_handler_t _func)
1300
+ {
1301
+ if (_func)
1302
+ {
1303
+ auto _handler = get_attach_signal_handler ().load (std::memory_order_relaxed);
1304
+ if (get_attach_signal_handler ().compare_exchange_strong (_handler, _func,
1305
+ std::memory_order_relaxed))
1306
+ {
1307
+ return _handler;
1308
+ }
1309
+ else
1310
+ {
1311
+ _handler = get_attach_signal_handler ().load (std::memory_order_seq_cst);
1312
+ get_attach_signal_handler ().store (_func);
1313
+ return _handler;
1314
+ }
1315
+ }
1316
+
1317
+ return get_attach_signal_handler ().load ();
1318
+ }
1319
+
1237
1320
void
1238
1321
configure_signal_handler (const std::shared_ptr<settings>& _config)
1239
1322
{
@@ -1266,6 +1349,15 @@ configure_signal_handler(const std::shared_ptr<settings>& _config)
1266
1349
_action.sa_handler = rocprofsys_trampoline_handler;
1267
1350
sigaction (_dyninst_trampoline_signal, &_action, nullptr );
1268
1351
}
1352
+
1353
+ // Set up custom signals handlers for detaching.
1354
+ // Avoid using timeory's signal handler because it kills the process.
1355
+ int DETACH_SIG = 10 ; // SIGUSR1
1356
+ struct sigaction sa;
1357
+ memset (&sa, 0 , sizeof (sa));
1358
+ sa.sa_sigaction = rocprofsys_attach_detach_action;
1359
+ sa.sa_flags = SA_SIGINFO;
1360
+ sigaction (DETACH_SIG, &sa, nullptr );
1269
1361
}
1270
1362
1271
1363
bool
@@ -1722,6 +1814,13 @@ is_attached()
1722
1814
return _v;
1723
1815
}
1724
1816
1817
+ bool &
1818
+ is_pre_attach_mode ()
1819
+ {
1820
+ static bool _v = false ;
1821
+ return _v;
1822
+ }
1823
+
1725
1824
bool &
1726
1825
is_binary_rewrite ()
1727
1826
{
0 commit comments