debug.c 36 KB

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