gtool5 Fortran 90/95 ライブラリ 1.0.0-rc5
English
Loading...
Searching...
No Matches
dc_trace Module Reference

デバッグ時の追跡用モジュール More...

Data Types

interface  debug
interface  datadump

Functions/Subroutines

integer function, public sublevel ()
subroutine, public dbg_scratch (on)
subroutine, public setdebug (debug)
subroutine, public beginsub (name, fmt, i, r, d, l, n, c1, c2, c3, ca, version)
subroutine, public endsub (name, fmt, i, r, d, l, n, c1, c2, c3, ca)
subroutine, public dbgmessage (fmt, i, r, d, l, n, c1, c2, c3, ca)

Variables

integer, save, public dbg = -1

Detailed Description

デバッグ時の追跡用モジュール

Author
Yasuhiro MORIKAWA, Eizi TOYODA

dc_trace はデバッグ時の原因の追跡を補助するためのサブルーチン群 を持つモジュールです。このモジュールを利用する事で、 以下のようにサブルーチンの階層構造がそのまま分かるような デバッグメッセージを出力する事が可能です。

:
#call HistoryPut0
#| call HistoryPutEx : time
#| | call TimeGoAhead : varname=time head=1.
#| | | call lookup_dimension
#| | | | call gtvarinquire : var.mapid=1
#| | | | | call gdncvarinqurie : var.id=1
#| | | | | end gdncvarinqurie : ok
#| | | | |-name=time
#| | | | end gtvarinquire
#| | | end lookup_dimension : ord=1
#| | end TimeGoAhead
#| end HistoryPutEx
#end HistoryPut0
:

手続一覧

手続名 説明
SetDebug デバッグモードをオンオフ
BeginSub 副プログラム開始のメッセージ出力
EndSub 副プログラム終了のメッセージ出力
DbgMessage デバッグ用メッセージ出力
DataDump 多次元データ出力

使用方法

副プログラムの実行文の先頭と最後で BeginSub と EndSub を使用します。

subroutine testroutine(file, var, times, url)
use dc_types, only: string
use dc_trace, only: beginsub, endsub
character(*), intent(in) :: file, var
integer, intent(in) :: times
character(*), intent(out):: url
character(STRING), parameter:: subname =
continue
call beginsub(subname, 'file=%c, var=%c, times=%d', &
& c1=trim(file), c2=trim(var), i=(/times/) )
url = trim(file) // trim(var)
call endsub(subname, 'url=%c', c1=trim(url) )
end subroutine TestRoutine
デバッグ時の追跡用モジュール
Definition dc_trace.f90:150
subroutine, public beginsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca, version)
Definition dc_trace.f90:476
subroutine, public endsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca)
Definition dc_trace.f90:599
種別型パラメタを提供します。
Definition dc_types.f90:55
integer, parameter, public string
文字列を保持する 文字型変数の種別型パラメタ
Definition dc_types.f90:137

主プログラムの実行文の先頭で SetDebug を使用します。

program main
use dc_trace, only: setdebug
continue
call setdebug
call testroutine(...)
end program main
subroutine, public setdebug(debug)
Definition dc_trace.f90:340
Note
BeginSub よりも前に SetDebug が呼ばれている必要があります。
BeginSub と同じ回数だけ EndSub が呼ばれていなければなりません。

Function/Subroutine Documentation

◆ beginsub()

subroutine, public dc_trace::beginsub ( character(*), intent(in) name,
character(*), intent(in), optional fmt,
integer, dimension(:), intent(in), optional i,
real, dimension(:), intent(in), optional r,
real(dp), dimension(:), intent(in), optional d,
logical, dimension(:), intent(in), optional l,
integer, dimension(:), intent(in), optional n,
character(*), intent(in), optional c1,
character(*), intent(in), optional c2,
character(*), intent(in), optional c3,
character(*), dimension(:), intent(in), optional ca,
character(*), intent(in), optional version )

副プログラム開始のメッセージ出力

