@@ -48,6 +48,7 @@ static const char usage_str[] =
4848 " Common Options:\n "
4949#ifdef ENABLE_THREADS
5050 " --threads Enable multi-threading support\n "
51+ " --only=IDENT Only trace a thread identified by IDENT (requires --threads)\n "
5152 " -d, --dump Dump stacks from all threads (implies --threads)\n "
5253#else
5354 " -d, --dump Dump the current interpreter stack\n "
@@ -189,6 +190,7 @@ int Prober::ParseOpts(int argc, char **argv) {
189190 {" seconds" , required_argument, 0 , ' s' },
190191#if ENABLE_THREADS
191192 {" threads" , no_argument, 0 , ' L' },
193+ {" only" , required_argument, 0 , ' i' },
192194#endif
193195 {" no-line-numbers" , no_argument, 0 , ' n' },
194196 {" output" , required_argument, 0 , ' o' },
@@ -238,7 +240,10 @@ int Prober::ParseOpts(int argc, char **argv) {
238240 std::cout << PYFLAME_VERSION_STR << " \n\n " << usage_str;
239241 return 0 ;
240242 break ;
241- #ifdef ENABLE_THREADS
243+ #if ENABLE_THREADS
244+ case ' i' :
245+ thread_id_ = std::stoul (optarg);
246+ break ;
242247 case ' L' :
243248 enable_threads_ = true ;
244249 break ;
@@ -309,6 +314,11 @@ int Prober::ParseOpts(int argc, char **argv) {
309314 std::cerr << " WARNING: Specifying a PID to trace without -p is deprecated; "
310315 " see Pyflame issue #99 for details.\n " ;
311316 }
317+ if (thread_id_ > 0 && !enable_threads_) {
318+ std::cerr << " Option --only requires --threads.\n " ;
319+ std::cerr << usage_str;
320+ return 1 ;
321+ }
312322 interval_ = ToMicroseconds (sample_rate_);
313323 return -1 ;
314324}
@@ -416,7 +426,9 @@ int Prober::ProbeLoop(const PyFrob &frobber, std::ostream *out) {
416426 }
417427
418428 for (const auto &thread : threads) {
419- call_stacks.push_back ({now, thread.frames ()});
429+ if (thread_id_ == 0 || thread.id () == thread_id_) {
430+ call_stacks.push_back ({now, thread.frames ()});
431+ }
420432 }
421433
422434 if (check_end && (now + interval_ >= end)) {
0 commit comments