Skip to content

Commit 455bcb1

Browse files
committed
f
1 parent 6beda4a commit 455bcb1

File tree

8 files changed

+94
-115
lines changed

8 files changed

+94
-115
lines changed

CHANGELOG

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Unreleased:
1919
starts daemons by default. Daemons must be running before capturing a
2020
trace. We recommend using the new `tracebox ctl` applet (e.g. `tracebox
2121
ctl start`) to manage persistent daemons. To restore the previous
22-
self-contained behavior, use the `--legacy` flag.
22+
self-contained behavior, use the `--autodaemonize` flag.
2323

2424
v53.0 - 2025-11-12:
2525
SDK:

docs/getting-started/ftrace.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,9 @@ from the command line using the config file we just created:
289289
./tracebox -c ticker.cfg --txt -o ticker.pftrace
290290
```
291291

292-
Alternatively using legacy mode:
292+
Alternatively using autodaemonize mode:
293293
```bash
294-
./tracebox --legacy -c ticker.cfg --txt -o ticker.pftrace
294+
./tracebox --autodaemonize -c ticker.cfg --txt -o ticker.pftrace
295295
```
296296

297297
Note: tracebox will take care of enabling tracing and our ticker events (as we

docs/getting-started/memory-profiling.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ Generate the heapprofd config and start the tracing session.
184184
./heap_profile -n trace_processor_shell --print-config | \
185185
./tracebox -c - --txt -o ~/trace_processor_memory.pftrace
186186
```
187-
Alternatively, use the legacy self-contained mode (no `ctl start` required):
187+
Alternatively, use the autodaemonize mode (no `ctl start` required):
188188
```bash
189189
./heap_profile -n trace_processor_shell --print-config | \
190-
./tracebox --legacy -c - --txt -o ~/trace_processor_memory.pftrace
190+
./tracebox --autodaemonize -c - --txt -o ~/trace_processor_memory.pftrace
191191
```
192192

193193
Open another terminal (or tab), start the process you want to profile,

docs/getting-started/system-tracing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ and running the following command:
174174
The scheduling information is captured using ftrace, so you may need to start
175175
the `tracebox` daemons with root privileges (e.g. `sudo ./tracebox ctl start`).
176176

177-
If you prefer the old behavior where tracebox starts temporary daemons for the duration of the trace, you can use the `--legacy` flag:
177+
If you prefer the old behavior where tracebox starts temporary daemons for the duration of the trace, you can use the `--autodaemonize` flag:
178178
```bash
179-
./tracebox --legacy -o trace_file.perfetto-trace --txt -c scheduling.cfg
179+
./tracebox --autodaemonize -o trace_file.perfetto-trace --txt -c scheduling.cfg
180180
```
181181

182182
</tabs?>

docs/reference/tracebox.md

Lines changed: 30 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ tracebox ctl start
1212
tracebox -t 10s -o trace.pftrace sched freq
1313
tracebox ctl stop
1414

15-
# Self-contained mode (legacy)
16-
tracebox --legacy -t 10s -o trace.pftrace sched
15+
# Autodaemonize mode (self-contained)
16+
tracebox --autodaemonize -t 10s -o trace.pftrace sched
1717

1818
# Invoke bundled applets
1919
tracebox [applet_name] [args ...]
@@ -24,15 +24,9 @@ tracebox [applet_name] [args ...]
2424
`tracebox` bundles the Perfetto tracing service (`traced`), system probes
2525
(`traced_probes`), and the `perfetto` command-line client into a single binary.
2626

27-
**Behavior change (2025):** `tracebox` now requires daemons to be running before
28-
capturing traces. Use `tracebox ctl` to manage daemons, or `--legacy` for
29-
the legacy self-contained mode.
30-
31-
If you are a user of `tracebox` in its default mode (i.e., you don't use
32-
`--legacy`), you should read the migration guide below. The new default
33-
behavior provides a more robust and predictable experience, especially for
34-
processes instrumented with the Perfetto SDK, but it requires a small change to
35-
your workflow.
27+
**Update (2025):** `tracebox` now prefers daemons to be managed explicitly via
28+
`tracebox ctl` for better SDK compatibility and performance. The classic
29+
self-contained behavior is still available via the `--autodaemonize` flag.
3630

3731
## DAEMON MANAGEMENT
3832

@@ -74,29 +68,29 @@ If systemd service files are detected:
7468
- As root: `ctl start` uses `systemctl start traced traced-probes`
7569
- As user: Instructs to use `sudo systemctl start traced traced-probes`
7670

77-
## LEGACY SELF-CONTAINED MODE
71+
## AUTODAEMONIZE MODE
7872