文字型変数 name に与えた副プログラム名を以下のように出力します。

# call name

複数回呼ぶ事で階層構造が分かるメッセージが出力されます (dc_trace の Overview 参照)。 必ず BeginSub と同様な数だけ EndSub を呼ぶようにしてください。

fmt およびそれ以降の引数を与える事で、付加メッセージも出力可能です:

# call name : fmt

書式は dc_string::CPrintf を参照して下さい。

version には副プログラムのバージョンナンバーを与えます。 version に与えられた文字列は、初回に呼び出された時のみ表示されます。

Note
このサブルーチンにより内部変数 level の値が 1 増えます。
Parameters
[in]name副プログラム名
[in]fmt付加メッセージ用フォーマット文字列 (省略可)
[in]iフォーマット用整数配列
[in]rフォーマット用実数配列
[in]dフォーマット用倍精度実数配列
[in]Lフォーマット用論理配列
[in]nフォーマット用整数配列
[in]c1フォーマット用文字列1
[in]c2フォーマット用文字列2
[in]c3フォーマット用文字列3
[in]caフォーマット用文字列配列
[in]version副プログラムのバージョンナンバー (省略可)

Definition at line 474 of file dc_trace.f90.

476 use dc_types, only: string, dp
477 use dc_string, only: cprintf, strinclude
478 character(*), intent(in) :: name
479 character(*), intent(in), optional:: fmt
480 integer, intent(in), optional:: i(:), n(:)
481 real, intent(in), optional:: r(:)
482 real(DP), intent(in), optional:: d(:)
483 logical, intent(in), optional:: L(:)
484 character(*), intent(in), optional:: c1, c2, c3
485 character(*), intent(in), optional:: ca(:)
486 character(*), intent(in), optional:: version
487 character(STRING) :: cbuf
488 character(STRING) :: name_ver
489 logical :: dbg_mode, print_version
490 integer :: alloc_size
491 continue
492 if ( dbg < 0 ) return
493 if (lfirst) call initialize
494 call debug( dbg_mode )
495 if ( dbg_mode ) then
496 name_ver = name
497 print_version = .false.
498 !---------------------------------
499 ! Print Version check
500 if (present(version)) then
501 if (.not. allocated(called_subname)) then
502 allocate(called_subname(1))
503 called_subname(1) = name
504 print_version = .true.
505 else
506 if (.not. strinclude(called_subname, trim(name))) then
507 alloc_size = size(called_subname)
508 allocate(called_subname_tmp(alloc_size))
509 called_subname_tmp = called_subname
510 deallocate(called_subname)
511 allocate(called_subname(alloc_size + 1))
512 called_subname(1:alloc_size) = called_subname_tmp
513 deallocate(called_subname_tmp)
514 called_subname(alloc_size + 1) = name
515 print_version = .true.
516 end if
517 end if
518 if (print_version) then
519 name_ver = cprintf('%c version=<%c>', &
520 & c1=trim(name), c2=trim(version))
521 end if
522 end if
523 !---------------------------------
524 ! Print Debug message
525 if (present(fmt)) then
526 cbuf = cprintf(fmt, i, r, d, l, n, c1, c2, c3, ca)
527 write(dbg, "(A, A, 'call ', A, ' : ', A)") trim(head), &
528 & repeat(indent, level), trim(name_ver), trim(cbuf)
529 else
530 write(dbg, "(A, A, 'call ',A)") trim(head), &
531 & repeat(indent, level), trim(name_ver)
532 endif
533 endif
534 ! call errtra ! --- for Fujitsu debug
535 if (level > size(table)) return
536 level = level + 1
537 table(level) = name
文字型変数の操作
Definition dc_string.f90:83
integer, parameter, public dp
倍精度実数型変数
Definition dc_types.f90:92

References dbg, dc_types::dp, and dc_types::string.

◆ dbg_scratch()

subroutine, public dc_trace::dbg_scratch ( logical, intent(in) on)

デバッグメッセージの抹消

動作未確認ですので利用の際にはご注意下さい。

