1 // SPDX-License-Identifier: GPL-2.0 2 #include "debug.h" 3 #include "dsos.h" 4 #include "dso.h" 5 #include "util.h" 6 #include "vdso.h" 7 #include "namespaces.h" 8 #include <errno.h> 9 #include <libgen.h> 10 #include <stdlib.h> 11 #include <string.h> 12 #include <symbol.h> // filename__read_build_id 13 #include <unistd.h> 14 15 void dsos__init(struct dsos *dsos) 16 { 17 init_rwsem(&dsos->lock); 18 19 dsos->cnt = 0; 20 dsos->allocated = 0; 21 dsos->dsos = NULL; 22 dsos->sorted = true; 23 } 24 25 static void dsos__purge(struct dsos *dsos) 26 { 27 down_write(&dsos->lock); 28 29 for (unsigned int i = 0; i < dsos->cnt; i++) { 30 struct dso *dso = dsos->dsos[i]; 31 32 dso__set_dsos(dso, NULL); 33 dso__put(dso); 34 } 35 36 zfree(&dsos->dsos); 37 dsos->cnt = 0; 38 dsos->allocated = 0; 39 dsos->sorted = true; 40 41 up_write(&dsos->lock); 42 } 43 44 void dsos__exit(struct dsos *dsos) 45 { 46 dsos__purge(dsos); 47 exit_rwsem(&dsos->lock); 48 } 49 50 51 static int __dsos__for_each_dso(struct dsos *dsos, 52 int (*cb)(struct dso *dso, void *data), 53 void *data) 54 { 55 for (unsigned int i = 0; i < dsos->cnt; i++) { 56 struct dso *dso = dsos->dsos[i]; 57 int err; 58 59 err = cb(dso, data); 60 if (err) 61 return err; 62 } 63 return 0; 64 } 65 66 struct dsos__read_build_ids_cb_args { 67 bool with_hits; 68 bool have_build_id; 69 }; 70 71 static int dsos__read_build_ids_cb(struct dso *dso, void *data) 72 { 73 struct dsos__read_build_ids_cb_args *args = data; 74 struct nscookie nsc; 75 struct build_id bid = { .size = 0, }; 76 77 if (args->with_hits && !dso__hit(dso) && !dso__is_vdso(dso)) 78 return 0; 79 if (dso__has_build_id(dso)) { 80 args->have_build_id = true; 81 return 0; 82 } 83 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); 84 if (filename__read_build_id(dso__long_name(dso), &bid) > 0) { 85 dso__set_build_id(dso, &bid); 86 args->have_build_id = true; 87 } else if (errno == ENOENT && dso__nsinfo(dso)) { 88 char *new_name = dso__filename_with_chroot(dso, dso__long_name(dso)); 89 90 if (new_name && filename__read_build_id(new_name, &bid) > 0) { 91 dso__set_build_id(dso, &bid); 92 args->have_build_id = true; 93 } 94 free(new_name); 95 } 96 nsinfo__mountns_exit(&nsc); 97 return 0; 98 } 99 100 bool dsos__read_build_ids(struct dsos *dsos, bool with_hits) 101 { 102 struct dsos__read_build_ids_cb_args args = { 103 .with_hits = with_hits, 104 .have_build_id = false, 105 }; 106 107 dsos__for_each_dso(dsos, dsos__read_build_ids_cb, &args); 108 return args.have_build_id; 109 } 110 111 static int __dso__cmp_long_name(const char *long_name, const struct dso_id *id, 112 const struct dso *b) 113 { 114 int rc = strcmp(long_name, dso__long_name(b)); 115 return rc ?: dso_id__cmp(id, dso__id_const(b)); 116 } 117 118 static int __dso__cmp_short_name(const char *short_name, const struct dso_id *id, 119 const struct dso *b) 120 { 121 int rc = strcmp(short_name, dso__short_name(b)); 122 return rc ?: dso_id__cmp(id, dso__id_const(b)); 123 } 124 125 static int dsos__cmp_long_name_id_short_name(const void *va, const void *vb) 126 { 127 const struct dso *a = *((const struct dso **)va); 128 const struct dso *b = *((const struct dso **)vb); 129 int rc = strcmp(dso__long_name(a), dso__long_name(b)); 130 131 if (!rc) { 132 rc = dso_id__cmp(dso__id_const(a), dso__id_const(b)); 133 if (!rc) 134 rc = strcmp(dso__short_name(a), dso__short_name(b)); 135 } 136 return rc; 137 } 138 139 struct dsos__key { 140 const char *long_name; 141 const struct dso_id *id; 142 }; 143 144 static int dsos__cmp_key_long_name_id(const void *vkey, const void *vdso) 145 { 146 const struct dsos__key *key = vkey; 147 const struct dso *dso = *((const struct dso **)vdso); 148 149 return __dso__cmp_long_name(key->long_name, key->id, dso); 150 } 151 152 /* 153 * Find a matching entry and/or link current entry to RB tree. 154 * Either one of the dso or name parameter must be non-NULL or the 155 * function will not work. 156 */ 157 static struct dso *__dsos__find_by_longname_id(struct dsos *dsos, 158 const char *name, 159 const struct dso_id *id, 160 bool write_locked) 161 SHARED_LOCKS_REQUIRED(dsos->lock) 162 { 163 struct dsos__key key = { 164 .long_name = name, 165 .id = id, 166 }; 167 struct dso **res; 168 169 if (dsos->dsos == NULL) 170 return NULL; 171 172 if (!dsos->sorted) { 173 if (!write_locked) { 174 struct dso *dso; 175 176 up_read(&dsos->lock); 177 down_write(&dsos->lock); 178 dso = __dsos__find_by_longname_id(dsos, name, id, 179 /*write_locked=*/true); 180 up_write(&dsos->lock); 181 down_read(&dsos->lock); 182 return dso; 183 } 184 qsort(dsos->dsos, dsos->cnt, sizeof(struct dso *), 185 dsos__cmp_long_name_id_short_name); 186 dsos->sorted = true; 187 } 188 189 res = bsearch(&key, dsos->dsos, dsos->cnt, sizeof(struct dso *), 190 dsos__cmp_key_long_name_id); 191 if (!res) 192 return NULL; 193 194 return dso__get(*res); 195 } 196 197 int __dsos__add(struct dsos *dsos, struct dso *dso) 198 { 199 if (!dso) 200 return -EINVAL; 201 202 if (dsos->cnt == dsos->allocated) { 203 unsigned int to_allocate = 2; 204 struct dso **temp; 205 206 if (dsos->allocated > 0) 207 to_allocate = dsos->allocated * 2; 208 temp = realloc(dsos->dsos, sizeof(struct dso *) * to_allocate); 209 if (!temp) 210 return -ENOMEM; 211 dsos->dsos = temp; 212 dsos->allocated = to_allocate; 213 } 214 if (!dsos->sorted) { 215 dsos->dsos[dsos->cnt++] = dso__get(dso); 216 } else { 217 int low = 0, high = dsos->cnt - 1; 218 int insert = dsos->cnt; /* Default to inserting at the end. */ 219 220 while (low <= high) { 221 int mid = low + (high - low) / 2; 222 int cmp = dsos__cmp_long_name_id_short_name(&dsos->dsos[mid], &dso); 223 224 if (cmp < 0) { 225 low = mid + 1; 226 } else { 227 high = mid - 1; 228 insert = mid; 229 } 230 } 231 memmove(&dsos->dsos[insert + 1], &dsos->dsos[insert], 232 (dsos->cnt - insert) * sizeof(struct dso *)); 233 dsos->cnt++; 234 dsos->dsos[insert] = dso__get(dso); 235 } 236 dso__set_dsos(dso, dsos); 237 return 0; 238 } 239 240 int dsos__add(struct dsos *dsos, struct dso *dso) 241 { 242 int ret; 243 244 down_write(&dsos->lock); 245 ret = __dsos__add(dsos, dso); 246 up_write(&dsos->lock); 247 return ret; 248 } 249 250 struct dsos__find_id_cb_args { 251 const char *name; 252 const struct dso_id *id; 253 struct dso *res; 254 }; 255 256 static int dsos__find_id_cb(struct dso *dso, void *data) 257 { 258 struct dsos__find_id_cb_args *args = data; 259 260 if (__dso__cmp_short_name(args->name, args->id, dso) == 0) { 261 args->res = dso__get(dso); 262 return 1; 263 } 264 return 0; 265 266 } 267 268 static struct dso *__dsos__find_id(struct dsos *dsos, const char *name, const struct dso_id *id, 269 bool cmp_short, bool write_locked) 270 SHARED_LOCKS_REQUIRED(dsos->lock) 271 { 272 struct dso *res; 273 274 if (cmp_short) { 275 struct dsos__find_id_cb_args args = { 276 .name = name, 277 .id = id, 278 .res = NULL, 279 }; 280 281 __dsos__for_each_dso(dsos, dsos__find_id_cb, &args); 282 return args.res; 283 } 284 res = __dsos__find_by_longname_id(dsos, name, id, write_locked); 285 return res; 286 } 287 288 struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short) 289 { 290 struct dso *res; 291 292 down_read(&dsos->lock); 293 res = __dsos__find_id(dsos, name, &dso_id_empty, cmp_short, /*write_locked=*/false); 294 up_read(&dsos->lock); 295 return res; 296 } 297 298 static void dso__set_basename(struct dso *dso) 299 { 300 char *base, *lname; 301 int tid; 302 303 if (perf_pid_map_tid(dso__long_name(dso), &tid)) { 304 if (asprintf(&base, "[JIT] tid %d", tid) < 0) 305 return; 306 } else { 307 /* 308 * basename() may modify path buffer, so we must pass 309 * a copy. 310 */ 311 lname = strdup(dso__long_name(dso)); 312 if (!lname) 313 return; 314 315 /* 316 * basename() may return a pointer to internal 317 * storage which is reused in subsequent calls 318 * so copy the result. 319 */ 320 base = strdup(basename(lname)); 321 322 free(lname); 323 324 if (!base) 325 return; 326 } 327 dso__set_short_name(dso, base, true); 328 } 329 330 static struct dso *__dsos__addnew_id(struct dsos *dsos, const char *name, const struct dso_id *id) 331 { 332 struct dso *dso = dso__new_id(name, id); 333 334 if (dso != NULL) { 335 /* 336 * The dsos lock is held on entry, so rename the dso before 337 * adding it to avoid needing to take the dsos lock again to say 338 * the array isn't sorted. 339 */ 340 dso__set_basename(dso); 341 __dsos__add(dsos, dso); 342 } 343 return dso; 344 } 345 346 static struct dso *__dsos__findnew_id(struct dsos *dsos, const char *name, const struct dso_id *id) 347 SHARED_LOCKS_REQUIRED(dsos->lock) 348 { 349 struct dso *dso = __dsos__find_id(dsos, name, id, false, /*write_locked=*/true); 350 351 if (dso) 352 __dso__improve_id(dso, id); 353 354 return dso ? dso : __dsos__addnew_id(dsos, name, id); 355 } 356 357 struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const struct dso_id *id) 358 { 359 struct dso *dso; 360 down_write(&dsos->lock); 361 dso = __dsos__findnew_id(dsos, name, id); 362 up_write(&dsos->lock); 363 return dso; 364 } 365 366 struct dsos__fprintf_buildid_cb_args { 367 FILE *fp; 368 bool (*skip)(struct dso *dso, int parm); 369 int parm; 370 size_t ret; 371 }; 372 373 static int dsos__fprintf_buildid_cb(struct dso *dso, void *data) 374 { 375 struct dsos__fprintf_buildid_cb_args *args = data; 376 char sbuild_id[SBUILD_ID_SIZE]; 377 378 if (args->skip && args->skip(dso, args->parm)) 379 return 0; 380 build_id__snprintf(dso__bid(dso), sbuild_id, sizeof(sbuild_id)); 381 args->ret += fprintf(args->fp, "%-40s %s\n", sbuild_id, dso__long_name(dso)); 382 return 0; 383 } 384 385 size_t dsos__fprintf_buildid(struct dsos *dsos, FILE *fp, 386 bool (*skip)(struct dso *dso, int parm), int parm) 387 { 388 struct dsos__fprintf_buildid_cb_args args = { 389 .fp = fp, 390 .skip = skip, 391 .parm = parm, 392 .ret = 0, 393 }; 394 395 dsos__for_each_dso(dsos, dsos__fprintf_buildid_cb, &args); 396 return args.ret; 397 } 398 399 struct dsos__fprintf_cb_args { 400 FILE *fp; 401 size_t ret; 402 }; 403 404 static int dsos__fprintf_cb(struct dso *dso, void *data) 405 { 406 struct dsos__fprintf_cb_args *args = data; 407 408 args->ret += dso__fprintf(dso, args->fp); 409 return 0; 410 } 411 412 size_t dsos__fprintf(struct dsos *dsos, FILE *fp) 413 { 414 struct dsos__fprintf_cb_args args = { 415 .fp = fp, 416 .ret = 0, 417 }; 418 419 dsos__for_each_dso(dsos, dsos__fprintf_cb, &args); 420 return args.ret; 421 } 422 423 static int dsos__hit_all_cb(struct dso *dso, void *data __maybe_unused) 424 { 425 dso__set_hit(dso); 426 return 0; 427 } 428 429 int dsos__hit_all(struct dsos *dsos) 430 { 431 return dsos__for_each_dso(dsos, dsos__hit_all_cb, NULL); 432 } 433 434 struct dso *dsos__findnew_module_dso(struct dsos *dsos, 435 struct machine *machine, 436 struct kmod_path *m, 437 const char *filename) 438 { 439 struct dso *dso; 440 441 down_write(&dsos->lock); 442 443 dso = __dsos__find_id(dsos, m->name, &dso_id_empty, /*cmp_short=*/true, 444 /*write_locked=*/true); 445 if (dso) { 446 up_write(&dsos->lock); 447 return dso; 448 } 449 /* 450 * Failed to find the dso so create it. Change the name before adding it 451 * to the array, to avoid unnecessary sorts and potential locking 452 * issues. 453 */ 454 dso = dso__new_id(m->name, /*id=*/NULL); 455 if (!dso) { 456 up_write(&dsos->lock); 457 return NULL; 458 } 459 dso__set_basename(dso); 460 dso__set_module_info(dso, m, machine); 461 dso__set_long_name(dso, strdup(filename), true); 462 dso__set_kernel(dso, DSO_SPACE__KERNEL); 463 __dsos__add(dsos, dso); 464 465 up_write(&dsos->lock); 466 return dso; 467 } 468 469 static int dsos__find_kernel_dso_cb(struct dso *dso, void *data) 470 { 471 struct dso **res = data; 472 /* 473 * The cpumode passed to is_kernel_module is not the cpumode of *this* 474 * event. If we insist on passing correct cpumode to is_kernel_module, 475 * we should record the cpumode when we adding this dso to the linked 476 * list. 477 * 478 * However we don't really need passing correct cpumode. We know the 479 * correct cpumode must be kernel mode (if not, we should not link it 480 * onto kernel_dsos list). 481 * 482 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN. 483 * is_kernel_module() treats it as a kernel cpumode. 484 */ 485 if (!dso__kernel(dso) || 486 is_kernel_module(dso__long_name(dso), PERF_RECORD_MISC_CPUMODE_UNKNOWN)) 487 return 0; 488 489 *res = dso__get(dso); 490 return 1; 491 } 492 493 struct dso *dsos__find_kernel_dso(struct dsos *dsos) 494 { 495 struct dso *res = NULL; 496 497 dsos__for_each_dso(dsos, dsos__find_kernel_dso_cb, &res); 498 return res; 499 } 500 501 int dsos__for_each_dso(struct dsos *dsos, int (*cb)(struct dso *dso, void *data), void *data) 502 { 503 int err; 504 505 down_read(&dsos->lock); 506 err = __dsos__for_each_dso(dsos, cb, data); 507 up_read(&dsos->lock); 508 return err; 509 } 510