debug.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  1. /*
  2. * arch/s390/kernel/debug.c
  3. * S/390 debug facility
  4. *
  5. * Copyright (C) 1999, 2000 IBM Deutschland Entwicklung GmbH,
  6. * IBM Corporation
  7. * Author(s): Michael Holzheu (holzheu@de.ibm.com),
  8. * Holger Smolinski (Holger.Smolinski@de.ibm.com)
  9. *
  10. * Bugreports to: <Linux390@de.ibm.com>
  11. */
  12. #define KMSG_COMPONENT "s390dbf"
  13. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  14. #include <linux/stddef.h>
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/slab.h>
  18. #include <linux/ctype.h>
  19. #include <linux/sysctl.h>
  20. #include <asm/uaccess.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/fs.h>
  24. #include <linux/debugfs.h>
  25. #include <asm/debug.h>
  26. #define DEBUG_PROLOG_ENTRY -1
  27. #define ALL_AREAS 0 /* copy all debug areas */
  28. #define NO_AREAS 1 /* copy no debug areas */
  29. /* typedefs */
  30. typedef struct file_private_info {
  31. loff_t offset; /* offset of last read in file */
  32. int act_area; /* number of last formated area */
  33. int act_page; /* act page in given area */
  34. int act_entry; /* last formated entry (offset */
  35. /* relative to beginning of last */
  36. /* formated page) */
  37. size_t act_entry_offset; /* up to this offset we copied */
  38. /* in last read the last formated */
  39. /* entry to userland */
  40. char temp_buf[2048]; /* buffer for output */
  41. debug_info_t *debug_info_org; /* original debug information */
  42. debug_info_t *debug_info_snap; /* snapshot of debug information */
  43. struct debug_view *view; /* used view of debug info */
  44. } file_private_info_t;
  45. typedef struct
  46. {
  47. char *string;
  48. /*
  49. * This assumes that all args are converted into longs
  50. * on L/390 this is the case for all types of parameter
  51. * except of floats, and long long (32 bit)
  52. *
  53. */
  54. long args[0];
  55. } debug_sprintf_entry_t;
  56. extern void tod_to_timeval(uint64_t todval, struct timespec *xtime);
  57. /* internal function prototyes */
  58. static int debug_init(void);
  59. static ssize_t debug_output(struct file *file, char __user *user_buf,
  60. size_t user_len, loff_t * offset);
  61. static ssize_t debug_input(struct file *file, const char __user *user_buf,
  62. size_t user_len, loff_t * offset);
  63. static int debug_open(struct inode *inode, struct file *file);
  64. static int debug_close(struct inode *inode, struct file *file);
  65. static debug_info_t *debug_info_create(const char *name, int pages_per_area,
  66. int nr_areas, int buf_size, mode_t mode);
  67. static void debug_info_get(debug_info_t *);
  68. static void debug_info_put(debug_info_t *);
  69. static int debug_prolog_level_fn(debug_info_t * id,
  70. struct debug_view *view, char *out_buf);
  71. static int debug_input_level_fn(debug_info_t * id, struct debug_view *view,
  72. struct file *file, const char __user *user_buf,
  73. size_t user_buf_size, loff_t * offset);
  74. static int debug_prolog_pages_fn(debug_info_t * id,
  75. struct debug_view *view, char *out_buf);
  76. static int debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
  77. struct file *file, const char __user *user_buf,
  78. size_t user_buf_size, loff_t * offset);
  79. static int debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
  80. struct file *file, const char __user *user_buf,
  81. size_t user_buf_size, loff_t * offset);
  82. static int debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
  83. char *out_buf, const char *in_buf);
  84. static int debug_raw_format_fn(debug_info_t * id,
  85. struct debug_view *view, char *out_buf,
  86. const char *in_buf);
  87. static int debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
  88. int area, debug_entry_t * entry, char *out_buf);
  89. static int debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
  90. char *out_buf, debug_sprintf_entry_t *curr_event);
  91. /* globals */
  92. struct debug_view debug_raw_view = {
  93. "raw",
  94. NULL,
  95. &debug_raw_header_fn,
  96. &debug_raw_format_fn,
  97. NULL,
  98. NULL
  99. };
  100. struct debug_view debug_hex_ascii_view = {
  101. "hex_ascii",
  102. NULL,
  103. &debug_dflt_header_fn,
  104. &debug_hex_ascii_format_fn,
  105. NULL,
  106. NULL
  107. };
  108. static struct debug_view debug_level_view = {
  109. "level",
  110. &debug_prolog_level_fn,
  111. NULL,
  112. NULL,
  113. &debug_input_level_fn,
  114. NULL
  115. };
  116. static struct debug_view debug_pages_view = {
  117. "pages",
  118. &debug_prolog_pages_fn,
  119. NULL,
  120. NULL,
  121. &debug_input_pages_fn,
  122. NULL
  123. };
  124. static struct debug_view debug_flush_view = {
  125. "flush",
  126. NULL,
  127. NULL,
  128. NULL,
  129. &debug_input_flush_fn,
  130. NULL
  131. };
  132. struct debug_view debug_sprintf_view = {
  133. "sprintf",
  134. NULL,
  135. &debug_dflt_header_fn,
  136. (debug_format_proc_t*)&debug_sprintf_format_fn,
  137. NULL,
  138. NULL
  139. };
  140. /* used by dump analysis tools to determine version of debug feature */
  141. static unsigned int __used debug_feature_version = __DEBUG_FEATURE_VERSION;
  142. /* static globals */
  143. static debug_info_t *debug_area_first = NULL;
  144. static debug_info_t *debug_area_last = NULL;
  145. static DEFINE_MUTEX(debug_mutex);
  146. static int initialized;
  147. static const struct file_operations debug_file_ops = {
  148. .owner = THIS_MODULE,
  149. .read = debug_output,
  150. .write = debug_input,
  151. .open = debug_open,
  152. .release = debug_close,
  153. };
  154. static struct dentry *debug_debugfs_root_entry;
  155. /* functions */
  156. /*
  157. * debug_areas_alloc
  158. * - Debug areas are implemented as a threedimensonal array:
  159. * areas[areanumber][pagenumber][pageoffset]
  160. */
  161. static debug_entry_t***
  162. debug_areas_alloc(int pages_per_area, int nr_areas)
  163. {
  164. debug_entry_t*** areas;
  165. int i,j;
  166. areas = kmalloc(nr_areas *
  167. sizeof(debug_entry_t**),
  168. GFP_KERNEL);
  169. if (!areas)
  170. goto fail_malloc_areas;
  171. for (i = 0; i < nr_areas; i++) {
  172. areas[i] = kmalloc(pages_per_area *
  173. sizeof(debug_entry_t*),GFP_KERNEL);
  174. if (!areas[i]) {
  175. goto fail_malloc_areas2;
  176. }
  177. for(j = 0; j < pages_per_area; j++) {
  178. areas[i][j] = kzalloc(PAGE_SIZE, GFP_KERNEL);
  179. if(!areas[i][j]) {
  180. for(j--; j >=0 ; j--) {
  181. kfree(areas[i][j]);
  182. }
  183. kfree(areas[i]);
  184. goto fail_malloc_areas2;
  185. }
  186. }
  187. }
  188. return areas;
  189. fail_malloc_areas2:
  190. for(i--; i >= 0; i--){
  191. for(j=0; j < pages_per_area;j++){
  192. kfree(areas[i][j]);
  193. }
  194. kfree(areas[i]);
  195. }
  196. kfree(areas);
  197. fail_malloc_areas:
  198. return NULL;
  199. }
  200. /*
  201. * debug_info_alloc
  202. * - alloc new debug-info
  203. */
  204. static debug_info_t*
  205. debug_info_alloc(const char *name, int pages_per_area, int nr_areas,
  206. int buf_size, int level, int mode)
  207. {
  208. debug_info_t* rc;
  209. /* alloc everything */
  210. rc = kmalloc(sizeof(debug_info_t), GFP_KERNEL);
  211. if(!rc)
  212. goto fail_malloc_rc;
  213. rc->active_entries = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
  214. if(!rc->active_entries)
  215. goto fail_malloc_active_entries;
  216. rc->active_pages = kcalloc(nr_areas, sizeof(int), GFP_KERNEL);
  217. if(!rc->active_pages)
  218. goto fail_malloc_active_pages;
  219. if((mode == ALL_AREAS) && (pages_per_area != 0)){
  220. rc->areas = debug_areas_alloc(pages_per_area, nr_areas);
  221. if(!rc->areas)
  222. goto fail_malloc_areas;
  223. } else {
  224. rc->areas = NULL;
  225. }
  226. /* initialize members */
  227. spin_lock_init(&rc->lock);
  228. rc->pages_per_area = pages_per_area;
  229. rc->nr_areas = nr_areas;
  230. rc->active_area = 0;
  231. rc->level = level;
  232. rc->buf_size = buf_size;
  233. rc->entry_size = sizeof(debug_entry_t) + buf_size;
  234. strlcpy(rc->name, name, sizeof(rc->name));
  235. memset(rc->views, 0, DEBUG_MAX_VIEWS * sizeof(struct debug_view *));
  236. memset(rc->debugfs_entries, 0 ,DEBUG_MAX_VIEWS *
  237. sizeof(struct dentry*));
  238. atomic_set(&(rc->ref_count), 0);
  239. return rc;
  240. fail_malloc_areas:
  241. kfree(rc->active_pages);
  242. fail_malloc_active_pages:
  243. kfree(rc->active_entries);
  244. fail_malloc_active_entries:
  245. kfree(rc);
  246. fail_malloc_rc:
  247. return NULL;
  248. }
  249. /*
  250. * debug_areas_free
  251. * - free all debug areas
  252. */
  253. static void
  254. debug_areas_free(debug_info_t* db_info)
  255. {
  256. int i,j;
  257. if(!db_info->areas)
  258. return;
  259. for (i = 0; i < db_info->nr_areas; i++) {
  260. for(j = 0; j < db_info->pages_per_area; j++) {
  261. kfree(db_info->areas[i][j]);
  262. }
  263. kfree(db_info->areas[i]);
  264. }
  265. kfree(db_info->areas);
  266. db_info->areas = NULL;
  267. }
  268. /*
  269. * debug_info_free
  270. * - free memory debug-info
  271. */
  272. static void
  273. debug_info_free(debug_info_t* db_info){
  274. debug_areas_free(db_info);
  275. kfree(db_info->active_entries);
  276. kfree(db_info->active_pages);
  277. kfree(db_info);
  278. }
  279. /*
  280. * debug_info_create
  281. * - create new debug-info
  282. */
  283. static debug_info_t*
  284. debug_info_create(const char *name, int pages_per_area, int nr_areas,
  285. int buf_size, mode_t mode)
  286. {
  287. debug_info_t* rc;
  288. rc = debug_info_alloc(name, pages_per_area, nr_areas, buf_size,
  289. DEBUG_DEFAULT_LEVEL, ALL_AREAS);
  290. if(!rc)
  291. goto out;
  292. rc->mode = mode & ~S_IFMT;
  293. /* create root directory */
  294. rc->debugfs_root_entry = debugfs_create_dir(rc->name,
  295. debug_debugfs_root_entry);
  296. /* append new element to linked list */
  297. if (!debug_area_first) {
  298. /* first element in list */
  299. debug_area_first = rc;
  300. rc->prev = NULL;
  301. } else {
  302. /* append element to end of list */
  303. debug_area_last->next = rc;
  304. rc->prev = debug_area_last;
  305. }
  306. debug_area_last = rc;
  307. rc->next = NULL;
  308. debug_info_get(rc);
  309. out:
  310. return rc;
  311. }
  312. /*
  313. * debug_info_copy
  314. * - copy debug-info
  315. */
  316. static debug_info_t*
  317. debug_info_copy(debug_info_t* in, int mode)
  318. {
  319. int i,j;
  320. debug_info_t* rc;
  321. unsigned long flags;
  322. /* get a consistent copy of the debug areas */
  323. do {
  324. rc = debug_info_alloc(in->name, in->pages_per_area,
  325. in->nr_areas, in->buf_size, in->level, mode);
  326. spin_lock_irqsave(&in->lock, flags);
  327. if(!rc)
  328. goto out;
  329. /* has something changed in the meantime ? */
  330. if((rc->pages_per_area == in->pages_per_area) &&
  331. (rc->nr_areas == in->nr_areas)) {
  332. break;
  333. }
  334. spin_unlock_irqrestore(&in->lock, flags);
  335. debug_info_free(rc);
  336. } while (1);
  337. if (mode == NO_AREAS)
  338. goto out;
  339. for(i = 0; i < in->nr_areas; i++){
  340. for(j = 0; j < in->pages_per_area; j++) {
  341. memcpy(rc->areas[i][j], in->areas[i][j],PAGE_SIZE);
  342. }
  343. }
  344. out:
  345. spin_unlock_irqrestore(&in->lock, flags);
  346. return rc;
  347. }
  348. /*
  349. * debug_info_get
  350. * - increments reference count for debug-info
  351. */
  352. static void
  353. debug_info_get(debug_info_t * db_info)
  354. {
  355. if (db_info)
  356. atomic_inc(&db_info->ref_count);
  357. }
  358. /*
  359. * debug_info_put:
  360. * - decreases reference count for debug-info and frees it if necessary
  361. */
  362. static void
  363. debug_info_put(debug_info_t *db_info)
  364. {
  365. int i;
  366. if (!db_info)
  367. return;
  368. if (atomic_dec_and_test(&db_info->ref_count)) {
  369. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  370. if (!db_info->views[i])
  371. continue;
  372. debugfs_remove(db_info->debugfs_entries[i]);
  373. }
  374. debugfs_remove(db_info->debugfs_root_entry);
  375. if(db_info == debug_area_first)
  376. debug_area_first = db_info->next;
  377. if(db_info == debug_area_last)
  378. debug_area_last = db_info->prev;
  379. if(db_info->prev) db_info->prev->next = db_info->next;
  380. if(db_info->next) db_info->next->prev = db_info->prev;
  381. debug_info_free(db_info);
  382. }
  383. }
  384. /*
  385. * debug_format_entry:
  386. * - format one debug entry and return size of formated data
  387. */
  388. static int
  389. debug_format_entry(file_private_info_t *p_info)
  390. {
  391. debug_info_t *id_snap = p_info->debug_info_snap;
  392. struct debug_view *view = p_info->view;
  393. debug_entry_t *act_entry;
  394. size_t len = 0;
  395. if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
  396. /* print prolog */
  397. if (view->prolog_proc)
  398. len += view->prolog_proc(id_snap,view,p_info->temp_buf);
  399. goto out;
  400. }
  401. if (!id_snap->areas) /* this is true, if we have a prolog only view */
  402. goto out; /* or if 'pages_per_area' is 0 */
  403. act_entry = (debug_entry_t *) ((char*)id_snap->areas[p_info->act_area]
  404. [p_info->act_page] + p_info->act_entry);
  405. if (act_entry->id.stck == 0LL)
  406. goto out; /* empty entry */
  407. if (view->header_proc)
  408. len += view->header_proc(id_snap, view, p_info->act_area,
  409. act_entry, p_info->temp_buf + len);
  410. if (view->format_proc)
  411. len += view->format_proc(id_snap, view, p_info->temp_buf + len,
  412. DEBUG_DATA(act_entry));
  413. out:
  414. return len;
  415. }
  416. /*
  417. * debug_next_entry:
  418. * - goto next entry in p_info
  419. */
  420. static inline int
  421. debug_next_entry(file_private_info_t *p_info)
  422. {
  423. debug_info_t *id;
  424. id = p_info->debug_info_snap;
  425. if(p_info->act_entry == DEBUG_PROLOG_ENTRY){
  426. p_info->act_entry = 0;
  427. p_info->act_page = 0;
  428. goto out;
  429. }
  430. if(!id->areas)
  431. return 1;
  432. p_info->act_entry += id->entry_size;
  433. /* switch to next page, if we reached the end of the page */
  434. if (p_info->act_entry > (PAGE_SIZE - id->entry_size)){
  435. /* next page */
  436. p_info->act_entry = 0;
  437. p_info->act_page += 1;
  438. if((p_info->act_page % id->pages_per_area) == 0) {
  439. /* next area */
  440. p_info->act_area++;
  441. p_info->act_page=0;
  442. }
  443. if(p_info->act_area >= id->nr_areas)
  444. return 1;
  445. }
  446. out:
  447. return 0;
  448. }
  449. /*
  450. * debug_output:
  451. * - called for user read()
  452. * - copies formated debug entries to the user buffer
  453. */
  454. static ssize_t
  455. debug_output(struct file *file, /* file descriptor */
  456. char __user *user_buf, /* user buffer */
  457. size_t len, /* length of buffer */
  458. loff_t *offset) /* offset in the file */
  459. {
  460. size_t count = 0;
  461. size_t entry_offset;
  462. file_private_info_t *p_info;
  463. p_info = ((file_private_info_t *) file->private_data);
  464. if (*offset != p_info->offset)
  465. return -EPIPE;
  466. if(p_info->act_area >= p_info->debug_info_snap->nr_areas)
  467. return 0;
  468. entry_offset = p_info->act_entry_offset;
  469. while(count < len){
  470. int formatted_line_size;
  471. int formatted_line_residue;
  472. int user_buf_residue;
  473. size_t copy_size;
  474. formatted_line_size = debug_format_entry(p_info);
  475. formatted_line_residue = formatted_line_size - entry_offset;
  476. user_buf_residue = len-count;
  477. copy_size = min(user_buf_residue, formatted_line_residue);
  478. if(copy_size){
  479. if (copy_to_user(user_buf + count, p_info->temp_buf
  480. + entry_offset, copy_size))
  481. return -EFAULT;
  482. count += copy_size;
  483. entry_offset += copy_size;
  484. }
  485. if(copy_size == formatted_line_residue){
  486. entry_offset = 0;
  487. if(debug_next_entry(p_info))
  488. goto out;
  489. }
  490. }
  491. out:
  492. p_info->offset = *offset + count;
  493. p_info->act_entry_offset = entry_offset;
  494. *offset = p_info->offset;
  495. return count;
  496. }
  497. /*
  498. * debug_input:
  499. * - called for user write()
  500. * - calls input function of view
  501. */
  502. static ssize_t
  503. debug_input(struct file *file, const char __user *user_buf, size_t length,
  504. loff_t *offset)
  505. {
  506. int rc = 0;
  507. file_private_info_t *p_info;
  508. mutex_lock(&debug_mutex);
  509. p_info = ((file_private_info_t *) file->private_data);
  510. if (p_info->view->input_proc)
  511. rc = p_info->view->input_proc(p_info->debug_info_org,
  512. p_info->view, file, user_buf,
  513. length, offset);
  514. else
  515. rc = -EPERM;
  516. mutex_unlock(&debug_mutex);
  517. return rc; /* number of input characters */
  518. }
  519. /*
  520. * debug_open:
  521. * - called for user open()
  522. * - copies formated output to private_data area of the file
  523. * handle
  524. */
  525. static int
  526. debug_open(struct inode *inode, struct file *file)
  527. {
  528. int i, rc = 0;
  529. file_private_info_t *p_info;
  530. debug_info_t *debug_info, *debug_info_snapshot;
  531. mutex_lock(&debug_mutex);
  532. debug_info = file->f_path.dentry->d_inode->i_private;
  533. /* find debug view */
  534. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  535. if (!debug_info->views[i])
  536. continue;
  537. else if (debug_info->debugfs_entries[i] ==
  538. file->f_path.dentry) {
  539. goto found; /* found view ! */
  540. }
  541. }
  542. /* no entry found */
  543. rc = -EINVAL;
  544. goto out;
  545. found:
  546. /* Make snapshot of current debug areas to get it consistent. */
  547. /* To copy all the areas is only needed, if we have a view which */
  548. /* formats the debug areas. */
  549. if(!debug_info->views[i]->format_proc &&
  550. !debug_info->views[i]->header_proc){
  551. debug_info_snapshot = debug_info_copy(debug_info, NO_AREAS);
  552. } else {
  553. debug_info_snapshot = debug_info_copy(debug_info, ALL_AREAS);
  554. }
  555. if(!debug_info_snapshot){
  556. rc = -ENOMEM;
  557. goto out;
  558. }
  559. p_info = kmalloc(sizeof(file_private_info_t),
  560. GFP_KERNEL);
  561. if(!p_info){
  562. if(debug_info_snapshot)
  563. debug_info_free(debug_info_snapshot);
  564. rc = -ENOMEM;
  565. goto out;
  566. }
  567. p_info->offset = 0;
  568. p_info->debug_info_snap = debug_info_snapshot;
  569. p_info->debug_info_org = debug_info;
  570. p_info->view = debug_info->views[i];
  571. p_info->act_area = 0;
  572. p_info->act_page = 0;
  573. p_info->act_entry = DEBUG_PROLOG_ENTRY;
  574. p_info->act_entry_offset = 0;
  575. file->private_data = p_info;
  576. debug_info_get(debug_info);
  577. out:
  578. mutex_unlock(&debug_mutex);
  579. return rc;
  580. }
  581. /*
  582. * debug_close:
  583. * - called for user close()
  584. * - deletes private_data area of the file handle
  585. */
  586. static int
  587. debug_close(struct inode *inode, struct file *file)
  588. {
  589. file_private_info_t *p_info;
  590. p_info = (file_private_info_t *) file->private_data;
  591. if(p_info->debug_info_snap)
  592. debug_info_free(p_info->debug_info_snap);
  593. debug_info_put(p_info->debug_info_org);
  594. kfree(file->private_data);
  595. return 0; /* success */
  596. }
  597. /*
  598. * debug_register_mode:
  599. * - Creates and initializes debug area for the caller
  600. * The mode parameter allows to specify access rights for the s390dbf files
  601. * - Returns handle for debug area
  602. */
  603. debug_info_t *debug_register_mode(const char *name, int pages_per_area,
  604. int nr_areas, int buf_size, mode_t mode,
  605. uid_t uid, gid_t gid)
  606. {
  607. debug_info_t *rc = NULL;
  608. /* Since debugfs currently does not support uid/gid other than root, */
  609. /* we do not allow gid/uid != 0 until we get support for that. */
  610. if ((uid != 0) || (gid != 0))
  611. pr_warning("Root becomes the owner of all s390dbf files "
  612. "in sysfs\n");
  613. BUG_ON(!initialized);
  614. mutex_lock(&debug_mutex);
  615. /* create new debug_info */
  616. rc = debug_info_create(name, pages_per_area, nr_areas, buf_size, mode);
  617. if(!rc)
  618. goto out;
  619. debug_register_view(rc, &debug_level_view);
  620. debug_register_view(rc, &debug_flush_view);
  621. debug_register_view(rc, &debug_pages_view);
  622. out:
  623. if (!rc){
  624. pr_err("Registering debug feature %s failed\n", name);
  625. }
  626. mutex_unlock(&debug_mutex);
  627. return rc;
  628. }
  629. EXPORT_SYMBOL(debug_register_mode);
  630. /*
  631. * debug_register:
  632. * - creates and initializes debug area for the caller
  633. * - returns handle for debug area
  634. */
  635. debug_info_t *debug_register(const char *name, int pages_per_area,
  636. int nr_areas, int buf_size)
  637. {
  638. return debug_register_mode(name, pages_per_area, nr_areas, buf_size,
  639. S_IRUSR | S_IWUSR, 0, 0);
  640. }
  641. /*
  642. * debug_unregister:
  643. * - give back debug area
  644. */
  645. void
  646. debug_unregister(debug_info_t * id)
  647. {
  648. if (!id)
  649. goto out;
  650. mutex_lock(&debug_mutex);
  651. debug_info_put(id);
  652. mutex_unlock(&debug_mutex);
  653. out:
  654. return;
  655. }
  656. /*
  657. * debug_set_size:
  658. * - set area size (number of pages) and number of areas
  659. */
  660. static int
  661. debug_set_size(debug_info_t* id, int nr_areas, int pages_per_area)
  662. {
  663. unsigned long flags;
  664. debug_entry_t *** new_areas;
  665. int rc=0;
  666. if(!id || (nr_areas <= 0) || (pages_per_area < 0))
  667. return -EINVAL;
  668. if(pages_per_area > 0){
  669. new_areas = debug_areas_alloc(pages_per_area, nr_areas);
  670. if(!new_areas) {
  671. pr_info("Allocating memory for %i pages failed\n",
  672. pages_per_area);
  673. rc = -ENOMEM;
  674. goto out;
  675. }
  676. } else {
  677. new_areas = NULL;
  678. }
  679. spin_lock_irqsave(&id->lock,flags);
  680. debug_areas_free(id);
  681. id->areas = new_areas;
  682. id->nr_areas = nr_areas;
  683. id->pages_per_area = pages_per_area;
  684. id->active_area = 0;
  685. memset(id->active_entries,0,sizeof(int)*id->nr_areas);
  686. memset(id->active_pages, 0, sizeof(int)*id->nr_areas);
  687. spin_unlock_irqrestore(&id->lock,flags);
  688. pr_info("%s: set new size (%i pages)\n" ,id->name, pages_per_area);
  689. out:
  690. return rc;
  691. }
  692. /*
  693. * debug_set_level:
  694. * - set actual debug level
  695. */
  696. void
  697. debug_set_level(debug_info_t* id, int new_level)
  698. {
  699. unsigned long flags;
  700. if(!id)
  701. return;
  702. spin_lock_irqsave(&id->lock,flags);
  703. if(new_level == DEBUG_OFF_LEVEL){
  704. id->level = DEBUG_OFF_LEVEL;
  705. pr_info("%s: switched off\n",id->name);
  706. } else if ((new_level > DEBUG_MAX_LEVEL) || (new_level < 0)) {
  707. pr_info("%s: level %i is out of range (%i - %i)\n",
  708. id->name, new_level, 0, DEBUG_MAX_LEVEL);
  709. } else {
  710. id->level = new_level;
  711. }
  712. spin_unlock_irqrestore(&id->lock,flags);
  713. }
  714. /*
  715. * proceed_active_entry:
  716. * - set active entry to next in the ring buffer
  717. */
  718. static inline void
  719. proceed_active_entry(debug_info_t * id)
  720. {
  721. if ((id->active_entries[id->active_area] += id->entry_size)
  722. > (PAGE_SIZE - id->entry_size)){
  723. id->active_entries[id->active_area] = 0;
  724. id->active_pages[id->active_area] =
  725. (id->active_pages[id->active_area] + 1) %
  726. id->pages_per_area;
  727. }
  728. }
  729. /*
  730. * proceed_active_area:
  731. * - set active area to next in the ring buffer
  732. */
  733. static inline void
  734. proceed_active_area(debug_info_t * id)
  735. {
  736. id->active_area++;
  737. id->active_area = id->active_area % id->nr_areas;
  738. }
  739. /*
  740. * get_active_entry:
  741. */
  742. static inline debug_entry_t*
  743. get_active_entry(debug_info_t * id)
  744. {
  745. return (debug_entry_t *) (((char *) id->areas[id->active_area]
  746. [id->active_pages[id->active_area]]) +
  747. id->active_entries[id->active_area]);
  748. }
  749. /*
  750. * debug_finish_entry:
  751. * - set timestamp, caller address, cpu number etc.
  752. */
  753. static inline void
  754. debug_finish_entry(debug_info_t * id, debug_entry_t* active, int level,
  755. int exception)
  756. {
  757. active->id.stck = get_clock();
  758. active->id.fields.cpuid = smp_processor_id();
  759. active->caller = __builtin_return_address(0);
  760. active->id.fields.exception = exception;
  761. active->id.fields.level = level;
  762. proceed_active_entry(id);
  763. if(exception)
  764. proceed_active_area(id);
  765. }
  766. static int debug_stoppable=1;
  767. static int debug_active=1;
  768. #define CTL_S390DBF_STOPPABLE 5678
  769. #define CTL_S390DBF_ACTIVE 5679
  770. /*
  771. * proc handler for the running debug_active sysctl
  772. * always allow read, allow write only if debug_stoppable is set or
  773. * if debug_active is already off
  774. */
  775. static int
  776. s390dbf_procactive(ctl_table *table, int write, struct file *filp,
  777. void __user *buffer, size_t *lenp, loff_t *ppos)
  778. {
  779. if (!write || debug_stoppable || !debug_active)
  780. return proc_dointvec(table, write, filp, buffer, lenp, ppos);
  781. else
  782. return 0;
  783. }
  784. static struct ctl_table s390dbf_table[] = {
  785. {
  786. .ctl_name = CTL_S390DBF_STOPPABLE,
  787. .procname = "debug_stoppable",
  788. .data = &debug_stoppable,
  789. .maxlen = sizeof(int),
  790. .mode = S_IRUGO | S_IWUSR,
  791. .proc_handler = &proc_dointvec,
  792. .strategy = &sysctl_intvec,
  793. },
  794. {
  795. .ctl_name = CTL_S390DBF_ACTIVE,
  796. .procname = "debug_active",
  797. .data = &debug_active,
  798. .maxlen = sizeof(int),
  799. .mode = S_IRUGO | S_IWUSR,
  800. .proc_handler = &s390dbf_procactive,
  801. .strategy = &sysctl_intvec,
  802. },
  803. { .ctl_name = 0 }
  804. };
  805. static struct ctl_table s390dbf_dir_table[] = {
  806. {
  807. .ctl_name = CTL_S390DBF,
  808. .procname = "s390dbf",
  809. .maxlen = 0,
  810. .mode = S_IRUGO | S_IXUGO,
  811. .child = s390dbf_table,
  812. },
  813. { .ctl_name = 0 }
  814. };
  815. static struct ctl_table_header *s390dbf_sysctl_header;
  816. void
  817. debug_stop_all(void)
  818. {
  819. if (debug_stoppable)
  820. debug_active = 0;
  821. }
  822. /*
  823. * debug_event_common:
  824. * - write debug entry with given size
  825. */
  826. debug_entry_t*
  827. debug_event_common(debug_info_t * id, int level, const void *buf, int len)
  828. {
  829. unsigned long flags;
  830. debug_entry_t *active;
  831. if (!debug_active || !id->areas)
  832. return NULL;
  833. spin_lock_irqsave(&id->lock, flags);
  834. active = get_active_entry(id);
  835. memset(DEBUG_DATA(active), 0, id->buf_size);
  836. memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
  837. debug_finish_entry(id, active, level, 0);
  838. spin_unlock_irqrestore(&id->lock, flags);
  839. return active;
  840. }
  841. /*
  842. * debug_exception_common:
  843. * - write debug entry with given size and switch to next debug area
  844. */
  845. debug_entry_t
  846. *debug_exception_common(debug_info_t * id, int level, const void *buf, int len)
  847. {
  848. unsigned long flags;
  849. debug_entry_t *active;
  850. if (!debug_active || !id->areas)
  851. return NULL;
  852. spin_lock_irqsave(&id->lock, flags);
  853. active = get_active_entry(id);
  854. memset(DEBUG_DATA(active), 0, id->buf_size);
  855. memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
  856. debug_finish_entry(id, active, level, 1);
  857. spin_unlock_irqrestore(&id->lock, flags);
  858. return active;
  859. }
  860. /*
  861. * counts arguments in format string for sprintf view
  862. */
  863. static inline int
  864. debug_count_numargs(char *string)
  865. {
  866. int numargs=0;
  867. while(*string) {
  868. if(*string++=='%')
  869. numargs++;
  870. }
  871. return(numargs);
  872. }
  873. /*
  874. * debug_sprintf_event:
  875. */
  876. debug_entry_t*
  877. debug_sprintf_event(debug_info_t* id, int level,char *string,...)
  878. {
  879. va_list ap;
  880. int numargs,idx;
  881. unsigned long flags;
  882. debug_sprintf_entry_t *curr_event;
  883. debug_entry_t *active;
  884. if((!id) || (level > id->level))
  885. return NULL;
  886. if (!debug_active || !id->areas)
  887. return NULL;
  888. numargs=debug_count_numargs(string);
  889. spin_lock_irqsave(&id->lock, flags);
  890. active = get_active_entry(id);
  891. curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
  892. va_start(ap,string);
  893. curr_event->string=string;
  894. for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
  895. curr_event->args[idx]=va_arg(ap,long);
  896. va_end(ap);
  897. debug_finish_entry(id, active, level, 0);
  898. spin_unlock_irqrestore(&id->lock, flags);
  899. return active;
  900. }
  901. /*
  902. * debug_sprintf_exception:
  903. */
  904. debug_entry_t*
  905. debug_sprintf_exception(debug_info_t* id, int level,char *string,...)
  906. {
  907. va_list ap;
  908. int numargs,idx;
  909. unsigned long flags;
  910. debug_sprintf_entry_t *curr_event;
  911. debug_entry_t *active;
  912. if((!id) || (level > id->level))
  913. return NULL;
  914. if (!debug_active || !id->areas)
  915. return NULL;
  916. numargs=debug_count_numargs(string);
  917. spin_lock_irqsave(&id->lock, flags);
  918. active = get_active_entry(id);
  919. curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
  920. va_start(ap,string);
  921. curr_event->string=string;
  922. for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
  923. curr_event->args[idx]=va_arg(ap,long);
  924. va_end(ap);
  925. debug_finish_entry(id, active, level, 1);
  926. spin_unlock_irqrestore(&id->lock, flags);
  927. return active;
  928. }
  929. /*
  930. * debug_init:
  931. * - is called exactly once to initialize the debug feature
  932. */
  933. static int
  934. __init debug_init(void)
  935. {
  936. int rc = 0;
  937. s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table);
  938. mutex_lock(&debug_mutex);
  939. debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT,NULL);
  940. initialized = 1;
  941. mutex_unlock(&debug_mutex);
  942. return rc;
  943. }
  944. /*
  945. * debug_register_view:
  946. */
  947. int
  948. debug_register_view(debug_info_t * id, struct debug_view *view)
  949. {
  950. int rc = 0;
  951. int i;
  952. unsigned long flags;
  953. mode_t mode;
  954. struct dentry *pde;
  955. if (!id)
  956. goto out;
  957. mode = (id->mode | S_IFREG) & ~S_IXUGO;
  958. if (!(view->prolog_proc || view->format_proc || view->header_proc))
  959. mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
  960. if (!view->input_proc)
  961. mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
  962. pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
  963. id , &debug_file_ops);
  964. if (!pde){
  965. pr_err("Registering view %s/%s failed due to out of "
  966. "memory\n", id->name,view->name);
  967. rc = -1;
  968. goto out;
  969. }
  970. spin_lock_irqsave(&id->lock, flags);
  971. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  972. if (!id->views[i])
  973. break;
  974. }
  975. if (i == DEBUG_MAX_VIEWS) {
  976. pr_err("Registering view %s/%s would exceed the maximum "
  977. "number of views %i\n", id->name, view->name, i);
  978. debugfs_remove(pde);
  979. rc = -1;
  980. } else {
  981. id->views[i] = view;
  982. id->debugfs_entries[i] = pde;
  983. }
  984. spin_unlock_irqrestore(&id->lock, flags);
  985. out:
  986. return rc;
  987. }
  988. /*
  989. * debug_unregister_view:
  990. */
  991. int
  992. debug_unregister_view(debug_info_t * id, struct debug_view *view)
  993. {
  994. int rc = 0;
  995. int i;
  996. unsigned long flags;
  997. if (!id)
  998. goto out;
  999. spin_lock_irqsave(&id->lock, flags);
  1000. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  1001. if (id->views[i] == view)
  1002. break;
  1003. }
  1004. if (i == DEBUG_MAX_VIEWS)
  1005. rc = -1;
  1006. else {
  1007. debugfs_remove(id->debugfs_entries[i]);
  1008. id->views[i] = NULL;
  1009. }
  1010. spin_unlock_irqrestore(&id->lock, flags);
  1011. out:
  1012. return rc;
  1013. }
  1014. static inline char *
  1015. debug_get_user_string(const char __user *user_buf, size_t user_len)
  1016. {
  1017. char* buffer;
  1018. buffer = kmalloc(user_len + 1, GFP_KERNEL);
  1019. if (!buffer)
  1020. return ERR_PTR(-ENOMEM);
  1021. if (copy_from_user(buffer, user_buf, user_len) != 0) {
  1022. kfree(buffer);
  1023. return ERR_PTR(-EFAULT);
  1024. }
  1025. /* got the string, now strip linefeed. */
  1026. if (buffer[user_len - 1] == '\n')
  1027. buffer[user_len - 1] = 0;
  1028. else
  1029. buffer[user_len] = 0;
  1030. return buffer;
  1031. }
  1032. static inline int
  1033. debug_get_uint(char *buf)
  1034. {
  1035. int rc;
  1036. for(; isspace(*buf); buf++);
  1037. rc = simple_strtoul(buf, &buf, 10);
  1038. if(*buf){
  1039. rc = -EINVAL;
  1040. }
  1041. return rc;
  1042. }
  1043. /*
  1044. * functions for debug-views
  1045. ***********************************
  1046. */
  1047. /*
  1048. * prints out actual debug level
  1049. */
  1050. static int
  1051. debug_prolog_pages_fn(debug_info_t * id,
  1052. struct debug_view *view, char *out_buf)
  1053. {
  1054. return sprintf(out_buf, "%i\n", id->pages_per_area);
  1055. }
  1056. /*
  1057. * reads new size (number of pages per debug area)
  1058. */
  1059. static int
  1060. debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
  1061. struct file *file, const char __user *user_buf,
  1062. size_t user_len, loff_t * offset)
  1063. {
  1064. char *str;
  1065. int rc,new_pages;
  1066. if (user_len > 0x10000)
  1067. user_len = 0x10000;
  1068. if (*offset != 0){
  1069. rc = -EPIPE;
  1070. goto out;
  1071. }
  1072. str = debug_get_user_string(user_buf,user_len);
  1073. if(IS_ERR(str)){
  1074. rc = PTR_ERR(str);
  1075. goto out;
  1076. }
  1077. new_pages = debug_get_uint(str);
  1078. if(new_pages < 0){
  1079. rc = -EINVAL;
  1080. goto free_str;
  1081. }
  1082. rc = debug_set_size(id,id->nr_areas, new_pages);
  1083. if(rc != 0){
  1084. rc = -EINVAL;
  1085. goto free_str;
  1086. }
  1087. rc = user_len;
  1088. free_str:
  1089. kfree(str);
  1090. out:
  1091. *offset += user_len;
  1092. return rc; /* number of input characters */
  1093. }
  1094. /*
  1095. * prints out actual debug level
  1096. */
  1097. static int
  1098. debug_prolog_level_fn(debug_info_t * id, struct debug_view *view, char *out_buf)
  1099. {
  1100. int rc = 0;
  1101. if(id->level == DEBUG_OFF_LEVEL) {
  1102. rc = sprintf(out_buf,"-\n");
  1103. }
  1104. else {
  1105. rc = sprintf(out_buf, "%i\n", id->level);
  1106. }
  1107. return rc;
  1108. }
  1109. /*
  1110. * reads new debug level
  1111. */
  1112. static int
  1113. debug_input_level_fn(debug_info_t * id, struct debug_view *view,
  1114. struct file *file, const char __user *user_buf,
  1115. size_t user_len, loff_t * offset)
  1116. {
  1117. char *str;
  1118. int rc,new_level;
  1119. if (user_len > 0x10000)
  1120. user_len = 0x10000;
  1121. if (*offset != 0){
  1122. rc = -EPIPE;
  1123. goto out;
  1124. }
  1125. str = debug_get_user_string(user_buf,user_len);
  1126. if(IS_ERR(str)){
  1127. rc = PTR_ERR(str);
  1128. goto out;
  1129. }
  1130. if(str[0] == '-'){
  1131. debug_set_level(id, DEBUG_OFF_LEVEL);
  1132. rc = user_len;
  1133. goto free_str;
  1134. } else {
  1135. new_level = debug_get_uint(str);
  1136. }
  1137. if(new_level < 0) {
  1138. pr_warning("%s is not a valid level for a debug "
  1139. "feature\n", str);
  1140. rc = -EINVAL;
  1141. } else {
  1142. debug_set_level(id, new_level);
  1143. rc = user_len;
  1144. }
  1145. free_str:
  1146. kfree(str);
  1147. out:
  1148. *offset += user_len;
  1149. return rc; /* number of input characters */
  1150. }
  1151. /*
  1152. * flushes debug areas
  1153. */
  1154. static void debug_flush(debug_info_t* id, int area)
  1155. {
  1156. unsigned long flags;
  1157. int i,j;
  1158. if(!id || !id->areas)
  1159. return;
  1160. spin_lock_irqsave(&id->lock,flags);
  1161. if(area == DEBUG_FLUSH_ALL){
  1162. id->active_area = 0;
  1163. memset(id->active_entries, 0, id->nr_areas * sizeof(int));
  1164. for (i = 0; i < id->nr_areas; i++) {
  1165. id->active_pages[i] = 0;
  1166. for(j = 0; j < id->pages_per_area; j++) {
  1167. memset(id->areas[i][j], 0, PAGE_SIZE);
  1168. }
  1169. }
  1170. } else if(area >= 0 && area < id->nr_areas) {
  1171. id->active_entries[area] = 0;
  1172. id->active_pages[area] = 0;
  1173. for(i = 0; i < id->pages_per_area; i++) {
  1174. memset(id->areas[area][i],0,PAGE_SIZE);
  1175. }
  1176. }
  1177. spin_unlock_irqrestore(&id->lock,flags);
  1178. }
  1179. /*
  1180. * view function: flushes debug areas
  1181. */
  1182. static int
  1183. debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
  1184. struct file *file, const char __user *user_buf,
  1185. size_t user_len, loff_t * offset)
  1186. {
  1187. char input_buf[1];
  1188. int rc = user_len;
  1189. if (user_len > 0x10000)
  1190. user_len = 0x10000;
  1191. if (*offset != 0){
  1192. rc = -EPIPE;
  1193. goto out;
  1194. }
  1195. if (copy_from_user(input_buf, user_buf, 1)){
  1196. rc = -EFAULT;
  1197. goto out;
  1198. }
  1199. if(input_buf[0] == '-') {
  1200. debug_flush(id, DEBUG_FLUSH_ALL);
  1201. goto out;
  1202. }
  1203. if (isdigit(input_buf[0])) {
  1204. int area = ((int) input_buf[0] - (int) '0');
  1205. debug_flush(id, area);
  1206. goto out;
  1207. }
  1208. pr_info("Flushing debug data failed because %c is not a valid "
  1209. "area\n", input_buf[0]);
  1210. out:
  1211. *offset += user_len;
  1212. return rc; /* number of input characters */
  1213. }
  1214. /*
  1215. * prints debug header in raw format
  1216. */
  1217. static int
  1218. debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
  1219. int area, debug_entry_t * entry, char *out_buf)
  1220. {
  1221. int rc;
  1222. rc = sizeof(debug_entry_t);
  1223. memcpy(out_buf,entry,sizeof(debug_entry_t));
  1224. return rc;
  1225. }
  1226. /*
  1227. * prints debug data in raw format
  1228. */
  1229. static int
  1230. debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
  1231. char *out_buf, const char *in_buf)
  1232. {
  1233. int rc;
  1234. rc = id->buf_size;
  1235. memcpy(out_buf, in_buf, id->buf_size);
  1236. return rc;
  1237. }
  1238. /*
  1239. * prints debug data in hex/ascii format
  1240. */
  1241. static int
  1242. debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
  1243. char *out_buf, const char *in_buf)
  1244. {
  1245. int i, rc = 0;
  1246. for (i = 0; i < id->buf_size; i++) {
  1247. rc += sprintf(out_buf + rc, "%02x ",
  1248. ((unsigned char *) in_buf)[i]);
  1249. }
  1250. rc += sprintf(out_buf + rc, "| ");
  1251. for (i = 0; i < id->buf_size; i++) {
  1252. unsigned char c = in_buf[i];
  1253. if (!isprint(c))
  1254. rc += sprintf(out_buf + rc, ".");
  1255. else
  1256. rc += sprintf(out_buf + rc, "%c", c);
  1257. }
  1258. rc += sprintf(out_buf + rc, "\n");
  1259. return rc;
  1260. }
  1261. /*
  1262. * prints header for debug entry
  1263. */
  1264. int
  1265. debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
  1266. int area, debug_entry_t * entry, char *out_buf)
  1267. {
  1268. struct timespec time_spec;
  1269. unsigned long long time;
  1270. char *except_str;
  1271. unsigned long caller;
  1272. int rc = 0;
  1273. unsigned int level;
  1274. level = entry->id.fields.level;
  1275. time = entry->id.stck;
  1276. /* adjust todclock to 1970 */
  1277. time -= 0x8126d60e46000000LL - (0x3c26700LL * 1000000 * 4096);
  1278. tod_to_timeval(time, &time_spec);
  1279. if (entry->id.fields.exception)
  1280. except_str = "*";
  1281. else
  1282. except_str = "-";
  1283. caller = ((unsigned long) entry->caller) & PSW_ADDR_INSN;
  1284. rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %p ",
  1285. area, time_spec.tv_sec, time_spec.tv_nsec / 1000, level,
  1286. except_str, entry->id.fields.cpuid, (void *) caller);
  1287. return rc;
  1288. }
  1289. /*
  1290. * prints debug data sprintf-formated:
  1291. * debug_sprinf_event/exception calls must be used together with this view
  1292. */
  1293. #define DEBUG_SPRINTF_MAX_ARGS 10
  1294. static int
  1295. debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
  1296. char *out_buf, debug_sprintf_entry_t *curr_event)
  1297. {
  1298. int num_longs, num_used_args = 0,i, rc = 0;
  1299. int index[DEBUG_SPRINTF_MAX_ARGS];
  1300. /* count of longs fit into one entry */
  1301. num_longs = id->buf_size / sizeof(long);
  1302. if(num_longs < 1)
  1303. goto out; /* bufsize of entry too small */
  1304. if(num_longs == 1) {
  1305. /* no args, we use only the string */
  1306. strcpy(out_buf, curr_event->string);
  1307. rc = strlen(curr_event->string);
  1308. goto out;
  1309. }
  1310. /* number of arguments used for sprintf (without the format string) */
  1311. num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
  1312. memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
  1313. for(i = 0; i < num_used_args; i++)
  1314. index[i] = i;
  1315. rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
  1316. curr_event->args[index[1]], curr_event->args[index[2]],
  1317. curr_event->args[index[3]], curr_event->args[index[4]],
  1318. curr_event->args[index[5]], curr_event->args[index[6]],
  1319. curr_event->args[index[7]], curr_event->args[index[8]],
  1320. curr_event->args[index[9]]);
  1321. out:
  1322. return rc;
  1323. }
  1324. /*
  1325. * clean up module
  1326. */
  1327. static void __exit debug_exit(void)
  1328. {
  1329. debugfs_remove(debug_debugfs_root_entry);
  1330. unregister_sysctl_table(s390dbf_sysctl_header);
  1331. return;
  1332. }
  1333. /*
  1334. * module definitions
  1335. */
  1336. postcore_initcall(debug_init);
  1337. module_exit(debug_exit);
  1338. MODULE_LICENSE("GPL");
  1339. EXPORT_SYMBOL(debug_register);
  1340. EXPORT_SYMBOL(debug_unregister);
  1341. EXPORT_SYMBOL(debug_set_level);
  1342. EXPORT_SYMBOL(debug_stop_all);
  1343. EXPORT_SYMBOL(debug_register_view);
  1344. EXPORT_SYMBOL(debug_unregister_view);
  1345. EXPORT_SYMBOL(debug_event_common);
  1346. EXPORT_SYMBOL(debug_exception_common);
  1347. EXPORT_SYMBOL(debug_hex_ascii_view);
  1348. EXPORT_SYMBOL(debug_raw_view);
  1349. EXPORT_SYMBOL(debug_dflt_header_fn);
  1350. EXPORT_SYMBOL(debug_sprintf_view);
  1351. EXPORT_SYMBOL(debug_sprintf_exception);
  1352. EXPORT_SYMBOL(debug_sprintf_event);