論理型変数 on に .true. を与える事で、 以降のデバッグメッセージを抹消する事が出来ます。

なお、論理型変数 on に .false. を与える事で、 直前に呼んだ Dbg_Scratch 以降のメッセージを デバッグメッセージとして再び出力し、 以降のデバッグメッセージも出力されるようにします。

Parameters
[in]on.true. の場合、以降のデバッグメッセージを抹消する。 .false. の場合、抹消されたメッセージを出力し、通常出力を再開する。

Definition at line 237 of file dc_trace.f90.

238 logical, intent(in):: on
239 integer, save:: saved_dbg = -1
240 logical:: x, p
241 character(80):: line
242 integer:: ios
243 continue
244 if (on) then
245 if (dbg < 0) return
246 saved_dbg = dbg
247 ! 有効な 1 〜 99 の装置番号の内の大きめの値を設定 (?)
248 dbg = 98
249 do
250 inquire(unit=dbg, exist=x, opened=p)
251 ! 装置番号 dbg が接続可能で、かつ未接続の場合
252 if (x .and. .not. p) then
253 ! 装置番号 deg をスクラッチファイルとして開く。
254 ! ※ スクラッチファイルとは、特殊な外部ファイルである。
255 ! これは名前なしの一時ファイルであり、開いている
256 ! 間だけ存在する。つまり、プログラムが終了すると
257 ! 存在しなくなる。
258 open(unit=dbg, status='SCRATCH')
259 ! 開く事が出来ればそれで終了。
260 return
261 endif
262 ! 装置番号 dbg が利用不可、または利用済の場合は 0 以下に
263 ! なるまで dbg - 1 して繰り返す。
264 dbg = dbg - 1
265 if (dbg < 0) exit
266 enddo
267 ! 装置番号 dbg が開けない場合、dbg と saved_dbg を初期化
268 dbg = saved_dbg
269 saved_dbg = -1
270 else
271 ! 以前に装置番号 dbg = 98〜0 でスクラッチファイルを開けてい
272 ! なければそれで終了
273 if (saved_dbg < 0) return
274 ! 装置番号 dbg に接続されたスクラッチファイルをその開始位置
275 ! に位置付ける。エラーが生じたら「100 continue」へ
276 rewind(dbg, err=100)
277 do
278 ! 装置番号 dbg に接続されたスクラッチファイルの一行を
279 ! line へ
280 read(dbg, '(A)', iostat=ios) line
281 if (ios /= 0) exit
282 ! line を装置番号 saved_dbg へ書き出す。
283 write(saved_dbg, '(A)', iostat=ios) trim(line)
284 if (ios /= 0) exit
285 enddo
286 100 continue
287 close(dbg, iostat=ios)
288 ! 最後に dbg と saved_dbg を初期化
289 dbg = saved_dbg
290 saved_dbg = -1
291 endif

References dbg.

◆ dbgmessage()

subroutine, public dc_trace::dbgmessage ( character(*), intent(in) fmt,
integer, dimension(:), intent(in), optional i,
real, dimension(:), intent(in), optional r,
real(dp), dimension(:), intent(in), optional d,
logical, dimension(:), intent(in), optional l,
integer, dimension(:), intent(in), optional n,
character(*), intent(in), optional c1,
character(*), intent(in), optional c2,
character(*), intent(in), optional c3,
character(*), dimension(:), intent(in), optional ca )

デバッグ用メッセージ出力

フォーマット文字列 fmt に従ってデバッグメッセージを出力します。 書式は dc_string::CPrintf を参照して下さい。

利用例に関しては dc_trace の Example を参照して下さい。

Note
このサブルーチンを用いても内部変数 level の値は変化しません。
Parameters
[in]fmtフォーマット文字列
[in]iフォーマット用整数配列
[in]rフォーマット用実数配列
[in]dフォーマット用倍精度実数配列
[in]Lフォーマット用論理配列
[in]nフォーマット用整数配列
[in]c1フォーマット用文字列1
[in]c2フォーマット用文字列2
[in]c3フォーマット用文字列3
[in]caフォーマット用文字列配列