79-
The `--legacy` flag preserves the legacy behavior of `tracebox` where it
80-
spawns temporary daemons for the duration of a single trace session. This mode
81-
uses private, temporary sockets and is fully self-contained.
73+
The `--autodaemonize` flag (formerly default behavior) spawns temporary daemons for the
74+
duration of a single trace session. This mode uses private, temporary sockets
75+
and is fully self-contained.
8276

8377
**When to use:**
84-
- For quick one-off traces where you don't want to manage daemons.
78+
- For quick one-off traces where setup simplicity is prioritized over performance.
8579
- If you cannot or do not want to run persistent background daemons.
86-
- For scripts that rely on the old behavior and haven't been migrated yet.
80+
- For existing scripts or workflows that expect self-contained execution.
8781

8882
**Limitations:**
8983
- **SDK Incompatibility:** Processes instrumented with the Perfetto SDK (e.g.
9084
using `track_event`) will generally fail to connect to these temporary daemons
9185
because they listen on private, unpredictable socket addresses.
9286
- **Inefficiency:** Spawns and kills daemons for every trace, which is slower
9387
for repeated tracing.
94-
- **Concurrency:** Running multiple instances of `tracebox --legacy`
88+
- **Concurrency:** Running multiple instances of `tracebox --autodaemonize`
9589
concurrently can lead to resource conflicts (e.g. with ftrace).
9690

9791
**Example:**
9892
```bash
99-
tracebox --legacy -t 10s -o trace.pftrace sched freq
93+
tracebox --autodaemonize -t 10s -o trace.pftrace sched freq
10094
```
10195

10296
## APPLET MODE
@@ -130,7 +124,7 @@ tracebox ctl status # Check daemon status
130124

131125
If daemons aren't running:
132126
- Use `tracebox ctl start` to start them
133-
- Or use `--legacy` for self-contained mode
127+
- Or use `--autodaemonize` for self-contained mode
134128

135129
### Permission denied
136130

@@ -171,43 +165,22 @@ SDK (e.g., Chrome, or your own apps) would fail to connect to these hidden
171165
daemons, resulting in traces missing all userspace data. The new model ensures
172166
daemons run on standard system sockets where SDK apps can find them.
173167

174-
### Migration Steps
175-
176-
**Scenario 1: You run interactive traces manually**
177-
178-
* **Old way:**
179-
```bash
180-
tracebox -t 10s -o trace.pftrace sched
181-
```
182-
* **New way (Recommended):**
183-
Run this once per boot (or when needed):
184-
```bash
185-
tracebox ctl start
186-
```
187-
Then run your traces as usual:
188-
```bash
189-
tracebox -t 10s -o trace.pftrace sched
190-
```
191-
When done (optional):
192-
```bash
193-
tracebox ctl stop
194-
```
195-
196-
**Scenario 2: You have scripts that wrap tracebox**
197-
198-
* **Option A (Better):** Update your scripts to ensure daemons are running
199-
using `tracebox ctl start` before tracing.
200-
* **Option B (Faster fix):** Update your scripts to use the `--legacy`
201-
flag to restore the old behavior.
202-
```bash
203-
tracebox --legacy -t 10s -o trace.pftrace sched
204-
```
205-
206-
**Scenario 3: You are tracing SDK-instrumented apps**
207-
208-
You **must** use the new explicit daemon mode. The legacy `--legacy` mode
209-
will likely not work because your app won't be able to discover the temporary
210-
sockets.
168+
`tracebox` supports two main workflows. Choose the one that best fits your needs.
169+
170+
### Option A: Persistent Daemons (Recommended)
171+
Best for: SDK tracing, repeated tracing, system-wide analysis.
172+
173+
1. Start daemons once: `tracebox ctl start`
174+
2. Record traces: `tracebox -t 10s ...`
175+
3. Stop (optional): `tracebox ctl stop`
176+
177+
### Option B: Autodaemonize Mode
178+
Best for: Quick ftrace-only debugging, scripts requiring self-contained binaries.
179+
180+
1. Record trace: `tracebox --autodaemonize -t 10s ...`
181+
182+
This mode behaves like older `tracebox` versions, spawning temporary daemons
183+
for the duration of the command.
211184

212185
## SEE ALSO
213186

