debug.c 35 KB

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