Definition at line 679 of file dc_trace.f90.

680 use dc_types, only: string, dp
681 use dc_string, only: cprintf, tochar
682 character(*), intent(in) :: fmt
683 integer, intent(in), optional:: i(:), n(:)
684 real, intent(in), optional:: r(:)
685 real(DP), intent(in), optional:: d(:)
686 logical, intent(in), optional:: L(:)
687 character(*), intent(in), optional:: c1, c2, c3
688 character(*), intent(in), optional:: ca(:)
689 character(STRING):: cbuf
690 character(STRING):: meshead_tmp
691 integer :: meshead_len
692 continue
693 if ( dbg < 0 ) return
694 cbuf = cprintf(fmt, i, r, d, l, n, c1, c2, c3, ca)
695 if (level < 1) then
696 meshead_tmp = ''
697 meshead_len = 0
698 else
699 meshead_tmp = meshead
700 meshead_len = len(meshead)
701 endif
702 write(dbg, "(A, A, A, A)") &
703 & trim(head), repeat( indent, max(level-1, 0) ), &
704 & meshead_tmp(1:meshead_len), trim(cbuf)

References dbg, dc_types::dp, and dc_types::string.

◆ endsub()

subroutine, public dc_trace::endsub ( character(*), intent(in) name,
character(*), intent(in), optional fmt,
integer, dimension(:), intent(in), optional i,
real, dimension(:), intent(in), optional r,
real(dp), dimension(:), intent(in), optional d,
logical, dimension(:), intent(in), optional l,
integer, dimension(:), intent(in), optional n,
character(*), intent(in), optional c1,
character(*), intent(in), optional c2,
character(*), intent(in), optional c3,
character(*), dimension(:), intent(in), optional ca )

副プログラム終了のメッセージ出力

文字型変数 name に与えた副プログラム名を以下のように出力します。

# end name

BeginSub に対して一対一対応していますので、name には対応する BeginSub の引数 name と同じものを与えて下さい。

fmt およびそれ以降の引数を与える事で、付加メッセージも出力可能です:

# end name fmt

書式は dc_string::CPrintf を参照して下さい。

Note
このサブルーチンにより内部変数 level の値が 1 減ります。
Parameters
[in]name副プログラム名
[in]fmt付加メッセージ用フォーマット文字列 (省略可)
[in]iフォーマット用整数配列
[in]rフォーマット用実数配列
[in]dフォーマット用倍精度実数配列
[in]Lフォーマット用論理配列
[in]nフォーマット用整数配列
[in]c1フォーマット用文字列1
[in]c2フォーマット用文字列2
[in]c3フォーマット用文字列3
[in]caフォーマット用文字列配列

Definition at line 598 of file dc_trace.f90.

599 use dc_types, only: dp
600 use dc_string, only: cprintf
601 character(*), intent(in) :: name
602 character(*), intent(in), optional:: fmt
603 integer, intent(in), optional:: i(:), n(:)
604 real, intent(in), optional:: r(:)
605 real(DP), intent(in), optional:: d(:)
606 logical, intent(in), optional:: L(:)
607 character(*), intent(in), optional:: c1, c2, c3
608 character(*), intent(in), optional:: ca(:)
609 character(STRING):: cbuf
610 logical:: debug_mode
611 continue
612 if ( dbg < 0 ) return
613 if (lfirst) call initialize
614 ! call errtra ! --- for Fujitsu debug
615 if (level <= 0) then
616 write(*, "(A, 'Warning EndSub[',A,'] without BeginSub')") &
617 & trim(head), trim(name)
618 else if (name /= table(level)) then
619 write(*, "(A, 'Warning EndSub[',A,'] but tos[',A,']')") &
620 & trim(head), trim(name), trim(table(level))
621 else
622 level = level - 1
623 endif
624 call debug( debug_mode )
625 if ( debug_mode ) then
626 if (present(fmt)) then
627 cbuf = cprintf(fmt, i, r, d, l, n, c1, c2, c3, ca)
628 write(dbg, "(A, A, 'end ', A, ' : ', A)") trim(head), &
629 & repeat(indent, level), trim(name), trim(cbuf)
630 else
631 write(dbg, "(A, A, 'end ', A)") trim(head), &
632 & repeat(indent, level), trim(name)
633 endif
634 endif