src/tracebox/tracebox.cc

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,22 @@ void PrintTraceboxUsage() {
6060
printf(R"(Welcome to Perfetto tracing!
6161
6262
Tracebox is a bundle containing all the tracing services and the perfetto
63-
cmdline client in one binary.
63+
cmdline client in one binary. It can be used in two modes:
6464
65-
QUICK START (end-to-end workflow):
66-
tracebox ctl start # Start daemons
67-
tracebox -t 10s -o trace.pftrace sched # Capture trace
68-
tracebox ctl stop # Stop daemons (optional)
65+
MODE 1: Daemon mode (Recommended)
66+
Background daemons are started once and shared across multiple tracing sessions.
67+
This supports SDKs (track_event), reduces latency and is generally more robust.
68+
69+
> tracebox ctl start
70+
> tracebox -t 10s -o trace.pftrace sched
71+
> tracebox ctl stop
72+
73+
MODE 2: Autodaemonize mode
74+
Spawns temporary daemons only for the duration of the trace.
75+
Useful for quick ftrace debugging or self-contained scripts.
76+
Note: SDK apps (track_event) might not connect due to private sockets.
77+
78+
> tracebox --autodaemonize -t 10s -o trace.pftrace sched
6979
)");
7080

7181
PrintTraceboxCtlUsage();
@@ -75,13 +85,6 @@ QUICK START (end-to-end workflow):
7585
applets += " " + std::string(applet.name);
7686

7787
printf(R"(
78-
ADVANCED: --legacy (NOT RECOMMENDED)
79-
Example: tracebox --legacy -t 10s -o trace.pftrace sched
80-
81-
Spawns temporary daemons for one trace only.
82-
Limitations: SDK apps won't connect, inefficient for multiple traces.
83-
Use only for quick one-offs or when persistent daemons aren't feasible.
84-
8588
Available applets:%s
8689
8790
See also:
@@ -91,8 +94,9 @@ See also:
9194
applets.c_str());
9295
}
9396

