debug.c 36 KB

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