References dbg, and dc_types::dp.

◆ setdebug()

subroutine, public dc_trace::setdebug ( integer, intent(in), optional debug)

デバッグモードをオンオフ

デバッグメッセージを出力したい時にこのサブルーチンを呼びます。

整数型変数 debug が与えられる場合は、その装置番号 debug に、 以降のサブルーチンによるデバッグメッセージを出力するようにします。 debug が与えられない場合、装置番号 0 (標準エラー出力) にデバッグメッセージが出力されるようになります。 装置番号 0 への出力が成功しない場合は代わりに 装置番号 6 (標準出力) にデバッグメッセージが出力されるようになります。

debug に負の整数を与える場合、デバッグモードが解除され、 以降デバッグメッセージは出力されません。

なお、この SetDebug を呼んだ際にも、装置番号 debug に以下のメッセージが表示されます。

#SetDebug: dbg = debug
Parameters
[in]debugデバッグメッセージ出力用の装置番号 (省略可)。 省略時は標準エラー出力を使用。負の値を与えるとデバッグモード解除。

Definition at line 339 of file dc_trace.f90.

340 use dc_types, only: stdout, stderr
341 implicit none
342 integer, intent(in), optional:: debug
343 integer:: ios
344 logical:: initflag_mpi
345 character(4):: myrank_str_mpi
346 integer:: myrank_mpi, err_mpi
347 continue
348 call mpi_initialized(initflag_mpi, err_mpi)
349 if ( initflag_mpi ) then
350 call mpi_comm_rank(mpi_comm_world, myrank_mpi, err_mpi)
351 if ( myrank_mpi > 9999 ) then
352 head = '#rOVER#'
353 else
354 write(unit=myrank_str_mpi, fmt="(i4.4)") myrank_mpi
355 head = '#r' // myrank_str_mpi // '#'
356 end if
357 else
358 head = '#'
359 end if
360 if (present(debug)) then
361 ! debug が与えられる時は装置番号として deg を用いる。
362 dbg = debug
363 write(dbg, "(A, 'SetDebug: dbg =', i4)", iostat=ios) &
364 & trim(head), dbg
365 if (ios == 0) return
366 else
367 ! debug が与えられ無い時は装置番号 0 (標準エラー出力)
368 dbg = stderr
369 write(dbg, "(A, 'SetDebug: dbg = ', I0)", iostat=ios) trim(head), dbg
370 if (ios == 0) return
371 ! 装置番号 0 への出力が失敗したら装置番号 6 (標準出力)
372 dbg = stdout
373 write(dbg, "(A, 'SetDebug: dbg = ', I0)", iostat=ios) trim(head), dbg
374 if (ios == 0) return
375 endif
376 ! 例外処理として dbg の初期化
377 dbg = -1
integer, parameter, public stdout
標準出力の装置番号
Definition dc_types.f90:117
integer, parameter, public stderr
標準エラー出力の装置番号
Definition dc_types.f90:122

References dbg, dc_types::stderr, and dc_types::stdout.

◆ sublevel()

integer function, public dc_trace::sublevel

副プログラムの階層レベルを返す

副プログラムの階層レベルを返します。レベルのデフォルトは 0 で、 BeginSub によりレベルは 1 増え、EndSub によりレベルは 1 減ります。

Returns
副プログラムの階層レベル

Definition at line 202 of file dc_trace.f90.

203 result = level

Variable Documentation

◆ dbg

integer, save, public dc_trace::dbg = -1

Definition at line 160 of file dc_trace.f90.

160 integer, save, public :: dbg = -1 ! SetDebug で設定された