94-
// Legacy mode: spawns temporary daemons with private sockets for one trace.
95-
int RunLegacy(int argc, char** argv) {
97+
// Autodaemonize mode: spawns temporary daemons with private sockets for one
98+
// trace.
99+
int RunAutodaemonize(int argc, char** argv) {
96100
auto* end = std::remove_if(argv, argv + argc, [](char* arg) {
97101
return !strcmp(arg, "--system-sockets");
98102
});
@@ -242,11 +246,11 @@ int TraceboxMain(int argc, char** argv) {
242246
return 1;
243247
}
244248

245-
bool legacy_mode = false;
249+
bool autodaemonize = false;
246250
bool use_system_sockets = false;
247251
for (int i = 1; i < argc;) {
248-
if (strcmp(argv[i], "--legacy") == 0) {
249-
legacy_mode = true;
252+
if (strcmp(argv[i], "--autodaemonize") == 0) {
253+
autodaemonize = true;
250254
memmove(static_cast<void*>(&argv[i]), static_cast<void*>(&argv[i + 1]),
251255
sizeof(char*) * static_cast<size_t>(argc - i - 1));
252256
argc--;
@@ -258,39 +262,41 @@ int TraceboxMain(int argc, char** argv) {
258262
}
259263
}
260264

261-
if (legacy_mode) {
265+
if (autodaemonize) {
266+
// If --system-sockets is passed with --autodaemonize, it's a valid (though
267+
// slightly contradictory in name) way to say "spawn daemons but use public
268+
// sockets". We warn if they try to mix them in a way that suggests they
269+
// expect the old default behavior without the flag.
262270
if (use_system_sockets) {
263271
PERFETTO_ELOG(
264-
"Legacy mode using system sockets is deprecated. Please prefer to "
265-
"start daemons separately with `tracebox ctl start` for better "
266-
"stability and compatibility. See "
267-
"https://perfetto.dev/docs/reference/tracebox for details.");
268-
} else {
269-
PERFETTO_ELOG(
270-
"Using legacy autodaemonize mode. This is fine for quick "
271-
"one-off traces, but for production use or SDK compatibility, please "
272-
"use `tracebox ctl start` followed by `tracebox`. See "
273-
"https://perfetto.dev/docs/reference/tracebox for details.");
272+
"Warning: --system-sockets with --autodaemonize is supported but "
273+
"deprecated. Prefer `tracebox ctl start` for persistent daemons.");
274274
}
275-
return RunLegacy(argc, argv);
275+
// We don't warn for plain --autodaemonize as it's a valid mode.
276+
return RunAutodaemonize(argc, argv);
276277
}
277278

278279
if (use_system_sockets) {
279280
PERFETTO_FATAL(
280-
"System sockets is not a valid option for the new mode. By default "
281-
"tracebox uses system sockets. If you want the old self-contained "
282-
"behavior (autostarting daemons), append --legacy.");
281+
"System sockets is the default. If you want the old self-contained "
282+
"behavior (spawning temporary daemons), use --autodaemonize.");
283283
}
284284

285285
ServiceSockets sockets = perfetto::GetRunningSockets();
286286
if (!sockets.IsValid()) {
287-
fprintf(stderr,
288-
"Error: Perfetto tracing daemons (traced, traced_probes) are not "
289-
"running.\n"
290-
"- To run daemons as the current user: `tracebox ctl start`\n"
291-
"- For a self-contained run: `tracebox --legacy ...` (Not "
292-
"Recommended)\n"
293-
"More info at: https://perfetto.dev/docs/reference/tracebox\n");
287+
fprintf(
288+
stderr,
289+
"Error: Perfetto tracing daemons (traced, traced_probes) are not "
290+
"running.\n\n"
291+
"Tracebox behavior has changed. It no longer spawns temporary daemons "
292+
"by default.\n"
293+
"You have two options:\n"
294+
"1. Start the daemons manually (Recommended):\n"
295+
" tracebox ctl start\n"
296+
" tracebox ...\n\n"
297+
"2. Use the --autodaemonize flag for the old behavior:\n"
298+
" tracebox --autodaemonize ...\n"
299+
"\nMore info at: https://perfetto.dev/docs/reference/tracebox\n");
294300
return 1;
295301
}
296302
perfetto::SetServiceSocketEnv(sockets);

src/tracebox/tracebox_ctl.cc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
namespace perfetto {
4646
namespace {
4747

48+
// Environment variables for socket paths.
49+
constexpr char kPerfettoProducerSockEnv[] = "PERFETTO_PRODUCER_SOCK_NAME";
50+
constexpr char kPerfettoConsumerSockEnv[] = "PERFETTO_CONSUMER_SOCK_NAME";
51+
4852
constexpr const char* kDaemons[] = {
4953
"traced",
5054
"traced_probes",
@@ -145,7 +149,7 @@ int CtlStop() {
145149
"to stop them.\n");
146150
return 1;
147151
#else
148-
printf("Stopping daemons...\n");
152+
PERFETTO_LOG("Stopping daemons...");
149153
bool found_any = false;
150154
bool all_stopped = true;
151155
for (const char* daemon : kDaemons) {
@@ -163,7 +167,7 @@ int CtlStop() {
163167
}
164168
}
165169
if (!found_any) {
166-
printf("No daemon PID files found.\n");
170+
PERFETTO_LOG("No daemon PID files found.");
167171
ServiceSockets sockets = GetRunningSockets();
168172
if (!sockets.IsValid()) {
169173
printf("No daemons detected.\n");
@@ -180,7 +184,7 @@ int CtlStop() {
180184
"traced-probes\n");
181185
return 0;
182186
}
183-
printf("Systemd service found. Trying to stop via systemctl...\n");
187+
PERFETTO_LOG("Systemd service found. Trying to stop via systemctl...");
184188
if (system("systemctl stop traced traced-probes") == 0) {
185189
printf("Daemons stopped.\n");
186190
return 0;
@@ -209,7 +213,7 @@ int CtlStart() {
209213
}
210214

211215
#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
212-
printf("Starting daemons...\n");
216+
PERFETTO_LOG("Starting daemons...");
213217
std::string tracebox_path = base::GetCurExecutablePath();
214218
std::vector<base::Subprocess> processes;
215219
for (const char* daemon : kDaemons) {
@@ -240,7 +244,7 @@ int CtlStart() {
240244
#else // Unix
241245
if (IsSystemdServiceInstalled()) {
242246
if (base::GetCurrentUserId() == 0) {
243-
printf("Starting daemons via systemd...\n");
247+
PERFETTO_LOG("Starting daemons via systemd...");
244248
if (system("systemctl start traced traced-probes") == 0) {
245249
ServiceSockets sys_sockets = GetRunningSockets();
246250
SetServiceSocketEnv(sys_sockets);
@@ -254,7 +258,7 @@ int CtlStart() {
254258
"systemctl start traced traced-probes");
255259
return 1;
256260
}
257-
printf("Starting daemons...\n");
261+
PERFETTO_LOG("Starting daemons...");
258262
std::string tracebox_path = base::GetCurExecutablePath();
259263
for (const char* daemon : kDaemons) {
260264
pid_t pid = StartDaemon(tracebox_path, daemon);
@@ -297,9 +301,9 @@ int CtlStatus() {
297301
pid_t pid = ReadPidFromFile(pid_path);
298302
if (pid > 0) {
299303
if (kill(pid, 0) == 0) {
300-
printf(" %-15s : Running (PID %d)\n", daemon, static_cast<int>(pid));
304+
printf(" %s : Running (PID %d)\n", daemon, static_cast<int>(pid));
301305
} else {
302-
printf(" %-15s : Not running (Stale PID file %d)\n", daemon,
306+
printf(" %s : Not running (Stale PID file %d)\n", daemon,
303307
static_cast<int>(pid));
304308
stale_found = true;
305309
}

0 commit comments

Comments
 (0)