debug.c 35 KB

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