probe-event.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374
  1. /*
  2. * probe-event.c : perf-probe definition to probe_events format converter
  3. *
  4. * Written by Masami Hiramatsu <mhiramat@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. */
  21. #include <sys/utsname.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <stdarg.h>
  31. #include <limits.h>
  32. #include <elf.h>
  33. #include "util.h"
  34. #include "event.h"
  35. #include "strlist.h"
  36. #include "debug.h"
  37. #include "cache.h"
  38. #include "color.h"
  39. #include "symbol.h"
  40. #include "thread.h"
  41. #include <api/fs/fs.h>
  42. #include "trace-event.h" /* For __maybe_unused */
  43. #include "probe-event.h"
  44. #include "probe-finder.h"
  45. #include "probe-file.h"
  46. #include "session.h"
  47. #define MAX_CMDLEN 256
  48. #define PERFPROBE_GROUP "probe"
  49. bool probe_event_dry_run; /* Dry run flag */
  50. struct probe_conf probe_conf;
  51. #define semantic_error(msg ...) pr_err("Semantic error :" msg)
  52. int e_snprintf(char *str, size_t size, const char *format, ...)
  53. {
  54. int ret;
  55. va_list ap;
  56. va_start(ap, format);
  57. ret = vsnprintf(str, size, format, ap);
  58. va_end(ap);
  59. if (ret >= (int)size)
  60. ret = -E2BIG;
  61. return ret;
  62. }
  63. static struct machine *host_machine;
  64. /* Initialize symbol maps and path of vmlinux/modules */
  65. int init_probe_symbol_maps(bool user_only)
  66. {
  67. int ret;
  68. symbol_conf.sort_by_name = true;
  69. symbol_conf.allow_aliases = true;
  70. ret = symbol__init(NULL);
  71. if (ret < 0) {
  72. pr_debug("Failed to init symbol map.\n");
  73. goto out;
  74. }
  75. if (host_machine || user_only) /* already initialized */
  76. return 0;
  77. if (symbol_conf.vmlinux_name)
  78. pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
  79. host_machine = machine__new_host();
  80. if (!host_machine) {
  81. pr_debug("machine__new_host() failed.\n");
  82. symbol__exit();
  83. ret = -1;
  84. }
  85. out:
  86. if (ret < 0)
  87. pr_warning("Failed to init vmlinux path.\n");
  88. return ret;
  89. }
  90. void exit_probe_symbol_maps(void)
  91. {
  92. machine__delete(host_machine);
  93. host_machine = NULL;
  94. symbol__exit();
  95. }
  96. static struct symbol *__find_kernel_function_by_name(const char *name,
  97. struct map **mapp)
  98. {
  99. return machine__find_kernel_function_by_name(host_machine, name, mapp,
  100. NULL);
  101. }
  102. static struct symbol *__find_kernel_function(u64 addr, struct map **mapp)
  103. {
  104. return machine__find_kernel_function(host_machine, addr, mapp, NULL);
  105. }
  106. static struct ref_reloc_sym *kernel_get_ref_reloc_sym(void)
  107. {
  108. /* kmap->ref_reloc_sym should be set if host_machine is initialized */
  109. struct kmap *kmap;
  110. struct map *map = machine__kernel_map(host_machine);
  111. if (map__load(map, NULL) < 0)
  112. return NULL;
  113. kmap = map__kmap(map);
  114. if (!kmap)
  115. return NULL;
  116. return kmap->ref_reloc_sym;
  117. }
  118. static int kernel_get_symbol_address_by_name(const char *name, u64 *addr,
  119. bool reloc, bool reladdr)
  120. {
  121. struct ref_reloc_sym *reloc_sym;
  122. struct symbol *sym;
  123. struct map *map;
  124. /* ref_reloc_sym is just a label. Need a special fix*/
  125. reloc_sym = kernel_get_ref_reloc_sym();
  126. if (reloc_sym && strcmp(name, reloc_sym->name) == 0)
  127. *addr = (reloc) ? reloc_sym->addr : reloc_sym->unrelocated_addr;
  128. else {
  129. sym = __find_kernel_function_by_name(name, &map);
  130. if (!sym)
  131. return -ENOENT;
  132. *addr = map->unmap_ip(map, sym->start) -
  133. ((reloc) ? 0 : map->reloc) -
  134. ((reladdr) ? map->start : 0);
  135. }
  136. return 0;
  137. }
  138. static struct map *kernel_get_module_map(const char *module)
  139. {
  140. struct map_groups *grp = &host_machine->kmaps;
  141. struct maps *maps = &grp->maps[MAP__FUNCTION];
  142. struct map *pos;
  143. /* A file path -- this is an offline module */
  144. if (module && strchr(module, '/'))
  145. return machine__findnew_module_map(host_machine, 0, module);
  146. if (!module)
  147. module = "kernel";
  148. for (pos = maps__first(maps); pos; pos = map__next(pos)) {
  149. /* short_name is "[module]" */
  150. if (strncmp(pos->dso->short_name + 1, module,
  151. pos->dso->short_name_len - 2) == 0 &&
  152. module[pos->dso->short_name_len - 2] == '\0') {
  153. return pos;
  154. }
  155. }
  156. return NULL;
  157. }
  158. struct map *get_target_map(const char *target, bool user)
  159. {
  160. /* Init maps of given executable or kernel */
  161. if (user)
  162. return dso__new_map(target);
  163. else
  164. return kernel_get_module_map(target);
  165. }
  166. static void put_target_map(struct map *map, bool user)
  167. {
  168. if (map && user) {
  169. /* Only the user map needs to be released */
  170. map__put(map);
  171. }
  172. }
  173. static int convert_exec_to_group(const char *exec, char **result)
  174. {
  175. char *ptr1, *ptr2, *exec_copy;
  176. char buf[64];
  177. int ret;
  178. exec_copy = strdup(exec);
  179. if (!exec_copy)
  180. return -ENOMEM;
  181. ptr1 = basename(exec_copy);
  182. if (!ptr1) {
  183. ret = -EINVAL;
  184. goto out;
  185. }
  186. ptr2 = strpbrk(ptr1, "-._");
  187. if (ptr2)
  188. *ptr2 = '\0';
  189. ret = e_snprintf(buf, 64, "%s_%s", PERFPROBE_GROUP, ptr1);
  190. if (ret < 0)
  191. goto out;
  192. *result = strdup(buf);
  193. ret = *result ? 0 : -ENOMEM;
  194. out:
  195. free(exec_copy);
  196. return ret;
  197. }
  198. static void clear_perf_probe_point(struct perf_probe_point *pp)
  199. {
  200. free(pp->file);
  201. free(pp->function);
  202. free(pp->lazy_line);
  203. }
  204. static void clear_probe_trace_events(struct probe_trace_event *tevs, int ntevs)
  205. {
  206. int i;
  207. for (i = 0; i < ntevs; i++)
  208. clear_probe_trace_event(tevs + i);
  209. }
  210. static bool kprobe_blacklist__listed(unsigned long address);
  211. static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
  212. {
  213. u64 etext_addr = 0;
  214. int ret;
  215. /* Get the address of _etext for checking non-probable text symbol */
  216. ret = kernel_get_symbol_address_by_name("_etext", &etext_addr,
  217. false, false);
  218. if (ret == 0 && etext_addr < address)
  219. pr_warning("%s is out of .text, skip it.\n", symbol);
  220. else if (kprobe_blacklist__listed(address))
  221. pr_warning("%s is blacklisted function, skip it.\n", symbol);
  222. else
  223. return false;
  224. return true;
  225. }
  226. /*
  227. * NOTE:
  228. * '.gnu.linkonce.this_module' section of kernel module elf directly
  229. * maps to 'struct module' from linux/module.h. This section contains
  230. * actual module name which will be used by kernel after loading it.
  231. * But, we cannot use 'struct module' here since linux/module.h is not
  232. * exposed to user-space. Offset of 'name' has remained same from long
  233. * time, so hardcoding it here.
  234. */
  235. #ifdef __LP64__
  236. #define MOD_NAME_OFFSET 24
  237. #else
  238. #define MOD_NAME_OFFSET 12
  239. #endif
  240. /*
  241. * @module can be module name of module file path. In case of path,
  242. * inspect elf and find out what is actual module name.
  243. * Caller has to free mod_name after using it.
  244. */
  245. static char *find_module_name(const char *module)
  246. {
  247. int fd;
  248. Elf *elf;
  249. GElf_Ehdr ehdr;
  250. GElf_Shdr shdr;
  251. Elf_Data *data;
  252. Elf_Scn *sec;
  253. char *mod_name = NULL;
  254. fd = open(module, O_RDONLY);
  255. if (fd < 0)
  256. return NULL;
  257. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  258. if (elf == NULL)
  259. goto elf_err;
  260. if (gelf_getehdr(elf, &ehdr) == NULL)
  261. goto ret_err;
  262. sec = elf_section_by_name(elf, &ehdr, &shdr,
  263. ".gnu.linkonce.this_module", NULL);
  264. if (!sec)
  265. goto ret_err;
  266. data = elf_getdata(sec, NULL);
  267. if (!data || !data->d_buf)
  268. goto ret_err;
  269. mod_name = strdup((char *)data->d_buf + MOD_NAME_OFFSET);
  270. ret_err:
  271. elf_end(elf);
  272. elf_err:
  273. close(fd);
  274. return mod_name;
  275. }
  276. #ifdef HAVE_DWARF_SUPPORT
  277. static int kernel_get_module_dso(const char *module, struct dso **pdso)
  278. {
  279. struct dso *dso;
  280. struct map *map;
  281. const char *vmlinux_name;
  282. int ret = 0;
  283. if (module) {
  284. char module_name[128];
  285. snprintf(module_name, sizeof(module_name), "[%s]", module);
  286. map = map_groups__find_by_name(&host_machine->kmaps, MAP__FUNCTION, module_name);
  287. if (map) {
  288. dso = map->dso;
  289. goto found;
  290. }
  291. pr_debug("Failed to find module %s.\n", module);
  292. return -ENOENT;
  293. }
  294. map = machine__kernel_map(host_machine);
  295. dso = map->dso;
  296. vmlinux_name = symbol_conf.vmlinux_name;
  297. dso->load_errno = 0;
  298. if (vmlinux_name)
  299. ret = dso__load_vmlinux(dso, map, vmlinux_name, false, NULL);
  300. else
  301. ret = dso__load_vmlinux_path(dso, map, NULL);
  302. found:
  303. *pdso = dso;
  304. return ret;
  305. }
  306. /*
  307. * Some binaries like glibc have special symbols which are on the symbol
  308. * table, but not in the debuginfo. If we can find the address of the
  309. * symbol from map, we can translate the address back to the probe point.
  310. */
  311. static int find_alternative_probe_point(struct debuginfo *dinfo,
  312. struct perf_probe_point *pp,
  313. struct perf_probe_point *result,
  314. const char *target, bool uprobes)
  315. {
  316. struct map *map = NULL;
  317. struct symbol *sym;
  318. u64 address = 0;
  319. int ret = -ENOENT;
  320. /* This can work only for function-name based one */
  321. if (!pp->function || pp->file)
  322. return -ENOTSUP;
  323. map = get_target_map(target, uprobes);
  324. if (!map)
  325. return -EINVAL;
  326. /* Find the address of given function */
  327. map__for_each_symbol_by_name(map, pp->function, sym) {
  328. if (uprobes)
  329. address = sym->start;
  330. else
  331. address = map->unmap_ip(map, sym->start) - map->reloc;
  332. break;
  333. }
  334. if (!address) {
  335. ret = -ENOENT;
  336. goto out;
  337. }
  338. pr_debug("Symbol %s address found : %" PRIx64 "\n",
  339. pp->function, address);
  340. ret = debuginfo__find_probe_point(dinfo, (unsigned long)address,
  341. result);
  342. if (ret <= 0)
  343. ret = (!ret) ? -ENOENT : ret;
  344. else {
  345. result->offset += pp->offset;
  346. result->line += pp->line;
  347. result->retprobe = pp->retprobe;
  348. ret = 0;
  349. }
  350. out:
  351. put_target_map(map, uprobes);
  352. return ret;
  353. }
  354. static int get_alternative_probe_event(struct debuginfo *dinfo,
  355. struct perf_probe_event *pev,
  356. struct perf_probe_point *tmp)
  357. {
  358. int ret;
  359. memcpy(tmp, &pev->point, sizeof(*tmp));
  360. memset(&pev->point, 0, sizeof(pev->point));
  361. ret = find_alternative_probe_point(dinfo, tmp, &pev->point,
  362. pev->target, pev->uprobes);
  363. if (ret < 0)
  364. memcpy(&pev->point, tmp, sizeof(*tmp));
  365. return ret;
  366. }
  367. static int get_alternative_line_range(struct debuginfo *dinfo,
  368. struct line_range *lr,
  369. const char *target, bool user)
  370. {
  371. struct perf_probe_point pp = { .function = lr->function,
  372. .file = lr->file,
  373. .line = lr->start };
  374. struct perf_probe_point result;
  375. int ret, len = 0;
  376. memset(&result, 0, sizeof(result));
  377. if (lr->end != INT_MAX)
  378. len = lr->end - lr->start;
  379. ret = find_alternative_probe_point(dinfo, &pp, &result,
  380. target, user);
  381. if (!ret) {
  382. lr->function = result.function;
  383. lr->file = result.file;
  384. lr->start = result.line;
  385. if (lr->end != INT_MAX)
  386. lr->end = lr->start + len;
  387. clear_perf_probe_point(&pp);
  388. }
  389. return ret;
  390. }
  391. /* Open new debuginfo of given module */
  392. static struct debuginfo *open_debuginfo(const char *module, bool silent)
  393. {
  394. const char *path = module;
  395. char reason[STRERR_BUFSIZE];
  396. struct debuginfo *ret = NULL;
  397. struct dso *dso = NULL;
  398. int err;
  399. if (!module || !strchr(module, '/')) {
  400. err = kernel_get_module_dso(module, &dso);
  401. if (err < 0) {
  402. if (!dso || dso->load_errno == 0) {
  403. if (!str_error_r(-err, reason, STRERR_BUFSIZE))
  404. strcpy(reason, "(unknown)");
  405. } else
  406. dso__strerror_load(dso, reason, STRERR_BUFSIZE);
  407. if (!silent)
  408. pr_err("Failed to find the path for %s: %s\n",
  409. module ?: "kernel", reason);
  410. return NULL;
  411. }
  412. path = dso->long_name;
  413. }
  414. ret = debuginfo__new(path);
  415. if (!ret && !silent) {
  416. pr_warning("The %s file has no debug information.\n", path);
  417. if (!module || !strtailcmp(path, ".ko"))
  418. pr_warning("Rebuild with CONFIG_DEBUG_INFO=y, ");
  419. else
  420. pr_warning("Rebuild with -g, ");
  421. pr_warning("or install an appropriate debuginfo package.\n");
  422. }
  423. return ret;
  424. }
  425. /* For caching the last debuginfo */
  426. static struct debuginfo *debuginfo_cache;
  427. static char *debuginfo_cache_path;
  428. static struct debuginfo *debuginfo_cache__open(const char *module, bool silent)
  429. {
  430. const char *path = module;
  431. /* If the module is NULL, it should be the kernel. */
  432. if (!module)
  433. path = "kernel";
  434. if (debuginfo_cache_path && !strcmp(debuginfo_cache_path, path))
  435. goto out;
  436. /* Copy module path */
  437. free(debuginfo_cache_path);
  438. debuginfo_cache_path = strdup(path);
  439. if (!debuginfo_cache_path) {
  440. debuginfo__delete(debuginfo_cache);
  441. debuginfo_cache = NULL;
  442. goto out;
  443. }
  444. debuginfo_cache = open_debuginfo(module, silent);
  445. if (!debuginfo_cache)
  446. zfree(&debuginfo_cache_path);
  447. out:
  448. return debuginfo_cache;
  449. }
  450. static void debuginfo_cache__exit(void)
  451. {
  452. debuginfo__delete(debuginfo_cache);
  453. debuginfo_cache = NULL;
  454. zfree(&debuginfo_cache_path);
  455. }
  456. static int get_text_start_address(const char *exec, unsigned long *address)
  457. {
  458. Elf *elf;
  459. GElf_Ehdr ehdr;
  460. GElf_Shdr shdr;
  461. int fd, ret = -ENOENT;
  462. fd = open(exec, O_RDONLY);
  463. if (fd < 0)
  464. return -errno;
  465. elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
  466. if (elf == NULL) {
  467. ret = -EINVAL;
  468. goto out_close;
  469. }
  470. if (gelf_getehdr(elf, &ehdr) == NULL)
  471. goto out;
  472. if (!elf_section_by_name(elf, &ehdr, &shdr, ".text", NULL))
  473. goto out;
  474. *address = shdr.sh_addr - shdr.sh_offset;
  475. ret = 0;
  476. out:
  477. elf_end(elf);
  478. out_close:
  479. close(fd);
  480. return ret;
  481. }
  482. /*
  483. * Convert trace point to probe point with debuginfo
  484. */
  485. static int find_perf_probe_point_from_dwarf(struct probe_trace_point *tp,
  486. struct perf_probe_point *pp,
  487. bool is_kprobe)
  488. {
  489. struct debuginfo *dinfo = NULL;
  490. unsigned long stext = 0;
  491. u64 addr = tp->address;
  492. int ret = -ENOENT;
  493. /* convert the address to dwarf address */
  494. if (!is_kprobe) {
  495. if (!addr) {
  496. ret = -EINVAL;
  497. goto error;
  498. }
  499. ret = get_text_start_address(tp->module, &stext);
  500. if (ret < 0)
  501. goto error;
  502. addr += stext;
  503. } else if (tp->symbol) {
  504. /* If the module is given, this returns relative address */
  505. ret = kernel_get_symbol_address_by_name(tp->symbol, &addr,
  506. false, !!tp->module);
  507. if (ret != 0)
  508. goto error;
  509. addr += tp->offset;
  510. }
  511. pr_debug("try to find information at %" PRIx64 " in %s\n", addr,
  512. tp->module ? : "kernel");
  513. dinfo = debuginfo_cache__open(tp->module, verbose == 0);
  514. if (dinfo)
  515. ret = debuginfo__find_probe_point(dinfo,
  516. (unsigned long)addr, pp);
  517. else
  518. ret = -ENOENT;
  519. if (ret > 0) {
  520. pp->retprobe = tp->retprobe;
  521. return 0;
  522. }
  523. error:
  524. pr_debug("Failed to find corresponding probes from debuginfo.\n");
  525. return ret ? : -ENOENT;
  526. }
  527. static int add_exec_to_probe_trace_events(struct probe_trace_event *tevs,
  528. int ntevs, const char *exec)
  529. {
  530. int i, ret = 0;
  531. unsigned long stext = 0;
  532. if (!exec)
  533. return 0;
  534. ret = get_text_start_address(exec, &stext);
  535. if (ret < 0)
  536. return ret;
  537. for (i = 0; i < ntevs && ret >= 0; i++) {
  538. /* point.address is the addres of point.symbol + point.offset */
  539. tevs[i].point.address -= stext;
  540. tevs[i].point.module = strdup(exec);
  541. if (!tevs[i].point.module) {
  542. ret = -ENOMEM;
  543. break;
  544. }
  545. tevs[i].uprobes = true;
  546. }
  547. return ret;
  548. }
  549. static int add_module_to_probe_trace_events(struct probe_trace_event *tevs,
  550. int ntevs, const char *module)
  551. {
  552. int i, ret = 0;
  553. char *mod_name = NULL;
  554. if (!module)
  555. return 0;
  556. mod_name = find_module_name(module);
  557. for (i = 0; i < ntevs; i++) {
  558. tevs[i].point.module =
  559. strdup(mod_name ? mod_name : module);
  560. if (!tevs[i].point.module) {
  561. ret = -ENOMEM;
  562. break;
  563. }
  564. }
  565. free(mod_name);
  566. return ret;
  567. }
  568. static int
  569. post_process_kernel_probe_trace_events(struct probe_trace_event *tevs,
  570. int ntevs)
  571. {
  572. struct ref_reloc_sym *reloc_sym;
  573. char *tmp;
  574. int i, skipped = 0;
  575. reloc_sym = kernel_get_ref_reloc_sym();
  576. if (!reloc_sym) {
  577. pr_warning("Relocated base symbol is not found!\n");
  578. return -EINVAL;
  579. }
  580. for (i = 0; i < ntevs; i++) {
  581. if (!tevs[i].point.address || tevs[i].point.retprobe)
  582. continue;
  583. /* If we found a wrong one, mark it by NULL symbol */
  584. if (kprobe_warn_out_range(tevs[i].point.symbol,
  585. tevs[i].point.address)) {
  586. tmp = NULL;
  587. skipped++;
  588. } else {
  589. tmp = strdup(reloc_sym->name);
  590. if (!tmp)
  591. return -ENOMEM;
  592. }
  593. /* If we have no realname, use symbol for it */
  594. if (!tevs[i].point.realname)
  595. tevs[i].point.realname = tevs[i].point.symbol;
  596. else
  597. free(tevs[i].point.symbol);
  598. tevs[i].point.symbol = tmp;
  599. tevs[i].point.offset = tevs[i].point.address -
  600. reloc_sym->unrelocated_addr;
  601. }
  602. return skipped;
  603. }
  604. void __weak
  605. arch__post_process_probe_trace_events(struct perf_probe_event *pev __maybe_unused,
  606. int ntevs __maybe_unused)
  607. {
  608. }
  609. /* Post processing the probe events */
  610. static int post_process_probe_trace_events(struct perf_probe_event *pev,
  611. struct probe_trace_event *tevs,
  612. int ntevs, const char *module,
  613. bool uprobe)
  614. {
  615. int ret;
  616. if (uprobe)
  617. ret = add_exec_to_probe_trace_events(tevs, ntevs, module);
  618. else if (module)
  619. /* Currently ref_reloc_sym based probe is not for drivers */
  620. ret = add_module_to_probe_trace_events(tevs, ntevs, module);
  621. else
  622. ret = post_process_kernel_probe_trace_events(tevs, ntevs);
  623. if (ret >= 0)
  624. arch__post_process_probe_trace_events(pev, ntevs);
  625. return ret;
  626. }
  627. /* Try to find perf_probe_event with debuginfo */
  628. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  629. struct probe_trace_event **tevs)
  630. {
  631. bool need_dwarf = perf_probe_event_need_dwarf(pev);
  632. struct perf_probe_point tmp;
  633. struct debuginfo *dinfo;
  634. int ntevs, ret = 0;
  635. dinfo = open_debuginfo(pev->target, !need_dwarf);
  636. if (!dinfo) {
  637. if (need_dwarf)
  638. return -ENOENT;
  639. pr_debug("Could not open debuginfo. Try to use symbols.\n");
  640. return 0;
  641. }
  642. pr_debug("Try to find probe point from debuginfo.\n");
  643. /* Searching trace events corresponding to a probe event */
  644. ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
  645. if (ntevs == 0) { /* Not found, retry with an alternative */
  646. ret = get_alternative_probe_event(dinfo, pev, &tmp);
  647. if (!ret) {
  648. ntevs = debuginfo__find_trace_events(dinfo, pev, tevs);
  649. /*
  650. * Write back to the original probe_event for
  651. * setting appropriate (user given) event name
  652. */
  653. clear_perf_probe_point(&pev->point);
  654. memcpy(&pev->point, &tmp, sizeof(tmp));
  655. }
  656. }
  657. debuginfo__delete(dinfo);
  658. if (ntevs > 0) { /* Succeeded to find trace events */
  659. pr_debug("Found %d probe_trace_events.\n", ntevs);
  660. ret = post_process_probe_trace_events(pev, *tevs, ntevs,
  661. pev->target, pev->uprobes);
  662. if (ret < 0 || ret == ntevs) {
  663. clear_probe_trace_events(*tevs, ntevs);
  664. zfree(tevs);
  665. }
  666. if (ret != ntevs)
  667. return ret < 0 ? ret : ntevs;
  668. ntevs = 0;
  669. /* Fall through */
  670. }
  671. if (ntevs == 0) { /* No error but failed to find probe point. */
  672. pr_warning("Probe point '%s' not found.\n",
  673. synthesize_perf_probe_point(&pev->point));
  674. return -ENOENT;
  675. }
  676. /* Error path : ntevs < 0 */
  677. pr_debug("An error occurred in debuginfo analysis (%d).\n", ntevs);
  678. if (ntevs < 0) {
  679. if (ntevs == -EBADF)
  680. pr_warning("Warning: No dwarf info found in the vmlinux - "
  681. "please rebuild kernel with CONFIG_DEBUG_INFO=y.\n");
  682. if (!need_dwarf) {
  683. pr_debug("Trying to use symbols.\n");
  684. return 0;
  685. }
  686. }
  687. return ntevs;
  688. }
  689. #define LINEBUF_SIZE 256
  690. #define NR_ADDITIONAL_LINES 2
  691. static int __show_one_line(FILE *fp, int l, bool skip, bool show_num)
  692. {
  693. char buf[LINEBUF_SIZE], sbuf[STRERR_BUFSIZE];
  694. const char *color = show_num ? "" : PERF_COLOR_BLUE;
  695. const char *prefix = NULL;
  696. do {
  697. if (fgets(buf, LINEBUF_SIZE, fp) == NULL)
  698. goto error;
  699. if (skip)
  700. continue;
  701. if (!prefix) {
  702. prefix = show_num ? "%7d " : " ";
  703. color_fprintf(stdout, color, prefix, l);
  704. }
  705. color_fprintf(stdout, color, "%s", buf);
  706. } while (strchr(buf, '\n') == NULL);
  707. return 1;
  708. error:
  709. if (ferror(fp)) {
  710. pr_warning("File read error: %s\n",
  711. str_error_r(errno, sbuf, sizeof(sbuf)));
  712. return -1;
  713. }
  714. return 0;
  715. }
  716. static int _show_one_line(FILE *fp, int l, bool skip, bool show_num)
  717. {
  718. int rv = __show_one_line(fp, l, skip, show_num);
  719. if (rv == 0) {
  720. pr_warning("Source file is shorter than expected.\n");
  721. rv = -1;
  722. }
  723. return rv;
  724. }
  725. #define show_one_line_with_num(f,l) _show_one_line(f,l,false,true)
  726. #define show_one_line(f,l) _show_one_line(f,l,false,false)
  727. #define skip_one_line(f,l) _show_one_line(f,l,true,false)
  728. #define show_one_line_or_eof(f,l) __show_one_line(f,l,false,false)
  729. /*
  730. * Show line-range always requires debuginfo to find source file and
  731. * line number.
  732. */
  733. static int __show_line_range(struct line_range *lr, const char *module,
  734. bool user)
  735. {
  736. int l = 1;
  737. struct int_node *ln;
  738. struct debuginfo *dinfo;
  739. FILE *fp;
  740. int ret;
  741. char *tmp;
  742. char sbuf[STRERR_BUFSIZE];
  743. /* Search a line range */
  744. dinfo = open_debuginfo(module, false);
  745. if (!dinfo)
  746. return -ENOENT;
  747. ret = debuginfo__find_line_range(dinfo, lr);
  748. if (!ret) { /* Not found, retry with an alternative */
  749. ret = get_alternative_line_range(dinfo, lr, module, user);
  750. if (!ret)
  751. ret = debuginfo__find_line_range(dinfo, lr);
  752. }
  753. debuginfo__delete(dinfo);
  754. if (ret == 0 || ret == -ENOENT) {
  755. pr_warning("Specified source line is not found.\n");
  756. return -ENOENT;
  757. } else if (ret < 0) {
  758. pr_warning("Debuginfo analysis failed.\n");
  759. return ret;
  760. }
  761. /* Convert source file path */
  762. tmp = lr->path;
  763. ret = get_real_path(tmp, lr->comp_dir, &lr->path);
  764. /* Free old path when new path is assigned */
  765. if (tmp != lr->path)
  766. free(tmp);
  767. if (ret < 0) {
  768. pr_warning("Failed to find source file path.\n");
  769. return ret;
  770. }
  771. setup_pager();
  772. if (lr->function)
  773. fprintf(stdout, "<%s@%s:%d>\n", lr->function, lr->path,
  774. lr->start - lr->offset);
  775. else
  776. fprintf(stdout, "<%s:%d>\n", lr->path, lr->start);
  777. fp = fopen(lr->path, "r");
  778. if (fp == NULL) {
  779. pr_warning("Failed to open %s: %s\n", lr->path,
  780. str_error_r(errno, sbuf, sizeof(sbuf)));
  781. return -errno;
  782. }
  783. /* Skip to starting line number */
  784. while (l < lr->start) {
  785. ret = skip_one_line(fp, l++);
  786. if (ret < 0)
  787. goto end;
  788. }
  789. intlist__for_each_entry(ln, lr->line_list) {
  790. for (; ln->i > l; l++) {
  791. ret = show_one_line(fp, l - lr->offset);
  792. if (ret < 0)
  793. goto end;
  794. }
  795. ret = show_one_line_with_num(fp, l++ - lr->offset);
  796. if (ret < 0)
  797. goto end;
  798. }
  799. if (lr->end == INT_MAX)
  800. lr->end = l + NR_ADDITIONAL_LINES;
  801. while (l <= lr->end) {
  802. ret = show_one_line_or_eof(fp, l++ - lr->offset);
  803. if (ret <= 0)
  804. break;
  805. }
  806. end:
  807. fclose(fp);
  808. return ret;
  809. }
  810. int show_line_range(struct line_range *lr, const char *module, bool user)
  811. {
  812. int ret;
  813. ret = init_probe_symbol_maps(user);
  814. if (ret < 0)
  815. return ret;
  816. ret = __show_line_range(lr, module, user);
  817. exit_probe_symbol_maps();
  818. return ret;
  819. }
  820. static int show_available_vars_at(struct debuginfo *dinfo,
  821. struct perf_probe_event *pev,
  822. struct strfilter *_filter)
  823. {
  824. char *buf;
  825. int ret, i, nvars;
  826. struct str_node *node;
  827. struct variable_list *vls = NULL, *vl;
  828. struct perf_probe_point tmp;
  829. const char *var;
  830. buf = synthesize_perf_probe_point(&pev->point);
  831. if (!buf)
  832. return -EINVAL;
  833. pr_debug("Searching variables at %s\n", buf);
  834. ret = debuginfo__find_available_vars_at(dinfo, pev, &vls);
  835. if (!ret) { /* Not found, retry with an alternative */
  836. ret = get_alternative_probe_event(dinfo, pev, &tmp);
  837. if (!ret) {
  838. ret = debuginfo__find_available_vars_at(dinfo, pev,
  839. &vls);
  840. /* Release the old probe_point */
  841. clear_perf_probe_point(&tmp);
  842. }
  843. }
  844. if (ret <= 0) {
  845. if (ret == 0 || ret == -ENOENT) {
  846. pr_err("Failed to find the address of %s\n", buf);
  847. ret = -ENOENT;
  848. } else
  849. pr_warning("Debuginfo analysis failed.\n");
  850. goto end;
  851. }
  852. /* Some variables are found */
  853. fprintf(stdout, "Available variables at %s\n", buf);
  854. for (i = 0; i < ret; i++) {
  855. vl = &vls[i];
  856. /*
  857. * A probe point might be converted to
  858. * several trace points.
  859. */
  860. fprintf(stdout, "\t@<%s+%lu>\n", vl->point.symbol,
  861. vl->point.offset);
  862. zfree(&vl->point.symbol);
  863. nvars = 0;
  864. if (vl->vars) {
  865. strlist__for_each_entry(node, vl->vars) {
  866. var = strchr(node->s, '\t') + 1;
  867. if (strfilter__compare(_filter, var)) {
  868. fprintf(stdout, "\t\t%s\n", node->s);
  869. nvars++;
  870. }
  871. }
  872. strlist__delete(vl->vars);
  873. }
  874. if (nvars == 0)
  875. fprintf(stdout, "\t\t(No matched variables)\n");
  876. }
  877. free(vls);
  878. end:
  879. free(buf);
  880. return ret;
  881. }
  882. /* Show available variables on given probe point */
  883. int show_available_vars(struct perf_probe_event *pevs, int npevs,
  884. struct strfilter *_filter)
  885. {
  886. int i, ret = 0;
  887. struct debuginfo *dinfo;
  888. ret = init_probe_symbol_maps(pevs->uprobes);
  889. if (ret < 0)
  890. return ret;
  891. dinfo = open_debuginfo(pevs->target, false);
  892. if (!dinfo) {
  893. ret = -ENOENT;
  894. goto out;
  895. }
  896. setup_pager();
  897. for (i = 0; i < npevs && ret >= 0; i++)
  898. ret = show_available_vars_at(dinfo, &pevs[i], _filter);
  899. debuginfo__delete(dinfo);
  900. out:
  901. exit_probe_symbol_maps();
  902. return ret;
  903. }
  904. #else /* !HAVE_DWARF_SUPPORT */
  905. static void debuginfo_cache__exit(void)
  906. {
  907. }
  908. static int
  909. find_perf_probe_point_from_dwarf(struct probe_trace_point *tp __maybe_unused,
  910. struct perf_probe_point *pp __maybe_unused,
  911. bool is_kprobe __maybe_unused)
  912. {
  913. return -ENOSYS;
  914. }
  915. static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
  916. struct probe_trace_event **tevs __maybe_unused)
  917. {
  918. if (perf_probe_event_need_dwarf(pev)) {
  919. pr_warning("Debuginfo-analysis is not supported.\n");
  920. return -ENOSYS;
  921. }
  922. return 0;
  923. }
  924. int show_line_range(struct line_range *lr __maybe_unused,
  925. const char *module __maybe_unused,
  926. bool user __maybe_unused)
  927. {
  928. pr_warning("Debuginfo-analysis is not supported.\n");
  929. return -ENOSYS;
  930. }
  931. int show_available_vars(struct perf_probe_event *pevs __maybe_unused,
  932. int npevs __maybe_unused,
  933. struct strfilter *filter __maybe_unused)
  934. {
  935. pr_warning("Debuginfo-analysis is not supported.\n");
  936. return -ENOSYS;
  937. }
  938. #endif
  939. void line_range__clear(struct line_range *lr)
  940. {
  941. free(lr->function);
  942. free(lr->file);
  943. free(lr->path);
  944. free(lr->comp_dir);
  945. intlist__delete(lr->line_list);
  946. memset(lr, 0, sizeof(*lr));
  947. }
  948. int line_range__init(struct line_range *lr)
  949. {
  950. memset(lr, 0, sizeof(*lr));
  951. lr->line_list = intlist__new(NULL);
  952. if (!lr->line_list)
  953. return -ENOMEM;
  954. else
  955. return 0;
  956. }
  957. static int parse_line_num(char **ptr, int *val, const char *what)
  958. {
  959. const char *start = *ptr;
  960. errno = 0;
  961. *val = strtol(*ptr, ptr, 0);
  962. if (errno || *ptr == start) {
  963. semantic_error("'%s' is not a valid number.\n", what);
  964. return -EINVAL;
  965. }
  966. return 0;
  967. }
  968. /* Check the name is good for event, group or function */
  969. static bool is_c_func_name(const char *name)
  970. {
  971. if (!isalpha(*name) && *name != '_')
  972. return false;
  973. while (*++name != '\0') {
  974. if (!isalpha(*name) && !isdigit(*name) && *name != '_')
  975. return false;
  976. }
  977. return true;
  978. }
  979. /*
  980. * Stuff 'lr' according to the line range described by 'arg'.
  981. * The line range syntax is described by:
  982. *
  983. * SRC[:SLN[+NUM|-ELN]]
  984. * FNC[@SRC][:SLN[+NUM|-ELN]]
  985. */
  986. int parse_line_range_desc(const char *arg, struct line_range *lr)
  987. {
  988. char *range, *file, *name = strdup(arg);
  989. int err;
  990. if (!name)
  991. return -ENOMEM;
  992. lr->start = 0;
  993. lr->end = INT_MAX;
  994. range = strchr(name, ':');
  995. if (range) {
  996. *range++ = '\0';
  997. err = parse_line_num(&range, &lr->start, "start line");
  998. if (err)
  999. goto err;
  1000. if (*range == '+' || *range == '-') {
  1001. const char c = *range++;
  1002. err = parse_line_num(&range, &lr->end, "end line");
  1003. if (err)
  1004. goto err;
  1005. if (c == '+') {
  1006. lr->end += lr->start;
  1007. /*
  1008. * Adjust the number of lines here.
  1009. * If the number of lines == 1, the
  1010. * the end of line should be equal to
  1011. * the start of line.
  1012. */
  1013. lr->end--;
  1014. }
  1015. }
  1016. pr_debug("Line range is %d to %d\n", lr->start, lr->end);
  1017. err = -EINVAL;
  1018. if (lr->start > lr->end) {
  1019. semantic_error("Start line must be smaller"
  1020. " than end line.\n");
  1021. goto err;
  1022. }
  1023. if (*range != '\0') {
  1024. semantic_error("Tailing with invalid str '%s'.\n", range);
  1025. goto err;
  1026. }
  1027. }
  1028. file = strchr(name, '@');
  1029. if (file) {
  1030. *file = '\0';
  1031. lr->file = strdup(++file);
  1032. if (lr->file == NULL) {
  1033. err = -ENOMEM;
  1034. goto err;
  1035. }
  1036. lr->function = name;
  1037. } else if (strchr(name, '/') || strchr(name, '.'))
  1038. lr->file = name;
  1039. else if (is_c_func_name(name))/* We reuse it for checking funcname */
  1040. lr->function = name;
  1041. else { /* Invalid name */
  1042. semantic_error("'%s' is not a valid function name.\n", name);
  1043. err = -EINVAL;
  1044. goto err;
  1045. }
  1046. return 0;
  1047. err:
  1048. free(name);
  1049. return err;
  1050. }
  1051. static int parse_perf_probe_event_name(char **arg, struct perf_probe_event *pev)
  1052. {
  1053. char *ptr;
  1054. ptr = strchr(*arg, ':');
  1055. if (ptr) {
  1056. *ptr = '\0';
  1057. if (!pev->sdt && !is_c_func_name(*arg))
  1058. goto ng_name;
  1059. pev->group = strdup(*arg);
  1060. if (!pev->group)
  1061. return -ENOMEM;
  1062. *arg = ptr + 1;
  1063. } else
  1064. pev->group = NULL;
  1065. if (!pev->sdt && !is_c_func_name(*arg)) {
  1066. ng_name:
  1067. semantic_error("%s is bad for event name -it must "
  1068. "follow C symbol-naming rule.\n", *arg);
  1069. return -EINVAL;
  1070. }
  1071. pev->event = strdup(*arg);
  1072. if (pev->event == NULL)
  1073. return -ENOMEM;
  1074. return 0;
  1075. }
  1076. /* Parse probepoint definition. */
  1077. static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
  1078. {
  1079. struct perf_probe_point *pp = &pev->point;
  1080. char *ptr, *tmp;
  1081. char c, nc = 0;
  1082. bool file_spec = false;
  1083. int ret;
  1084. /*
  1085. * <Syntax>
  1086. * perf probe [GRP:][EVENT=]SRC[:LN|;PTN]
  1087. * perf probe [GRP:][EVENT=]FUNC[@SRC][+OFFS|%return|:LN|;PAT]
  1088. * perf probe %[GRP:]SDT_EVENT
  1089. */
  1090. if (!arg)
  1091. return -EINVAL;
  1092. /*
  1093. * If the probe point starts with '%',
  1094. * or starts with "sdt_" and has a ':' but no '=',
  1095. * then it should be a SDT/cached probe point.
  1096. */
  1097. if (arg[0] == '%' ||
  1098. (!strncmp(arg, "sdt_", 4) &&
  1099. !!strchr(arg, ':') && !strchr(arg, '='))) {
  1100. pev->sdt = true;
  1101. if (arg[0] == '%')
  1102. arg++;
  1103. }
  1104. ptr = strpbrk(arg, ";=@+%");
  1105. if (pev->sdt) {
  1106. if (ptr) {
  1107. if (*ptr != '@') {
  1108. semantic_error("%s must be an SDT name.\n",
  1109. arg);
  1110. return -EINVAL;
  1111. }
  1112. /* This must be a target file name or build id */
  1113. tmp = build_id_cache__complement(ptr + 1);
  1114. if (tmp) {
  1115. pev->target = build_id_cache__origname(tmp);
  1116. free(tmp);
  1117. } else
  1118. pev->target = strdup(ptr + 1);
  1119. if (!pev->target)
  1120. return -ENOMEM;
  1121. *ptr = '\0';
  1122. }
  1123. ret = parse_perf_probe_event_name(&arg, pev);
  1124. if (ret == 0) {
  1125. if (asprintf(&pev->point.function, "%%%s", pev->event) < 0)
  1126. ret = -errno;
  1127. }
  1128. return ret;
  1129. }
  1130. if (ptr && *ptr == '=') { /* Event name */
  1131. *ptr = '\0';
  1132. tmp = ptr + 1;
  1133. ret = parse_perf_probe_event_name(&arg, pev);
  1134. if (ret < 0)
  1135. return ret;
  1136. arg = tmp;
  1137. }
  1138. /*
  1139. * Check arg is function or file name and copy it.
  1140. *
  1141. * We consider arg to be a file spec if and only if it satisfies
  1142. * all of the below criteria::
  1143. * - it does not include any of "+@%",
  1144. * - it includes one of ":;", and
  1145. * - it has a period '.' in the name.
  1146. *
  1147. * Otherwise, we consider arg to be a function specification.
  1148. */
  1149. if (!strpbrk(arg, "+@%") && (ptr = strpbrk(arg, ";:")) != NULL) {
  1150. /* This is a file spec if it includes a '.' before ; or : */
  1151. if (memchr(arg, '.', ptr - arg))
  1152. file_spec = true;
  1153. }
  1154. ptr = strpbrk(arg, ";:+@%");
  1155. if (ptr) {
  1156. nc = *ptr;
  1157. *ptr++ = '\0';
  1158. }
  1159. if (arg[0] == '\0')
  1160. tmp = NULL;
  1161. else {
  1162. tmp = strdup(arg);
  1163. if (tmp == NULL)
  1164. return -ENOMEM;
  1165. }
  1166. if (file_spec)
  1167. pp->file = tmp;
  1168. else {
  1169. pp->function = tmp;
  1170. /*
  1171. * Keep pp->function even if this is absolute address,
  1172. * so it can mark whether abs_address is valid.
  1173. * Which make 'perf probe lib.bin 0x0' possible.
  1174. *
  1175. * Note that checking length of tmp is not needed
  1176. * because when we access tmp[1] we know tmp[0] is '0',
  1177. * so tmp[1] should always valid (but could be '\0').
  1178. */
  1179. if (tmp && !strncmp(tmp, "0x", 2)) {
  1180. pp->abs_address = strtoul(pp->function, &tmp, 0);
  1181. if (*tmp != '\0') {
  1182. semantic_error("Invalid absolute address.\n");
  1183. return -EINVAL;
  1184. }
  1185. }
  1186. }
  1187. /* Parse other options */
  1188. while (ptr) {
  1189. arg = ptr;
  1190. c = nc;
  1191. if (c == ';') { /* Lazy pattern must be the last part */
  1192. pp->lazy_line = strdup(arg);
  1193. if (pp->lazy_line == NULL)
  1194. return -ENOMEM;
  1195. break;
  1196. }
  1197. ptr = strpbrk(arg, ";:+@%");
  1198. if (ptr) {
  1199. nc = *ptr;
  1200. *ptr++ = '\0';
  1201. }
  1202. switch (c) {
  1203. case ':': /* Line number */
  1204. pp->line = strtoul(arg, &tmp, 0);
  1205. if (*tmp != '\0') {
  1206. semantic_error("There is non-digit char"
  1207. " in line number.\n");
  1208. return -EINVAL;
  1209. }
  1210. break;
  1211. case '+': /* Byte offset from a symbol */
  1212. pp->offset = strtoul(arg, &tmp, 0);
  1213. if (*tmp != '\0') {
  1214. semantic_error("There is non-digit character"
  1215. " in offset.\n");
  1216. return -EINVAL;
  1217. }
  1218. break;
  1219. case '@': /* File name */
  1220. if (pp->file) {
  1221. semantic_error("SRC@SRC is not allowed.\n");
  1222. return -EINVAL;
  1223. }
  1224. pp->file = strdup(arg);
  1225. if (pp->file == NULL)
  1226. return -ENOMEM;
  1227. break;
  1228. case '%': /* Probe places */
  1229. if (strcmp(arg, "return") == 0) {
  1230. pp->retprobe = 1;
  1231. } else { /* Others not supported yet */
  1232. semantic_error("%%%s is not supported.\n", arg);
  1233. return -ENOTSUP;
  1234. }
  1235. break;
  1236. default: /* Buggy case */
  1237. pr_err("This program has a bug at %s:%d.\n",
  1238. __FILE__, __LINE__);
  1239. return -ENOTSUP;
  1240. break;
  1241. }
  1242. }
  1243. /* Exclusion check */
  1244. if (pp->lazy_line && pp->line) {
  1245. semantic_error("Lazy pattern can't be used with"
  1246. " line number.\n");
  1247. return -EINVAL;
  1248. }
  1249. if (pp->lazy_line && pp->offset) {
  1250. semantic_error("Lazy pattern can't be used with offset.\n");
  1251. return -EINVAL;
  1252. }
  1253. if (pp->line && pp->offset) {
  1254. semantic_error("Offset can't be used with line number.\n");
  1255. return -EINVAL;
  1256. }
  1257. if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
  1258. semantic_error("File always requires line number or "
  1259. "lazy pattern.\n");
  1260. return -EINVAL;
  1261. }
  1262. if (pp->offset && !pp->function) {
  1263. semantic_error("Offset requires an entry function.\n");
  1264. return -EINVAL;
  1265. }
  1266. if (pp->retprobe && !pp->function) {
  1267. semantic_error("Return probe requires an entry function.\n");
  1268. return -EINVAL;
  1269. }
  1270. if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
  1271. semantic_error("Offset/Line/Lazy pattern can't be used with "
  1272. "return probe.\n");
  1273. return -EINVAL;
  1274. }
  1275. pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
  1276. pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
  1277. pp->lazy_line);
  1278. return 0;
  1279. }
  1280. /* Parse perf-probe event argument */
  1281. static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
  1282. {
  1283. char *tmp, *goodname;
  1284. struct perf_probe_arg_field **fieldp;
  1285. pr_debug("parsing arg: %s into ", str);
  1286. tmp = strchr(str, '=');
  1287. if (tmp) {
  1288. arg->name = strndup(str, tmp - str);
  1289. if (arg->name == NULL)
  1290. return -ENOMEM;
  1291. pr_debug("name:%s ", arg->name);
  1292. str = tmp + 1;
  1293. }
  1294. tmp = strchr(str, ':');
  1295. if (tmp) { /* Type setting */
  1296. *tmp = '\0';
  1297. arg->type = strdup(tmp + 1);
  1298. if (arg->type == NULL)
  1299. return -ENOMEM;
  1300. pr_debug("type:%s ", arg->type);
  1301. }
  1302. tmp = strpbrk(str, "-.[");
  1303. if (!is_c_varname(str) || !tmp) {
  1304. /* A variable, register, symbol or special value */
  1305. arg->var = strdup(str);
  1306. if (arg->var == NULL)
  1307. return -ENOMEM;
  1308. pr_debug("%s\n", arg->var);
  1309. return 0;
  1310. }
  1311. /* Structure fields or array element */
  1312. arg->var = strndup(str, tmp - str);
  1313. if (arg->var == NULL)
  1314. return -ENOMEM;
  1315. goodname = arg->var;
  1316. pr_debug("%s, ", arg->var);
  1317. fieldp = &arg->field;
  1318. do {
  1319. *fieldp = zalloc(sizeof(struct perf_probe_arg_field));
  1320. if (*fieldp == NULL)
  1321. return -ENOMEM;
  1322. if (*tmp == '[') { /* Array */
  1323. str = tmp;
  1324. (*fieldp)->index = strtol(str + 1, &tmp, 0);
  1325. (*fieldp)->ref = true;
  1326. if (*tmp != ']' || tmp == str + 1) {
  1327. semantic_error("Array index must be a"
  1328. " number.\n");
  1329. return -EINVAL;
  1330. }
  1331. tmp++;
  1332. if (*tmp == '\0')
  1333. tmp = NULL;
  1334. } else { /* Structure */
  1335. if (*tmp == '.') {
  1336. str = tmp + 1;
  1337. (*fieldp)->ref = false;
  1338. } else if (tmp[1] == '>') {
  1339. str = tmp + 2;
  1340. (*fieldp)->ref = true;
  1341. } else {
  1342. semantic_error("Argument parse error: %s\n",
  1343. str);
  1344. return -EINVAL;
  1345. }
  1346. tmp = strpbrk(str, "-.[");
  1347. }
  1348. if (tmp) {
  1349. (*fieldp)->name = strndup(str, tmp - str);
  1350. if ((*fieldp)->name == NULL)
  1351. return -ENOMEM;
  1352. if (*str != '[')
  1353. goodname = (*fieldp)->name;
  1354. pr_debug("%s(%d), ", (*fieldp)->name, (*fieldp)->ref);
  1355. fieldp = &(*fieldp)->next;
  1356. }
  1357. } while (tmp);
  1358. (*fieldp)->name = strdup(str);
  1359. if ((*fieldp)->name == NULL)
  1360. return -ENOMEM;
  1361. if (*str != '[')
  1362. goodname = (*fieldp)->name;
  1363. pr_debug("%s(%d)\n", (*fieldp)->name, (*fieldp)->ref);
  1364. /* If no name is specified, set the last field name (not array index)*/
  1365. if (!arg->name) {
  1366. arg->name = strdup(goodname);
  1367. if (arg->name == NULL)
  1368. return -ENOMEM;
  1369. }
  1370. return 0;
  1371. }
  1372. /* Parse perf-probe event command */
  1373. int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
  1374. {
  1375. char **argv;
  1376. int argc, i, ret = 0;
  1377. argv = argv_split(cmd, &argc);
  1378. if (!argv) {
  1379. pr_debug("Failed to split arguments.\n");
  1380. return -ENOMEM;
  1381. }
  1382. if (argc - 1 > MAX_PROBE_ARGS) {
  1383. semantic_error("Too many probe arguments (%d).\n", argc - 1);
  1384. ret = -ERANGE;
  1385. goto out;
  1386. }
  1387. /* Parse probe point */
  1388. ret = parse_perf_probe_point(argv[0], pev);
  1389. if (ret < 0)
  1390. goto out;
  1391. /* Copy arguments and ensure return probe has no C argument */
  1392. pev->nargs = argc - 1;
  1393. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1394. if (pev->args == NULL) {
  1395. ret = -ENOMEM;
  1396. goto out;
  1397. }
  1398. for (i = 0; i < pev->nargs && ret >= 0; i++) {
  1399. ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
  1400. if (ret >= 0 &&
  1401. is_c_varname(pev->args[i].var) && pev->point.retprobe) {
  1402. semantic_error("You can't specify local variable for"
  1403. " kretprobe.\n");
  1404. ret = -EINVAL;
  1405. }
  1406. }
  1407. out:
  1408. argv_free(argv);
  1409. return ret;
  1410. }
  1411. /* Return true if this perf_probe_event requires debuginfo */
  1412. bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
  1413. {
  1414. int i;
  1415. if (pev->point.file || pev->point.line || pev->point.lazy_line)
  1416. return true;
  1417. for (i = 0; i < pev->nargs; i++)
  1418. if (is_c_varname(pev->args[i].var) ||
  1419. !strcmp(pev->args[i].var, "$params") ||
  1420. !strcmp(pev->args[i].var, "$vars"))
  1421. return true;
  1422. return false;
  1423. }
  1424. /* Parse probe_events event into struct probe_point */
  1425. int parse_probe_trace_command(const char *cmd, struct probe_trace_event *tev)
  1426. {
  1427. struct probe_trace_point *tp = &tev->point;
  1428. char pr;
  1429. char *p;
  1430. char *argv0_str = NULL, *fmt, *fmt1_str, *fmt2_str, *fmt3_str;
  1431. int ret, i, argc;
  1432. char **argv;
  1433. pr_debug("Parsing probe_events: %s\n", cmd);
  1434. argv = argv_split(cmd, &argc);
  1435. if (!argv) {
  1436. pr_debug("Failed to split arguments.\n");
  1437. return -ENOMEM;
  1438. }
  1439. if (argc < 2) {
  1440. semantic_error("Too few probe arguments.\n");
  1441. ret = -ERANGE;
  1442. goto out;
  1443. }
  1444. /* Scan event and group name. */
  1445. argv0_str = strdup(argv[0]);
  1446. if (argv0_str == NULL) {
  1447. ret = -ENOMEM;
  1448. goto out;
  1449. }
  1450. fmt1_str = strtok_r(argv0_str, ":", &fmt);
  1451. fmt2_str = strtok_r(NULL, "/", &fmt);
  1452. fmt3_str = strtok_r(NULL, " \t", &fmt);
  1453. if (fmt1_str == NULL || strlen(fmt1_str) != 1 || fmt2_str == NULL
  1454. || fmt3_str == NULL) {
  1455. semantic_error("Failed to parse event name: %s\n", argv[0]);
  1456. ret = -EINVAL;
  1457. goto out;
  1458. }
  1459. pr = fmt1_str[0];
  1460. tev->group = strdup(fmt2_str);
  1461. tev->event = strdup(fmt3_str);
  1462. if (tev->group == NULL || tev->event == NULL) {
  1463. ret = -ENOMEM;
  1464. goto out;
  1465. }
  1466. pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
  1467. tp->retprobe = (pr == 'r');
  1468. /* Scan module name(if there), function name and offset */
  1469. p = strchr(argv[1], ':');
  1470. if (p) {
  1471. tp->module = strndup(argv[1], p - argv[1]);
  1472. if (!tp->module) {
  1473. ret = -ENOMEM;
  1474. goto out;
  1475. }
  1476. tev->uprobes = (tp->module[0] == '/');
  1477. p++;
  1478. } else
  1479. p = argv[1];
  1480. fmt1_str = strtok_r(p, "+", &fmt);
  1481. /* only the address started with 0x */
  1482. if (fmt1_str[0] == '0') {
  1483. /*
  1484. * Fix a special case:
  1485. * if address == 0, kernel reports something like:
  1486. * p:probe_libc/abs_0 /lib/libc-2.18.so:0x (null) arg1=%ax
  1487. * Newer kernel may fix that, but we want to
  1488. * support old kernel also.
  1489. */
  1490. if (strcmp(fmt1_str, "0x") == 0) {
  1491. if (!argv[2] || strcmp(argv[2], "(null)")) {
  1492. ret = -EINVAL;
  1493. goto out;
  1494. }
  1495. tp->address = 0;
  1496. free(argv[2]);
  1497. for (i = 2; argv[i + 1] != NULL; i++)
  1498. argv[i] = argv[i + 1];
  1499. argv[i] = NULL;
  1500. argc -= 1;
  1501. } else
  1502. tp->address = strtoul(fmt1_str, NULL, 0);
  1503. } else {
  1504. /* Only the symbol-based probe has offset */
  1505. tp->symbol = strdup(fmt1_str);
  1506. if (tp->symbol == NULL) {
  1507. ret = -ENOMEM;
  1508. goto out;
  1509. }
  1510. fmt2_str = strtok_r(NULL, "", &fmt);
  1511. if (fmt2_str == NULL)
  1512. tp->offset = 0;
  1513. else
  1514. tp->offset = strtoul(fmt2_str, NULL, 10);
  1515. }
  1516. tev->nargs = argc - 2;
  1517. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  1518. if (tev->args == NULL) {
  1519. ret = -ENOMEM;
  1520. goto out;
  1521. }
  1522. for (i = 0; i < tev->nargs; i++) {
  1523. p = strchr(argv[i + 2], '=');
  1524. if (p) /* We don't need which register is assigned. */
  1525. *p++ = '\0';
  1526. else
  1527. p = argv[i + 2];
  1528. tev->args[i].name = strdup(argv[i + 2]);
  1529. /* TODO: parse regs and offset */
  1530. tev->args[i].value = strdup(p);
  1531. if (tev->args[i].name == NULL || tev->args[i].value == NULL) {
  1532. ret = -ENOMEM;
  1533. goto out;
  1534. }
  1535. }
  1536. ret = 0;
  1537. out:
  1538. free(argv0_str);
  1539. argv_free(argv);
  1540. return ret;
  1541. }
  1542. /* Compose only probe arg */
  1543. char *synthesize_perf_probe_arg(struct perf_probe_arg *pa)
  1544. {
  1545. struct perf_probe_arg_field *field = pa->field;
  1546. struct strbuf buf;
  1547. char *ret = NULL;
  1548. int err;
  1549. if (strbuf_init(&buf, 64) < 0)
  1550. return NULL;
  1551. if (pa->name && pa->var)
  1552. err = strbuf_addf(&buf, "%s=%s", pa->name, pa->var);
  1553. else
  1554. err = strbuf_addstr(&buf, pa->name ?: pa->var);
  1555. if (err)
  1556. goto out;
  1557. while (field) {
  1558. if (field->name[0] == '[')
  1559. err = strbuf_addstr(&buf, field->name);
  1560. else
  1561. err = strbuf_addf(&buf, "%s%s", field->ref ? "->" : ".",
  1562. field->name);
  1563. field = field->next;
  1564. if (err)
  1565. goto out;
  1566. }
  1567. if (pa->type)
  1568. if (strbuf_addf(&buf, ":%s", pa->type) < 0)
  1569. goto out;
  1570. ret = strbuf_detach(&buf, NULL);
  1571. out:
  1572. strbuf_release(&buf);
  1573. return ret;
  1574. }
  1575. /* Compose only probe point (not argument) */
  1576. char *synthesize_perf_probe_point(struct perf_probe_point *pp)
  1577. {
  1578. struct strbuf buf;
  1579. char *tmp, *ret = NULL;
  1580. int len, err = 0;
  1581. if (strbuf_init(&buf, 64) < 0)
  1582. return NULL;
  1583. if (pp->function) {
  1584. if (strbuf_addstr(&buf, pp->function) < 0)
  1585. goto out;
  1586. if (pp->offset)
  1587. err = strbuf_addf(&buf, "+%lu", pp->offset);
  1588. else if (pp->line)
  1589. err = strbuf_addf(&buf, ":%d", pp->line);
  1590. else if (pp->retprobe)
  1591. err = strbuf_addstr(&buf, "%return");
  1592. if (err)
  1593. goto out;
  1594. }
  1595. if (pp->file) {
  1596. tmp = pp->file;
  1597. len = strlen(tmp);
  1598. if (len > 30) {
  1599. tmp = strchr(pp->file + len - 30, '/');
  1600. tmp = tmp ? tmp + 1 : pp->file + len - 30;
  1601. }
  1602. err = strbuf_addf(&buf, "@%s", tmp);
  1603. if (!err && !pp->function && pp->line)
  1604. err = strbuf_addf(&buf, ":%d", pp->line);
  1605. }
  1606. if (!err)
  1607. ret = strbuf_detach(&buf, NULL);
  1608. out:
  1609. strbuf_release(&buf);
  1610. return ret;
  1611. }
  1612. char *synthesize_perf_probe_command(struct perf_probe_event *pev)
  1613. {
  1614. struct strbuf buf;
  1615. char *tmp, *ret = NULL;
  1616. int i;
  1617. if (strbuf_init(&buf, 64))
  1618. return NULL;
  1619. if (pev->event)
  1620. if (strbuf_addf(&buf, "%s:%s=", pev->group ?: PERFPROBE_GROUP,
  1621. pev->event) < 0)
  1622. goto out;
  1623. tmp = synthesize_perf_probe_point(&pev->point);
  1624. if (!tmp || strbuf_addstr(&buf, tmp) < 0)
  1625. goto out;
  1626. free(tmp);
  1627. for (i = 0; i < pev->nargs; i++) {
  1628. tmp = synthesize_perf_probe_arg(pev->args + i);
  1629. if (!tmp || strbuf_addf(&buf, " %s", tmp) < 0)
  1630. goto out;
  1631. free(tmp);
  1632. }
  1633. ret = strbuf_detach(&buf, NULL);
  1634. out:
  1635. strbuf_release(&buf);
  1636. return ret;
  1637. }
  1638. static int __synthesize_probe_trace_arg_ref(struct probe_trace_arg_ref *ref,
  1639. struct strbuf *buf, int depth)
  1640. {
  1641. int err;
  1642. if (ref->next) {
  1643. depth = __synthesize_probe_trace_arg_ref(ref->next, buf,
  1644. depth + 1);
  1645. if (depth < 0)
  1646. return depth;
  1647. }
  1648. err = strbuf_addf(buf, "%+ld(", ref->offset);
  1649. return (err < 0) ? err : depth;
  1650. }
  1651. static int synthesize_probe_trace_arg(struct probe_trace_arg *arg,
  1652. struct strbuf *buf)
  1653. {
  1654. struct probe_trace_arg_ref *ref = arg->ref;
  1655. int depth = 0, err;
  1656. /* Argument name or separator */
  1657. if (arg->name)
  1658. err = strbuf_addf(buf, " %s=", arg->name);
  1659. else
  1660. err = strbuf_addch(buf, ' ');
  1661. if (err)
  1662. return err;
  1663. /* Special case: @XXX */
  1664. if (arg->value[0] == '@' && arg->ref)
  1665. ref = ref->next;
  1666. /* Dereferencing arguments */
  1667. if (ref) {
  1668. depth = __synthesize_probe_trace_arg_ref(ref, buf, 1);
  1669. if (depth < 0)
  1670. return depth;
  1671. }
  1672. /* Print argument value */
  1673. if (arg->value[0] == '@' && arg->ref)
  1674. err = strbuf_addf(buf, "%s%+ld", arg->value, arg->ref->offset);
  1675. else
  1676. err = strbuf_addstr(buf, arg->value);
  1677. /* Closing */
  1678. while (!err && depth--)
  1679. err = strbuf_addch(buf, ')');
  1680. /* Print argument type */
  1681. if (!err && arg->type)
  1682. err = strbuf_addf(buf, ":%s", arg->type);
  1683. return err;
  1684. }
  1685. char *synthesize_probe_trace_command(struct probe_trace_event *tev)
  1686. {
  1687. struct probe_trace_point *tp = &tev->point;
  1688. struct strbuf buf;
  1689. char *ret = NULL;
  1690. int i, err;
  1691. /* Uprobes must have tp->module */
  1692. if (tev->uprobes && !tp->module)
  1693. return NULL;
  1694. if (strbuf_init(&buf, 32) < 0)
  1695. return NULL;
  1696. if (strbuf_addf(&buf, "%c:%s/%s ", tp->retprobe ? 'r' : 'p',
  1697. tev->group, tev->event) < 0)
  1698. goto error;
  1699. /*
  1700. * If tp->address == 0, then this point must be a
  1701. * absolute address uprobe.
  1702. * try_to_find_absolute_address() should have made
  1703. * tp->symbol to "0x0".
  1704. */
  1705. if (tev->uprobes && !tp->address) {
  1706. if (!tp->symbol || strcmp(tp->symbol, "0x0"))
  1707. goto error;
  1708. }
  1709. /* Use the tp->address for uprobes */
  1710. if (tev->uprobes)
  1711. err = strbuf_addf(&buf, "%s:0x%lx", tp->module, tp->address);
  1712. else if (!strncmp(tp->symbol, "0x", 2))
  1713. /* Absolute address. See try_to_find_absolute_address() */
  1714. err = strbuf_addf(&buf, "%s%s0x%lx", tp->module ?: "",
  1715. tp->module ? ":" : "", tp->address);
  1716. else
  1717. err = strbuf_addf(&buf, "%s%s%s+%lu", tp->module ?: "",
  1718. tp->module ? ":" : "", tp->symbol, tp->offset);
  1719. if (err)
  1720. goto error;
  1721. for (i = 0; i < tev->nargs; i++)
  1722. if (synthesize_probe_trace_arg(&tev->args[i], &buf) < 0)
  1723. goto error;
  1724. ret = strbuf_detach(&buf, NULL);
  1725. error:
  1726. strbuf_release(&buf);
  1727. return ret;
  1728. }
  1729. static int find_perf_probe_point_from_map(struct probe_trace_point *tp,
  1730. struct perf_probe_point *pp,
  1731. bool is_kprobe)
  1732. {
  1733. struct symbol *sym = NULL;
  1734. struct map *map;
  1735. u64 addr = tp->address;
  1736. int ret = -ENOENT;
  1737. if (!is_kprobe) {
  1738. map = dso__new_map(tp->module);
  1739. if (!map)
  1740. goto out;
  1741. sym = map__find_symbol(map, addr, NULL);
  1742. } else {
  1743. if (tp->symbol && !addr) {
  1744. if (kernel_get_symbol_address_by_name(tp->symbol,
  1745. &addr, true, false) < 0)
  1746. goto out;
  1747. }
  1748. if (addr) {
  1749. addr += tp->offset;
  1750. sym = __find_kernel_function(addr, &map);
  1751. }
  1752. }
  1753. if (!sym)
  1754. goto out;
  1755. pp->retprobe = tp->retprobe;
  1756. pp->offset = addr - map->unmap_ip(map, sym->start);
  1757. pp->function = strdup(sym->name);
  1758. ret = pp->function ? 0 : -ENOMEM;
  1759. out:
  1760. if (map && !is_kprobe) {
  1761. map__put(map);
  1762. }
  1763. return ret;
  1764. }
  1765. static int convert_to_perf_probe_point(struct probe_trace_point *tp,
  1766. struct perf_probe_point *pp,
  1767. bool is_kprobe)
  1768. {
  1769. char buf[128];
  1770. int ret;
  1771. ret = find_perf_probe_point_from_dwarf(tp, pp, is_kprobe);
  1772. if (!ret)
  1773. return 0;
  1774. ret = find_perf_probe_point_from_map(tp, pp, is_kprobe);
  1775. if (!ret)
  1776. return 0;
  1777. pr_debug("Failed to find probe point from both of dwarf and map.\n");
  1778. if (tp->symbol) {
  1779. pp->function = strdup(tp->symbol);
  1780. pp->offset = tp->offset;
  1781. } else {
  1782. ret = e_snprintf(buf, 128, "0x%" PRIx64, (u64)tp->address);
  1783. if (ret < 0)
  1784. return ret;
  1785. pp->function = strdup(buf);
  1786. pp->offset = 0;
  1787. }
  1788. if (pp->function == NULL)
  1789. return -ENOMEM;
  1790. pp->retprobe = tp->retprobe;
  1791. return 0;
  1792. }
  1793. static int convert_to_perf_probe_event(struct probe_trace_event *tev,
  1794. struct perf_probe_event *pev, bool is_kprobe)
  1795. {
  1796. struct strbuf buf = STRBUF_INIT;
  1797. int i, ret;
  1798. /* Convert event/group name */
  1799. pev->event = strdup(tev->event);
  1800. pev->group = strdup(tev->group);
  1801. if (pev->event == NULL || pev->group == NULL)
  1802. return -ENOMEM;
  1803. /* Convert trace_point to probe_point */
  1804. ret = convert_to_perf_probe_point(&tev->point, &pev->point, is_kprobe);
  1805. if (ret < 0)
  1806. return ret;
  1807. /* Convert trace_arg to probe_arg */
  1808. pev->nargs = tev->nargs;
  1809. pev->args = zalloc(sizeof(struct perf_probe_arg) * pev->nargs);
  1810. if (pev->args == NULL)
  1811. return -ENOMEM;
  1812. for (i = 0; i < tev->nargs && ret >= 0; i++) {
  1813. if (tev->args[i].name)
  1814. pev->args[i].name = strdup(tev->args[i].name);
  1815. else {
  1816. if ((ret = strbuf_init(&buf, 32)) < 0)
  1817. goto error;
  1818. ret = synthesize_probe_trace_arg(&tev->args[i], &buf);
  1819. pev->args[i].name = strbuf_detach(&buf, NULL);
  1820. }
  1821. if (pev->args[i].name == NULL && ret >= 0)
  1822. ret = -ENOMEM;
  1823. }
  1824. error:
  1825. if (ret < 0)
  1826. clear_perf_probe_event(pev);
  1827. return ret;
  1828. }
  1829. void clear_perf_probe_event(struct perf_probe_event *pev)
  1830. {
  1831. struct perf_probe_arg_field *field, *next;
  1832. int i;
  1833. free(pev->event);
  1834. free(pev->group);
  1835. free(pev->target);
  1836. clear_perf_probe_point(&pev->point);
  1837. for (i = 0; i < pev->nargs; i++) {
  1838. free(pev->args[i].name);
  1839. free(pev->args[i].var);
  1840. free(pev->args[i].type);
  1841. field = pev->args[i].field;
  1842. while (field) {
  1843. next = field->next;
  1844. zfree(&field->name);
  1845. free(field);
  1846. field = next;
  1847. }
  1848. }
  1849. free(pev->args);
  1850. memset(pev, 0, sizeof(*pev));
  1851. }
  1852. #define strdup_or_goto(str, label) \
  1853. ({ char *__p = NULL; if (str && !(__p = strdup(str))) goto label; __p; })
  1854. static int perf_probe_point__copy(struct perf_probe_point *dst,
  1855. struct perf_probe_point *src)
  1856. {
  1857. dst->file = strdup_or_goto(src->file, out_err);
  1858. dst->function = strdup_or_goto(src->function, out_err);
  1859. dst->lazy_line = strdup_or_goto(src->lazy_line, out_err);
  1860. dst->line = src->line;
  1861. dst->retprobe = src->retprobe;
  1862. dst->offset = src->offset;
  1863. return 0;
  1864. out_err:
  1865. clear_perf_probe_point(dst);
  1866. return -ENOMEM;
  1867. }
  1868. static int perf_probe_arg__copy(struct perf_probe_arg *dst,
  1869. struct perf_probe_arg *src)
  1870. {
  1871. struct perf_probe_arg_field *field, **ppfield;
  1872. dst->name = strdup_or_goto(src->name, out_err);
  1873. dst->var = strdup_or_goto(src->var, out_err);
  1874. dst->type = strdup_or_goto(src->type, out_err);
  1875. field = src->field;
  1876. ppfield = &(dst->field);
  1877. while (field) {
  1878. *ppfield = zalloc(sizeof(*field));
  1879. if (!*ppfield)
  1880. goto out_err;
  1881. (*ppfield)->name = strdup_or_goto(field->name, out_err);
  1882. (*ppfield)->index = field->index;
  1883. (*ppfield)->ref = field->ref;
  1884. field = field->next;
  1885. ppfield = &((*ppfield)->next);
  1886. }
  1887. return 0;
  1888. out_err:
  1889. return -ENOMEM;
  1890. }
  1891. int perf_probe_event__copy(struct perf_probe_event *dst,
  1892. struct perf_probe_event *src)
  1893. {
  1894. int i;
  1895. dst->event = strdup_or_goto(src->event, out_err);
  1896. dst->group = strdup_or_goto(src->group, out_err);
  1897. dst->target = strdup_or_goto(src->target, out_err);
  1898. dst->uprobes = src->uprobes;
  1899. if (perf_probe_point__copy(&dst->point, &src->point) < 0)
  1900. goto out_err;
  1901. dst->args = zalloc(sizeof(struct perf_probe_arg) * src->nargs);
  1902. if (!dst->args)
  1903. goto out_err;
  1904. dst->nargs = src->nargs;
  1905. for (i = 0; i < src->nargs; i++)
  1906. if (perf_probe_arg__copy(&dst->args[i], &src->args[i]) < 0)
  1907. goto out_err;
  1908. return 0;
  1909. out_err:
  1910. clear_perf_probe_event(dst);
  1911. return -ENOMEM;
  1912. }
  1913. void clear_probe_trace_event(struct probe_trace_event *tev)
  1914. {
  1915. struct probe_trace_arg_ref *ref, *next;
  1916. int i;
  1917. free(tev->event);
  1918. free(tev->group);
  1919. free(tev->point.symbol);
  1920. free(tev->point.realname);
  1921. free(tev->point.module);
  1922. for (i = 0; i < tev->nargs; i++) {
  1923. free(tev->args[i].name);
  1924. free(tev->args[i].value);
  1925. free(tev->args[i].type);
  1926. ref = tev->args[i].ref;
  1927. while (ref) {
  1928. next = ref->next;
  1929. free(ref);
  1930. ref = next;
  1931. }
  1932. }
  1933. free(tev->args);
  1934. memset(tev, 0, sizeof(*tev));
  1935. }
  1936. struct kprobe_blacklist_node {
  1937. struct list_head list;
  1938. unsigned long start;
  1939. unsigned long end;
  1940. char *symbol;
  1941. };
  1942. static void kprobe_blacklist__delete(struct list_head *blacklist)
  1943. {
  1944. struct kprobe_blacklist_node *node;
  1945. while (!list_empty(blacklist)) {
  1946. node = list_first_entry(blacklist,
  1947. struct kprobe_blacklist_node, list);
  1948. list_del(&node->list);
  1949. free(node->symbol);
  1950. free(node);
  1951. }
  1952. }
  1953. static int kprobe_blacklist__load(struct list_head *blacklist)
  1954. {
  1955. struct kprobe_blacklist_node *node;
  1956. const char *__debugfs = debugfs__mountpoint();
  1957. char buf[PATH_MAX], *p;
  1958. FILE *fp;
  1959. int ret;
  1960. if (__debugfs == NULL)
  1961. return -ENOTSUP;
  1962. ret = e_snprintf(buf, PATH_MAX, "%s/kprobes/blacklist", __debugfs);
  1963. if (ret < 0)
  1964. return ret;
  1965. fp = fopen(buf, "r");
  1966. if (!fp)
  1967. return -errno;
  1968. ret = 0;
  1969. while (fgets(buf, PATH_MAX, fp)) {
  1970. node = zalloc(sizeof(*node));
  1971. if (!node) {
  1972. ret = -ENOMEM;
  1973. break;
  1974. }
  1975. INIT_LIST_HEAD(&node->list);
  1976. list_add_tail(&node->list, blacklist);
  1977. if (sscanf(buf, "0x%lx-0x%lx", &node->start, &node->end) != 2) {
  1978. ret = -EINVAL;
  1979. break;
  1980. }
  1981. p = strchr(buf, '\t');
  1982. if (p) {
  1983. p++;
  1984. if (p[strlen(p) - 1] == '\n')
  1985. p[strlen(p) - 1] = '\0';
  1986. } else
  1987. p = (char *)"unknown";
  1988. node->symbol = strdup(p);
  1989. if (!node->symbol) {
  1990. ret = -ENOMEM;
  1991. break;
  1992. }
  1993. pr_debug2("Blacklist: 0x%lx-0x%lx, %s\n",
  1994. node->start, node->end, node->symbol);
  1995. ret++;
  1996. }
  1997. if (ret < 0)
  1998. kprobe_blacklist__delete(blacklist);
  1999. fclose(fp);
  2000. return ret;
  2001. }
  2002. static struct kprobe_blacklist_node *
  2003. kprobe_blacklist__find_by_address(struct list_head *blacklist,
  2004. unsigned long address)
  2005. {
  2006. struct kprobe_blacklist_node *node;
  2007. list_for_each_entry(node, blacklist, list) {
  2008. if (node->start <= address && address <= node->end)
  2009. return node;
  2010. }
  2011. return NULL;
  2012. }
  2013. static LIST_HEAD(kprobe_blacklist);
  2014. static void kprobe_blacklist__init(void)
  2015. {
  2016. if (!list_empty(&kprobe_blacklist))
  2017. return;
  2018. if (kprobe_blacklist__load(&kprobe_blacklist) < 0)
  2019. pr_debug("No kprobe blacklist support, ignored\n");
  2020. }
  2021. static void kprobe_blacklist__release(void)
  2022. {
  2023. kprobe_blacklist__delete(&kprobe_blacklist);
  2024. }
  2025. static bool kprobe_blacklist__listed(unsigned long address)
  2026. {
  2027. return !!kprobe_blacklist__find_by_address(&kprobe_blacklist, address);
  2028. }
  2029. static int perf_probe_event__sprintf(const char *group, const char *event,
  2030. struct perf_probe_event *pev,
  2031. const char *module,
  2032. struct strbuf *result)
  2033. {
  2034. int i, ret;
  2035. char *buf;
  2036. if (asprintf(&buf, "%s:%s", group, event) < 0)
  2037. return -errno;
  2038. ret = strbuf_addf(result, " %-20s (on ", buf);
  2039. free(buf);
  2040. if (ret)
  2041. return ret;
  2042. /* Synthesize only event probe point */
  2043. buf = synthesize_perf_probe_point(&pev->point);
  2044. if (!buf)
  2045. return -ENOMEM;
  2046. ret = strbuf_addstr(result, buf);
  2047. free(buf);
  2048. if (!ret && module)
  2049. ret = strbuf_addf(result, " in %s", module);
  2050. if (!ret && pev->nargs > 0) {
  2051. ret = strbuf_add(result, " with", 5);
  2052. for (i = 0; !ret && i < pev->nargs; i++) {
  2053. buf = synthesize_perf_probe_arg(&pev->args[i]);
  2054. if (!buf)
  2055. return -ENOMEM;
  2056. ret = strbuf_addf(result, " %s", buf);
  2057. free(buf);
  2058. }
  2059. }
  2060. if (!ret)
  2061. ret = strbuf_addch(result, ')');
  2062. return ret;
  2063. }
  2064. /* Show an event */
  2065. int show_perf_probe_event(const char *group, const char *event,
  2066. struct perf_probe_event *pev,
  2067. const char *module, bool use_stdout)
  2068. {
  2069. struct strbuf buf = STRBUF_INIT;
  2070. int ret;
  2071. ret = perf_probe_event__sprintf(group, event, pev, module, &buf);
  2072. if (ret >= 0) {
  2073. if (use_stdout)
  2074. printf("%s\n", buf.buf);
  2075. else
  2076. pr_info("%s\n", buf.buf);
  2077. }
  2078. strbuf_release(&buf);
  2079. return ret;
  2080. }
  2081. static bool filter_probe_trace_event(struct probe_trace_event *tev,
  2082. struct strfilter *filter)
  2083. {
  2084. char tmp[128];
  2085. /* At first, check the event name itself */
  2086. if (strfilter__compare(filter, tev->event))
  2087. return true;
  2088. /* Next, check the combination of name and group */
  2089. if (e_snprintf(tmp, 128, "%s:%s", tev->group, tev->event) < 0)
  2090. return false;
  2091. return strfilter__compare(filter, tmp);
  2092. }
  2093. static int __show_perf_probe_events(int fd, bool is_kprobe,
  2094. struct strfilter *filter)
  2095. {
  2096. int ret = 0;
  2097. struct probe_trace_event tev;
  2098. struct perf_probe_event pev;
  2099. struct strlist *rawlist;
  2100. struct str_node *ent;
  2101. memset(&tev, 0, sizeof(tev));
  2102. memset(&pev, 0, sizeof(pev));
  2103. rawlist = probe_file__get_rawlist(fd);
  2104. if (!rawlist)
  2105. return -ENOMEM;
  2106. strlist__for_each_entry(ent, rawlist) {
  2107. ret = parse_probe_trace_command(ent->s, &tev);
  2108. if (ret >= 0) {
  2109. if (!filter_probe_trace_event(&tev, filter))
  2110. goto next;
  2111. ret = convert_to_perf_probe_event(&tev, &pev,
  2112. is_kprobe);
  2113. if (ret < 0)
  2114. goto next;
  2115. ret = show_perf_probe_event(pev.group, pev.event,
  2116. &pev, tev.point.module,
  2117. true);
  2118. }
  2119. next:
  2120. clear_perf_probe_event(&pev);
  2121. clear_probe_trace_event(&tev);
  2122. if (ret < 0)
  2123. break;
  2124. }
  2125. strlist__delete(rawlist);
  2126. /* Cleanup cached debuginfo if needed */
  2127. debuginfo_cache__exit();
  2128. return ret;
  2129. }
  2130. /* List up current perf-probe events */
  2131. int show_perf_probe_events(struct strfilter *filter)
  2132. {
  2133. int kp_fd, up_fd, ret;
  2134. setup_pager();
  2135. if (probe_conf.cache)
  2136. return probe_cache__show_all_caches(filter);
  2137. ret = init_probe_symbol_maps(false);
  2138. if (ret < 0)
  2139. return ret;
  2140. ret = probe_file__open_both(&kp_fd, &up_fd, 0);
  2141. if (ret < 0)
  2142. return ret;
  2143. if (kp_fd >= 0)
  2144. ret = __show_perf_probe_events(kp_fd, true, filter);
  2145. if (up_fd >= 0 && ret >= 0)
  2146. ret = __show_perf_probe_events(up_fd, false, filter);
  2147. if (kp_fd > 0)
  2148. close(kp_fd);
  2149. if (up_fd > 0)
  2150. close(up_fd);
  2151. exit_probe_symbol_maps();
  2152. return ret;
  2153. }
  2154. static int get_new_event_name(char *buf, size_t len, const char *base,
  2155. struct strlist *namelist, bool allow_suffix)
  2156. {
  2157. int i, ret;
  2158. char *p, *nbase;
  2159. if (*base == '.')
  2160. base++;
  2161. nbase = strdup(base);
  2162. if (!nbase)
  2163. return -ENOMEM;
  2164. /* Cut off the dot suffixes (e.g. .const, .isra)*/
  2165. p = strchr(nbase, '.');
  2166. if (p && p != nbase)
  2167. *p = '\0';
  2168. /* Try no suffix number */
  2169. ret = e_snprintf(buf, len, "%s", nbase);
  2170. if (ret < 0) {
  2171. pr_debug("snprintf() failed: %d\n", ret);
  2172. goto out;
  2173. }
  2174. if (!strlist__has_entry(namelist, buf))
  2175. goto out;
  2176. if (!allow_suffix) {
  2177. pr_warning("Error: event \"%s\" already exists.\n"
  2178. " Hint: Remove existing event by 'perf probe -d'\n"
  2179. " or force duplicates by 'perf probe -f'\n"
  2180. " or set 'force=yes' in BPF source.\n",
  2181. buf);
  2182. ret = -EEXIST;
  2183. goto out;
  2184. }
  2185. /* Try to add suffix */
  2186. for (i = 1; i < MAX_EVENT_INDEX; i++) {
  2187. ret = e_snprintf(buf, len, "%s_%d", nbase, i);
  2188. if (ret < 0) {
  2189. pr_debug("snprintf() failed: %d\n", ret);
  2190. goto out;
  2191. }
  2192. if (!strlist__has_entry(namelist, buf))
  2193. break;
  2194. }
  2195. if (i == MAX_EVENT_INDEX) {
  2196. pr_warning("Too many events are on the same function.\n");
  2197. ret = -ERANGE;
  2198. }
  2199. out:
  2200. free(nbase);
  2201. return ret;
  2202. }
  2203. /* Warn if the current kernel's uprobe implementation is old */
  2204. static void warn_uprobe_event_compat(struct probe_trace_event *tev)
  2205. {
  2206. int i;
  2207. char *buf = synthesize_probe_trace_command(tev);
  2208. /* Old uprobe event doesn't support memory dereference */
  2209. if (!tev->uprobes || tev->nargs == 0 || !buf)
  2210. goto out;
  2211. for (i = 0; i < tev->nargs; i++)
  2212. if (strglobmatch(tev->args[i].value, "[$@+-]*")) {
  2213. pr_warning("Please upgrade your kernel to at least "
  2214. "3.14 to have access to feature %s\n",
  2215. tev->args[i].value);
  2216. break;
  2217. }
  2218. out:
  2219. free(buf);
  2220. }
  2221. /* Set new name from original perf_probe_event and namelist */
  2222. static int probe_trace_event__set_name(struct probe_trace_event *tev,
  2223. struct perf_probe_event *pev,
  2224. struct strlist *namelist,
  2225. bool allow_suffix)
  2226. {
  2227. const char *event, *group;
  2228. char buf[64];
  2229. int ret;
  2230. /* If probe_event or trace_event already have the name, reuse it */
  2231. if (pev->event && !pev->sdt)
  2232. event = pev->event;
  2233. else if (tev->event)
  2234. event = tev->event;
  2235. else {
  2236. /* Or generate new one from probe point */
  2237. if (pev->point.function &&
  2238. (strncmp(pev->point.function, "0x", 2) != 0) &&
  2239. !strisglob(pev->point.function))
  2240. event = pev->point.function;
  2241. else
  2242. event = tev->point.realname;
  2243. }
  2244. if (pev->group && !pev->sdt)
  2245. group = pev->group;
  2246. else if (tev->group)
  2247. group = tev->group;
  2248. else
  2249. group = PERFPROBE_GROUP;
  2250. /* Get an unused new event name */
  2251. ret = get_new_event_name(buf, 64, event,
  2252. namelist, allow_suffix);
  2253. if (ret < 0)
  2254. return ret;
  2255. event = buf;
  2256. tev->event = strdup(event);
  2257. tev->group = strdup(group);
  2258. if (tev->event == NULL || tev->group == NULL)
  2259. return -ENOMEM;
  2260. /* Add added event name to namelist */
  2261. strlist__add(namelist, event);
  2262. return 0;
  2263. }
  2264. static int __open_probe_file_and_namelist(bool uprobe,
  2265. struct strlist **namelist)
  2266. {
  2267. int fd;
  2268. fd = probe_file__open(PF_FL_RW | (uprobe ? PF_FL_UPROBE : 0));
  2269. if (fd < 0)
  2270. return fd;
  2271. /* Get current event names */
  2272. *namelist = probe_file__get_namelist(fd);
  2273. if (!(*namelist)) {
  2274. pr_debug("Failed to get current event list.\n");
  2275. close(fd);
  2276. return -ENOMEM;
  2277. }
  2278. return fd;
  2279. }
  2280. static int __add_probe_trace_events(struct perf_probe_event *pev,
  2281. struct probe_trace_event *tevs,
  2282. int ntevs, bool allow_suffix)
  2283. {
  2284. int i, fd[2] = {-1, -1}, up, ret;
  2285. struct probe_trace_event *tev = NULL;
  2286. struct probe_cache *cache = NULL;
  2287. struct strlist *namelist[2] = {NULL, NULL};
  2288. up = pev->uprobes ? 1 : 0;
  2289. fd[up] = __open_probe_file_and_namelist(up, &namelist[up]);
  2290. if (fd[up] < 0)
  2291. return fd[up];
  2292. ret = 0;
  2293. for (i = 0; i < ntevs; i++) {
  2294. tev = &tevs[i];
  2295. up = tev->uprobes ? 1 : 0;
  2296. if (fd[up] == -1) { /* Open the kprobe/uprobe_events */
  2297. fd[up] = __open_probe_file_and_namelist(up,
  2298. &namelist[up]);
  2299. if (fd[up] < 0)
  2300. goto close_out;
  2301. }
  2302. /* Skip if the symbol is out of .text or blacklisted */
  2303. if (!tev->point.symbol && !pev->uprobes)
  2304. continue;
  2305. /* Set new name for tev (and update namelist) */
  2306. ret = probe_trace_event__set_name(tev, pev, namelist[up],
  2307. allow_suffix);
  2308. if (ret < 0)
  2309. break;
  2310. ret = probe_file__add_event(fd[up], tev);
  2311. if (ret < 0)
  2312. break;
  2313. /*
  2314. * Probes after the first probe which comes from same
  2315. * user input are always allowed to add suffix, because
  2316. * there might be several addresses corresponding to
  2317. * one code line.
  2318. */
  2319. allow_suffix = true;
  2320. }
  2321. if (ret == -EINVAL && pev->uprobes)
  2322. warn_uprobe_event_compat(tev);
  2323. if (ret == 0 && probe_conf.cache) {
  2324. cache = probe_cache__new(pev->target);
  2325. if (!cache ||
  2326. probe_cache__add_entry(cache, pev, tevs, ntevs) < 0 ||
  2327. probe_cache__commit(cache) < 0)
  2328. pr_warning("Failed to add event to probe cache\n");
  2329. probe_cache__delete(cache);
  2330. }
  2331. close_out:
  2332. for (up = 0; up < 2; up++) {
  2333. strlist__delete(namelist[up]);
  2334. if (fd[up] >= 0)
  2335. close(fd[up]);
  2336. }
  2337. return ret;
  2338. }
  2339. static int find_probe_functions(struct map *map, char *name,
  2340. struct symbol **syms)
  2341. {
  2342. int found = 0;
  2343. struct symbol *sym;
  2344. struct rb_node *tmp;
  2345. if (map__load(map, NULL) < 0)
  2346. return 0;
  2347. map__for_each_symbol(map, sym, tmp) {
  2348. if (strglobmatch(sym->name, name)) {
  2349. found++;
  2350. if (syms && found < probe_conf.max_probes)
  2351. syms[found - 1] = sym;
  2352. }
  2353. }
  2354. return found;
  2355. }
  2356. void __weak arch__fix_tev_from_maps(struct perf_probe_event *pev __maybe_unused,
  2357. struct probe_trace_event *tev __maybe_unused,
  2358. struct map *map __maybe_unused,
  2359. struct symbol *sym __maybe_unused) { }
  2360. /*
  2361. * Find probe function addresses from map.
  2362. * Return an error or the number of found probe_trace_event
  2363. */
  2364. static int find_probe_trace_events_from_map(struct perf_probe_event *pev,
  2365. struct probe_trace_event **tevs)
  2366. {
  2367. struct map *map = NULL;
  2368. struct ref_reloc_sym *reloc_sym = NULL;
  2369. struct symbol *sym;
  2370. struct symbol **syms = NULL;
  2371. struct probe_trace_event *tev;
  2372. struct perf_probe_point *pp = &pev->point;
  2373. struct probe_trace_point *tp;
  2374. int num_matched_functions;
  2375. int ret, i, j, skipped = 0;
  2376. char *mod_name;
  2377. map = get_target_map(pev->target, pev->uprobes);
  2378. if (!map) {
  2379. ret = -EINVAL;
  2380. goto out;
  2381. }
  2382. syms = malloc(sizeof(struct symbol *) * probe_conf.max_probes);
  2383. if (!syms) {
  2384. ret = -ENOMEM;
  2385. goto out;
  2386. }
  2387. /*
  2388. * Load matched symbols: Since the different local symbols may have
  2389. * same name but different addresses, this lists all the symbols.
  2390. */
  2391. num_matched_functions = find_probe_functions(map, pp->function, syms);
  2392. if (num_matched_functions == 0) {
  2393. pr_err("Failed to find symbol %s in %s\n", pp->function,
  2394. pev->target ? : "kernel");
  2395. ret = -ENOENT;
  2396. goto out;
  2397. } else if (num_matched_functions > probe_conf.max_probes) {
  2398. pr_err("Too many functions matched in %s\n",
  2399. pev->target ? : "kernel");
  2400. ret = -E2BIG;
  2401. goto out;
  2402. }
  2403. /* Note that the symbols in the kmodule are not relocated */
  2404. if (!pev->uprobes && !pp->retprobe && !pev->target) {
  2405. reloc_sym = kernel_get_ref_reloc_sym();
  2406. if (!reloc_sym) {
  2407. pr_warning("Relocated base symbol is not found!\n");
  2408. ret = -EINVAL;
  2409. goto out;
  2410. }
  2411. }
  2412. /* Setup result trace-probe-events */
  2413. *tevs = zalloc(sizeof(*tev) * num_matched_functions);
  2414. if (!*tevs) {
  2415. ret = -ENOMEM;
  2416. goto out;
  2417. }
  2418. ret = 0;
  2419. for (j = 0; j < num_matched_functions; j++) {
  2420. sym = syms[j];
  2421. tev = (*tevs) + ret;
  2422. tp = &tev->point;
  2423. if (ret == num_matched_functions) {
  2424. pr_warning("Too many symbols are listed. Skip it.\n");
  2425. break;
  2426. }
  2427. ret++;
  2428. if (pp->offset > sym->end - sym->start) {
  2429. pr_warning("Offset %ld is bigger than the size of %s\n",
  2430. pp->offset, sym->name);
  2431. ret = -ENOENT;
  2432. goto err_out;
  2433. }
  2434. /* Add one probe point */
  2435. tp->address = map->unmap_ip(map, sym->start) + pp->offset;
  2436. /* Check the kprobe (not in module) is within .text */
  2437. if (!pev->uprobes && !pev->target &&
  2438. kprobe_warn_out_range(sym->name, tp->address)) {
  2439. tp->symbol = NULL; /* Skip it */
  2440. skipped++;
  2441. } else if (reloc_sym) {
  2442. tp->symbol = strdup_or_goto(reloc_sym->name, nomem_out);
  2443. tp->offset = tp->address - reloc_sym->addr;
  2444. } else {
  2445. tp->symbol = strdup_or_goto(sym->name, nomem_out);
  2446. tp->offset = pp->offset;
  2447. }
  2448. tp->realname = strdup_or_goto(sym->name, nomem_out);
  2449. tp->retprobe = pp->retprobe;
  2450. if (pev->target) {
  2451. if (pev->uprobes) {
  2452. tev->point.module = strdup_or_goto(pev->target,
  2453. nomem_out);
  2454. } else {
  2455. mod_name = find_module_name(pev->target);
  2456. tev->point.module =
  2457. strdup(mod_name ? mod_name : pev->target);
  2458. free(mod_name);
  2459. if (!tev->point.module)
  2460. goto nomem_out;
  2461. }
  2462. }
  2463. tev->uprobes = pev->uprobes;
  2464. tev->nargs = pev->nargs;
  2465. if (tev->nargs) {
  2466. tev->args = zalloc(sizeof(struct probe_trace_arg) *
  2467. tev->nargs);
  2468. if (tev->args == NULL)
  2469. goto nomem_out;
  2470. }
  2471. for (i = 0; i < tev->nargs; i++) {
  2472. if (pev->args[i].name)
  2473. tev->args[i].name =
  2474. strdup_or_goto(pev->args[i].name,
  2475. nomem_out);
  2476. tev->args[i].value = strdup_or_goto(pev->args[i].var,
  2477. nomem_out);
  2478. if (pev->args[i].type)
  2479. tev->args[i].type =
  2480. strdup_or_goto(pev->args[i].type,
  2481. nomem_out);
  2482. }
  2483. arch__fix_tev_from_maps(pev, tev, map, sym);
  2484. }
  2485. if (ret == skipped) {
  2486. ret = -ENOENT;
  2487. goto err_out;
  2488. }
  2489. out:
  2490. put_target_map(map, pev->uprobes);
  2491. free(syms);
  2492. return ret;
  2493. nomem_out:
  2494. ret = -ENOMEM;
  2495. err_out:
  2496. clear_probe_trace_events(*tevs, num_matched_functions);
  2497. zfree(tevs);
  2498. goto out;
  2499. }
  2500. static int try_to_find_absolute_address(struct perf_probe_event *pev,
  2501. struct probe_trace_event **tevs)
  2502. {
  2503. struct perf_probe_point *pp = &pev->point;
  2504. struct probe_trace_event *tev;
  2505. struct probe_trace_point *tp;
  2506. int i, err;
  2507. if (!(pev->point.function && !strncmp(pev->point.function, "0x", 2)))
  2508. return -EINVAL;
  2509. if (perf_probe_event_need_dwarf(pev))
  2510. return -EINVAL;
  2511. /*
  2512. * This is 'perf probe /lib/libc.so 0xabcd'. Try to probe at
  2513. * absolute address.
  2514. *
  2515. * Only one tev can be generated by this.
  2516. */
  2517. *tevs = zalloc(sizeof(*tev));
  2518. if (!*tevs)
  2519. return -ENOMEM;
  2520. tev = *tevs;
  2521. tp = &tev->point;
  2522. /*
  2523. * Don't use tp->offset, use address directly, because
  2524. * in synthesize_probe_trace_command() address cannot be
  2525. * zero.
  2526. */
  2527. tp->address = pev->point.abs_address;
  2528. tp->retprobe = pp->retprobe;
  2529. tev->uprobes = pev->uprobes;
  2530. err = -ENOMEM;
  2531. /*
  2532. * Give it a '0x' leading symbol name.
  2533. * In __add_probe_trace_events, a NULL symbol is interpreted as
  2534. * invalud.
  2535. */
  2536. if (asprintf(&tp->symbol, "0x%lx", tp->address) < 0)
  2537. goto errout;
  2538. /* For kprobe, check range */
  2539. if ((!tev->uprobes) &&
  2540. (kprobe_warn_out_range(tev->point.symbol,
  2541. tev->point.address))) {
  2542. err = -EACCES;
  2543. goto errout;
  2544. }
  2545. if (asprintf(&tp->realname, "abs_%lx", tp->address) < 0)
  2546. goto errout;
  2547. if (pev->target) {
  2548. tp->module = strdup(pev->target);
  2549. if (!tp->module)
  2550. goto errout;
  2551. }
  2552. if (tev->group) {
  2553. tev->group = strdup(pev->group);
  2554. if (!tev->group)
  2555. goto errout;
  2556. }
  2557. if (pev->event) {
  2558. tev->event = strdup(pev->event);
  2559. if (!tev->event)
  2560. goto errout;
  2561. }
  2562. tev->nargs = pev->nargs;
  2563. tev->args = zalloc(sizeof(struct probe_trace_arg) * tev->nargs);
  2564. if (!tev->args) {
  2565. err = -ENOMEM;
  2566. goto errout;
  2567. }
  2568. for (i = 0; i < tev->nargs; i++)
  2569. copy_to_probe_trace_arg(&tev->args[i], &pev->args[i]);
  2570. return 1;
  2571. errout:
  2572. if (*tevs) {
  2573. clear_probe_trace_events(*tevs, 1);
  2574. *tevs = NULL;
  2575. }
  2576. return err;
  2577. }
  2578. /* Concatinate two arrays */
  2579. static void *memcat(void *a, size_t sz_a, void *b, size_t sz_b)
  2580. {
  2581. void *ret;
  2582. ret = malloc(sz_a + sz_b);
  2583. if (ret) {
  2584. memcpy(ret, a, sz_a);
  2585. memcpy(ret + sz_a, b, sz_b);
  2586. }
  2587. return ret;
  2588. }
  2589. static int
  2590. concat_probe_trace_events(struct probe_trace_event **tevs, int *ntevs,
  2591. struct probe_trace_event **tevs2, int ntevs2)
  2592. {
  2593. struct probe_trace_event *new_tevs;
  2594. int ret = 0;
  2595. if (ntevs == 0) {
  2596. *tevs = *tevs2;
  2597. *ntevs = ntevs2;
  2598. *tevs2 = NULL;
  2599. return 0;
  2600. }
  2601. if (*ntevs + ntevs2 > probe_conf.max_probes)
  2602. ret = -E2BIG;
  2603. else {
  2604. /* Concatinate the array of probe_trace_event */
  2605. new_tevs = memcat(*tevs, (*ntevs) * sizeof(**tevs),
  2606. *tevs2, ntevs2 * sizeof(**tevs2));
  2607. if (!new_tevs)
  2608. ret = -ENOMEM;
  2609. else {
  2610. free(*tevs);
  2611. *tevs = new_tevs;
  2612. *ntevs += ntevs2;
  2613. }
  2614. }
  2615. if (ret < 0)
  2616. clear_probe_trace_events(*tevs2, ntevs2);
  2617. zfree(tevs2);
  2618. return ret;
  2619. }
  2620. /*
  2621. * Try to find probe_trace_event from given probe caches. Return the number
  2622. * of cached events found, if an error occurs return the error.
  2623. */
  2624. static int find_cached_events(struct perf_probe_event *pev,
  2625. struct probe_trace_event **tevs,
  2626. const char *target)
  2627. {
  2628. struct probe_cache *cache;
  2629. struct probe_cache_entry *entry;
  2630. struct probe_trace_event *tmp_tevs = NULL;
  2631. int ntevs = 0;
  2632. int ret = 0;
  2633. cache = probe_cache__new(target);
  2634. /* Return 0 ("not found") if the target has no probe cache. */
  2635. if (!cache)
  2636. return 0;
  2637. for_each_probe_cache_entry(entry, cache) {
  2638. /* Skip the cache entry which has no name */
  2639. if (!entry->pev.event || !entry->pev.group)
  2640. continue;
  2641. if ((!pev->group || strglobmatch(entry->pev.group, pev->group)) &&
  2642. strglobmatch(entry->pev.event, pev->event)) {
  2643. ret = probe_cache_entry__get_event(entry, &tmp_tevs);
  2644. if (ret > 0)
  2645. ret = concat_probe_trace_events(tevs, &ntevs,
  2646. &tmp_tevs, ret);
  2647. if (ret < 0)
  2648. break;
  2649. }
  2650. }
  2651. probe_cache__delete(cache);
  2652. if (ret < 0) {
  2653. clear_probe_trace_events(*tevs, ntevs);
  2654. zfree(tevs);
  2655. } else {
  2656. ret = ntevs;
  2657. if (ntevs > 0 && target && target[0] == '/')
  2658. pev->uprobes = true;
  2659. }
  2660. return ret;
  2661. }
  2662. /* Try to find probe_trace_event from all probe caches */
  2663. static int find_cached_events_all(struct perf_probe_event *pev,
  2664. struct probe_trace_event **tevs)
  2665. {
  2666. struct probe_trace_event *tmp_tevs = NULL;
  2667. struct strlist *bidlist;
  2668. struct str_node *nd;
  2669. char *pathname;
  2670. int ntevs = 0;
  2671. int ret;
  2672. /* Get the buildid list of all valid caches */
  2673. bidlist = build_id_cache__list_all(true);
  2674. if (!bidlist) {
  2675. ret = -errno;
  2676. pr_debug("Failed to get buildids: %d\n", ret);
  2677. return ret;
  2678. }
  2679. ret = 0;
  2680. strlist__for_each_entry(nd, bidlist) {
  2681. pathname = build_id_cache__origname(nd->s);
  2682. ret = find_cached_events(pev, &tmp_tevs, pathname);
  2683. /* In the case of cnt == 0, we just skip it */
  2684. if (ret > 0)
  2685. ret = concat_probe_trace_events(tevs, &ntevs,
  2686. &tmp_tevs, ret);
  2687. free(pathname);
  2688. if (ret < 0)
  2689. break;
  2690. }
  2691. strlist__delete(bidlist);
  2692. if (ret < 0) {
  2693. clear_probe_trace_events(*tevs, ntevs);
  2694. zfree(tevs);
  2695. } else
  2696. ret = ntevs;
  2697. return ret;
  2698. }
  2699. static int find_probe_trace_events_from_cache(struct perf_probe_event *pev,
  2700. struct probe_trace_event **tevs)
  2701. {
  2702. struct probe_cache *cache;
  2703. struct probe_cache_entry *entry;
  2704. struct probe_trace_event *tev;
  2705. struct str_node *node;
  2706. int ret, i;
  2707. if (pev->sdt) {
  2708. /* For SDT/cached events, we use special search functions */
  2709. if (!pev->target)
  2710. return find_cached_events_all(pev, tevs);
  2711. else
  2712. return find_cached_events(pev, tevs, pev->target);
  2713. }
  2714. cache = probe_cache__new(pev->target);
  2715. if (!cache)
  2716. return 0;
  2717. entry = probe_cache__find(cache, pev);
  2718. if (!entry) {
  2719. /* SDT must be in the cache */
  2720. ret = pev->sdt ? -ENOENT : 0;
  2721. goto out;
  2722. }
  2723. ret = strlist__nr_entries(entry->tevlist);
  2724. if (ret > probe_conf.max_probes) {
  2725. pr_debug("Too many entries matched in the cache of %s\n",
  2726. pev->target ? : "kernel");
  2727. ret = -E2BIG;
  2728. goto out;
  2729. }
  2730. *tevs = zalloc(ret * sizeof(*tev));
  2731. if (!*tevs) {
  2732. ret = -ENOMEM;
  2733. goto out;
  2734. }
  2735. i = 0;
  2736. strlist__for_each_entry(node, entry->tevlist) {
  2737. tev = &(*tevs)[i++];
  2738. ret = parse_probe_trace_command(node->s, tev);
  2739. if (ret < 0)
  2740. goto out;
  2741. /* Set the uprobes attribute as same as original */
  2742. tev->uprobes = pev->uprobes;
  2743. }
  2744. ret = i;
  2745. out:
  2746. probe_cache__delete(cache);
  2747. return ret;
  2748. }
  2749. static int convert_to_probe_trace_events(struct perf_probe_event *pev,
  2750. struct probe_trace_event **tevs)
  2751. {
  2752. int ret;
  2753. if (!pev->group && !pev->sdt) {
  2754. /* Set group name if not given */
  2755. if (!pev->uprobes) {
  2756. pev->group = strdup(PERFPROBE_GROUP);
  2757. ret = pev->group ? 0 : -ENOMEM;
  2758. } else
  2759. ret = convert_exec_to_group(pev->target, &pev->group);
  2760. if (ret != 0) {
  2761. pr_warning("Failed to make a group name.\n");
  2762. return ret;
  2763. }
  2764. }
  2765. ret = try_to_find_absolute_address(pev, tevs);
  2766. if (ret > 0)
  2767. return ret;
  2768. /* At first, we need to lookup cache entry */
  2769. ret = find_probe_trace_events_from_cache(pev, tevs);
  2770. if (ret > 0 || pev->sdt) /* SDT can be found only in the cache */
  2771. return ret == 0 ? -ENOENT : ret; /* Found in probe cache */
  2772. /* Convert perf_probe_event with debuginfo */
  2773. ret = try_to_find_probe_trace_events(pev, tevs);
  2774. if (ret != 0)
  2775. return ret; /* Found in debuginfo or got an error */
  2776. return find_probe_trace_events_from_map(pev, tevs);
  2777. }
  2778. int convert_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2779. {
  2780. int i, ret;
  2781. /* Loop 1: convert all events */
  2782. for (i = 0; i < npevs; i++) {
  2783. /* Init kprobe blacklist if needed */
  2784. if (!pevs[i].uprobes)
  2785. kprobe_blacklist__init();
  2786. /* Convert with or without debuginfo */
  2787. ret = convert_to_probe_trace_events(&pevs[i], &pevs[i].tevs);
  2788. if (ret < 0)
  2789. return ret;
  2790. pevs[i].ntevs = ret;
  2791. }
  2792. /* This just release blacklist only if allocated */
  2793. kprobe_blacklist__release();
  2794. return 0;
  2795. }
  2796. int apply_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2797. {
  2798. int i, ret = 0;
  2799. /* Loop 2: add all events */
  2800. for (i = 0; i < npevs; i++) {
  2801. ret = __add_probe_trace_events(&pevs[i], pevs[i].tevs,
  2802. pevs[i].ntevs,
  2803. probe_conf.force_add);
  2804. if (ret < 0)
  2805. break;
  2806. }
  2807. return ret;
  2808. }
  2809. void cleanup_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2810. {
  2811. int i, j;
  2812. /* Loop 3: cleanup and free trace events */
  2813. for (i = 0; i < npevs; i++) {
  2814. for (j = 0; j < pevs[i].ntevs; j++)
  2815. clear_probe_trace_event(&pevs[i].tevs[j]);
  2816. zfree(&pevs[i].tevs);
  2817. pevs[i].ntevs = 0;
  2818. clear_perf_probe_event(&pevs[i]);
  2819. }
  2820. }
  2821. int add_perf_probe_events(struct perf_probe_event *pevs, int npevs)
  2822. {
  2823. int ret;
  2824. ret = init_probe_symbol_maps(pevs->uprobes);
  2825. if (ret < 0)
  2826. return ret;
  2827. ret = convert_perf_probe_events(pevs, npevs);
  2828. if (ret == 0)
  2829. ret = apply_perf_probe_events(pevs, npevs);
  2830. cleanup_perf_probe_events(pevs, npevs);
  2831. exit_probe_symbol_maps();
  2832. return ret;
  2833. }
  2834. int del_perf_probe_events(struct strfilter *filter)
  2835. {
  2836. int ret, ret2, ufd = -1, kfd = -1;
  2837. char *str = strfilter__string(filter);
  2838. if (!str)
  2839. return -EINVAL;
  2840. /* Get current event names */
  2841. ret = probe_file__open_both(&kfd, &ufd, PF_FL_RW);
  2842. if (ret < 0)
  2843. goto out;
  2844. ret = probe_file__del_events(kfd, filter);
  2845. if (ret < 0 && ret != -ENOENT)
  2846. goto error;
  2847. ret2 = probe_file__del_events(ufd, filter);
  2848. if (ret2 < 0 && ret2 != -ENOENT) {
  2849. ret = ret2;
  2850. goto error;
  2851. }
  2852. ret = 0;
  2853. error:
  2854. if (kfd >= 0)
  2855. close(kfd);
  2856. if (ufd >= 0)
  2857. close(ufd);
  2858. out:
  2859. free(str);
  2860. return ret;
  2861. }
  2862. /* TODO: don't use a global variable for filter ... */
  2863. static struct strfilter *available_func_filter;
  2864. /*
  2865. * If a symbol corresponds to a function with global binding and
  2866. * matches filter return 0. For all others return 1.
  2867. */
  2868. static int filter_available_functions(struct map *map __maybe_unused,
  2869. struct symbol *sym)
  2870. {
  2871. if (strfilter__compare(available_func_filter, sym->name))
  2872. return 0;
  2873. return 1;
  2874. }
  2875. int show_available_funcs(const char *target, struct strfilter *_filter,
  2876. bool user)
  2877. {
  2878. struct map *map;
  2879. int ret;
  2880. ret = init_probe_symbol_maps(user);
  2881. if (ret < 0)
  2882. return ret;
  2883. /* Get a symbol map */
  2884. if (user)
  2885. map = dso__new_map(target);
  2886. else
  2887. map = kernel_get_module_map(target);
  2888. if (!map) {
  2889. pr_err("Failed to get a map for %s\n", (target) ? : "kernel");
  2890. return -EINVAL;
  2891. }
  2892. /* Load symbols with given filter */
  2893. available_func_filter = _filter;
  2894. ret = map__load(map, filter_available_functions);
  2895. if (ret) {
  2896. if (ret == -2) {
  2897. char *str = strfilter__string(_filter);
  2898. pr_err("Failed to find symbols matched to \"%s\"\n",
  2899. str);
  2900. free(str);
  2901. } else
  2902. pr_err("Failed to load symbols in %s\n",
  2903. (target) ? : "kernel");
  2904. goto end;
  2905. }
  2906. if (!dso__sorted_by_name(map->dso, map->type))
  2907. dso__sort_by_name(map->dso, map->type);
  2908. /* Show all (filtered) symbols */
  2909. setup_pager();
  2910. dso__fprintf_symbols_by_name(map->dso, map->type, stdout);
  2911. end:
  2912. if (user) {
  2913. map__put(map);
  2914. }
  2915. exit_probe_symbol_maps();
  2916. return ret;
  2917. }
  2918. int copy_to_probe_trace_arg(struct probe_trace_arg *tvar,
  2919. struct perf_probe_arg *pvar)
  2920. {
  2921. tvar->value = strdup(pvar->var);
  2922. if (tvar->value == NULL)
  2923. return -ENOMEM;
  2924. if (pvar->type) {
  2925. tvar->type = strdup(pvar->type);
  2926. if (tvar->type == NULL)
  2927. return -ENOMEM;
  2928. }
  2929. if (pvar->name) {
  2930. tvar->name = strdup(pvar->name);
  2931. if (tvar->name == NULL)
  2932. return -ENOMEM;
  2933. } else
  2934. tvar->name = NULL;
  2935. return 0;
  2936. }