debug.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  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, struct file *filp,
  775. void __user *buffer, size_t *lenp, loff_t *ppos)
  776. {
  777. if (!write || debug_stoppable || !debug_active)
  778. return proc_dointvec(table, write, filp, buffer, lenp, ppos);
  779. else
  780. return 0;
  781. }
  782. static struct ctl_table s390dbf_table[] = {
  783. {
  784. .ctl_name = CTL_S390DBF_STOPPABLE,
  785. .procname = "debug_stoppable",
  786. .data = &debug_stoppable,
  787. .maxlen = sizeof(int),
  788. .mode = S_IRUGO | S_IWUSR,
  789. .proc_handler = &proc_dointvec,
  790. .strategy = &sysctl_intvec,
  791. },
  792. {
  793. .ctl_name = CTL_S390DBF_ACTIVE,
  794. .procname = "debug_active",
  795. .data = &debug_active,
  796. .maxlen = sizeof(int),
  797. .mode = S_IRUGO | S_IWUSR,
  798. .proc_handler = &s390dbf_procactive,
  799. .strategy = &sysctl_intvec,
  800. },
  801. { .ctl_name = 0 }
  802. };
  803. static struct ctl_table s390dbf_dir_table[] = {
  804. {
  805. .ctl_name = CTL_S390DBF,
  806. .procname = "s390dbf",
  807. .maxlen = 0,
  808. .mode = S_IRUGO | S_IXUGO,
  809. .child = s390dbf_table,
  810. },
  811. { .ctl_name = 0 }
  812. };
  813. static struct ctl_table_header *s390dbf_sysctl_header;
  814. void
  815. debug_stop_all(void)
  816. {
  817. if (debug_stoppable)
  818. debug_active = 0;
  819. }
  820. /*
  821. * debug_event_common:
  822. * - write debug entry with given size
  823. */
  824. debug_entry_t*
  825. debug_event_common(debug_info_t * id, int level, const void *buf, int len)
  826. {
  827. unsigned long flags;
  828. debug_entry_t *active;
  829. if (!debug_active || !id->areas)
  830. return NULL;
  831. spin_lock_irqsave(&id->lock, flags);
  832. active = get_active_entry(id);
  833. memset(DEBUG_DATA(active), 0, id->buf_size);
  834. memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
  835. debug_finish_entry(id, active, level, 0);
  836. spin_unlock_irqrestore(&id->lock, flags);
  837. return active;
  838. }
  839. /*
  840. * debug_exception_common:
  841. * - write debug entry with given size and switch to next debug area
  842. */
  843. debug_entry_t
  844. *debug_exception_common(debug_info_t * id, int level, const void *buf, int len)
  845. {
  846. unsigned long flags;
  847. debug_entry_t *active;
  848. if (!debug_active || !id->areas)
  849. return NULL;
  850. spin_lock_irqsave(&id->lock, flags);
  851. active = get_active_entry(id);
  852. memset(DEBUG_DATA(active), 0, id->buf_size);
  853. memcpy(DEBUG_DATA(active), buf, min(len, id->buf_size));
  854. debug_finish_entry(id, active, level, 1);
  855. spin_unlock_irqrestore(&id->lock, flags);
  856. return active;
  857. }
  858. /*
  859. * counts arguments in format string for sprintf view
  860. */
  861. static inline int
  862. debug_count_numargs(char *string)
  863. {
  864. int numargs=0;
  865. while(*string) {
  866. if(*string++=='%')
  867. numargs++;
  868. }
  869. return(numargs);
  870. }
  871. /*
  872. * debug_sprintf_event:
  873. */
  874. debug_entry_t*
  875. debug_sprintf_event(debug_info_t* id, int level,char *string,...)
  876. {
  877. va_list ap;
  878. int numargs,idx;
  879. unsigned long flags;
  880. debug_sprintf_entry_t *curr_event;
  881. debug_entry_t *active;
  882. if((!id) || (level > id->level))
  883. return NULL;
  884. if (!debug_active || !id->areas)
  885. return NULL;
  886. numargs=debug_count_numargs(string);
  887. spin_lock_irqsave(&id->lock, flags);
  888. active = get_active_entry(id);
  889. curr_event=(debug_sprintf_entry_t *) DEBUG_DATA(active);
  890. va_start(ap,string);
  891. curr_event->string=string;
  892. for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
  893. curr_event->args[idx]=va_arg(ap,long);
  894. va_end(ap);
  895. debug_finish_entry(id, active, level, 0);
  896. spin_unlock_irqrestore(&id->lock, flags);
  897. return active;
  898. }
  899. /*
  900. * debug_sprintf_exception:
  901. */
  902. debug_entry_t*
  903. debug_sprintf_exception(debug_info_t* id, int level,char *string,...)
  904. {
  905. va_list ap;
  906. int numargs,idx;
  907. unsigned long flags;
  908. debug_sprintf_entry_t *curr_event;
  909. debug_entry_t *active;
  910. if((!id) || (level > id->level))
  911. return NULL;
  912. if (!debug_active || !id->areas)
  913. return NULL;
  914. numargs=debug_count_numargs(string);
  915. spin_lock_irqsave(&id->lock, flags);
  916. active = get_active_entry(id);
  917. curr_event=(debug_sprintf_entry_t *)DEBUG_DATA(active);
  918. va_start(ap,string);
  919. curr_event->string=string;
  920. for(idx=0;idx<min(numargs,(int)(id->buf_size / sizeof(long))-1);idx++)
  921. curr_event->args[idx]=va_arg(ap,long);
  922. va_end(ap);
  923. debug_finish_entry(id, active, level, 1);
  924. spin_unlock_irqrestore(&id->lock, flags);
  925. return active;
  926. }
  927. /*
  928. * debug_init:
  929. * - is called exactly once to initialize the debug feature
  930. */
  931. static int
  932. __init debug_init(void)
  933. {
  934. int rc = 0;
  935. s390dbf_sysctl_header = register_sysctl_table(s390dbf_dir_table);
  936. mutex_lock(&debug_mutex);
  937. debug_debugfs_root_entry = debugfs_create_dir(DEBUG_DIR_ROOT,NULL);
  938. initialized = 1;
  939. mutex_unlock(&debug_mutex);
  940. return rc;
  941. }
  942. /*
  943. * debug_register_view:
  944. */
  945. int
  946. debug_register_view(debug_info_t * id, struct debug_view *view)
  947. {
  948. int rc = 0;
  949. int i;
  950. unsigned long flags;
  951. mode_t mode;
  952. struct dentry *pde;
  953. if (!id)
  954. goto out;
  955. mode = (id->mode | S_IFREG) & ~S_IXUGO;
  956. if (!(view->prolog_proc || view->format_proc || view->header_proc))
  957. mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
  958. if (!view->input_proc)
  959. mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
  960. pde = debugfs_create_file(view->name, mode, id->debugfs_root_entry,
  961. id , &debug_file_ops);
  962. if (!pde){
  963. pr_err("Registering view %s/%s failed due to out of "
  964. "memory\n", id->name,view->name);
  965. rc = -1;
  966. goto out;
  967. }
  968. spin_lock_irqsave(&id->lock, flags);
  969. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  970. if (!id->views[i])
  971. break;
  972. }
  973. if (i == DEBUG_MAX_VIEWS) {
  974. pr_err("Registering view %s/%s would exceed the maximum "
  975. "number of views %i\n", id->name, view->name, i);
  976. debugfs_remove(pde);
  977. rc = -1;
  978. } else {
  979. id->views[i] = view;
  980. id->debugfs_entries[i] = pde;
  981. }
  982. spin_unlock_irqrestore(&id->lock, flags);
  983. out:
  984. return rc;
  985. }
  986. /*
  987. * debug_unregister_view:
  988. */
  989. int
  990. debug_unregister_view(debug_info_t * id, struct debug_view *view)
  991. {
  992. int rc = 0;
  993. int i;
  994. unsigned long flags;
  995. if (!id)
  996. goto out;
  997. spin_lock_irqsave(&id->lock, flags);
  998. for (i = 0; i < DEBUG_MAX_VIEWS; i++) {
  999. if (id->views[i] == view)
  1000. break;
  1001. }
  1002. if (i == DEBUG_MAX_VIEWS)
  1003. rc = -1;
  1004. else {
  1005. debugfs_remove(id->debugfs_entries[i]);
  1006. id->views[i] = NULL;
  1007. }
  1008. spin_unlock_irqrestore(&id->lock, flags);
  1009. out:
  1010. return rc;
  1011. }
  1012. static inline char *
  1013. debug_get_user_string(const char __user *user_buf, size_t user_len)
  1014. {
  1015. char* buffer;
  1016. buffer = kmalloc(user_len + 1, GFP_KERNEL);
  1017. if (!buffer)
  1018. return ERR_PTR(-ENOMEM);
  1019. if (copy_from_user(buffer, user_buf, user_len) != 0) {
  1020. kfree(buffer);
  1021. return ERR_PTR(-EFAULT);
  1022. }
  1023. /* got the string, now strip linefeed. */
  1024. if (buffer[user_len - 1] == '\n')
  1025. buffer[user_len - 1] = 0;
  1026. else
  1027. buffer[user_len] = 0;
  1028. return buffer;
  1029. }
  1030. static inline int
  1031. debug_get_uint(char *buf)
  1032. {
  1033. int rc;
  1034. for(; isspace(*buf); buf++);
  1035. rc = simple_strtoul(buf, &buf, 10);
  1036. if(*buf){
  1037. rc = -EINVAL;
  1038. }
  1039. return rc;
  1040. }
  1041. /*
  1042. * functions for debug-views
  1043. ***********************************
  1044. */
  1045. /*
  1046. * prints out actual debug level
  1047. */
  1048. static int
  1049. debug_prolog_pages_fn(debug_info_t * id,
  1050. struct debug_view *view, char *out_buf)
  1051. {
  1052. return sprintf(out_buf, "%i\n", id->pages_per_area);
  1053. }
  1054. /*
  1055. * reads new size (number of pages per debug area)
  1056. */
  1057. static int
  1058. debug_input_pages_fn(debug_info_t * id, struct debug_view *view,
  1059. struct file *file, const char __user *user_buf,
  1060. size_t user_len, loff_t * offset)
  1061. {
  1062. char *str;
  1063. int rc,new_pages;
  1064. if (user_len > 0x10000)
  1065. user_len = 0x10000;
  1066. if (*offset != 0){
  1067. rc = -EPIPE;
  1068. goto out;
  1069. }
  1070. str = debug_get_user_string(user_buf,user_len);
  1071. if(IS_ERR(str)){
  1072. rc = PTR_ERR(str);
  1073. goto out;
  1074. }
  1075. new_pages = debug_get_uint(str);
  1076. if(new_pages < 0){
  1077. rc = -EINVAL;
  1078. goto free_str;
  1079. }
  1080. rc = debug_set_size(id,id->nr_areas, new_pages);
  1081. if(rc != 0){
  1082. rc = -EINVAL;
  1083. goto free_str;
  1084. }
  1085. rc = user_len;
  1086. free_str:
  1087. kfree(str);
  1088. out:
  1089. *offset += user_len;
  1090. return rc; /* number of input characters */
  1091. }
  1092. /*
  1093. * prints out actual debug level
  1094. */
  1095. static int
  1096. debug_prolog_level_fn(debug_info_t * id, struct debug_view *view, char *out_buf)
  1097. {
  1098. int rc = 0;
  1099. if(id->level == DEBUG_OFF_LEVEL) {
  1100. rc = sprintf(out_buf,"-\n");
  1101. }
  1102. else {
  1103. rc = sprintf(out_buf, "%i\n", id->level);
  1104. }
  1105. return rc;
  1106. }
  1107. /*
  1108. * reads new debug level
  1109. */
  1110. static int
  1111. debug_input_level_fn(debug_info_t * id, struct debug_view *view,
  1112. struct file *file, const char __user *user_buf,
  1113. size_t user_len, loff_t * offset)
  1114. {
  1115. char *str;
  1116. int rc,new_level;
  1117. if (user_len > 0x10000)
  1118. user_len = 0x10000;
  1119. if (*offset != 0){
  1120. rc = -EPIPE;
  1121. goto out;
  1122. }
  1123. str = debug_get_user_string(user_buf,user_len);
  1124. if(IS_ERR(str)){
  1125. rc = PTR_ERR(str);
  1126. goto out;
  1127. }
  1128. if(str[0] == '-'){
  1129. debug_set_level(id, DEBUG_OFF_LEVEL);
  1130. rc = user_len;
  1131. goto free_str;
  1132. } else {
  1133. new_level = debug_get_uint(str);
  1134. }
  1135. if(new_level < 0) {
  1136. pr_warning("%s is not a valid level for a debug "
  1137. "feature\n", str);
  1138. rc = -EINVAL;
  1139. } else {
  1140. debug_set_level(id, new_level);
  1141. rc = user_len;
  1142. }
  1143. free_str:
  1144. kfree(str);
  1145. out:
  1146. *offset += user_len;
  1147. return rc; /* number of input characters */
  1148. }
  1149. /*
  1150. * flushes debug areas
  1151. */
  1152. static void debug_flush(debug_info_t* id, int area)
  1153. {
  1154. unsigned long flags;
  1155. int i,j;
  1156. if(!id || !id->areas)
  1157. return;
  1158. spin_lock_irqsave(&id->lock,flags);
  1159. if(area == DEBUG_FLUSH_ALL){
  1160. id->active_area = 0;
  1161. memset(id->active_entries, 0, id->nr_areas * sizeof(int));
  1162. for (i = 0; i < id->nr_areas; i++) {
  1163. id->active_pages[i] = 0;
  1164. for(j = 0; j < id->pages_per_area; j++) {
  1165. memset(id->areas[i][j], 0, PAGE_SIZE);
  1166. }
  1167. }
  1168. } else if(area >= 0 && area < id->nr_areas) {
  1169. id->active_entries[area] = 0;
  1170. id->active_pages[area] = 0;
  1171. for(i = 0; i < id->pages_per_area; i++) {
  1172. memset(id->areas[area][i],0,PAGE_SIZE);
  1173. }
  1174. }
  1175. spin_unlock_irqrestore(&id->lock,flags);
  1176. }
  1177. /*
  1178. * view function: flushes debug areas
  1179. */
  1180. static int
  1181. debug_input_flush_fn(debug_info_t * id, struct debug_view *view,
  1182. struct file *file, const char __user *user_buf,
  1183. size_t user_len, loff_t * offset)
  1184. {
  1185. char input_buf[1];
  1186. int rc = user_len;
  1187. if (user_len > 0x10000)
  1188. user_len = 0x10000;
  1189. if (*offset != 0){
  1190. rc = -EPIPE;
  1191. goto out;
  1192. }
  1193. if (copy_from_user(input_buf, user_buf, 1)){
  1194. rc = -EFAULT;
  1195. goto out;
  1196. }
  1197. if(input_buf[0] == '-') {
  1198. debug_flush(id, DEBUG_FLUSH_ALL);
  1199. goto out;
  1200. }
  1201. if (isdigit(input_buf[0])) {
  1202. int area = ((int) input_buf[0] - (int) '0');
  1203. debug_flush(id, area);
  1204. goto out;
  1205. }
  1206. pr_info("Flushing debug data failed because %c is not a valid "
  1207. "area\n", input_buf[0]);
  1208. out:
  1209. *offset += user_len;
  1210. return rc; /* number of input characters */
  1211. }
  1212. /*
  1213. * prints debug header in raw format
  1214. */
  1215. static int
  1216. debug_raw_header_fn(debug_info_t * id, struct debug_view *view,
  1217. int area, debug_entry_t * entry, char *out_buf)
  1218. {
  1219. int rc;
  1220. rc = sizeof(debug_entry_t);
  1221. memcpy(out_buf,entry,sizeof(debug_entry_t));
  1222. return rc;
  1223. }
  1224. /*
  1225. * prints debug data in raw format
  1226. */
  1227. static int
  1228. debug_raw_format_fn(debug_info_t * id, struct debug_view *view,
  1229. char *out_buf, const char *in_buf)
  1230. {
  1231. int rc;
  1232. rc = id->buf_size;
  1233. memcpy(out_buf, in_buf, id->buf_size);
  1234. return rc;
  1235. }
  1236. /*
  1237. * prints debug data in hex/ascii format
  1238. */
  1239. static int
  1240. debug_hex_ascii_format_fn(debug_info_t * id, struct debug_view *view,
  1241. char *out_buf, const char *in_buf)
  1242. {
  1243. int i, rc = 0;
  1244. for (i = 0; i < id->buf_size; i++) {
  1245. rc += sprintf(out_buf + rc, "%02x ",
  1246. ((unsigned char *) in_buf)[i]);
  1247. }
  1248. rc += sprintf(out_buf + rc, "| ");
  1249. for (i = 0; i < id->buf_size; i++) {
  1250. unsigned char c = in_buf[i];
  1251. if (!isprint(c))
  1252. rc += sprintf(out_buf + rc, ".");
  1253. else
  1254. rc += sprintf(out_buf + rc, "%c", c);
  1255. }
  1256. rc += sprintf(out_buf + rc, "\n");
  1257. return rc;
  1258. }
  1259. /*
  1260. * prints header for debug entry
  1261. */
  1262. int
  1263. debug_dflt_header_fn(debug_info_t * id, struct debug_view *view,
  1264. int area, debug_entry_t * entry, char *out_buf)
  1265. {
  1266. struct timespec time_spec;
  1267. char *except_str;
  1268. unsigned long caller;
  1269. int rc = 0;
  1270. unsigned int level;
  1271. level = entry->id.fields.level;
  1272. stck_to_timespec(entry->id.stck, &time_spec);
  1273. if (entry->id.fields.exception)
  1274. except_str = "*";
  1275. else
  1276. except_str = "-";
  1277. caller = ((unsigned long) entry->caller) & PSW_ADDR_INSN;
  1278. rc += sprintf(out_buf, "%02i %011lu:%06lu %1u %1s %02i %p ",
  1279. area, time_spec.tv_sec, time_spec.tv_nsec / 1000, level,
  1280. except_str, entry->id.fields.cpuid, (void *) caller);
  1281. return rc;
  1282. }
  1283. /*
  1284. * prints debug data sprintf-formated:
  1285. * debug_sprinf_event/exception calls must be used together with this view
  1286. */
  1287. #define DEBUG_SPRINTF_MAX_ARGS 10
  1288. static int
  1289. debug_sprintf_format_fn(debug_info_t * id, struct debug_view *view,
  1290. char *out_buf, debug_sprintf_entry_t *curr_event)
  1291. {
  1292. int num_longs, num_used_args = 0,i, rc = 0;
  1293. int index[DEBUG_SPRINTF_MAX_ARGS];
  1294. /* count of longs fit into one entry */
  1295. num_longs = id->buf_size / sizeof(long);
  1296. if(num_longs < 1)
  1297. goto out; /* bufsize of entry too small */
  1298. if(num_longs == 1) {
  1299. /* no args, we use only the string */
  1300. strcpy(out_buf, curr_event->string);
  1301. rc = strlen(curr_event->string);
  1302. goto out;
  1303. }
  1304. /* number of arguments used for sprintf (without the format string) */
  1305. num_used_args = min(DEBUG_SPRINTF_MAX_ARGS, (num_longs - 1));
  1306. memset(index,0, DEBUG_SPRINTF_MAX_ARGS * sizeof(int));
  1307. for(i = 0; i < num_used_args; i++)
  1308. index[i] = i;
  1309. rc = sprintf(out_buf, curr_event->string, curr_event->args[index[0]],
  1310. curr_event->args[index[1]], curr_event->args[index[2]],
  1311. curr_event->args[index[3]], curr_event->args[index[4]],
  1312. curr_event->args[index[5]], curr_event->args[index[6]],
  1313. curr_event->args[index[7]], curr_event->args[index[8]],
  1314. curr_event->args[index[9]]);
  1315. out:
  1316. return rc;
  1317. }
  1318. /*
  1319. * clean up module
  1320. */
  1321. static void __exit debug_exit(void)
  1322. {
  1323. debugfs_remove(debug_debugfs_root_entry);
  1324. unregister_sysctl_table(s390dbf_sysctl_header);
  1325. return;
  1326. }
  1327. /*
  1328. * module definitions
  1329. */
  1330. postcore_initcall(debug_init);
  1331. module_exit(debug_exit);
  1332. MODULE_LICENSE("GPL");
  1333. EXPORT_SYMBOL(debug_register);
  1334. EXPORT_SYMBOL(debug_unregister);
  1335. EXPORT_SYMBOL(debug_set_level);
  1336. EXPORT_SYMBOL(debug_stop_all);
  1337. EXPORT_SYMBOL(debug_register_view);
  1338. EXPORT_SYMBOL(debug_unregister_view);
  1339. EXPORT_SYMBOL(debug_event_common);
  1340. EXPORT_SYMBOL(debug_exception_common);
  1341. EXPORT_SYMBOL(debug_hex_ascii_view);
  1342. EXPORT_SYMBOL(debug_raw_view);
  1343. EXPORT_SYMBOL(debug_dflt_header_fn);
  1344. EXPORT_SYMBOL(debug_sprintf_view);
  1345. EXPORT_SYMBOL(debug_sprintf_exception);
  1346. EXPORT_SYMBOL(debug_sprintf_event);