cs-etm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * Copyright(C) 2015 Linaro Limited. All rights reserved.
  3. * Author: Mathieu Poirier <mathieu.poirier@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <api/fs/fs.h>
  18. #include <linux/bitops.h>
  19. #include <linux/coresight-pmu.h>
  20. #include <linux/kernel.h>
  21. #include <linux/log2.h>
  22. #include <linux/types.h>
  23. #include "cs-etm.h"
  24. #include "../../perf.h"
  25. #include "../../util/auxtrace.h"
  26. #include "../../util/cpumap.h"
  27. #include "../../util/evlist.h"
  28. #include "../../util/evsel.h"
  29. #include "../../util/pmu.h"
  30. #include "../../util/thread_map.h"
  31. #include "../../util/cs-etm.h"
  32. #include <stdlib.h>
  33. #define ENABLE_SINK_MAX 128
  34. #define CS_BUS_DEVICE_PATH "/bus/coresight/devices/"
  35. struct cs_etm_recording {
  36. struct auxtrace_record itr;
  37. struct perf_pmu *cs_etm_pmu;
  38. struct perf_evlist *evlist;
  39. bool snapshot_mode;
  40. size_t snapshot_size;
  41. };
  42. static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu);
  43. static int cs_etm_parse_snapshot_options(struct auxtrace_record *itr,
  44. struct record_opts *opts,
  45. const char *str)
  46. {
  47. struct cs_etm_recording *ptr =
  48. container_of(itr, struct cs_etm_recording, itr);
  49. unsigned long long snapshot_size = 0;
  50. char *endptr;
  51. if (str) {
  52. snapshot_size = strtoull(str, &endptr, 0);
  53. if (*endptr || snapshot_size > SIZE_MAX)
  54. return -1;
  55. }
  56. opts->auxtrace_snapshot_mode = true;
  57. opts->auxtrace_snapshot_size = snapshot_size;
  58. ptr->snapshot_size = snapshot_size;
  59. return 0;
  60. }
  61. static int cs_etm_recording_options(struct auxtrace_record *itr,
  62. struct perf_evlist *evlist,
  63. struct record_opts *opts)
  64. {
  65. struct cs_etm_recording *ptr =
  66. container_of(itr, struct cs_etm_recording, itr);
  67. struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
  68. struct perf_evsel *evsel, *cs_etm_evsel = NULL;
  69. const struct cpu_map *cpus = evlist->cpus;
  70. bool privileged = (geteuid() == 0 || perf_event_paranoid() < 0);
  71. ptr->evlist = evlist;
  72. ptr->snapshot_mode = opts->auxtrace_snapshot_mode;
  73. evlist__for_each_entry(evlist, evsel) {
  74. if (evsel->attr.type == cs_etm_pmu->type) {
  75. if (cs_etm_evsel) {
  76. pr_err("There may be only one %s event\n",
  77. CORESIGHT_ETM_PMU_NAME);
  78. return -EINVAL;
  79. }
  80. evsel->attr.freq = 0;
  81. evsel->attr.sample_period = 1;
  82. cs_etm_evsel = evsel;
  83. opts->full_auxtrace = true;
  84. }
  85. }
  86. /* no need to continue if at least one event of interest was found */
  87. if (!cs_etm_evsel)
  88. return 0;
  89. if (opts->use_clockid) {
  90. pr_err("Cannot use clockid (-k option) with %s\n",
  91. CORESIGHT_ETM_PMU_NAME);
  92. return -EINVAL;
  93. }
  94. /* we are in snapshot mode */
  95. if (opts->auxtrace_snapshot_mode) {
  96. /*
  97. * No size were given to '-S' or '-m,', so go with
  98. * the default
  99. */
  100. if (!opts->auxtrace_snapshot_size &&
  101. !opts->auxtrace_mmap_pages) {
  102. if (privileged) {
  103. opts->auxtrace_mmap_pages = MiB(4) / page_size;
  104. } else {
  105. opts->auxtrace_mmap_pages =
  106. KiB(128) / page_size;
  107. if (opts->mmap_pages == UINT_MAX)
  108. opts->mmap_pages = KiB(256) / page_size;
  109. }
  110. } else if (!opts->auxtrace_mmap_pages && !privileged &&
  111. opts->mmap_pages == UINT_MAX) {
  112. opts->mmap_pages = KiB(256) / page_size;
  113. }
  114. /*
  115. * '-m,xyz' was specified but no snapshot size, so make the
  116. * snapshot size as big as the auxtrace mmap area.
  117. */
  118. if (!opts->auxtrace_snapshot_size) {
  119. opts->auxtrace_snapshot_size =
  120. opts->auxtrace_mmap_pages * (size_t)page_size;
  121. }
  122. /*
  123. * -Sxyz was specified but no auxtrace mmap area, so make the
  124. * auxtrace mmap area big enough to fit the requested snapshot
  125. * size.
  126. */
  127. if (!opts->auxtrace_mmap_pages) {
  128. size_t sz = opts->auxtrace_snapshot_size;
  129. sz = round_up(sz, page_size) / page_size;
  130. opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
  131. }
  132. /* Snapshost size can't be bigger than the auxtrace area */
  133. if (opts->auxtrace_snapshot_size >
  134. opts->auxtrace_mmap_pages * (size_t)page_size) {
  135. pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
  136. opts->auxtrace_snapshot_size,
  137. opts->auxtrace_mmap_pages * (size_t)page_size);
  138. return -EINVAL;
  139. }
  140. /* Something went wrong somewhere - this shouldn't happen */
  141. if (!opts->auxtrace_snapshot_size ||
  142. !opts->auxtrace_mmap_pages) {
  143. pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
  144. return -EINVAL;
  145. }
  146. }
  147. /* We are in full trace mode but '-m,xyz' wasn't specified */
  148. if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
  149. if (privileged) {
  150. opts->auxtrace_mmap_pages = MiB(4) / page_size;
  151. } else {
  152. opts->auxtrace_mmap_pages = KiB(128) / page_size;
  153. if (opts->mmap_pages == UINT_MAX)
  154. opts->mmap_pages = KiB(256) / page_size;
  155. }
  156. }
  157. /* Validate auxtrace_mmap_pages provided by user */
  158. if (opts->auxtrace_mmap_pages) {
  159. unsigned int max_page = (KiB(128) / page_size);
  160. size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
  161. if (!privileged &&
  162. opts->auxtrace_mmap_pages > max_page) {
  163. opts->auxtrace_mmap_pages = max_page;
  164. pr_err("auxtrace too big, truncating to %d\n",
  165. max_page);
  166. }
  167. if (!is_power_of_2(sz)) {
  168. pr_err("Invalid mmap size for %s: must be a power of 2\n",
  169. CORESIGHT_ETM_PMU_NAME);
  170. return -EINVAL;
  171. }
  172. }
  173. if (opts->auxtrace_snapshot_mode)
  174. pr_debug2("%s snapshot size: %zu\n", CORESIGHT_ETM_PMU_NAME,
  175. opts->auxtrace_snapshot_size);
  176. if (cs_etm_evsel) {
  177. /*
  178. * To obtain the auxtrace buffer file descriptor, the auxtrace
  179. * event must come first.
  180. */
  181. perf_evlist__to_front(evlist, cs_etm_evsel);
  182. /*
  183. * In the case of per-cpu mmaps, we need the CPU on the
  184. * AUX event.
  185. */
  186. if (!cpu_map__empty(cpus))
  187. perf_evsel__set_sample_bit(cs_etm_evsel, CPU);
  188. }
  189. /* Add dummy event to keep tracking */
  190. if (opts->full_auxtrace) {
  191. struct perf_evsel *tracking_evsel;
  192. int err;
  193. err = parse_events(evlist, "dummy:u", NULL);
  194. if (err)
  195. return err;
  196. tracking_evsel = perf_evlist__last(evlist);
  197. perf_evlist__set_tracking_event(evlist, tracking_evsel);
  198. tracking_evsel->attr.freq = 0;
  199. tracking_evsel->attr.sample_period = 1;
  200. /* In per-cpu case, always need the time of mmap events etc */
  201. if (!cpu_map__empty(cpus))
  202. perf_evsel__set_sample_bit(tracking_evsel, TIME);
  203. }
  204. return 0;
  205. }
  206. static u64 cs_etm_get_config(struct auxtrace_record *itr)
  207. {
  208. u64 config = 0;
  209. struct cs_etm_recording *ptr =
  210. container_of(itr, struct cs_etm_recording, itr);
  211. struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
  212. struct perf_evlist *evlist = ptr->evlist;
  213. struct perf_evsel *evsel;
  214. evlist__for_each_entry(evlist, evsel) {
  215. if (evsel->attr.type == cs_etm_pmu->type) {
  216. /*
  217. * Variable perf_event_attr::config is assigned to
  218. * ETMv3/PTM. The bit fields have been made to match
  219. * the ETMv3.5 ETRMCR register specification. See the
  220. * PMU_FORMAT_ATTR() declarations in
  221. * drivers/hwtracing/coresight/coresight-perf.c for
  222. * details.
  223. */
  224. config = evsel->attr.config;
  225. break;
  226. }
  227. }
  228. return config;
  229. }
  230. static size_t
  231. cs_etm_info_priv_size(struct auxtrace_record *itr __maybe_unused,
  232. struct perf_evlist *evlist __maybe_unused)
  233. {
  234. int i;
  235. int etmv3 = 0, etmv4 = 0;
  236. const struct cpu_map *cpus = evlist->cpus;
  237. /* cpu map is not empty, we have specific CPUs to work with */
  238. if (!cpu_map__empty(cpus)) {
  239. for (i = 0; i < cpu_map__nr(cpus); i++) {
  240. if (cs_etm_is_etmv4(itr, cpus->map[i]))
  241. etmv4++;
  242. else
  243. etmv3++;
  244. }
  245. } else {
  246. /* get configuration for all CPUs in the system */
  247. for (i = 0; i < cpu__max_cpu(); i++) {
  248. if (cs_etm_is_etmv4(itr, i))
  249. etmv4++;
  250. else
  251. etmv3++;
  252. }
  253. }
  254. return (CS_ETM_HEADER_SIZE +
  255. (etmv4 * CS_ETMV4_PRIV_SIZE) +
  256. (etmv3 * CS_ETMV3_PRIV_SIZE));
  257. }
  258. static const char *metadata_etmv3_ro[CS_ETM_PRIV_MAX] = {
  259. [CS_ETM_ETMCCER] = "mgmt/etmccer",
  260. [CS_ETM_ETMIDR] = "mgmt/etmidr",
  261. };
  262. static const char *metadata_etmv4_ro[CS_ETMV4_PRIV_MAX] = {
  263. [CS_ETMV4_TRCIDR0] = "trcidr/trcidr0",
  264. [CS_ETMV4_TRCIDR1] = "trcidr/trcidr1",
  265. [CS_ETMV4_TRCIDR2] = "trcidr/trcidr2",
  266. [CS_ETMV4_TRCIDR8] = "trcidr/trcidr8",
  267. [CS_ETMV4_TRCAUTHSTATUS] = "mgmt/trcauthstatus",
  268. };
  269. static bool cs_etm_is_etmv4(struct auxtrace_record *itr, int cpu)
  270. {
  271. bool ret = false;
  272. char path[PATH_MAX];
  273. int scan;
  274. unsigned int val;
  275. struct cs_etm_recording *ptr =
  276. container_of(itr, struct cs_etm_recording, itr);
  277. struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
  278. /* Take any of the RO files for ETMv4 and see if it present */
  279. snprintf(path, PATH_MAX, "cpu%d/%s",
  280. cpu, metadata_etmv4_ro[CS_ETMV4_TRCIDR0]);
  281. scan = perf_pmu__scan_file(cs_etm_pmu, path, "%x", &val);
  282. /* The file was read successfully, we have a winner */
  283. if (scan == 1)
  284. ret = true;
  285. return ret;
  286. }
  287. static int cs_etm_get_ro(struct perf_pmu *pmu, int cpu, const char *path)
  288. {
  289. char pmu_path[PATH_MAX];
  290. int scan;
  291. unsigned int val = 0;
  292. /* Get RO metadata from sysfs */
  293. snprintf(pmu_path, PATH_MAX, "cpu%d/%s", cpu, path);
  294. scan = perf_pmu__scan_file(pmu, pmu_path, "%x", &val);
  295. if (scan != 1)
  296. pr_err("%s: error reading: %s\n", __func__, pmu_path);
  297. return val;
  298. }
  299. static void cs_etm_get_metadata(int cpu, u32 *offset,
  300. struct auxtrace_record *itr,
  301. struct auxtrace_info_event *info)
  302. {
  303. u32 increment;
  304. u64 magic;
  305. struct cs_etm_recording *ptr =
  306. container_of(itr, struct cs_etm_recording, itr);
  307. struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
  308. /* first see what kind of tracer this cpu is affined to */
  309. if (cs_etm_is_etmv4(itr, cpu)) {
  310. magic = __perf_cs_etmv4_magic;
  311. /* Get trace configuration register */
  312. info->priv[*offset + CS_ETMV4_TRCCONFIGR] =
  313. cs_etm_get_config(itr);
  314. /* Get traceID from the framework */
  315. info->priv[*offset + CS_ETMV4_TRCTRACEIDR] =
  316. coresight_get_trace_id(cpu);
  317. /* Get read-only information from sysFS */
  318. info->priv[*offset + CS_ETMV4_TRCIDR0] =
  319. cs_etm_get_ro(cs_etm_pmu, cpu,
  320. metadata_etmv4_ro[CS_ETMV4_TRCIDR0]);
  321. info->priv[*offset + CS_ETMV4_TRCIDR1] =
  322. cs_etm_get_ro(cs_etm_pmu, cpu,
  323. metadata_etmv4_ro[CS_ETMV4_TRCIDR1]);
  324. info->priv[*offset + CS_ETMV4_TRCIDR2] =
  325. cs_etm_get_ro(cs_etm_pmu, cpu,
  326. metadata_etmv4_ro[CS_ETMV4_TRCIDR2]);
  327. info->priv[*offset + CS_ETMV4_TRCIDR8] =
  328. cs_etm_get_ro(cs_etm_pmu, cpu,
  329. metadata_etmv4_ro[CS_ETMV4_TRCIDR8]);
  330. info->priv[*offset + CS_ETMV4_TRCAUTHSTATUS] =
  331. cs_etm_get_ro(cs_etm_pmu, cpu,
  332. metadata_etmv4_ro
  333. [CS_ETMV4_TRCAUTHSTATUS]);
  334. /* How much space was used */
  335. increment = CS_ETMV4_PRIV_MAX;
  336. } else {
  337. magic = __perf_cs_etmv3_magic;
  338. /* Get configuration register */
  339. info->priv[*offset + CS_ETM_ETMCR] = cs_etm_get_config(itr);
  340. /* Get traceID from the framework */
  341. info->priv[*offset + CS_ETM_ETMTRACEIDR] =
  342. coresight_get_trace_id(cpu);
  343. /* Get read-only information from sysFS */
  344. info->priv[*offset + CS_ETM_ETMCCER] =
  345. cs_etm_get_ro(cs_etm_pmu, cpu,
  346. metadata_etmv3_ro[CS_ETM_ETMCCER]);
  347. info->priv[*offset + CS_ETM_ETMIDR] =
  348. cs_etm_get_ro(cs_etm_pmu, cpu,
  349. metadata_etmv3_ro[CS_ETM_ETMIDR]);
  350. /* How much space was used */
  351. increment = CS_ETM_PRIV_MAX;
  352. }
  353. /* Build generic header portion */
  354. info->priv[*offset + CS_ETM_MAGIC] = magic;
  355. info->priv[*offset + CS_ETM_CPU] = cpu;
  356. /* Where the next CPU entry should start from */
  357. *offset += increment;
  358. }
  359. static int cs_etm_info_fill(struct auxtrace_record *itr,
  360. struct perf_session *session,
  361. struct auxtrace_info_event *info,
  362. size_t priv_size)
  363. {
  364. int i;
  365. u32 offset;
  366. u64 nr_cpu, type;
  367. const struct cpu_map *cpus = session->evlist->cpus;
  368. struct cs_etm_recording *ptr =
  369. container_of(itr, struct cs_etm_recording, itr);
  370. struct perf_pmu *cs_etm_pmu = ptr->cs_etm_pmu;
  371. if (priv_size != cs_etm_info_priv_size(itr, session->evlist))
  372. return -EINVAL;
  373. if (!session->evlist->nr_mmaps)
  374. return -EINVAL;
  375. /* If the cpu_map is empty all CPUs are involved */
  376. nr_cpu = cpu_map__empty(cpus) ? cpu__max_cpu() : cpu_map__nr(cpus);
  377. /* Get PMU type as dynamically assigned by the core */
  378. type = cs_etm_pmu->type;
  379. /* First fill out the session header */
  380. info->type = PERF_AUXTRACE_CS_ETM;
  381. info->priv[CS_HEADER_VERSION_0] = 0;
  382. info->priv[CS_PMU_TYPE_CPUS] = type << 32;
  383. info->priv[CS_PMU_TYPE_CPUS] |= nr_cpu;
  384. info->priv[CS_ETM_SNAPSHOT] = ptr->snapshot_mode;
  385. offset = CS_ETM_SNAPSHOT + 1;
  386. /* cpu map is not empty, we have specific CPUs to work with */
  387. if (!cpu_map__empty(cpus)) {
  388. for (i = 0; i < cpu_map__nr(cpus) && offset < priv_size; i++)
  389. cs_etm_get_metadata(cpus->map[i], &offset, itr, info);
  390. } else {
  391. /* get configuration for all CPUs in the system */
  392. for (i = 0; i < cpu__max_cpu(); i++)
  393. cs_etm_get_metadata(i, &offset, itr, info);
  394. }
  395. return 0;
  396. }
  397. static int cs_etm_find_snapshot(struct auxtrace_record *itr __maybe_unused,
  398. int idx, struct auxtrace_mmap *mm,
  399. unsigned char *data __maybe_unused,
  400. u64 *head, u64 *old)
  401. {
  402. pr_debug3("%s: mmap index %d old head %zu new head %zu size %zu\n",
  403. __func__, idx, (size_t)*old, (size_t)*head, mm->len);
  404. *old = *head;
  405. *head += mm->len;
  406. return 0;
  407. }
  408. static int cs_etm_snapshot_start(struct auxtrace_record *itr)
  409. {
  410. struct cs_etm_recording *ptr =
  411. container_of(itr, struct cs_etm_recording, itr);
  412. struct perf_evsel *evsel;
  413. evlist__for_each_entry(ptr->evlist, evsel) {
  414. if (evsel->attr.type == ptr->cs_etm_pmu->type)
  415. return perf_evsel__disable(evsel);
  416. }
  417. return -EINVAL;
  418. }
  419. static int cs_etm_snapshot_finish(struct auxtrace_record *itr)
  420. {
  421. struct cs_etm_recording *ptr =
  422. container_of(itr, struct cs_etm_recording, itr);
  423. struct perf_evsel *evsel;
  424. evlist__for_each_entry(ptr->evlist, evsel) {
  425. if (evsel->attr.type == ptr->cs_etm_pmu->type)
  426. return perf_evsel__enable(evsel);
  427. }
  428. return -EINVAL;
  429. }
  430. static u64 cs_etm_reference(struct auxtrace_record *itr __maybe_unused)
  431. {
  432. return (((u64) rand() << 0) & 0x00000000FFFFFFFFull) |
  433. (((u64) rand() << 32) & 0xFFFFFFFF00000000ull);
  434. }
  435. static void cs_etm_recording_free(struct auxtrace_record *itr)
  436. {
  437. struct cs_etm_recording *ptr =
  438. container_of(itr, struct cs_etm_recording, itr);
  439. free(ptr);
  440. }
  441. static int cs_etm_read_finish(struct auxtrace_record *itr, int idx)
  442. {
  443. struct cs_etm_recording *ptr =
  444. container_of(itr, struct cs_etm_recording, itr);
  445. struct perf_evsel *evsel;
  446. evlist__for_each_entry(ptr->evlist, evsel) {
  447. if (evsel->attr.type == ptr->cs_etm_pmu->type)
  448. return perf_evlist__enable_event_idx(ptr->evlist,
  449. evsel, idx);
  450. }
  451. return -EINVAL;
  452. }
  453. struct auxtrace_record *cs_etm_record_init(int *err)
  454. {
  455. struct perf_pmu *cs_etm_pmu;
  456. struct cs_etm_recording *ptr;
  457. cs_etm_pmu = perf_pmu__find(CORESIGHT_ETM_PMU_NAME);
  458. if (!cs_etm_pmu) {
  459. *err = -EINVAL;
  460. goto out;
  461. }
  462. ptr = zalloc(sizeof(struct cs_etm_recording));
  463. if (!ptr) {
  464. *err = -ENOMEM;
  465. goto out;
  466. }
  467. ptr->cs_etm_pmu = cs_etm_pmu;
  468. ptr->itr.parse_snapshot_options = cs_etm_parse_snapshot_options;
  469. ptr->itr.recording_options = cs_etm_recording_options;
  470. ptr->itr.info_priv_size = cs_etm_info_priv_size;
  471. ptr->itr.info_fill = cs_etm_info_fill;
  472. ptr->itr.find_snapshot = cs_etm_find_snapshot;
  473. ptr->itr.snapshot_start = cs_etm_snapshot_start;
  474. ptr->itr.snapshot_finish = cs_etm_snapshot_finish;
  475. ptr->itr.reference = cs_etm_reference;
  476. ptr->itr.free = cs_etm_recording_free;
  477. ptr->itr.read_finish = cs_etm_read_finish;
  478. *err = 0;
  479. return &ptr->itr;
  480. out:
  481. return NULL;
  482. }
  483. static FILE *cs_device__open_file(const char *name)
  484. {
  485. struct stat st;
  486. char path[PATH_MAX];
  487. const char *sysfs;
  488. sysfs = sysfs__mountpoint();
  489. if (!sysfs)
  490. return NULL;
  491. snprintf(path, PATH_MAX,
  492. "%s" CS_BUS_DEVICE_PATH "%s", sysfs, name);
  493. printf("path: %s\n", path);
  494. if (stat(path, &st) < 0)
  495. return NULL;
  496. return fopen(path, "w");
  497. }
  498. static __attribute__((format(printf, 2, 3)))
  499. int cs_device__print_file(const char *name, const char *fmt, ...)
  500. {
  501. va_list args;
  502. FILE *file;
  503. int ret = -EINVAL;
  504. va_start(args, fmt);
  505. file = cs_device__open_file(name);
  506. if (file) {
  507. ret = vfprintf(file, fmt, args);
  508. fclose(file);
  509. }
  510. va_end(args);
  511. return ret;
  512. }
  513. int cs_etm_set_drv_config(struct perf_evsel_config_term *term)
  514. {
  515. int ret;
  516. char enable_sink[ENABLE_SINK_MAX];
  517. snprintf(enable_sink, ENABLE_SINK_MAX, "%s/%s",
  518. term->val.drv_cfg, "enable_sink");
  519. ret = cs_device__print_file(enable_sink, "%d", 1);
  520. if (ret < 0)
  521. return ret;
  522. return 0;
  523. }