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